Skip to content

Commit 8756e15

Browse files
committed
Add more tests to account API methods
1 parent e006b34 commit 8756e15

File tree

6 files changed

+576
-15
lines changed

6 files changed

+576
-15
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
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, use } from 'chai';
19+
import * as chaiAsPromised from 'chai-as-promised';
20+
import { deleteAccount, deleteLinkedAccounts, getAccountInfo } from './account';
21+
import { Endpoint } from '..';
22+
import { ServerError } from '../errors';
23+
import { FirebaseError } from '@firebase/util';
24+
import * as mockFetch from '../../../test/mock_fetch';
25+
import { mockEndpoint, mockAuth } from '../../../test/api/helper';
26+
import { ProviderId } from '../../core/providers';
27+
28+
use(chaiAsPromised);
29+
30+
describe('deleteAccount', () => {
31+
const request = {
32+
idToken: 'id-token'
33+
};
34+
35+
beforeEach(mockFetch.setUp);
36+
afterEach(mockFetch.tearDown);
37+
38+
it('should POST to the correct endpoint', async () => {
39+
const mock = mockEndpoint(Endpoint.DELETE_ACCOUNT, {});
40+
41+
await deleteAccount(mockAuth, request);
42+
expect(mock.calls[0].request).to.eql(request);
43+
expect(mock.calls[0].method).to.eq('POST');
44+
expect(mock.calls[0].headers).to.eql({
45+
'Content-Type': 'application/json'
46+
});
47+
});
48+
49+
it('should handle errors', async () => {
50+
const mock = mockEndpoint(
51+
Endpoint.DELETE_ACCOUNT,
52+
{
53+
error: {
54+
code: 400,
55+
message: ServerError.INVALID_ID_TOKEN,
56+
errors: [
57+
{
58+
message: ServerError.INVALID_ID_TOKEN
59+
}
60+
]
61+
}
62+
},
63+
400
64+
);
65+
66+
await expect(deleteAccount(mockAuth, request)).to.be.rejectedWith(
67+
FirebaseError,
68+
'Firebase: This user\'s credential isn\'t valid for this project. This can happen if the user\'s token has been tampered with]: or if the user isn\'t for the project associated with this API key. (auth/invalid-user-token).'
69+
);
70+
expect(mock.calls[0].request).to.eql(request);
71+
});
72+
});
73+
74+
describe('deleteLinkedAccounts', () => {
75+
const request = {
76+
idToken: 'id-token',
77+
deleteProvider: [ProviderId.GOOGLE]
78+
};
79+
80+
beforeEach(mockFetch.setUp);
81+
afterEach(mockFetch.tearDown);
82+
83+
it('should POST to the correct endpoint', async () => {
84+
const mock = mockEndpoint(Endpoint.SET_ACCOUNT_INFO, {
85+
providerUserInfo: [{
86+
providerId: ProviderId.GOOGLE,
87+
88+
}]
89+
});
90+
91+
const response = await deleteLinkedAccounts(mockAuth, request);
92+
expect(response.providerUserInfo[0].providerId).to.eq('google.com');
93+
expect(response.providerUserInfo[0].email).to.eq('[email protected]');
94+
expect(mock.calls[0].request).to.eql(request);
95+
expect(mock.calls[0].method).to.eq('POST');
96+
expect(mock.calls[0].headers).to.eql({
97+
'Content-Type': 'application/json'
98+
});
99+
});
100+
101+
it('should handle errors', async () => {
102+
const mock = mockEndpoint(
103+
Endpoint.SET_ACCOUNT_INFO,
104+
{
105+
error: {
106+
code: 400,
107+
message: ServerError.INVALID_PROVIDER_ID,
108+
errors: [
109+
{
110+
message: ServerError.INVALID_PROVIDER_ID
111+
}
112+
]
113+
}
114+
},
115+
400
116+
);
117+
118+
await expect(deleteLinkedAccounts(mockAuth, request)).to.be.rejectedWith(
119+
FirebaseError,
120+
'Firebase: The specified provider ID is invalid. (auth/invalid-provider-id).'
121+
);
122+
expect(mock.calls[0].request).to.eql(request);
123+
});
124+
});
125+
126+
describe('getAccountInfo', () => {
127+
const request = {
128+
idToken: 'id-token'
129+
};
130+
131+
beforeEach(mockFetch.setUp);
132+
afterEach(mockFetch.tearDown);
133+
134+
it('should POST to the correct endpoint', async () => {
135+
const mock = mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
136+
users: [{
137+
displayName: 'my-name',
138+
139+
}]
140+
});
141+
142+
const response = await getAccountInfo(mockAuth, request);
143+
expect(response.users[0].displayName).to.eq('my-name');
144+
expect(response.users[0].email).to.eq('[email protected]');
145+
expect(mock.calls[0].request).to.eql(request);
146+
expect(mock.calls[0].method).to.eq('POST');
147+
expect(mock.calls[0].headers).to.eql({
148+
'Content-Type': 'application/json'
149+
});
150+
});
151+
152+
it('should handle errors', async () => {
153+
const mock = mockEndpoint(
154+
Endpoint.GET_ACCOUNT_INFO,
155+
{
156+
error: {
157+
code: 400,
158+
message: ServerError.INVALID_ID_TOKEN,
159+
errors: [
160+
{
161+
message: ServerError.INVALID_ID_TOKEN
162+
}
163+
]
164+
}
165+
},
166+
400
167+
);
168+
169+
await expect(getAccountInfo(mockAuth, request)).to.be.rejectedWith(
170+
FirebaseError,
171+
'Firebase: This user\'s credential isn\'t valid for this project. This can happen if the user\'s token has been tampered with]: or if the user isn\'t for the project associated with this API key. (auth/invalid-user-token).'
172+
);
173+
expect(mock.calls[0].request).to.eql(request);
174+
});
175+
});

