|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const sinon = require('sinon'); |
| 8 | +const assert = { ...sinon.assert, ...require('chai').assert }; |
| 9 | +const mocks = require('../../../mocks'); |
| 10 | + |
| 11 | +const TEST_EMAIL = 'foo@gmail.com'; |
| 12 | +const MOCK_UID = '23d4847823f24b0f95e1524987cb0391'; |
| 13 | +const MOCK_REFRESH_TOKEN = '40f61392cf69b0be709fbd3122d0726bb32247b476b2a28451345e7a5555cec7'; |
| 14 | +const MOCK_REFRESH_TOKEN_2 = '00661392cf69b0be709fbd3122d0726bb32247b476b2a28451345e7a5555cec7'; |
| 15 | +const MOCK_REFRESH_TOKEN_ID_2 = '0e4f2255bed0ae53af401150488e69f22beae103b7d6857a5194df00c9827d19'; |
| 16 | +const OAUTH_CLIENT_ID = '3c49430b43dfba77'; |
| 17 | +const MOCK_CHECK_RESPONSE = { |
| 18 | + user: MOCK_UID, |
| 19 | + client_id: OAUTH_CLIENT_ID, |
| 20 | + scope: ['https://identity.mozilla.com/apps/oldsync', 'openid'] |
| 21 | +}; |
| 22 | + |
| 23 | +describe('newTokenNotification', () => { |
| 24 | + let db; |
| 25 | + let oauthdb; |
| 26 | + let mailer; |
| 27 | + let devices; |
| 28 | + let request; |
| 29 | + let credentials; |
| 30 | + let grant; |
| 31 | + const oauthUtils = require('../../../../lib/routes/utils/oauth'); |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + db = mocks.mockDB({ |
| 35 | + email: TEST_EMAIL, |
| 36 | + emailVerified: true, |
| 37 | + uid: MOCK_UID |
| 38 | + }); |
| 39 | + oauthdb = mocks.mockOAuthDB({ |
| 40 | + checkAccessToken: sinon.spy(async () => { |
| 41 | + return MOCK_CHECK_RESPONSE; |
| 42 | + }) |
| 43 | + }); |
| 44 | + mailer = mocks.mockMailer(); |
| 45 | + devices = mocks.mockDevices(); |
| 46 | + credentials = { |
| 47 | + uid: MOCK_UID, |
| 48 | + refreshTokenId: MOCK_REFRESH_TOKEN |
| 49 | + }; |
| 50 | + request = mocks.mockRequest({credentials}); |
| 51 | + grant = { |
| 52 | + scope: 'profile https://identity.mozilla.com/apps/oldsync', |
| 53 | + refresh_token: MOCK_REFRESH_TOKEN_2 |
| 54 | + }; |
| 55 | + }); |
| 56 | + |
| 57 | + it('creates a device and sends an email with credentials uid', async () => { |
| 58 | + await oauthUtils.newTokenNotification(db, oauthdb, mailer, devices, request, grant); |
| 59 | + |
| 60 | + assert.equal(oauthdb.checkAccessToken.callCount, 0); |
| 61 | + assert.equal(mailer.sendNewDeviceLoginNotification.callCount, 1, 'sent email notification'); |
| 62 | + assert.equal(devices.upsert.callCount, 1, 'created a device'); |
| 63 | + const args = devices.upsert.args[0]; |
| 64 | + assert.equal(args[1].refreshTokenId, request.auth.credentials.refreshTokenId); |
| 65 | + }); |
| 66 | + |
| 67 | + it('creates a device and sends an email with checkAccessToken uid', async () => { |
| 68 | + credentials = {}; |
| 69 | + request = mocks.mockRequest({credentials}); |
| 70 | + await oauthUtils.newTokenNotification(db, oauthdb, mailer, devices, request, grant); |
| 71 | + |
| 72 | + assert.equal(oauthdb.checkAccessToken.callCount, 1); |
| 73 | + assert.equal(mailer.sendNewDeviceLoginNotification.callCount, 1, 'sent email notification'); |
| 74 | + assert.equal(devices.upsert.callCount, 1, 'created a device'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('does nothing for non-NOTIFICATION_SCOPES', async () => { |
| 78 | + grant.scope = 'profile'; |
| 79 | + await oauthUtils.newTokenNotification(db, oauthdb, mailer, devices, request, grant); |
| 80 | + |
| 81 | + assert.equal(oauthdb.checkAccessToken.callCount, 0); |
| 82 | + assert.equal(mailer.sendNewDeviceLoginNotification.callCount, 0); |
| 83 | + assert.equal(devices.upsert.callCount, 0); |
| 84 | + }); |
| 85 | + |
| 86 | + it('uses refreshTokenId from grant if not provided', async () => { |
| 87 | + credentials = { |
| 88 | + uid: MOCK_UID, |
| 89 | + }; |
| 90 | + request = mocks.mockRequest({credentials}); |
| 91 | + await oauthUtils.newTokenNotification(db, oauthdb, mailer, devices, request, grant); |
| 92 | + |
| 93 | + assert.equal(oauthdb.checkAccessToken.callCount, 0); |
| 94 | + assert.equal(mailer.sendNewDeviceLoginNotification.callCount, 1); |
| 95 | + assert.equal(devices.upsert.callCount, 1); |
| 96 | + const args = devices.upsert.args[0]; |
| 97 | + assert.equal(args[1].refreshTokenId, MOCK_REFRESH_TOKEN_ID_2); |
| 98 | + }); |
| 99 | + |
| 100 | +}); |
0 commit comments