Skip to content

Commit 3d83c42

Browse files
committed
unit test for JSON parse failure
1 parent bd3ca0d commit 3d83c42

File tree

3 files changed

+99
-25
lines changed

3 files changed

+99
-25
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"lodash": "~1.0.0-rc.3",
1212
"mocha": "*",
1313
"nconf": "^0.10.0",
14+
"nock": "^10.0.1",
1415
"should": "~1.2.1"
1516
},
1617
"keywords": [

test/auth.tests.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
var assert = require('assert')
2-
, auth = require("../lib/auth")
3-
, config = require('./config');
2+
, auth = require("../lib/auth")
3+
, config = require('./config');
44

55
describe('login to waad', function () {
6-
it('should obtain an access token', function (done) {
7-
auth.getAccessToken(config.v1.TENANTID, config.v1.APPPRINCIPALID, config.v1.SYMMETRICKEY, function(err, token) {
8-
if (err) {
9-
console.log(err);
10-
}
6+
it('should obtain an access token', function (done) {
7+
auth.getAccessToken(config.v1.TENANTID, config.v1.APPPRINCIPALID, config.v1.SYMMETRICKEY, function(err, token) {
8+
if (err) {
9+
console.log(err);
10+
return done(err);
11+
}
1112

12-
assert.notEqual(null, token);
13-
done();
14-
});
13+
assert.notEqual(null, token);
14+
done();
1515
});
16+
});
1617

17-
it('should fail for wrong tenantId', function (done) {
18-
auth.getAccessToken('wrong-tenant-id', config.v1.APPPRINCIPALID, config.v1.SYMMETRICKEY, function(err, token) {
19-
assert.ok(err.message.indexOf('AADSTS90002: No service namespace named \'wrong-tenant-id\' was found in the data store.'));
20-
done();
21-
});
18+
it('should fail for wrong tenantId', function (done) {
19+
auth.getAccessToken('wrong-tenant-id', config.v1.APPPRINCIPALID, config.v1.SYMMETRICKEY, function(err, token) {
20+
assert.ok(err.message.indexOf('AADSTS90002: No service namespace named \'wrong-tenant-id\' was found in the data store.'));
21+
done();
2222
});
23+
});
2324

24-
it('should fail for wrong service principal', function (done) {
25-
auth.getAccessToken(config.v1.TENANTID, 'wrong-principal', config.v1.SYMMETRICKEY, function(err, token) {
26-
assert.ok(err.message.indexOf('AADSTS70001: Application with identifier \'wrong-principal\' was not found in the directory') > -1);
27-
done();
28-
});
25+
it('should fail for wrong service principal', function (done) {
26+
auth.getAccessToken(config.v1.TENANTID, 'wrong-principal', config.v1.SYMMETRICKEY, function(err, token) {
27+
assert.ok(err.message.indexOf('AADSTS70001: Application with identifier \'wrong-principal\' was not found in the directory') > -1);
28+
done();
2929
});
30+
});
3031

31-
it('should fail for wrong service key', function (done) {
32-
auth.getAccessToken(config.v1.TENANTID, config.v1.APPPRINCIPALID, 'wrong-key', function(err, token) {
33-
assert.ok(err.message.indexOf('AADSTS70002: Error validating credentials. AADSTS50012: Client assertion contains an invalid signature.') > -1);
34-
done();
35-
});
32+
it('should fail for wrong service key', function (done) {
33+
auth.getAccessToken(config.v1.TENANTID, config.v1.APPPRINCIPALID, 'wrong-key', function(err, token) {
34+
assert.ok(err.message.indexOf('AADSTS70002: Error validating credentials. AADSTS50012: Client assertion contains an invalid signature.') > -1);
35+
done();
3636
});
37+
});
3738
});

test/auth2.tests.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var assert = require('assert')
2+
, auth = require('../lib/auth')
3+
, config = require('./config')
4+
, nock = require('nock');
5+
6+
describe('login to waad', function () {
7+
function assertParseError(actual, expected, token) {
8+
assert.deepEqual(actual, expected);
9+
assert.equal(token, null);
10+
}
11+
12+
var message = 'Failed to parse response from graph API';
13+
var parseError = new Error(message);
14+
15+
parseError.name = 'waad_parse_error';
16+
parseError.stack = 'Error stack'
17+
parseError.body = { error: 'body' };
18+
19+
var tenantId = config.v1.TENANTID;
20+
var spnAppPrincipalId = config.v1.APPPRINCIPALID;
21+
var spnSymmetricKeyBase64 = config.v1.SYMMETRICKEY;
22+
var tenantDomain = config.v2.WAAD_TENANTDOMAIN;
23+
var clientId = config.v2.WAAD_CLIENTID;
24+
var clientSecret = config.v2.WAAD_CLIENTSECRET;
25+
26+
describe('v1 fails to parse JSON response', function () {
27+
afterEach(function () {
28+
nock.cleanAll();
29+
});
30+
31+
it('should return error details when it fails to parse the JSON response when getting an access token', function (done) {
32+
nock('https://accounts.accesscontrol.windows.net')
33+
.post('/tokens/OAuth/2')
34+
.replyWithError(parseError);
35+
36+
auth.getAccessToken(tenantId, spnAppPrincipalId, spnSymmetricKeyBase64, function(err, token) {
37+
assertParseError(err, parseError, token);
38+
done();
39+
});
40+
});
41+
42+
it('should return error details when it fails to parse the JSON response when getting an access token with client credentials', function (done) {
43+
nock('https://accounts.accesscontrol.windows.net')
44+
.post('/' + tenantId + '/tokens/OAuth/2')
45+
.replyWithError(parseError);
46+
47+
auth.getAccessTokenWithClientCredentials(tenantId, spnAppPrincipalId, clientId, clientSecret, function(err, token) {
48+
assertParseError(err, parseError, token);
49+
done();
50+
});
51+
});
52+
});
53+
54+
describe('v2 fails to parse JSON response', function () {
55+
beforeEach(function () {
56+
nock('https://login.windows.net')
57+
.post('/' + tenantDomain + '/oauth2/token')
58+
.replyWithError(parseError);
59+
});
60+
61+
afterEach(function () {
62+
nock.cleanAll();
63+
});
64+
65+
it('should return error details when it fails to parse the JSON response', function (done) {
66+
auth.getAccessTokenWithClientCredentials2(tenantDomain, clientId, clientSecret, function(err, token) {
67+
assertParseError(err, parseError, token);
68+
done();
69+
});
70+
});
71+
});
72+
});

0 commit comments

Comments
 (0)