Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 5x 10x 5x 5x 1x 2x 1x 1x 2x 2x 1x 3x 3x 6x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 2x 2x 1x 1x 5x 5x 10x 1x 2x 2x 1x 1x 2x 1x 1x 1x 2x 2x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x | /** * @license * Copyright 2019 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { expect } from 'chai'; import { SinonFakeTimers, SinonStub, stub, useFakeTimers } from 'sinon'; import * as generateAuthTokenModule from '../api/generate-auth-token'; import { AppConfig } from '../interfaces/app-config'; import { CompletedAuthToken, RegisteredInstallationEntry, RequestStatus, UnregisteredInstallationEntry } from '../interfaces/installation-entry'; import { getFakeAppConfig } from '../testing/get-fake-app'; import '../testing/setup'; import { TOKEN_EXPIRATION_BUFFER } from '../util/constants'; import { sleep } from '../util/sleep'; import { get, set } from './idb-manager'; import { refreshAuthToken } from './refresh-auth-token'; const FID = 'carry-the-blessed-home'; const AUTH_TOKEN = 'authTokenFromServer'; const DB_AUTH_TOKEN = 'authTokenFromDB'; const ONE_WEEK_MS = 7 * 24 * 60 * 60 * 1000; describe('refreshAuthToken', () => { let appConfig: AppConfig; let generateAuthTokenSpy: SinonStub< [AppConfig, RegisteredInstallationEntry], Promise<CompletedAuthToken> >; beforeEach(() => { appConfig = getFakeAppConfig(); generateAuthTokenSpy = stub( generateAuthTokenModule, 'generateAuthToken' ).callsFake(async () => { await sleep(100); // Request would take some time const result: CompletedAuthToken = { token: AUTH_TOKEN, expiresIn: ONE_WEEK_MS, requestStatus: RequestStatus.COMPLETED, creationTime: Date.now() }; return result; }); }); it('throws when there is no installation in the DB', async () => { await expect(refreshAuthToken(appConfig)).to.be.rejected; }); it('throws when there is an unregistered installation in the db', async () => { const installationEntry: UnregisteredInstallationEntry = { fid: FID, registrationStatus: RequestStatus.NOT_STARTED }; await set(appConfig, installationEntry); await expect(refreshAuthToken(appConfig)).to.be.rejected; }); describe('when there is a valid auth token in the DB', () => { beforeEach(async () => { const installationEntry: RegisteredInstallationEntry = { fid: FID, registrationStatus: RequestStatus.COMPLETED, refreshToken: 'refreshToken', authToken: { token: AUTH_TOKEN, expiresIn: ONE_WEEK_MS, requestStatus: RequestStatus.COMPLETED, creationTime: Date.now() } }; await set(appConfig, installationEntry); }); it('returns the token from the DB', async () => { const { token } = await refreshAuthToken(appConfig); expect(token).to.equal(AUTH_TOKEN); }); it('does not call any server APIs', async () => { await refreshAuthToken(appConfig); expect(generateAuthTokenSpy).not.to.be.called; }); it('works even if the app is offline', async () => { stub(navigator, 'onLine').value(false); const { token } = await refreshAuthToken(appConfig); expect(token).to.equal(AUTH_TOKEN); }); }); describe('when there is an auth token that is about to expire in the DB', () => { let clock: SinonFakeTimers; beforeEach(async () => { clock = useFakeTimers({ shouldAdvanceTime: true }); const installationEntry: RegisteredInstallationEntry = { fid: FID, registrationStatus: RequestStatus.COMPLETED, refreshToken: 'refreshToken', authToken: { token: DB_AUTH_TOKEN, expiresIn: ONE_WEEK_MS, requestStatus: RequestStatus.COMPLETED, creationTime: // Expires in ten minutes Date.now() - ONE_WEEK_MS + TOKEN_EXPIRATION_BUFFER + 10 * 60 * 1000 } }; await set(appConfig, installationEntry); }); it('returns a different token after expiration', async () => { const token1 = await refreshAuthToken(appConfig); expect(token1.token).to.equal(DB_AUTH_TOKEN); // Wait 30 minutes. clock.tick('30:00'); const token2 = await refreshAuthToken(appConfig); await expect(token2.token).to.equal(AUTH_TOKEN); await expect(token2.token).not.to.equal(DB_AUTH_TOKEN); expect(generateAuthTokenSpy).to.be.calledOnce; }); }); describe('when there is an expired auth token in the DB', () => { beforeEach(async () => { const installationEntry: RegisteredInstallationEntry = { fid: FID, registrationStatus: RequestStatus.COMPLETED, refreshToken: 'refreshToken', authToken: { token: DB_AUTH_TOKEN, expiresIn: ONE_WEEK_MS, requestStatus: RequestStatus.COMPLETED, creationTime: Date.now() - 2 * ONE_WEEK_MS } }; await set(appConfig, installationEntry); }); it('does not call generateAuthToken twice on subsequent calls', async () => { await refreshAuthToken(appConfig); await refreshAuthToken(appConfig); expect(generateAuthTokenSpy).to.be.calledOnce; }); it('does not call generateAuthToken twice on simultaneous calls', async () => { await Promise.all([ refreshAuthToken(appConfig), refreshAuthToken(appConfig) ]); expect(generateAuthTokenSpy).to.be.calledOnce; }); it('returns a new token', async () => { const { token } = await refreshAuthToken(appConfig); await expect(token).to.equal(AUTH_TOKEN); await expect(token).not.to.equal(DB_AUTH_TOKEN); expect(generateAuthTokenSpy).to.be.calledOnce; }); it('throws if the app is offline', async () => { stub(navigator, 'onLine').value(false); await expect(refreshAuthToken(appConfig)).to.be.rejected; }); it('saves the new token in the DB', async () => { const { token } = await refreshAuthToken(appConfig); const installationEntry = (await get( appConfig )) as RegisteredInstallationEntry; expect(installationEntry).not.to.be.undefined; expect(installationEntry.registrationStatus).to.equal( RequestStatus.COMPLETED ); const authToken = installationEntry.authToken as CompletedAuthToken; expect(authToken.requestStatus).to.equal(RequestStatus.COMPLETED); expect(authToken.token).to.equal(token); }); }); }); |