Skip to content

Commit

Permalink
test: refactor to use assert.rejects
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed May 26, 2021
1 parent 9e8ce96 commit ba7f129
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 115 deletions.
7 changes: 3 additions & 4 deletions test/backchannel_logout/backchannel_logout.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { strict: assert } = require('assert');
const { parse: parseUrl } = require('url');

const sinon = require('sinon').createSandbox();
Expand All @@ -7,8 +8,6 @@ const nock = require('nock');

const bootstrap = require('../test_helper');

const fail = () => { throw new Error('expected promise to be rejected'); };

describe('Back-Channel Logout 1.0', () => {
before(bootstrap(__dirname));

Expand Down Expand Up @@ -61,8 +60,8 @@ describe('Back-Channel Logout 1.0', () => {
.post('/backchannel_logout')
.reply(500);

return client.backchannelLogout('subject', 'foo').then(fail, (err) => {
expect(err.message).to.eql('expected 200 OK from https://no-sid.example.com/backchannel_logout, got: 500 Internal Server Error');
return assert.rejects(client.backchannelLogout('subject', 'foo'), {
message: 'expected 200 OK from https://no-sid.example.com/backchannel_logout, got: 500 Internal Server Error',
});
});
});
Expand Down
13 changes: 7 additions & 6 deletions test/base_token/base_token.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { strict: assert } = require('assert');

const sinon = require('sinon').createSandbox();
const { expect } = require('chai');
const timekeeper = require('timekeeper');

const bootstrap = require('../test_helper');

const fail = () => { throw new Error('expected promise to be rejected'); };

describe('BaseToken', () => {
before(bootstrap(__dirname));

Expand Down Expand Up @@ -136,9 +136,10 @@ describe('BaseToken', () => {
const value = await token.save();
const adapterThrow = new Error('adapter throw!');
sinon.stub(this.TestAdapter.for('Session'), 'findByUid').callsFake(async () => { throw adapterThrow; });
await this.provider.RefreshToken.find(value).then(fail, (err) => {
return assert.rejects(this.provider.RefreshToken.find(value), (err) => {
this.TestAdapter.for('Session').findByUid.restore();
expect(err).to.equal(adapterThrow);
return true;
});
});

Expand All @@ -156,12 +157,12 @@ describe('BaseToken', () => {
it('rethrows adapter#findByUserCode errors (Device Code)', async function () {
const adapterThrow = new Error('adapter throw!');
sinon.stub(this.TestAdapter.for('DeviceCode'), 'findByUserCode').callsFake(async () => { throw adapterThrow; });
await this.provider.DeviceCode.findByUserCode('123-456-789').then(() => {
return assert.rejects(this.provider.DeviceCode.findByUserCode('123-456-789').then(() => {
this.TestAdapter.for('DeviceCode').findByUserCode.restore();
fail();
}, (err) => {
}), (err) => {
this.TestAdapter.for('DeviceCode').findByUserCode.restore();
expect(err).to.equal(adapterThrow);
return true;
});
});
});
21 changes: 12 additions & 9 deletions test/configuration/client_keystore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const JWT = require('../../lib/helpers/jwt');
const epochTime = require('../../lib/helpers/epoch_time');
const bootstrap = require('../test_helper');

const fail = () => { throw new Error('expected promise to be rejected'); };

const endpoint = nock('https://client.example.com/');
const keystore = new jose.JWKS.KeyStore();

Expand Down Expand Up @@ -63,15 +61,17 @@ describe('client keystore refresh', () => {
const client = await this.provider.Client.find('client');
sinon.stub(client.asymmetricKeyStore, 'fresh').returns(false);
return Promise.all([
client.asymmetricKeyStore.refresh().then(fail, (err) => {
assert.rejects(client.asymmetricKeyStore.refresh(), (err) => {
expect(err).to.be.an('error');
expect(err.message).to.equal('invalid_client_metadata');
expect(err.error_description).to.eql('client JSON Web Key Set failed to be refreshed');
return true;
}),
client.asymmetricKeyStore.refresh().then(fail, (err) => {
assert.rejects(client.asymmetricKeyStore.refresh(), (err) => {
expect(err).to.be.an('error');
expect(err.message).to.equal('invalid_client_metadata');
expect(err.error_description).to.eql('client JSON Web Key Set failed to be refreshed');
return true;
}),
]);
});
Expand Down Expand Up @@ -101,10 +101,11 @@ describe('client keystore refresh', () => {

const client = await this.provider.Client.find('client');
sinon.stub(client.asymmetricKeyStore, 'fresh').returns(false);
await client.asymmetricKeyStore.refresh().then(fail, (err) => {
return assert.rejects(client.asymmetricKeyStore.refresh(), (err) => {
expect(err).to.be.an('error');
expect(err.message).to.equal('invalid_client_metadata');
expect(err.error_description).to.eql('client JSON Web Key Set failed to be refreshed');
return true;
});
});

Expand All @@ -113,10 +114,11 @@ describe('client keystore refresh', () => {

const client = await this.provider.Client.find('client');
sinon.stub(client.asymmetricKeyStore, 'fresh').returns(false);
await client.asymmetricKeyStore.refresh().then(fail, (err) => {
return assert.rejects(client.asymmetricKeyStore.refresh(), (err) => {
expect(err).to.be.an('error');
expect(err.message).to.equal('invalid_client_metadata');
expect(err.error_description).to.eql('client JSON Web Key Set failed to be refreshed');
return true;
});
});

Expand All @@ -125,10 +127,11 @@ describe('client keystore refresh', () => {

const client = await this.provider.Client.find('client');
sinon.stub(client.asymmetricKeyStore, 'fresh').returns(false);
await client.asymmetricKeyStore.refresh().then(fail, (err) => {
return assert.rejects(client.asymmetricKeyStore.refresh(), (err) => {
expect(err).to.be.an('error');
expect(err.message).to.equal('invalid_client_metadata');
expect(err.error_description).to.eql('client JSON Web Key Set failed to be refreshed');
return true;
});
});

Expand Down Expand Up @@ -225,10 +228,10 @@ describe('client keystore refresh', () => {

const client = await this.provider.Client.find('client');
client.asymmetricKeyStore.freshUntil = epochTime() - 1;
await JWT.verify(
return assert.rejects(JWT.verify(
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgA',
client.asymmetricKeyStore,
).then(fail, () => {});
));
});

it('refreshes stale keystores before id_token encryption', async function () {
Expand Down
59 changes: 35 additions & 24 deletions test/configuration/client_metadata.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { strict: assert } = require('assert');
const util = require('util');

const { expect } = require('chai');
Expand Down Expand Up @@ -43,29 +44,29 @@ describe('Client metadata validation', () => {
});
}

const fail = () => { throw new Error('expected promise to be rejected'); };

const mustBeString = (prop, values = [[], 123, true, null, false, {}, ''], metadata, configuration) => {
values.forEach((value) => {
let msg = util.format('must be a string, %j provided', value);
if (metadata) msg = util.format(`${msg}, [client %j]`, omit(metadata, ['jwks.keys']));
if (configuration) msg = util.format(`${msg}, [provider %j]`, configuration);
it(msg, () => addClient({ ...metadata, [prop]: value }, configuration).then(fail, (err) => {
// eslint-disable-next-line max-len
it(msg, () => assert.rejects(addClient({ ...metadata, [prop]: value }, configuration), (err) => {
if (prop === 'redirect_uris') {
expect(err.message).to.equal('invalid_redirect_uri');
} else {
expect(err.message).to.equal('invalid_client_metadata');
}
expect(err.error_description).to.equal(`${prop} must be a non-empty string if provided`);
return true;
}));
});
};

const mustBeUri = (prop, protocols, configuration, metadata) => {
it('must be a uri', () => addClient({
it('must be a uri', () => assert.rejects(addClient({
...metadata,
[prop]: 'whatever://not but not a uri',
}, configuration).then(fail, (err) => {
}, configuration), (err) => {
if (prop === 'redirect_uris') {
expect(err.message).to.equal('invalid_redirect_uri');
} else {
Expand All @@ -76,6 +77,7 @@ describe('Client metadata validation', () => {
} else {
expect(err.error_description).to.equal(`${prop} must be a web uri`);
}
return true;
}));

protocols.forEach((protocol) => {
Expand All @@ -89,15 +91,16 @@ describe('Client metadata validation', () => {
values.forEach((value) => {
let msg = util.format('must be a array, %j provided', value);
if (configuration) msg = util.format(`${msg}, [provider %j]`, configuration);
it(msg, () => addClient({
it(msg, () => assert.rejects(addClient({
[prop]: value,
}, configuration).then(fail, (err) => {
}, configuration), (err) => {
if (prop === 'redirect_uris') {
expect(err.message).to.equal('invalid_redirect_uri');
} else {
expect(err.message).to.equal('invalid_client_metadata');
}
expect(err.error_description).to.equal(`${prop} must be an array`);
return true;
}));
});
};
Expand All @@ -107,15 +110,16 @@ describe('Client metadata validation', () => {
let msg = util.format('must be a boolean, %j provided', value);
if (metadata) msg = util.format(`${msg}, [client %j]`, omit(metadata, ['jwks.keys']));
if (configuration) msg = util.format(`${msg}, [provider %j]`, configuration);
it(msg, () => addClient({
it(msg, () => assert.rejects(addClient({
[prop]: value,
}, configuration).then(fail, (err) => {
}, configuration), (err) => {
if (prop === 'redirect_uris') {
expect(err.message).to.equal('invalid_redirect_uri');
} else {
expect(err.message).to.equal('invalid_client_metadata');
}
expect(err.error_description).to.equal(`${prop} must be a boolean`);
return true;
}));
});
};
Expand All @@ -138,16 +142,17 @@ describe('Client metadata validation', () => {
(values || [null, undefined, '']).forEach((value) => {
let msg = util.format('is required, %j provided', value);
if (configuration) msg = util.format(`${msg}, [provider %j]`, configuration);
it(msg, () => addClient({
it(msg, () => assert.rejects(addClient({
[prop]: value,
...metadata,
}, configuration).then(fail, (err) => {
}, configuration), (err) => {
if (prop === 'redirect_uris') {
expect(err.message).to.equal('invalid_redirect_uri');
} else {
expect(err.message).to.equal('invalid_client_metadata');
}
expect(err.error_description).to.equal(`${prop} is mandatory property`);
return true;
}));
});
};
Expand All @@ -171,16 +176,17 @@ describe('Client metadata validation', () => {
let msg = util.format('rejects %j', value);
if (metadata) msg = util.format(`${msg}, [client %j]`, omit(metadata, ['jwks.keys']));
if (configuration) msg = util.format(`${msg}, [provider %j]`, configuration);
it(msg, () => addClient({ ...metadata, [prop]: value }, configuration).then(fail, (err) => {
// eslint-disable-next-line max-len
it(msg, () => assert.rejects(addClient({ ...metadata, [prop]: value }, configuration), (err) => {
if (prop === 'redirect_uris') {
expect(err.message).to.equal('invalid_redirect_uri');
} else {
expect(err.message).to.equal('invalid_client_metadata');
}
if (description) {
const assert = description.exec ? 'match' : 'equal';
expect(err.error_description).to[assert](description);
expect(err.error_description).to[description.exec ? 'match' : 'equal'](description);
}
return true;
}));
};

Expand Down Expand Up @@ -636,11 +642,12 @@ describe('Client metadata validation', () => {
mustBeString(this.title, undefined, {
jwks: { keys: [sigKey] },
}, configuration);
it('is required when id_token_encrypted_response_enc is also provided', () => addClient({
it('is required when id_token_encrypted_response_enc is also provided', () => assert.rejects(addClient({
id_token_encrypted_response_enc: 'whatever',
}, configuration).then(fail, (err) => {
}, configuration), (err) => {
expect(err.message).to.equal('invalid_client_metadata');
expect(err.error_description).to.equal('id_token_encrypted_response_alg is mandatory property when id_token_encrypted_response_enc is provided');
return true;
}));
allows(this.title, 'dir', undefined, configuration);
[
Expand Down Expand Up @@ -688,11 +695,12 @@ describe('Client metadata validation', () => {
mustBeString(this.title, undefined, {
jwks: { keys: [sigKey] },
}, configuration);
it('is required when userinfo_encrypted_response_enc is also provided', () => addClient({
it('is required when userinfo_encrypted_response_enc is also provided', () => assert.rejects(addClient({
userinfo_encrypted_response_enc: 'whatever',
}, configuration).then(fail, (err) => {
}, configuration), (err) => {
expect(err.message).to.equal('invalid_client_metadata');
expect(err.error_description).to.equal('userinfo_encrypted_response_alg is mandatory property when userinfo_encrypted_response_enc is provided');
return true;
}));
allows(this.title, 'dir', undefined, configuration);
[
Expand Down Expand Up @@ -741,11 +749,12 @@ describe('Client metadata validation', () => {
mustBeString(this.title, undefined, {
jwks: { keys: [sigKey] },
}, configuration);
it('is required when introspection_encrypted_response_enc is also provided', () => addClient({
it('is required when introspection_encrypted_response_enc is also provided', () => assert.rejects(addClient({
introspection_encrypted_response_enc: 'whatever',
}, configuration).then(fail, (err) => {
}, configuration), (err) => {
expect(err.message).to.equal('invalid_client_metadata');
expect(err.error_description).to.equal('introspection_encrypted_response_alg is mandatory property when introspection_encrypted_response_enc is provided');
return true;
}));
allows(this.title, 'dir', undefined, configuration);
[
Expand Down Expand Up @@ -794,11 +803,12 @@ describe('Client metadata validation', () => {
mustBeString(this.title, undefined, {
jwks: { keys: [sigKey] },
}, configuration);
it('is required when authorization_encrypted_response_enc is also provided', () => addClient({
it('is required when authorization_encrypted_response_enc is also provided', () => assert.rejects(addClient({
authorization_encrypted_response_enc: 'whatever',
}, configuration).then(fail, (err) => {
}, configuration), (err) => {
expect(err.message).to.equal('invalid_client_metadata');
expect(err.error_description).to.equal('authorization_encrypted_response_alg is mandatory property when authorization_encrypted_response_enc is provided');
return true;
}));
allows(this.title, 'dir', undefined, configuration);
[
Expand Down Expand Up @@ -853,11 +863,12 @@ describe('Client metadata validation', () => {
defaultsTo(this.title, undefined);
defaultsTo(this.title, undefined, undefined, configuration);
mustBeString(this.title, undefined, undefined, configuration);
it('is required when request_object_encryption_enc is also provided', () => addClient({
it('is required when request_object_encryption_enc is also provided', () => assert.rejects(addClient({
request_object_encryption_enc: 'whatever',
}, configuration).then(fail, (err) => {
}, configuration), (err) => {
expect(err.message).to.equal('invalid_client_metadata');
expect(err.error_description).to.equal('request_object_encryption_alg is mandatory property when request_object_encryption_enc is provided');
return true;
}));
allows(this.title, 'dir', undefined, configuration);
[
Expand Down
Loading

0 comments on commit ba7f129

Please sign in to comment.