Skip to content

Fix throwing a Promise object instead of Error #1826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 69 additions & 42 deletions packages/installations/src/api/create-installation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

import { FirebaseError } from '@firebase/util';
import { expect } from 'chai';
import { SinonStub, stub } from 'sinon';
import { CreateInstallationResponse } from '../interfaces/api-response';
Expand All @@ -35,7 +36,7 @@ import { createInstallation } from './create-installation';

const FID = 'defenders-of-the-faith';

describe('api', () => {
describe('createInstallation', () => {
let appConfig: AppConfig;
let fetchSpy: SinonStub<[RequestInfo, RequestInit?], Promise<Response>>;
let inProgressInstallationEntry: InProgressInstallationEntry;
Expand All @@ -48,53 +49,79 @@ describe('api', () => {
registrationStatus: RequestStatus.IN_PROGRESS,
registrationTime: Date.now()
};
});

const response: CreateInstallationResponse = {
refreshToken: 'refreshToken',
authToken: {
token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
expiresIn: '604800s'
}
};
describe('successful request', () => {
beforeEach(() => {
const response: CreateInstallationResponse = {
refreshToken: 'refreshToken',
authToken: {
token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
expiresIn: '604800s'
}
};

fetchSpy = stub(self, 'fetch').resolves(
new Response(JSON.stringify(response))
);
});
fetchSpy = stub(self, 'fetch').resolves(
new Response(JSON.stringify(response))
);
});

it('registers a pending InstallationEntry', async () => {
const registeredInstallationEntry = await createInstallation(
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`;

it('registers a pending InstallationEntry', async () => {
const registeredInstallationEntry = await createInstallation(
appConfig,
inProgressInstallationEntry
);
expect(registeredInstallationEntry.registrationStatus).to.equal(
RequestStatus.COMPLETED
);
await createInstallation(appConfig, inProgressInstallationEntry);
expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
compareHeaders(expectedHeaders, actualHeaders);
});
});

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'
describe('failed request', () => {
beforeEach(() => {
const response = {
error: {
code: 409,
message: 'Requested entity already exists',
status: 'ALREADY_EXISTS'
}
};

fetchSpy = stub(self, 'fetch').resolves(
new Response(JSON.stringify(response), { status: 409 })
);
});
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 createInstallation(appConfig, inProgressInstallationEntry);
expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
compareHeaders(expectedHeaders, actualHeaders);
it('throws a FirebaseError with the error information from the server', async () => {
await expect(
createInstallation(appConfig, inProgressInstallationEntry)
).to.be.rejectedWith(FirebaseError);
});
});
});
2 changes: 1 addition & 1 deletion packages/installations/src/api/create-installation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export async function createInstallation(
};
return registeredInstallationEntry;
} else {
throw getErrorFromResponse('Create Installation', response);
throw await getErrorFromResponse('Create Installation', response);
}
}
61 changes: 44 additions & 17 deletions packages/installations/src/api/delete-installation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

import { FirebaseError } from '@firebase/util';
import { expect } from 'chai';
import { SinonStub, stub } from 'sinon';
import { AppConfig } from '../interfaces/app-config';
Expand Down Expand Up @@ -49,27 +50,53 @@ describe('deleteInstallation', () => {
requestStatus: RequestStatus.NOT_STARTED
}
};

fetchSpy = stub(self, 'fetch').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'
describe('successful request', () => {
beforeEach(() => {
fetchSpy = stub(self, 'fetch').resolves(new Response());
});
const expectedRequest: RequestInit = {
method: 'DELETE',
headers: expectedHeaders
};
const expectedEndpoint = `${INSTALLATIONS_API_URL}/projects/projectId/installations/${FID}`;

await deleteInstallation(appConfig, registeredInstallationEntry);
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 deleteInstallation(appConfig, registeredInstallationEntry);

expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
compareHeaders(expectedHeaders, actualHeaders);
});
});

describe('failed request', () => {
beforeEach(() => {
const response = {
error: {
code: 409,
message: 'Requested entity already exists',
status: 'ALREADY_EXISTS'
}
};

expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
compareHeaders(expectedHeaders, actualHeaders);
fetchSpy = stub(self, 'fetch').resolves(
new Response(JSON.stringify(response), { status: 409 })
);
});

it('throws a FirebaseError with the error information from the server', async () => {
await expect(
deleteInstallation(appConfig, registeredInstallationEntry)
).to.be.rejectedWith(FirebaseError);
});
});
});
2 changes: 1 addition & 1 deletion packages/installations/src/api/delete-installation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function deleteInstallation(

const response = await fetch(endpoint, request);
if (!response.ok) {
throw getErrorFromResponse('Delete Installation', response);
throw await getErrorFromResponse('Delete Installation', response);
}
}

Expand Down
103 changes: 66 additions & 37 deletions packages/installations/src/api/generate-auth-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

import { FirebaseError } from '@firebase/util';
import { expect } from 'chai';
import { SinonStub, stub } from 'sinon';
import { GenerateAuthTokenResponse } from '../interfaces/api-response';
Expand Down Expand Up @@ -52,49 +53,77 @@ describe('generateAuthToken', () => {
requestStatus: RequestStatus.NOT_STARTED
}
};
});

const response: GenerateAuthTokenResponse = {
token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
expiresIn: '604800s'
};
describe('successful request', () => {
beforeEach(() => {
const response: GenerateAuthTokenResponse = {
token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
expiresIn: '604800s'
};

fetchSpy = stub(self, 'fetch').resolves(
new Response(JSON.stringify(response))
);
});
fetchSpy = stub(self, 'fetch').resolves(
new Response(JSON.stringify(response))
);
});

it('fetches a new Authentication Token', async () => {
const completedAuthToken: CompletedAuthToken = await generateAuthToken(
appConfig,
registeredInstallationEntry
);
expect(completedAuthToken.requestStatus).to.equal(RequestStatus.COMPLETED);
});
it('fetches a new Authentication Token', async () => {
const completedAuthToken: CompletedAuthToken = await generateAuthToken(
appConfig,
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'
});
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 generateAuthToken(appConfig, registeredInstallationEntry);

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'
expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
compareHeaders(expectedHeaders, actualHeaders);
});
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 generateAuthToken(appConfig, registeredInstallationEntry);
describe('failed request', () => {
beforeEach(() => {
const response = {
error: {
code: 409,
message: 'Requested entity already exists',
status: 'ALREADY_EXISTS'
}
};

expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
compareHeaders(expectedHeaders, actualHeaders);
fetchSpy = stub(self, 'fetch').resolves(
new Response(JSON.stringify(response), { status: 409 })
);
});

it('throws a FirebaseError with the error information from the server', async () => {
await expect(
generateAuthToken(appConfig, registeredInstallationEntry)
).to.be.rejectedWith(FirebaseError);
});
});
});
2 changes: 1 addition & 1 deletion packages/installations/src/api/generate-auth-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function generateAuthToken(
);
return completedAuthToken;
} else {
throw getErrorFromResponse('Generate Auth Token', response);
throw await getErrorFromResponse('Generate Auth Token', response);
}
}

Expand Down