mirror of
https://github.com/musix-org/musix-oss
synced 2025-12-15 03:59:20 +00:00
Modules
This commit is contained in:
74
node_modules/@firebase/installations/src/api/common.test.ts
generated
vendored
Normal file
74
node_modules/@firebase/installations/src/api/common.test.ts
generated
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* @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 { SinonStub, stub } from 'sinon';
|
||||
import '../testing/setup';
|
||||
import { retryIfServerError } from './common';
|
||||
|
||||
describe('common', () => {
|
||||
describe('retryIfServerError', () => {
|
||||
let fetchStub: SinonStub<[], Promise<Response>>;
|
||||
|
||||
beforeEach(() => {
|
||||
fetchStub = stub();
|
||||
});
|
||||
|
||||
it('retries once if the server returns a 5xx error', async () => {
|
||||
const expectedResponse = new Response();
|
||||
fetchStub.onCall(0).resolves(new Response(null, { status: 500 }));
|
||||
fetchStub.onCall(1).resolves(expectedResponse);
|
||||
|
||||
await expect(retryIfServerError(fetchStub)).to.eventually.equal(
|
||||
expectedResponse
|
||||
);
|
||||
expect(fetchStub).to.be.calledTwice;
|
||||
});
|
||||
|
||||
it('does not retry again if the server returns a 5xx error twice', async () => {
|
||||
const expectedResponse = new Response(null, { status: 500 });
|
||||
fetchStub.onCall(0).resolves(new Response(null, { status: 500 }));
|
||||
fetchStub.onCall(1).resolves(expectedResponse);
|
||||
fetchStub.onCall(2).resolves(new Response());
|
||||
|
||||
await expect(retryIfServerError(fetchStub)).to.eventually.equal(
|
||||
expectedResponse
|
||||
);
|
||||
expect(fetchStub).to.be.calledTwice;
|
||||
});
|
||||
|
||||
it('does not retry if the error is not 5xx', async () => {
|
||||
const expectedResponse = new Response(null, { status: 404 });
|
||||
fetchStub.resolves(expectedResponse);
|
||||
|
||||
await expect(retryIfServerError(fetchStub)).to.eventually.equal(
|
||||
expectedResponse
|
||||
);
|
||||
expect(fetchStub).to.be.calledOnce;
|
||||
});
|
||||
|
||||
it('does not retry if response is ok', async () => {
|
||||
const expectedResponse = new Response();
|
||||
fetchStub.resolves(expectedResponse);
|
||||
|
||||
await expect(retryIfServerError(fetchStub)).to.eventually.equal(
|
||||
expectedResponse
|
||||
);
|
||||
expect(fetchStub).to.be.calledOnce;
|
||||
});
|
||||
});
|
||||
});
|
||||
111
node_modules/@firebase/installations/src/api/common.ts
generated
vendored
Normal file
111
node_modules/@firebase/installations/src/api/common.ts
generated
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* @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 { FirebaseError } from '@firebase/util';
|
||||
import { GenerateAuthTokenResponse } from '../interfaces/api-response';
|
||||
import { AppConfig } from '../interfaces/app-config';
|
||||
import {
|
||||
CompletedAuthToken,
|
||||
RegisteredInstallationEntry,
|
||||
RequestStatus
|
||||
} from '../interfaces/installation-entry';
|
||||
import {
|
||||
INSTALLATIONS_API_URL,
|
||||
INTERNAL_AUTH_VERSION
|
||||
} from '../util/constants';
|
||||
import { ERROR_FACTORY, ErrorCode } from '../util/errors';
|
||||
|
||||
export function getInstallationsEndpoint({ projectId }: AppConfig): string {
|
||||
return `${INSTALLATIONS_API_URL}/projects/${projectId}/installations`;
|
||||
}
|
||||
|
||||
export function extractAuthTokenInfoFromResponse(
|
||||
response: GenerateAuthTokenResponse
|
||||
): CompletedAuthToken {
|
||||
return {
|
||||
token: response.token,
|
||||
requestStatus: RequestStatus.COMPLETED,
|
||||
expiresIn: getExpiresInFromResponseExpiresIn(response.expiresIn),
|
||||
creationTime: Date.now()
|
||||
};
|
||||
}
|
||||
|
||||
export async function getErrorFromResponse(
|
||||
requestName: string,
|
||||
response: Response
|
||||
): Promise<FirebaseError> {
|
||||
const responseJson: ErrorResponse = await response.json();
|
||||
const errorData = responseJson.error;
|
||||
return ERROR_FACTORY.create(ErrorCode.REQUEST_FAILED, {
|
||||
requestName,
|
||||
serverCode: errorData.code,
|
||||
serverMessage: errorData.message,
|
||||
serverStatus: errorData.status
|
||||
});
|
||||
}
|
||||
|
||||
export function getHeaders({ apiKey }: AppConfig): Headers {
|
||||
return new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
'x-goog-api-key': apiKey
|
||||
});
|
||||
}
|
||||
|
||||
export function getHeadersWithAuth(
|
||||
appConfig: AppConfig,
|
||||
{ refreshToken }: RegisteredInstallationEntry
|
||||
): Headers {
|
||||
const headers = getHeaders(appConfig);
|
||||
headers.append('Authorization', getAuthorizationHeader(refreshToken));
|
||||
return headers;
|
||||
}
|
||||
|
||||
export interface ErrorResponse {
|
||||
error: {
|
||||
code: number;
|
||||
message: string;
|
||||
status: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the passed in fetch wrapper and returns the response.
|
||||
* If the returned response has a status of 5xx, re-runs the function once and
|
||||
* returns the response.
|
||||
*/
|
||||
export async function retryIfServerError(
|
||||
fn: () => Promise<Response>
|
||||
): Promise<Response> {
|
||||
const result = await fn();
|
||||
|
||||
if (result.status >= 500 && result.status < 600) {
|
||||
// Internal Server Error. Retry request.
|
||||
return fn();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function getExpiresInFromResponseExpiresIn(responseExpiresIn: string): number {
|
||||
// This works because the server will never respond with fractions of a second.
|
||||
return Number(responseExpiresIn.replace('s', '000'));
|
||||
}
|
||||
|
||||
function getAuthorizationHeader(refreshToken: string): string {
|
||||
return `${INTERNAL_AUTH_VERSION} ${refreshToken}`;
|
||||
}
|
||||
165
node_modules/@firebase/installations/src/api/create-installation-request.test.ts
generated
vendored
Normal file
165
node_modules/@firebase/installations/src/api/create-installation-request.test.ts
generated
vendored
Normal file
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* @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 { FirebaseError } from '@firebase/util';
|
||||
import { expect } from 'chai';
|
||||
import { SinonStub, stub } from 'sinon';
|
||||
import { CreateInstallationResponse } from '../interfaces/api-response';
|
||||
import { AppConfig } from '../interfaces/app-config';
|
||||
import {
|
||||
InProgressInstallationEntry,
|
||||
RequestStatus
|
||||
} from '../interfaces/installation-entry';
|
||||
import { compareHeaders } from '../testing/compare-headers';
|
||||
import { getFakeAppConfig } from '../testing/fake-generators';
|
||||
import '../testing/setup';
|
||||
import {
|
||||
INSTALLATIONS_API_URL,
|
||||
INTERNAL_AUTH_VERSION,
|
||||
PACKAGE_VERSION
|
||||
} from '../util/constants';
|
||||
import { ErrorResponse } from './common';
|
||||
import { createInstallationRequest } from './create-installation-request';
|
||||
|
||||
const FID = 'defenders-of-the-faith';
|
||||
|
||||
describe('createInstallationRequest', () => {
|
||||
let appConfig: AppConfig;
|
||||
let fetchSpy: SinonStub<[RequestInfo, RequestInit?], Promise<Response>>;
|
||||
let inProgressInstallationEntry: InProgressInstallationEntry;
|
||||
let response: CreateInstallationResponse;
|
||||
|
||||
beforeEach(() => {
|
||||
appConfig = getFakeAppConfig();
|
||||
|
||||
inProgressInstallationEntry = {
|
||||
fid: FID,
|
||||
registrationStatus: RequestStatus.IN_PROGRESS,
|
||||
registrationTime: Date.now()
|
||||
};
|
||||
|
||||
response = {
|
||||
refreshToken: 'refreshToken',
|
||||
authToken: {
|
||||
token:
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
|
||||
expiresIn: '604800s'
|
||||
},
|
||||
fid: FID
|
||||
};
|
||||
fetchSpy = stub(self, 'fetch');
|
||||
});
|
||||
|
||||
describe('successful request', () => {
|
||||
beforeEach(() => {
|
||||
fetchSpy.resolves(new Response(JSON.stringify(response)));
|
||||
});
|
||||
|
||||
it('registers a pending InstallationEntry', async () => {
|
||||
const registeredInstallationEntry = await createInstallationRequest(
|
||||
appConfig,
|
||||
inProgressInstallationEntry
|
||||
);
|
||||
expect(registeredInstallationEntry.registrationStatus).to.equal(
|
||||
RequestStatus.COMPLETED
|
||||
);
|
||||
});
|
||||
|
||||
it('calls the createInstallation server API with correct parameters', async () => {
|
||||
const expectedHeaders = new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
'x-goog-api-key': 'apiKey'
|
||||
});
|
||||
const expectedBody = {
|
||||
fid: FID,
|
||||
authVersion: INTERNAL_AUTH_VERSION,
|
||||
appId: appConfig.appId,
|
||||
sdkVersion: PACKAGE_VERSION
|
||||
};
|
||||
const expectedRequest: RequestInit = {
|
||||
method: 'POST',
|
||||
headers: expectedHeaders,
|
||||
body: JSON.stringify(expectedBody)
|
||||
};
|
||||
const expectedEndpoint = `${INSTALLATIONS_API_URL}/projects/projectId/installations`;
|
||||
|
||||
await createInstallationRequest(appConfig, inProgressInstallationEntry);
|
||||
expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
|
||||
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
|
||||
compareHeaders(expectedHeaders, actualHeaders);
|
||||
});
|
||||
});
|
||||
|
||||
it('returns the FID from the request if the response does not contain one', async () => {
|
||||
response = {
|
||||
refreshToken: 'refreshToken',
|
||||
authToken: {
|
||||
token:
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
|
||||
expiresIn: '604800s'
|
||||
}
|
||||
};
|
||||
fetchSpy.resolves(new Response(JSON.stringify(response)));
|
||||
|
||||
const registeredInstallationEntry = await createInstallationRequest(
|
||||
appConfig,
|
||||
inProgressInstallationEntry
|
||||
);
|
||||
expect(registeredInstallationEntry.fid).to.equal(FID);
|
||||
});
|
||||
|
||||
describe('failed request', () => {
|
||||
it('throws a FirebaseError with the error information from the server', async () => {
|
||||
const errorResponse: ErrorResponse = {
|
||||
error: {
|
||||
code: 409,
|
||||
message: 'Requested entity already exists',
|
||||
status: 'ALREADY_EXISTS'
|
||||
}
|
||||
};
|
||||
|
||||
fetchSpy.resolves(
|
||||
new Response(JSON.stringify(errorResponse), { status: 409 })
|
||||
);
|
||||
|
||||
await expect(
|
||||
createInstallationRequest(appConfig, inProgressInstallationEntry)
|
||||
).to.be.rejectedWith(FirebaseError);
|
||||
});
|
||||
|
||||
it('retries once if the server returns a 5xx error', async () => {
|
||||
const errorResponse: ErrorResponse = {
|
||||
error: {
|
||||
code: 500,
|
||||
message: 'Internal server error',
|
||||
status: 'SERVER_ERROR'
|
||||
}
|
||||
};
|
||||
|
||||
fetchSpy
|
||||
.onCall(0)
|
||||
.resolves(new Response(JSON.stringify(errorResponse), { status: 500 }));
|
||||
fetchSpy.onCall(1).resolves(new Response(JSON.stringify(response)));
|
||||
|
||||
await expect(
|
||||
createInstallationRequest(appConfig, inProgressInstallationEntry)
|
||||
).to.be.fulfilled;
|
||||
expect(fetchSpy).to.be.calledTwice;
|
||||
});
|
||||
});
|
||||
});
|
||||
67
node_modules/@firebase/installations/src/api/create-installation-request.ts
generated
vendored
Normal file
67
node_modules/@firebase/installations/src/api/create-installation-request.ts
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* @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 { CreateInstallationResponse } from '../interfaces/api-response';
|
||||
import { AppConfig } from '../interfaces/app-config';
|
||||
import {
|
||||
InProgressInstallationEntry,
|
||||
RegisteredInstallationEntry,
|
||||
RequestStatus
|
||||
} from '../interfaces/installation-entry';
|
||||
import { INTERNAL_AUTH_VERSION, PACKAGE_VERSION } from '../util/constants';
|
||||
import {
|
||||
extractAuthTokenInfoFromResponse,
|
||||
getErrorFromResponse,
|
||||
getHeaders,
|
||||
getInstallationsEndpoint,
|
||||
retryIfServerError
|
||||
} from './common';
|
||||
|
||||
export async function createInstallationRequest(
|
||||
appConfig: AppConfig,
|
||||
{ fid }: InProgressInstallationEntry
|
||||
): Promise<RegisteredInstallationEntry> {
|
||||
const endpoint = getInstallationsEndpoint(appConfig);
|
||||
|
||||
const headers = getHeaders(appConfig);
|
||||
const body = {
|
||||
fid,
|
||||
authVersion: INTERNAL_AUTH_VERSION,
|
||||
appId: appConfig.appId,
|
||||
sdkVersion: PACKAGE_VERSION
|
||||
};
|
||||
|
||||
const request: RequestInit = {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(body)
|
||||
};
|
||||
|
||||
const response = await retryIfServerError(() => fetch(endpoint, request));
|
||||
if (response.ok) {
|
||||
const responseValue: CreateInstallationResponse = await response.json();
|
||||
const registeredInstallationEntry: RegisteredInstallationEntry = {
|
||||
fid: responseValue.fid || fid,
|
||||
registrationStatus: RequestStatus.COMPLETED,
|
||||
refreshToken: responseValue.refreshToken,
|
||||
authToken: extractAuthTokenInfoFromResponse(responseValue.authToken)
|
||||
};
|
||||
return registeredInstallationEntry;
|
||||
} else {
|
||||
throw await getErrorFromResponse('Create Installation', response);
|
||||
}
|
||||
}
|
||||
123
node_modules/@firebase/installations/src/api/delete-installation-request.test.ts
generated
vendored
Normal file
123
node_modules/@firebase/installations/src/api/delete-installation-request.test.ts
generated
vendored
Normal file
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* @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 { FirebaseError } from '@firebase/util';
|
||||
import { expect } from 'chai';
|
||||
import { SinonStub, stub } from 'sinon';
|
||||
import { AppConfig } from '../interfaces/app-config';
|
||||
import {
|
||||
RegisteredInstallationEntry,
|
||||
RequestStatus
|
||||
} from '../interfaces/installation-entry';
|
||||
import { compareHeaders } from '../testing/compare-headers';
|
||||
import { getFakeAppConfig } from '../testing/fake-generators';
|
||||
import '../testing/setup';
|
||||
import {
|
||||
INSTALLATIONS_API_URL,
|
||||
INTERNAL_AUTH_VERSION
|
||||
} from '../util/constants';
|
||||
import { ErrorResponse } from './common';
|
||||
import { deleteInstallationRequest } from './delete-installation-request';
|
||||
|
||||
const FID = 'foreclosure-of-a-dream';
|
||||
|
||||
describe('deleteInstallationRequest', () => {
|
||||
let appConfig: AppConfig;
|
||||
let fetchSpy: SinonStub<[RequestInfo, RequestInit?], Promise<Response>>;
|
||||
let registeredInstallationEntry: RegisteredInstallationEntry;
|
||||
|
||||
beforeEach(() => {
|
||||
appConfig = getFakeAppConfig();
|
||||
|
||||
registeredInstallationEntry = {
|
||||
fid: FID,
|
||||
registrationStatus: RequestStatus.COMPLETED,
|
||||
refreshToken: 'refreshToken',
|
||||
authToken: {
|
||||
requestStatus: RequestStatus.NOT_STARTED
|
||||
}
|
||||
};
|
||||
|
||||
fetchSpy = stub(self, 'fetch');
|
||||
});
|
||||
|
||||
describe('successful request', () => {
|
||||
beforeEach(() => {
|
||||
fetchSpy.resolves(new Response());
|
||||
});
|
||||
|
||||
it('calls the deleteInstallation server API with correct parameters', async () => {
|
||||
const expectedHeaders = new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
Authorization: `${INTERNAL_AUTH_VERSION} refreshToken`,
|
||||
'x-goog-api-key': 'apiKey'
|
||||
});
|
||||
const expectedRequest: RequestInit = {
|
||||
method: 'DELETE',
|
||||
headers: expectedHeaders
|
||||
};
|
||||
const expectedEndpoint = `${INSTALLATIONS_API_URL}/projects/projectId/installations/${FID}`;
|
||||
|
||||
await deleteInstallationRequest(appConfig, registeredInstallationEntry);
|
||||
|
||||
expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
|
||||
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
|
||||
compareHeaders(expectedHeaders, actualHeaders);
|
||||
});
|
||||
});
|
||||
|
||||
describe('failed request', () => {
|
||||
it('throws a FirebaseError with the error information from the server', async () => {
|
||||
const errorResponse: ErrorResponse = {
|
||||
error: {
|
||||
code: 409,
|
||||
message: 'Requested entity already exists',
|
||||
status: 'ALREADY_EXISTS'
|
||||
}
|
||||
};
|
||||
|
||||
fetchSpy.resolves(
|
||||
new Response(JSON.stringify(errorResponse), { status: 409 })
|
||||
);
|
||||
|
||||
await expect(
|
||||
deleteInstallationRequest(appConfig, registeredInstallationEntry)
|
||||
).to.be.rejectedWith(FirebaseError);
|
||||
});
|
||||
|
||||
it('retries once if the server returns a 5xx error', async () => {
|
||||
const errorResponse: ErrorResponse = {
|
||||
error: {
|
||||
code: 500,
|
||||
message: 'Internal server error',
|
||||
status: 'SERVER_ERROR'
|
||||
}
|
||||
};
|
||||
|
||||
fetchSpy
|
||||
.onCall(0)
|
||||
.resolves(new Response(JSON.stringify(errorResponse), { status: 500 }));
|
||||
fetchSpy.onCall(1).resolves(new Response());
|
||||
|
||||
await expect(
|
||||
deleteInstallationRequest(appConfig, registeredInstallationEntry)
|
||||
).to.be.fulfilled;
|
||||
expect(fetchSpy).to.be.calledTwice;
|
||||
});
|
||||
});
|
||||
});
|
||||
50
node_modules/@firebase/installations/src/api/delete-installation-request.ts
generated
vendored
Normal file
50
node_modules/@firebase/installations/src/api/delete-installation-request.ts
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* @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 { AppConfig } from '../interfaces/app-config';
|
||||
import { RegisteredInstallationEntry } from '../interfaces/installation-entry';
|
||||
import {
|
||||
getErrorFromResponse,
|
||||
getHeadersWithAuth,
|
||||
getInstallationsEndpoint,
|
||||
retryIfServerError
|
||||
} from './common';
|
||||
|
||||
export async function deleteInstallationRequest(
|
||||
appConfig: AppConfig,
|
||||
installationEntry: RegisteredInstallationEntry
|
||||
): Promise<void> {
|
||||
const endpoint = getDeleteEndpoint(appConfig, installationEntry);
|
||||
|
||||
const headers = getHeadersWithAuth(appConfig, installationEntry);
|
||||
const request: RequestInit = {
|
||||
method: 'DELETE',
|
||||
headers
|
||||
};
|
||||
|
||||
const response = await retryIfServerError(() => fetch(endpoint, request));
|
||||
if (!response.ok) {
|
||||
throw await getErrorFromResponse('Delete Installation', response);
|
||||
}
|
||||
}
|
||||
|
||||
function getDeleteEndpoint(
|
||||
appConfig: AppConfig,
|
||||
{ fid }: RegisteredInstallationEntry
|
||||
): string {
|
||||
return `${getInstallationsEndpoint(appConfig)}/${fid}`;
|
||||
}
|
||||
150
node_modules/@firebase/installations/src/api/generate-auth-token-request.test.ts
generated
vendored
Normal file
150
node_modules/@firebase/installations/src/api/generate-auth-token-request.test.ts
generated
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* @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 { FirebaseError } from '@firebase/util';
|
||||
import { expect } from 'chai';
|
||||
import { SinonStub, stub } from 'sinon';
|
||||
import { GenerateAuthTokenResponse } from '../interfaces/api-response';
|
||||
import { FirebaseDependencies } from '../interfaces/firebase-dependencies';
|
||||
import {
|
||||
CompletedAuthToken,
|
||||
RegisteredInstallationEntry,
|
||||
RequestStatus
|
||||
} from '../interfaces/installation-entry';
|
||||
import { compareHeaders } from '../testing/compare-headers';
|
||||
import { getFakeDependencies } from '../testing/fake-generators';
|
||||
import '../testing/setup';
|
||||
import {
|
||||
INSTALLATIONS_API_URL,
|
||||
INTERNAL_AUTH_VERSION,
|
||||
PACKAGE_VERSION
|
||||
} from '../util/constants';
|
||||
import { ErrorResponse } from './common';
|
||||
import { generateAuthTokenRequest } from './generate-auth-token-request';
|
||||
|
||||
const FID = 'evil-has-no-boundaries';
|
||||
|
||||
describe('generateAuthTokenRequest', () => {
|
||||
let dependencies: FirebaseDependencies;
|
||||
let fetchSpy: SinonStub<[RequestInfo, RequestInit?], Promise<Response>>;
|
||||
let registeredInstallationEntry: RegisteredInstallationEntry;
|
||||
let response: GenerateAuthTokenResponse;
|
||||
|
||||
beforeEach(() => {
|
||||
dependencies = getFakeDependencies();
|
||||
|
||||
registeredInstallationEntry = {
|
||||
fid: FID,
|
||||
registrationStatus: RequestStatus.COMPLETED,
|
||||
refreshToken: 'refreshToken',
|
||||
authToken: {
|
||||
requestStatus: RequestStatus.NOT_STARTED
|
||||
}
|
||||
};
|
||||
|
||||
response = {
|
||||
token:
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
|
||||
expiresIn: '604800s'
|
||||
};
|
||||
|
||||
fetchSpy = stub(self, 'fetch');
|
||||
});
|
||||
|
||||
describe('successful request', () => {
|
||||
beforeEach(() => {
|
||||
fetchSpy.resolves(new Response(JSON.stringify(response)));
|
||||
});
|
||||
|
||||
it('fetches a new Authentication Token', async () => {
|
||||
const completedAuthToken: CompletedAuthToken = await generateAuthTokenRequest(
|
||||
dependencies,
|
||||
registeredInstallationEntry
|
||||
);
|
||||
expect(completedAuthToken.requestStatus).to.equal(
|
||||
RequestStatus.COMPLETED
|
||||
);
|
||||
});
|
||||
|
||||
it('calls the generateAuthToken server API with correct parameters', async () => {
|
||||
const expectedHeaders = new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
Authorization: `${INTERNAL_AUTH_VERSION} refreshToken`,
|
||||
'x-goog-api-key': 'apiKey',
|
||||
'x-firebase-client': 'a/1.2.3 b/2.3.4'
|
||||
});
|
||||
const expectedBody = {
|
||||
installation: {
|
||||
sdkVersion: PACKAGE_VERSION
|
||||
}
|
||||
};
|
||||
const expectedRequest: RequestInit = {
|
||||
method: 'POST',
|
||||
headers: expectedHeaders,
|
||||
body: JSON.stringify(expectedBody)
|
||||
};
|
||||
const expectedEndpoint = `${INSTALLATIONS_API_URL}/projects/projectId/installations/${FID}/authTokens:generate`;
|
||||
|
||||
await generateAuthTokenRequest(dependencies, registeredInstallationEntry);
|
||||
|
||||
expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
|
||||
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
|
||||
compareHeaders(expectedHeaders, actualHeaders);
|
||||
});
|
||||
});
|
||||
|
||||
describe('failed request', () => {
|
||||
it('throws a FirebaseError with the error information from the server', async () => {
|
||||
const errorResponse: ErrorResponse = {
|
||||
error: {
|
||||
code: 409,
|
||||
message: 'Requested entity already exists',
|
||||
status: 'ALREADY_EXISTS'
|
||||
}
|
||||
};
|
||||
|
||||
fetchSpy.resolves(
|
||||
new Response(JSON.stringify(errorResponse), { status: 409 })
|
||||
);
|
||||
|
||||
await expect(
|
||||
generateAuthTokenRequest(dependencies, registeredInstallationEntry)
|
||||
).to.be.rejectedWith(FirebaseError);
|
||||
});
|
||||
|
||||
it('retries once if the server returns a 5xx error', async () => {
|
||||
const errorResponse: ErrorResponse = {
|
||||
error: {
|
||||
code: 500,
|
||||
message: 'Internal server error',
|
||||
status: 'SERVER_ERROR'
|
||||
}
|
||||
};
|
||||
|
||||
fetchSpy
|
||||
.onCall(0)
|
||||
.resolves(new Response(JSON.stringify(errorResponse), { status: 500 }));
|
||||
fetchSpy.onCall(1).resolves(new Response(JSON.stringify(response)));
|
||||
|
||||
await expect(
|
||||
generateAuthTokenRequest(dependencies, registeredInstallationEntry)
|
||||
).to.be.fulfilled;
|
||||
expect(fetchSpy).to.be.calledTwice;
|
||||
});
|
||||
});
|
||||
});
|
||||
79
node_modules/@firebase/installations/src/api/generate-auth-token-request.ts
generated
vendored
Normal file
79
node_modules/@firebase/installations/src/api/generate-auth-token-request.ts
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* @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 { GenerateAuthTokenResponse } from '../interfaces/api-response';
|
||||
import { AppConfig } from '../interfaces/app-config';
|
||||
import { FirebaseDependencies } from '../interfaces/firebase-dependencies';
|
||||
import {
|
||||
CompletedAuthToken,
|
||||
RegisteredInstallationEntry
|
||||
} from '../interfaces/installation-entry';
|
||||
import { PACKAGE_VERSION } from '../util/constants';
|
||||
import {
|
||||
extractAuthTokenInfoFromResponse,
|
||||
getErrorFromResponse,
|
||||
getHeadersWithAuth,
|
||||
getInstallationsEndpoint,
|
||||
retryIfServerError
|
||||
} from './common';
|
||||
|
||||
export async function generateAuthTokenRequest(
|
||||
{ appConfig, platformLoggerProvider }: FirebaseDependencies,
|
||||
installationEntry: RegisteredInstallationEntry
|
||||
): Promise<CompletedAuthToken> {
|
||||
const endpoint = getGenerateAuthTokenEndpoint(appConfig, installationEntry);
|
||||
|
||||
const headers = getHeadersWithAuth(appConfig, installationEntry);
|
||||
|
||||
// If platform logger exists, add the platform info string to the header.
|
||||
const platformLogger = platformLoggerProvider.getImmediate({
|
||||
optional: true
|
||||
});
|
||||
if (platformLogger) {
|
||||
headers.append('x-firebase-client', platformLogger.getPlatformInfoString());
|
||||
}
|
||||
|
||||
const body = {
|
||||
installation: {
|
||||
sdkVersion: PACKAGE_VERSION
|
||||
}
|
||||
};
|
||||
|
||||
const request: RequestInit = {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(body)
|
||||
};
|
||||
|
||||
const response = await retryIfServerError(() => fetch(endpoint, request));
|
||||
if (response.ok) {
|
||||
const responseValue: GenerateAuthTokenResponse = await response.json();
|
||||
const completedAuthToken: CompletedAuthToken = extractAuthTokenInfoFromResponse(
|
||||
responseValue
|
||||
);
|
||||
return completedAuthToken;
|
||||
} else {
|
||||
throw await getErrorFromResponse('Generate Auth Token', response);
|
||||
}
|
||||
}
|
||||
|
||||
function getGenerateAuthTokenEndpoint(
|
||||
appConfig: AppConfig,
|
||||
{ fid }: RegisteredInstallationEntry
|
||||
): string {
|
||||
return `${getInstallationsEndpoint(appConfig)}/${fid}/authTokens:generate`;
|
||||
}
|
||||
Reference in New Issue
Block a user