Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luisrudge committed Jul 19, 2019
1 parent bd808cc commit a049014
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ build
**/.env
out/
test-results.xml
.nyc_output

# Release process
.release
Expand Down
7 changes: 7 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"reporter" : ["text-summary", "lcov"],
"include" : ["src/**/*.js"],
"sourceMap" : false,
"instrument" : true,
"all" : true
}
66 changes: 34 additions & 32 deletions src/management/ManagementTokenProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,40 @@ var ManagementTokenProvider = function(options) {
clientInfo: this.options.clientInfo
};
this.authenticationClient = new AuthenticationClient(authenticationClientOptions);

var self = this;
this.getCachedAccessToken = Promise.promisify(
memoizer({
load: function(options, callback) {
self
.clientCredentialsGrant(options.domain, options.scope, options.audience)
.then(function(data) {
callback(null, data);
})
.catch(function(err) {
callback(err);
});
},
hash: function(options) {
return options.domain + '-' + options.clientId + '-' + options.scope;
},
itemMaxAge: function(options, data) {
if (options.cacheTTLInSeconds) {
return options.cacheTTLInSeconds * 1000;
}

// if the expires_in is lower than 10 seconds, do not subtract 10 additional seconds.
if (data.expires_in && data.expires_in < 10 /* seconds */) {
return data.expires_in * 1000;
} else if (data.expires_in) {
// Subtract 10 seconds from expires_in to fetch a new one, before it expires.
return data.expires_in * 1000 - 10000 /* milliseconds */;
}
return 60 * 60 * 1000; //1h
},
max: 100
})
);
};

/**
Expand All @@ -97,38 +131,6 @@ ManagementTokenProvider.prototype.getAccessToken = function() {
}
};

ManagementTokenProvider.prototype.getCachedAccessToken = Promise.promisify(
memoizer({
load: function(options, callback) {
this.clientCredentialsGrant(options.domain, options.scope, options.audience)
.then(function(data) {
callback(null, data);
})
.catch(function(err) {
callback(err);
});
},
hash: function(options) {
return options.domain + '-' + options.clientId + '-' + options.scope;
},
itemMaxAge: function(options, data) {
if (options.cacheTTLInSeconds) {
return options.cacheTTLInSeconds * 1000;
}

// if the expires_in is lower than 10 seconds, do not subtract 10 additional seconds.
if (data.expires_in && data.expires_in < 10 /* seconds */) {
return data.expires_in * 1000;
} else if (data.expires_in) {
// Subtract 10 seconds from expires_in to fetch a new one, before it expires.
return data.expires_in * 1000 - 10000 /* milliseconds */;
}
return 60 * 60 * 1000; //1h
},
max: 100
})
);

