|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google Inc. |
| 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 { restore, SinonStub, stub } from 'sinon'; |
| 20 | +import { createInstallation, generateAuthToken } from './api'; |
| 21 | +import { |
| 22 | + INSTALLATIONS_API_URL, |
| 23 | + INTERNAL_AUTH_VERSION, |
| 24 | + PACKAGE_VERSION |
| 25 | +} from './constants'; |
| 26 | +import { |
| 27 | + CreateInstallationResponse, |
| 28 | + GenerateAuthTokenResponse |
| 29 | +} from './interfaces/api-response'; |
| 30 | +import { AppConfig } from './interfaces/app-config'; |
| 31 | +import { |
| 32 | + CompletedAuthToken, |
| 33 | + InProgressInstallationEntry, |
| 34 | + RegisteredInstallationEntry, |
| 35 | + RequestStatus |
| 36 | +} from './interfaces/installation-entry'; |
| 37 | +import { compareHeaders } from './testing/compare-headers'; |
| 38 | +import './testing/setup'; |
| 39 | + |
| 40 | +const FID = 'defenders-of-the-faith'; |
| 41 | + |
| 42 | +describe('api', () => { |
| 43 | + let appConfig: AppConfig; |
| 44 | + let fetchSpy: SinonStub<[RequestInfo, RequestInit?], Promise<Response>>; |
| 45 | + |
| 46 | + beforeEach(async () => { |
| 47 | + appConfig = { |
| 48 | + apiKey: 'apiKey', |
| 49 | + projectId: 'projectId', |
| 50 | + appId: '1:777777777777:web:d93b5ca1475efe57' |
| 51 | + }; |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + restore(); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('createInstallation', () => { |
| 59 | + let inProgressInstallationEntry: InProgressInstallationEntry; |
| 60 | + |
| 61 | + beforeEach(() => { |
| 62 | + inProgressInstallationEntry = { |
| 63 | + fid: FID, |
| 64 | + registrationStatus: RequestStatus.IN_PROGRESS, |
| 65 | + registrationTime: Date.now() |
| 66 | + }; |
| 67 | + |
| 68 | + const response: CreateInstallationResponse = { |
| 69 | + refreshToken: 'refreshToken', |
| 70 | + authToken: { |
| 71 | + token: |
| 72 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCeKKF2QT4fwpMeJf36POk6yJV_adQssw5c', |
| 73 | + expiresIn: '604800s' |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + fetchSpy = stub(self, 'fetch').resolves( |
| 78 | + new Response(JSON.stringify(response)) |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('registers a pending InstallationEntry', async () => { |
| 83 | + const registeredInstallationEntry = await createInstallation( |
| 84 | + appConfig, |
| 85 | + inProgressInstallationEntry |
| 86 | + ); |
| 87 | + expect(registeredInstallationEntry.registrationStatus).to.equal( |
| 88 | + RequestStatus.COMPLETED |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('calls the createInstallation server API with correct parameters', async () => { |
| 93 | + const expectedHeaders = new Headers({ |
| 94 | + 'Content-Type': 'application/json', |
| 95 | + Accept: 'application/json', |
| 96 | + 'x-goog-api-key': 'apiKey' |
| 97 | + }); |
| 98 | + const expectedBody = { |
| 99 | + fid: FID, |
| 100 | + authVersion: INTERNAL_AUTH_VERSION, |
| 101 | + appId: appConfig.appId, |
| 102 | + sdkVersion: PACKAGE_VERSION |
| 103 | + }; |
| 104 | + const expectedRequest: RequestInit = { |
| 105 | + method: 'POST', |
| 106 | + headers: expectedHeaders, |
| 107 | + body: JSON.stringify(expectedBody) |
| 108 | + }; |
| 109 | + const expectedEndpoint = `${INSTALLATIONS_API_URL}/projects/projectId/installations`; |
| 110 | + |
| 111 | + await createInstallation(appConfig, inProgressInstallationEntry); |
| 112 | + expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest); |
| 113 | + const actualHeaders = fetchSpy.lastCall.lastArg.headers; |
| 114 | + compareHeaders(expectedHeaders, actualHeaders); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('generateAuthToken', () => { |
| 119 | + let registeredInstallationEntry: RegisteredInstallationEntry; |
| 120 | + |
| 121 | + beforeEach(async () => { |
| 122 | + registeredInstallationEntry = { |
| 123 | + fid: FID, |
| 124 | + registrationStatus: RequestStatus.COMPLETED, |
| 125 | + refreshToken: 'refreshToken', |
| 126 | + authToken: { |
| 127 | + requestStatus: RequestStatus.NOT_STARTED |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + const response: GenerateAuthTokenResponse = { |
| 132 | + token: |
| 133 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCeKKF2QT4fwpMeJf36POk6yJV_adQssw5c', |
| 134 | + expiresIn: '604800s' |
| 135 | + }; |
| 136 | + |
| 137 | + fetchSpy = stub(self, 'fetch').resolves( |
| 138 | + new Response(JSON.stringify(response)) |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it('fetches a new Authentication Token', async () => { |
| 143 | + const completedAuthToken: CompletedAuthToken = await generateAuthToken( |
| 144 | + appConfig, |
| 145 | + registeredInstallationEntry |
| 146 | + ); |
| 147 | + expect(completedAuthToken.requestStatus).to.equal( |
| 148 | + RequestStatus.COMPLETED |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + it('calls the generateAuthToken server API with correct parameters', async () => { |
| 153 | + const expectedHeaders = new Headers({ |
| 154 | + 'Content-Type': 'application/json', |
| 155 | + Accept: 'application/json', |
| 156 | + Authorization: `${INTERNAL_AUTH_VERSION} refreshToken`, |
| 157 | + 'x-goog-api-key': 'apiKey' |
| 158 | + }); |
| 159 | + const expectedBody = { |
| 160 | + installation: { |
| 161 | + sdkVersion: PACKAGE_VERSION |
| 162 | + } |
| 163 | + }; |
| 164 | + const expectedRequest: RequestInit = { |
| 165 | + method: 'POST', |
| 166 | + headers: expectedHeaders, |
| 167 | + body: JSON.stringify(expectedBody) |
| 168 | + }; |
| 169 | + const expectedEndpoint = `${INSTALLATIONS_API_URL}/projects/projectId/installations/${FID}/authTokens:generate`; |
| 170 | + |
| 171 | + await generateAuthToken(appConfig, registeredInstallationEntry); |
| 172 | + |
| 173 | + expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest); |
| 174 | + const actualHeaders = fetchSpy.lastCall.lastArg.headers; |
| 175 | + compareHeaders(expectedHeaders, actualHeaders); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments