Skip to content

Commit

Permalink
Added some auth tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ProfMoo committed Dec 4, 2024
1 parent 68b569c commit 83b80ac
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 14 deletions.
62 changes: 62 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/mocha": "^10.0.10",
"@types/node": "^20.11.0",
"@types/node-jose": "^1.1.10",
"@types/proxyquire": "^1.3.31",
"@types/sinon": "^17.0.3",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
Expand All @@ -48,6 +49,7 @@
"mocha": "^11.0.1",
"prettier": "^3.0.3",
"prettier-eslint": "^16.0.0",
"proxyquire": "^2.1.3",
"rimraf": "^3.0.2",
"sinon": "^19.0.2",
"ts-node": "^10.9.2",
Expand Down
38 changes: 24 additions & 14 deletions src/auth/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { expect } from 'chai';
import sinon from 'sinon';
import * as fs from 'fs';
import proxyquire from 'proxyquire';
import { JWK, JWS } from 'node-jose';
import { buildJWT } from './index';

describe('buildJWT', () => {
describe('BuildJWT', () => {
let readFileSyncStub: sinon.SinonStub;
let asKeyStub: sinon.SinonStub;
let signStub: sinon.SinonStub;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let buildJWT: any;

beforeEach(() => {
readFileSyncStub = sinon.stub(fs, 'readFileSync');
readFileSyncStub = sinon.stub();
asKeyStub = sinon.stub(JWK, 'asKey');
signStub = sinon.stub(JWS, 'createSign');

buildJWT = proxyquire('./index', {
fs: { readFileSync: readFileSyncStub },
}).buildJWT;
});

afterEach(() => {
Expand All @@ -23,9 +28,11 @@ describe('buildJWT', () => {
const url = 'https://api.example.com/resource';
const method = 'POST';
const apiKeyName = 'test-api-key';
const apiPrivateKey = 'test-private-key';
const pemPrivateKey =
const apiPrivateKey =
'-----BEGIN EC PRIVATE KEY-----\ntest-private-key\n-----END EC PRIVATE KEY-----';
// The method strips the newline characters under the hood, so we've removed that from the expected value
const pemPrivateKey =
'-----BEGIN EC PRIVATE KEY-----test-private-key-----END EC PRIVATE KEY-----';
const privateKey = { kty: 'EC' };

asKeyStub.resolves(privateKey);
Expand All @@ -46,10 +53,11 @@ describe('buildJWT', () => {
const method = 'POST';
const apiKey = {
name: 'test-api-key',
privateKey: 'test-private-key',
privateKey:
'-----BEGIN EC PRIVATE KEY-----test-private-key-----END EC PRIVATE KEY-----',
};
const pemPrivateKey =
'-----BEGIN EC PRIVATE KEY-----\ntest-private-key\n-----END EC PRIVATE KEY-----';
'-----BEGIN EC PRIVATE KEY-----test-private-key-----END EC PRIVATE KEY-----';
const privateKey = { kty: 'EC' };

readFileSyncStub.returns(JSON.stringify(apiKey));
Expand All @@ -75,9 +83,10 @@ describe('buildJWT', () => {
const url = 'https://api.example.com/resource';
const method = 'POST';
const apiKeyName = 'test-api-key';
const apiPrivateKey = 'test-private-key';
const pemPrivateKey =
const apiPrivateKey =
'-----BEGIN EC PRIVATE KEY-----\ntest-private-key\n-----END EC PRIVATE KEY-----';
const pemPrivateKey =
'-----BEGIN EC PRIVATE KEY-----test-private-key-----END EC PRIVATE KEY-----';
const privateKey = { kty: 'RSA' };

asKeyStub.resolves(privateKey);
Expand All @@ -86,7 +95,7 @@ describe('buildJWT', () => {
await buildJWT(url, method, apiKeyName, apiPrivateKey);
expect.fail('Expected buildJWT to throw an error');
} catch (error) {
expect((error as Error).message).to.equal('Not an EC private key');
expect((error as Error).message).to.contain('Not an EC private key');
}

expect(asKeyStub.calledOnceWithExactly(pemPrivateKey, 'pem')).to.be.true;
Expand All @@ -97,9 +106,10 @@ describe('buildJWT', () => {
const url = 'https://api.example.com/resource';
const method = 'POST';
const apiKeyName = 'test-api-key';
const apiPrivateKey = 'test-private-key';
const pemPrivateKey =
const apiPrivateKey =
'-----BEGIN EC PRIVATE KEY-----\ntest-private-key\n-----END EC PRIVATE KEY-----';
const pemPrivateKey =
'-----BEGIN EC PRIVATE KEY-----test-private-key-----END EC PRIVATE KEY-----';

asKeyStub.rejects(new Error('Invalid key'));

Expand All @@ -108,7 +118,7 @@ describe('buildJWT', () => {
expect.fail('Expected buildJWT to throw an error');
} catch (error) {
expect((error as Error).message).to.include(
'jwt: Could not decode or parse private key. Invalid key',
'jwt: Could not decode or parse private key. Error: Invalid key',
);
}

Expand Down

0 comments on commit 83b80ac

Please sign in to comment.