Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import {assert} from 'chai'; |
| 6 | import sinon from 'sinon'; |
| 7 | import loadGapi, {fetchGapiEmail, getSigninInstance} from './gapi-loader.js'; |
| 8 | |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 9 | describe.skip('gapi-loader', () => { |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 10 | let signinImpl; |
| 11 | beforeEach(() => { |
| 12 | window.CS_env = {gapi_client_id: 'rutabaga'}; |
| 13 | signinImpl = { |
| 14 | init: sinon.stub(), |
| 15 | getUserProfileAsync: () => ( |
| 16 | Promise.resolve({ |
| 17 | getEmail: sinon.stub().returns('rutabaga@google.com'), |
| 18 | }) |
| 19 | ), |
| 20 | }; |
| 21 | // Preload signinImpl with a fake for testing. |
| 22 | getSigninInstance(signinImpl, true); |
| 23 | delete window.__gapiLoadPromise; |
| 24 | }); |
| 25 | |
| 26 | afterEach(() => { |
| 27 | delete window.CS_env; |
| 28 | }); |
| 29 | |
| 30 | describe('loadGapi()', () => { |
| 31 | it('errors out if no client_id', () => { |
| 32 | window.CS_env.gapi_client_id = undefined; |
| 33 | assert.throws(() => loadGapi()); |
| 34 | }); |
| 35 | |
| 36 | it('returns the same promise when called multiple times', () => { |
| 37 | const callOne = loadGapi(); |
| 38 | const callTwo = loadGapi(); |
| 39 | |
| 40 | assert.strictEqual(callOne, callTwo); |
| 41 | assert.strictEqual(callOne, window.__gapiLoadPromise); |
| 42 | assert.strictEqual(callTwo, window.__gapiLoadPromise); |
| 43 | assert.instanceOf(callOne, Promise); |
| 44 | }); |
| 45 | |
| 46 | it('calls init and returns the current email if any', async () => { |
| 47 | const response = await loadGapi(); |
| 48 | sinon.assert.calledWith(signinImpl.init, window.CS_env.gapi_client_id, |
| 49 | ['client'], ['https://www.googleapis.com/auth/buganizer']); |
| 50 | assert.equal(response, 'rutabaga@google.com'); |
| 51 | }); |
| 52 | }); |
| 53 | |
| 54 | describe('fetchGapiEmail()', () => { |
| 55 | it('returns a profile for allowed domains', async () => { |
| 56 | getSigninInstance({ |
| 57 | getUserProfileAsync: () => Promise.resolve({ |
| 58 | getEmail: sinon.stub().returns('rutabaga@google.com'), |
| 59 | }), |
| 60 | }, true); |
| 61 | assert.deepEqual(await fetchGapiEmail(), 'rutabaga@google.com'); |
| 62 | }); |
| 63 | |
| 64 | it('returns nothing for non-allowed domains', async () => { |
| 65 | getSigninInstance({ |
| 66 | getUserProfileAsync: () => Promise.resolve({ |
| 67 | getEmail: sinon.stub().returns('rutabaga@rutabaga.com'), |
| 68 | }), |
| 69 | }, true); |
| 70 | assert.deepEqual(await fetchGapiEmail(), null); |
| 71 | }); |
| 72 | }); |
| 73 | }); |