Skip to content

Commit

Permalink
Log to createInstallation endpoint also
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 committed Feb 17, 2022
1 parent ba871b2 commit 4133773
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 71 deletions.
7 changes: 2 additions & 5 deletions packages/installations/src/api/get-id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@ import {
import { getFakeInstallations } from '../testing/fake-generators';
import '../testing/setup';
import { getId } from './get-id';
import {
FirebaseInstallationsImpl,
AppConfig
} from '../interfaces/installation-impl';
import { FirebaseInstallationsImpl } from '../interfaces/installation-impl';

const FID = 'disciples-of-the-watch';

describe('getId', () => {
let installations: FirebaseInstallationsImpl;
let getInstallationEntrySpy: SinonStub<
[AppConfig],
[FirebaseInstallationsImpl],
Promise<getInstallationEntryModule.InstallationEntryWithRegistrationPromise>
>;

Expand Down
2 changes: 1 addition & 1 deletion packages/installations/src/api/get-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { Installations } from '../interfaces/public-types';
export async function getId(installations: Installations): Promise<string> {
const installationsImpl = installations as FirebaseInstallationsImpl;
const { installationEntry, registrationPromise } = await getInstallationEntry(
installationsImpl.appConfig
installationsImpl
);

if (registrationPromise) {
Expand Down
2 changes: 1 addition & 1 deletion packages/installations/src/api/get-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const setupInstallationEntryMap: Map<
describe('getToken', () => {
let installations: FirebaseInstallationsImpl;
let createInstallationRequestSpy: SinonStub<
[AppConfig, InProgressInstallationEntry],
[FirebaseInstallationsImpl, InProgressInstallationEntry],
Promise<RegisteredInstallationEntry>
>;
let generateAuthTokenRequestSpy: SinonStub<
Expand Down
9 changes: 4 additions & 5 deletions packages/installations/src/api/get-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import { getInstallationEntry } from '../helpers/get-installation-entry';
import { refreshAuthToken } from '../helpers/refresh-auth-token';
import {
FirebaseInstallationsImpl,
AppConfig
FirebaseInstallationsImpl
} from '../interfaces/installation-impl';
import { Installations } from '../interfaces/public-types';

Expand All @@ -36,7 +35,7 @@ export async function getToken(
forceRefresh = false
): Promise<string> {
const installationsImpl = installations as FirebaseInstallationsImpl;
await completeInstallationRegistration(installationsImpl.appConfig);
await completeInstallationRegistration(installationsImpl);

// At this point we either have a Registered Installation in the DB, or we've
// already thrown an error.
Expand All @@ -45,9 +44,9 @@ export async function getToken(
}

async function completeInstallationRegistration(
appConfig: AppConfig
installations: FirebaseInstallationsImpl
): Promise<void> {
const { registrationPromise } = await getInstallationEntry(appConfig);
const { registrationPromise } = await getInstallationEntry(installations);

if (registrationPromise) {
// A createInstallation request is in progress. Wait until it finishes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import { FirebaseError } from '@firebase/util';
import { expect } from 'chai';
import { SinonStub, stub } from 'sinon';
import { CreateInstallationResponse } from '../interfaces/api-response';
import { AppConfig } from '../interfaces/installation-impl';
import {
FirebaseInstallationsImpl
} from '../interfaces/installation-impl';
import {
InProgressInstallationEntry,
RequestStatus
} from '../interfaces/installation-entry';
import { compareHeaders } from '../testing/compare-headers';
import { getFakeAppConfig } from '../testing/fake-generators';
import { getFakeInstallations } from '../testing/fake-generators';
import '../testing/setup';
import {
INSTALLATIONS_API_URL,
Expand All @@ -38,13 +40,13 @@ import { createInstallationRequest } from './create-installation-request';
const FID = 'defenders-of-the-faith';

describe('createInstallationRequest', () => {
let appConfig: AppConfig;
let fakeInstallations: FirebaseInstallationsImpl;
let fetchSpy: SinonStub<[RequestInfo, RequestInit?], Promise<Response>>;
let inProgressInstallationEntry: InProgressInstallationEntry;
let response: CreateInstallationResponse;

beforeEach(() => {
appConfig = getFakeAppConfig();
fakeInstallations = getFakeInstallations();

inProgressInstallationEntry = {
fid: FID,
Expand All @@ -71,7 +73,7 @@ describe('createInstallationRequest', () => {

it('registers a pending InstallationEntry', async () => {
const registeredInstallationEntry = await createInstallationRequest(
appConfig,
fakeInstallations,
inProgressInstallationEntry
);
expect(registeredInstallationEntry.registrationStatus).to.equal(
Expand All @@ -83,12 +85,13 @@ describe('createInstallationRequest', () => {
const expectedHeaders = new Headers({
'Content-Type': 'application/json',
Accept: 'application/json',
'x-goog-api-key': 'apiKey'
'x-goog-api-key': 'apiKey',
'x-firebase-client': 'a/1.2.3 b/2.3.4'
});
const expectedBody = {
fid: FID,
authVersion: INTERNAL_AUTH_VERSION,
appId: appConfig.appId,
appId: fakeInstallations.appConfig.appId,
sdkVersion: PACKAGE_VERSION
};
const expectedRequest: RequestInit = {
Expand All @@ -98,7 +101,10 @@ describe('createInstallationRequest', () => {
};
const expectedEndpoint = `${INSTALLATIONS_API_URL}/projects/projectId/installations`;

await createInstallationRequest(appConfig, inProgressInstallationEntry);
await createInstallationRequest(
fakeInstallations,
inProgressInstallationEntry
);
expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
compareHeaders(expectedHeaders, actualHeaders);
Expand All @@ -117,7 +123,7 @@ describe('createInstallationRequest', () => {
fetchSpy.resolves(new Response(JSON.stringify(response)));

const registeredInstallationEntry = await createInstallationRequest(
appConfig,
fakeInstallations,
inProgressInstallationEntry
);
expect(registeredInstallationEntry.fid).to.equal(FID);
Expand All @@ -138,7 +144,10 @@ describe('createInstallationRequest', () => {
);

await expect(
createInstallationRequest(appConfig, inProgressInstallationEntry)
createInstallationRequest(
fakeInstallations,
inProgressInstallationEntry
)
).to.be.rejectedWith(FirebaseError);
});

Expand All @@ -157,7 +166,10 @@ describe('createInstallationRequest', () => {
fetchSpy.onCall(1).resolves(new Response(JSON.stringify(response)));

await expect(
createInstallationRequest(appConfig, inProgressInstallationEntry)
createInstallationRequest(
fakeInstallations,
inProgressInstallationEntry
)
).to.be.fulfilled;
expect(fetchSpy).to.be.calledTwice;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,25 @@ import {
getInstallationsEndpoint,
retryIfServerError
} from './common';
import { AppConfig } from '../interfaces/installation-impl';
import { FirebaseInstallationsImpl } from '../interfaces/installation-impl';

export async function createInstallationRequest(
appConfig: AppConfig,
{ appConfig, heartbeatServiceProvider }: FirebaseInstallationsImpl,
{ fid }: InProgressInstallationEntry
): Promise<RegisteredInstallationEntry> {
const endpoint = getInstallationsEndpoint(appConfig);

const headers = getHeaders(appConfig);

// If heartbeat service exists, add the heartbeat string to the header.
const heartbeatService = heartbeatServiceProvider.getImmediate({
optional: true
});
if (heartbeatService) {
const heartbeatsHeader = await heartbeatService.getHeartbeatsHeader();
headers.append('x-firebase-client', heartbeatsHeader);
}

const body = {
fid,
authVersion: INTERNAL_AUTH_VERSION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ describe('generateAuthTokenRequest', () => {
});
const expectedBody = {
installation: {
sdkVersion: PACKAGE_VERSION
sdkVersion: PACKAGE_VERSION,
appId: installations.appConfig.appId
}
};
const expectedRequest: RequestInit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export async function generateAuthTokenRequest(

const body = {
installation: {
sdkVersion: PACKAGE_VERSION
sdkVersion: PACKAGE_VERSION,
appId: appConfig.appId
}
};

Expand Down
Loading

0 comments on commit 4133773

Please sign in to comment.