packages-exp/auth-exp/src/api/account_management/account.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ export async function deleteAccount(
3535
);
3636
}
3737

38+
export interface ProviderUserInfo {
39+
rawId?: string;
40+
providerId?: string;
41+
email?: string;
42+
displayName?: string;
43+
photoUrl?: string;
44+
phoneNumber?: string;
45+
}
46+
3847
export interface DeleteLinkedAccountsRequest {
3948
idToken: string;
4049
deleteProvider: string[];
@@ -69,15 +78,6 @@ export interface APIUserInfo {
6978
mfaInfo?: APIMFAInfo[];
7079
}
7180

72-
export interface ProviderUserInfo {
73-
rawId?: string;
74-
providerId?: string;
75-
email?: string;
76-
displayName?: string;
77-
photoUrl?: string;
78-
phoneNumber?: string;
79-
}
80-
8181
export interface GetAccountInfoRequest {
8282
idToken: string;
8383
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
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, use } from 'chai';
19+
import * as chaiAsPromised from 'chai-as-promised';
20+
import { resetPassword, updateEmailPassword } from './email_and_password';
21+
import { Endpoint } from '..';
22+
import { ServerError } from '../errors';
23+
import { FirebaseError } from '@firebase/util';
24+
import * as mockFetch from '../../../test/mock_fetch';
25+
import { mockEndpoint, mockAuth } from '../../../test/api/helper';
26+
27+
use(chaiAsPromised);
28+
29+
describe('resetPassword', () => {
30+
const request = {
31+
oobCode: 'oob-code',
32+
newPassword: 'new-password'
33+
};
34+
35+
beforeEach(mockFetch.setUp);
36+
afterEach(mockFetch.tearDown);
37+
38+
it('should POST to the correct endpoint', async () => {
39+
const mock = mockEndpoint(Endpoint.RESET_PASSWORD, {
40+
41+
});
42+
43+
const response = await resetPassword(mockAuth, request);
44+
expect(response.email).to.eq('[email protected]');
45+
expect(mock.calls[0].request).to.eql(request);
46+
expect(mock.calls[0].method).to.eq('POST');
47+
expect(mock.calls[0].headers).to.eql({
48+
'Content-Type': 'application/json'
49+
});
50+
});
51+
52+
it('should handle errors', async () => {
53+
const mock = mockEndpoint(
54+
Endpoint.RESET_PASSWORD,
55+
{
56+
error: {
57+
code: 400,
58+
message: ServerError.RESET_PASSWORD_EXCEED_LIMIT,
59+
errors: [
60+
{
61+
message: ServerError.RESET_PASSWORD_EXCEED_LIMIT
62+
}
63+
]
64+
}
65+
},
66+
400
67+
);
68+
69+
await expect(resetPassword(mockAuth, request)).to.be.rejectedWith(
70+
FirebaseError,
71+
'Firebase: We have blocked all requests from this device due to unusual activity. Try again later. (auth/too-many-requests).'
72+
);
73+
expect(mock.calls[0].request).to.eql(request);
74+
});
75+
});
76+
77+
78+
describe('updateEmailPassword', () => {
79+
const request = {
80+
idToken: 'id-token',
81+
returnSecureToken: true,
82+
83+
password: 'new-password'
84+
};
85+
86+
beforeEach(mockFetch.setUp);
87+
afterEach(mockFetch.tearDown);
88+
89+
it('should POST to the correct endpoint', async () => {
90+
const mock = mockEndpoint(Endpoint.SET_ACCOUNT_INFO, {
91+
idToken: 'id-token'
92+
});
93+
94+
const response = await updateEmailPassword(mockAuth, request);
95+
expect(response.idToken).to.eq('id-token');
96+
expect(mock.calls[0].request).to.eql(request);
97+
expect(mock.calls[0].method).to.eq('POST');
98+
expect(mock.calls[0].headers).to.eql({
99+
'Content-Type': 'application/json'
100+
});
101+
});
102+
103+
it('should handle errors', async () => {
104+
const mock = mockEndpoint(
105+
Endpoint.SET_ACCOUNT_INFO,
106+
{
107+
error: {
108+
code: 400,
109+
message: ServerError.INVALID_EMAIL,
110+
errors: [
111+
{
112+
message: ServerError.INVALID_EMAIL
113+
}
114+
]
115+
}
116+
},
117+
400
118+
);
119+
120+
await expect(updateEmailPassword(mockAuth, request)).to.be.rejectedWith(
121+
FirebaseError,
122+
'Firebase: The email address is badly formatted. (auth/invalid-email).'
123+
);
124+
expect(mock.calls[0].request).to.eql(request);
125+
});
126+
});
127+

0 commit comments

Comments
 (0)