ManagementTokenProvider.prototype.clientCredentialsGrant = function(domain, scope, audience) {
return this.authenticationClient.clientCredentialsGrant({
audience: audience,
Expand Down
15 changes: 10 additions & 5 deletions src/management/TenantManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,30 @@ TenantManager.prototype.updateSettings = function(data, cb) {
* @memberOf module:management.TenantManager.prototype
*
* @example
* management.tenant.getSettings(function (err, settings) {
* management.tenant.getSettings({ include_fields: true, fields: 'friendly_name' }, function (err, settings) {
* if (err) {
* // Handle error.
* }
*
* console.log(settings);
* });
*
* @param {Function} [cb] Callback function.
* @param {Object} [data] Request parameters
* @param {Function} [cb] Callback function.
*
* @return {Promise|undefined}
*/
TenantManager.prototype.getSettings = function(cb) {
TenantManager.prototype.getSettings = function(data, cb) {
if (data instanceof Function && !cb) {
cb = data;
data = {};
}
if (cb && cb instanceof Function) {
return this.resource.get({}, cb);
return this.resource.get(data, cb);
}

// Return a promise.
return this.resource.get({});
return this.resource.get(data);
};

module.exports = TenantManager;
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var utils = (module.exports = {});
* @memberOf module:utils
*/
utils.jsonToBase64 = function(json) {
var bytes = new Buffer(JSON.stringify(json));
var bytes = Buffer.from(JSON.stringify(json));

return bytes
.toString('base64')
Expand Down
22 changes: 11 additions & 11 deletions test/auth/oauth-with-idtoken-validation.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var pem = require('pem');
// Constants.
var DOMAIN = 'tenant.auth0.com';
var CLIENT_ID = 'TEST_CLIENT_ID';
var CLIENT_SECRET = new Buffer('TEST_CLIENT_SECRET', 'base64');
var CLIENT_SECRET = Buffer.from('TEST_CLIENT_SECRET', 'base64');

var OAUthWithIDTokenValidation = require('../../src/auth/OAUthWithIDTokenValidation');
var PARAMS = { params: true };
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('OAUthWithIDTokenValidation', function() {
return new Promise(res => res({ id_token: 'foobar' }));
}
};
sinon.stub(jwt, 'verify', function(idtoken, getKey, options, callback) {
sinon.stub(jwt, 'verify').callsFake(function(idtoken, getKey, options, callback) {
expect(idtoken).to.be.equal('foobar');
expect(options).to.be.eql({
audience: CLIENT_ID,
Expand All @@ -104,7 +104,7 @@ describe('OAUthWithIDTokenValidation', function() {
return new Promise(res => res({ id_token: 'foobar' }));
}
};
sinon.stub(jwt, 'verify', function(idtoken, getKey, options, callback) {
sinon.stub(jwt, 'verify').callsFake(function(idtoken, getKey, options, callback) {
callback(null, { verification: 'result' });
});
var oauthWithValidation = new OAUthWithIDTokenValidation(oauth, {});
Expand All @@ -119,7 +119,7 @@ describe('OAUthWithIDTokenValidation', function() {
return new Promise(res => res({ id_token: 'foobar' }));
}
};
sinon.stub(jwt, 'verify', function(idtoken, getKey, options, callback) {
sinon.stub(jwt, 'verify').callsFake(function(idtoken, getKey, options, callback) {
callback({ the: 'error' });
});
var oauthWithValidation = new OAUthWithIDTokenValidation(oauth, {});
Expand All @@ -134,7 +134,7 @@ describe('OAUthWithIDTokenValidation', function() {
return new Promise(res => res({ id_token: 'foobar' }));
}
};
sinon.stub(jwt, 'verify', function(idtoken, getKey, options, callback) {
sinon.stub(jwt, 'verify').callsFake(function(idtoken, getKey, options, callback) {
getKey({ alg: 'HS256' }, function(err, key) {
expect(key).to.be.eql(Buffer.from(CLIENT_SECRET, 'base64'));
done();
Expand All @@ -151,7 +151,7 @@ describe('OAUthWithIDTokenValidation', function() {
return new Promise(res => res({ id_token: 'foobar' }));
}
};
sinon.stub(jwt, 'verify', function(idtoken, getKey, options, callback) {
sinon.stub(jwt, 'verify').callsFake(function(idtoken, getKey, options, callback) {
getKey({ alg: 'HS256' }, function(err, key) {
expect(err.message).to.contain(
'Validation of `id_token` requires a `clientSecret` when using the HS256 algorithm. To ensure tokens are validated, please switch the signing algorithm to RS256 or provide a `clientSecret` in the constructor.'
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('OAUthWithIDTokenValidation', function() {
'jwks-rsa': jwksClientStub
});

sinon.stub(jwt, 'verify', function(idtoken, getKey, options, callback) {
sinon.stub(jwt, 'verify').callsFake(function(idtoken, getKey, options, callback) {
getKey({ alg: 'RS256' }, function(err, key) {
expect(jwksClientStub.getCall(0).args[0].jwksUri).to.be.equal(
'https://tenant.auth0.com/.well-known/jwks.json'
Expand Down Expand Up @@ -215,7 +215,7 @@ describe('OAUthWithIDTokenValidation', function() {
'jwks-rsa': jwksClientStub
});

sinon.stub(jwt, 'verify', function(idtoken, getKey, options, callback) {
sinon.stub(jwt, 'verify').callsFake(function(idtoken, getKey, options, callback) {
getKey({ kid: 'kid', alg: 'RS256' }, function(err, key) {
expect(err).to.be.eql({ the: 'error' });
done();
Expand Down Expand Up @@ -245,7 +245,7 @@ describe('OAUthWithIDTokenValidation', function() {
'jwks-rsa': jwksClientStub
});

sinon.stub(jwt, 'verify', function(idtoken, getKey, options, callback) {
sinon.stub(jwt, 'verify').callsFake(function(idtoken, getKey, options, callback) {
getKey({ kid: 'kid', alg: 'RS256' }, function(err, key) {
expect(key).to.be.equal('publicKey');
done();
Expand Down Expand Up @@ -275,7 +275,7 @@ describe('OAUthWithIDTokenValidation', function() {
'jwks-rsa': jwksClientStub
});

sinon.stub(jwt, 'verify', function(idtoken, getKey, options, callback) {
sinon.stub(jwt, 'verify').callsFake(function(idtoken, getKey, options, callback) {
getKey({ kid: 'kid', alg: 'RS256' }, function(err, key) {
expect(key).to.be.equal('publicKey');
done();
Expand Down Expand Up @@ -305,7 +305,7 @@ describe('OAUthWithIDTokenValidation', function() {
'jwks-rsa': jwksClientStub
});

sinon.stub(jwt, 'verify', function(idtoken, getKey, options, callback) {
sinon.stub(jwt, 'verify').callsFake(function(idtoken, getKey, options, callback) {
getKey({ kid: 'kid', alg: 'RS256' }, function(err, key) {
expect(key).to.be.equal('rsaPublicKey');
done();
Expand Down
42 changes: 4 additions & 38 deletions test/auth/oauth.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,48 +583,16 @@ describe('OAuthAuthenticator', function() {
});
});

it('should use the default client ID if none specified', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.post(path)
.query({ client_id: CLIENT_ID })
.reply(200);

this.authenticator.socialSignIn(userData).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});

it('should allow the user to specify a custom client ID', function(done) {
nock.cleanAll();

var data = extend({}, userData);
var request = nock(API_URL)
.post(path)
.query({ client_id: 'OVERRIDEN_ID' })
.reply(200);

data.client_id = 'OVERRIDEN_ID';

this.authenticator.socialSignIn(data).then(function() {
expect(request.isDone()).to.be.true;

done();
});
});

it('should use the openid scope by default', function(done) {
nock.cleanAll();

var request = nock(API_URL)
.post(path)
.query({ scope: 'openid' })
.post(path, data)
.reply(200);

this.authenticator.socialSignIn(userData).then(function() {
this.authenticator.socialSignIn(data).then(function() {
expect(request.isDone()).to.be.true;

done();
Expand All @@ -635,13 +603,11 @@ describe('OAuthAuthenticator', function() {
nock.cleanAll();

var data = extend({}, userData);
data.scope = 'openid name email';
var request = nock(API_URL)
.post(path)
.query({ scope: 'openid name email' })
.post(path, data)
.reply(200);

data.scope = 'openid name email';

this.authenticator.socialSignIn(data).then(function() {
expect(request.isDone()).to.be.true;

Expand Down
3 changes: 1 addition & 2 deletions test/management/tickets.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ describe('TicketsManager', function() {
nock.cleanAll();

var request = nock(API_URL)
.post('/tickets/email-verification')
.query({
.post('/tickets/email-verification', {
include_fields: true,
fields: 'test'
})
Expand Down

0 comments on commit a049014

Please sign in to comment.