|
| 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 } from 'chai'; |
| 19 | +import * as sinon from 'sinon'; |
| 20 | + |
| 21 | +import { testAuth, testUser } from '../../../test/helpers/mock_auth'; |
| 22 | +import { Auth } from '../../model/auth'; |
| 23 | +import { User } from '../../model/user'; |
| 24 | +import { AuthInternal } from './firebase_internal'; |
| 25 | + |
| 26 | +describe('src/core/auth/firebase_internal', () => { |
| 27 | + let auth: Auth; |
| 28 | + let authInternal: AuthInternal; |
| 29 | + beforeEach(async () => { |
| 30 | + auth = await testAuth(); |
| 31 | + authInternal = new AuthInternal(auth); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + sinon.restore(); |
| 36 | + }); |
| 37 | + |
| 38 | + context('getUid', () => { |
| 39 | + it('returns null if currentUser is undefined', () => { |
| 40 | + expect(authInternal.getUid()).to.be.null; |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns the uid of the user if set', async () => { |
| 44 | + const user = testUser(auth, 'uid'); |
| 45 | + await auth.updateCurrentUser(user); |
| 46 | + expect(authInternal.getUid()).to.eq('uid'); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + context('getToken', () => { |
| 51 | + it('returns null if currentUser is undefined', async () => { |
| 52 | + expect(await authInternal.getToken()).to.be.null; |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns the id token of the current user correctly', async () => { |
| 56 | + const user = testUser(auth, 'uid'); |
| 57 | + await auth.updateCurrentUser(user); |
| 58 | + user.stsTokenManager.accessToken = 'access-token'; |
| 59 | + user.stsTokenManager.expirationTime = Date.now() + 1000 * 60 * 60 * 24; |
| 60 | + expect(await authInternal.getToken()).to.eql({ |
| 61 | + accessToken: 'access-token' |
| 62 | + }); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + context('token listeners', () => { |
| 67 | + let isProactiveRefresh = false; |
| 68 | + let user: User; |
| 69 | + |
| 70 | + beforeEach(async () => { |
| 71 | + user = testUser(auth, 'uid', undefined, true); |
| 72 | + await auth.updateCurrentUser(user); |
| 73 | + sinon.stub(user.stsTokenManager, 'getToken').returns( |
| 74 | + Promise.resolve({ |
| 75 | + accessToken: 'access-token', |
| 76 | + refreshToken: 'refresh-tken', |
| 77 | + wasRefreshed: true |
| 78 | + }) |
| 79 | + ); |
| 80 | + sinon |
| 81 | + .stub(user, '_startProactiveRefresh') |
| 82 | + .callsFake(() => (isProactiveRefresh = true)); |
| 83 | + sinon |
| 84 | + .stub(user, '_stopProactiveRefresh') |
| 85 | + .callsFake(() => (isProactiveRefresh = false)); |
| 86 | + }); |
| 87 | + |
| 88 | + context('addAuthTokenListener', () => { |
| 89 | + it('gets called with the token, starts proactive refresh', done => { |
| 90 | + // The listener always fires first time. Ignore that one |
| 91 | + let firstCall = true; |
| 92 | + authInternal.addAuthTokenListener(token => { |
| 93 | + if (firstCall) { |
| 94 | + firstCall = false; |
| 95 | + // eslint-disable-next-line @typescript-eslint/no-floating-promises |
| 96 | + user.getIdToken(true); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + expect(token).to.eq('access-token'); |
| 101 | + expect(isProactiveRefresh).to.be.true; |
| 102 | + done(); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('gets called on subsequent updates', async () => { |
| 107 | + let tokenCount = 0; |
| 108 | + authInternal.addAuthTokenListener(() => { |
| 109 | + tokenCount++; |
| 110 | + }); |
| 111 | + |
| 112 | + await user.getIdToken(true); |
| 113 | + await user.getIdToken(true); |
| 114 | + await user.getIdToken(true); |
| 115 | + await user.getIdToken(true); |
| 116 | + |
| 117 | + expect(tokenCount).to.eq(5); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + context('removeAuthTokenListener', () => { |
| 122 | + it('listeners no longer receive token updates', async () => { |
| 123 | + let tokenCount = 0; |
| 124 | + function listener(): void { |
| 125 | + tokenCount++; |
| 126 | + } |
| 127 | + authInternal.addAuthTokenListener(listener); |
| 128 | + |
| 129 | + await user.getIdToken(true); |
| 130 | + expect(tokenCount).to.eq(2); |
| 131 | + authInternal.removeAuthTokenListener(listener); |
| 132 | + await user.getIdToken(true); |
| 133 | + await user.getIdToken(true); |
| 134 | + await user.getIdToken(true); |
| 135 | + expect(tokenCount).to.eq(2); |
| 136 | + }); |
| 137 | + |
| 138 | + it('toggles proactive refresh when listeners fall to 0', () => { |
| 139 | + function listenerA(): void {} |
| 140 | + |
| 141 | + authInternal.addAuthTokenListener(listenerA); |
| 142 | + expect(isProactiveRefresh).to.be.true; |
| 143 | + authInternal.removeAuthTokenListener(listenerA); |
| 144 | + expect(isProactiveRefresh).to.be.false; |
| 145 | + }); |
| 146 | + |
| 147 | + it('toggles proactive refresh when single listener subbed twice', () => { |
| 148 | + function listenerA(): void {} |
| 149 | + |
| 150 | + authInternal.addAuthTokenListener(listenerA); |
| 151 | + authInternal.addAuthTokenListener(listenerA); |
| 152 | + expect(isProactiveRefresh).to.be.true; |
| 153 | + authInternal.removeAuthTokenListener(listenerA); |
| 154 | + expect(isProactiveRefresh).to.be.false; |
| 155 | + }); |
| 156 | + |
| 157 | + it('toggles proactive refresh properly multiple listeners', () => { |
| 158 | + function listenerA(): void {} |
| 159 | + function listenerB(): void {} |
| 160 | + |
| 161 | + authInternal.addAuthTokenListener(listenerA); |
| 162 | + authInternal.addAuthTokenListener(listenerB); |
| 163 | + expect(isProactiveRefresh).to.be.true; |
| 164 | + authInternal.removeAuthTokenListener(listenerA); |
| 165 | + expect(isProactiveRefresh).to.be.true; |
| 166 | + authInternal.removeAuthTokenListener(listenerB); |
| 167 | + expect(isProactiveRefresh).to.be.false; |
| 168 | + |
| 169 | + authInternal.addAuthTokenListener(listenerB); |
| 170 | + expect(isProactiveRefresh).to.be.true; |
| 171 | + }); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments