|
| 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