|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect, use } from 'chai'; |
| 19 | +import * as sinon from 'sinon'; |
| 20 | +import * as sinonChai from 'sinon-chai'; |
| 21 | + |
| 22 | +import { FirebaseApp } from '@firebase/app-types-exp'; |
| 23 | +import { FirebaseError } from '@firebase/util'; |
| 24 | + |
| 25 | +import { testUser } from '../../../test/mock_auth'; |
| 26 | +import { Auth } from '../../model/auth'; |
| 27 | +import { Persistence } from '../persistence'; |
| 28 | +import { browserLocalPersistence } from '../persistence/browser'; |
| 29 | +import { inMemoryPersistence } from '../persistence/in_memory'; |
| 30 | +import { PersistenceUserManager } from '../persistence/persistence_user_manager'; |
| 31 | +import { ClientPlatform, getClientVersion } from '../util/version'; |
| 32 | +import { |
| 33 | + DEFAULT_API_HOST, |
| 34 | + DEFAULT_API_SCHEME, |
| 35 | + initializeAuth |
| 36 | +} from './auth_impl'; |
| 37 | + |
| 38 | +use(sinonChai); |
| 39 | + |
| 40 | +const FAKE_APP: FirebaseApp = { |
| 41 | + name: 'test-app', |
| 42 | + options: { |
| 43 | + apiKey: 'api-key', |
| 44 | + authDomain: 'auth-domain' |
| 45 | + }, |
| 46 | + automaticDataCollectionEnabled: false |
| 47 | +}; |
| 48 | + |
| 49 | +describe('AuthImpl', () => { |
| 50 | + let auth: Auth; |
| 51 | + let persistenceStub: sinon.SinonStubbedInstance<Persistence>; |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + persistenceStub = sinon.stub(inMemoryPersistence); |
| 55 | + auth = initializeAuth(FAKE_APP, { persistence: inMemoryPersistence }); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(sinon.restore); |
| 59 | + |
| 60 | + describe('#updateCurrentUser', () => { |
| 61 | + it('sets the field on the auth object', async () => { |
| 62 | + const user = testUser('uid'); |
| 63 | + await auth.updateCurrentUser(user); |
| 64 | + expect(auth.currentUser).to.eql(user); |
| 65 | + }); |
| 66 | + |
| 67 | + it('orders async operations correctly', async () => { |
| 68 | + const users = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(n => { |
| 69 | + return testUser(`${n}`); |
| 70 | + }); |
| 71 | + |
| 72 | + persistenceStub.set.callsFake(() => { |
| 73 | + return new Promise(resolve => { |
| 74 | + // Force into the async flow to make this test actually meaningful |
| 75 | + setTimeout(() => resolve(), 1); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + await Promise.all(users.map(u => auth.updateCurrentUser(u))); |
| 80 | + for (let i = 0; i < 10; i++) { |
| 81 | + expect(persistenceStub.set.getCall(i)).to.have.been.calledWith( |
| 82 | + sinon.match.any, |
| 83 | + users[i].toPlainObject() |
| 84 | + ); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + it('setting to null triggers a remove call', async () => { |
| 89 | + await auth.updateCurrentUser(null); |
| 90 | + expect(persistenceStub.remove).to.have.been.called; |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('#signOut', () => { |
| 95 | + it('sets currentUser to null, calls remove', async () => { |
| 96 | + await auth.updateCurrentUser(testUser('test')); |
| 97 | + await auth.signOut(); |
| 98 | + expect(persistenceStub.remove).to.have.been.called; |
| 99 | + expect(auth.currentUser).to.be.null; |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('#setPersistence', () => { |
| 104 | + it('swaps underlying persistence', async () => { |
| 105 | + const newPersistence = browserLocalPersistence; |
| 106 | + const newStub = sinon.stub(newPersistence); |
| 107 | + persistenceStub.get.returns( |
| 108 | + Promise.resolve(testUser('test').toPlainObject()) |
| 109 | + ); |
| 110 | + |
| 111 | + await auth.setPersistence(newPersistence); |
| 112 | + expect(persistenceStub.get).to.have.been.called; |
| 113 | + expect(persistenceStub.remove).to.have.been.called; |
| 114 | + expect(newStub.set).to.have.been.calledWith( |
| 115 | + sinon.match.any, |
| 116 | + testUser('test').toPlainObject() |
| 117 | + ); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe('initializeAuth', () => { |
| 123 | + afterEach(sinon.restore); |
| 124 | + |
| 125 | + it('throws an API error if key not provided', () => { |
| 126 | + expect(() => |
| 127 | + initializeAuth({ |
| 128 | + ...FAKE_APP, |
| 129 | + options: {} // apiKey is missing |
| 130 | + }) |
| 131 | + ).to.throw( |
| 132 | + FirebaseError, |
| 133 | + 'Firebase: Your API key is invalid, please check you have copied it correctly. (auth/invalid-api-key).' |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('persistence manager creation', () => { |
| 138 | + let createManagerStub: sinon.SinonSpy; |
| 139 | + beforeEach(() => { |
| 140 | + createManagerStub = sinon.spy(PersistenceUserManager, 'create'); |
| 141 | + }); |
| 142 | + |
| 143 | + async function initAndWait( |
| 144 | + persistence: Persistence | Persistence[] |
| 145 | + ): Promise<Auth> { |
| 146 | + const auth = initializeAuth(FAKE_APP, { persistence }); |
| 147 | + // Auth initializes async. We can make sure the initialization is |
| 148 | + // flushed by awaiting a method on the queue. |
| 149 | + await auth.setPersistence(inMemoryPersistence); |
| 150 | + return auth; |
| 151 | + } |
| 152 | + |
| 153 | + it('converts single persistence to array', async () => { |
| 154 | + const auth = await initAndWait(inMemoryPersistence); |
| 155 | + expect(createManagerStub).to.have.been.calledWith(auth, [ |
| 156 | + inMemoryPersistence |
| 157 | + ]); |
| 158 | + }); |
| 159 | + |
| 160 | + it('pulls the user from storage', async () => { |
| 161 | + sinon |
| 162 | + .stub(inMemoryPersistence, 'get') |
| 163 | + .returns(Promise.resolve(testUser('uid').toPlainObject())); |
| 164 | + const auth = await initAndWait(inMemoryPersistence); |
| 165 | + expect(auth.currentUser!.uid).to.eq('uid'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('calls create with the persistence in order', async () => { |
| 169 | + const auth = await initAndWait([ |
| 170 | + inMemoryPersistence, |
| 171 | + browserLocalPersistence |
| 172 | + ]); |
| 173 | + expect(createManagerStub).to.have.been.calledWith(auth, [ |
| 174 | + inMemoryPersistence, |
| 175 | + browserLocalPersistence |
| 176 | + ]); |
| 177 | + }); |
| 178 | + |
| 179 | + it('sets auth name and config', async () => { |
| 180 | + const auth = await initAndWait(inMemoryPersistence); |
| 181 | + expect(auth.name).to.eq(FAKE_APP.name); |
| 182 | + expect(auth.config).to.eql({ |
| 183 | + apiKey: FAKE_APP.options.apiKey, |
| 184 | + authDomain: FAKE_APP.options.authDomain, |
| 185 | + apiHost: DEFAULT_API_HOST, |
| 186 | + apiScheme: DEFAULT_API_SCHEME, |
| 187 | + sdkClientVersion: getClientVersion(ClientPlatform.BROWSER) |
| 188 | + }); |
| 189 | + }); |
| 190 | + }); |
| 191 | +}); |
0 commit comments