Skip to content

Commit 11dd5ee

Browse files
authored
Add remaining API methods to auth-next (#2901)
* User should be an interface for now we can make an implementation class later * Add API call to signUp * [AUTOMATED]: Prettier Code Styling * Update tests to test a little more * Add remaining API methods to auth-next * [AUTOMATED]: Prettier Code Styling * [AUTOMATED]: License Headers * Add more tests to account API methods * Pass in SDK version & correctly send GET request params * [AUTOMATED]: Prettier Code Styling * [AUTOMATED]: Prettier Code Styling * Fix import ordering * Minor formatting change * [AUTOMATED]: Prettier Code Styling
1 parent 18c5312 commit 11dd5ee

37 files changed

+2464
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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 { FirebaseError } from '@firebase/util';
19+
import { expect, use } from 'chai';
20+
import * as chaiAsPromised from 'chai-as-promised';
21+
import { Endpoint } from '..';
22+
import { mockEndpoint } from '../../../test/api/helper';
23+
import { mockAuth } from '../../../test/mock_auth';
24+
import * as mockFetch from '../../../test/mock_fetch';
25+
import { ProviderId } from '../../core/providers';
26+
import { ServerError } from '../errors';
27+
import { deleteAccount, deleteLinkedAccounts, getAccountInfo } from './account';
28+
29+
use(chaiAsPromised);
30+
31+
describe('deleteAccount', () => {
32+
const request = {
33+
idToken: 'id-token'
34+
};
35+
36+
beforeEach(mockFetch.setUp);
37+
afterEach(mockFetch.tearDown);
38+
39+
it('should POST to the correct endpoint', async () => {
40+
const mock = mockEndpoint(Endpoint.DELETE_ACCOUNT, {});
41+
42+
await deleteAccount(mockAuth, request);
43+
expect(mock.calls[0].request).to.eql(request);
44+
expect(mock.calls[0].method).to.eq('POST');
45+
expect(mock.calls[0].headers).to.eql({
46+
'Content-Type': 'application/json',
47+
'X-Client-Version': 'testSDK/0.0.0'
48+
});
49+
});
50+
51+
it('should handle errors', async () => {
52+
const mock = mockEndpoint(
53+
Endpoint.DELETE_ACCOUNT,
54+
{
55+
error: {
56+
code: 400,
57+
message: ServerError.INVALID_ID_TOKEN,
58+
errors: [
59+
{
60+
message: ServerError.INVALID_ID_TOKEN
61+
}
62+
]
63+
}
64+
},
65+
400
66+
);
67+
68+
await expect(deleteAccount(mockAuth, request)).to.be.rejectedWith(
69+
FirebaseError,
70+
"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)."
71+
);
72+
expect(mock.calls[0].request).to.eql(request);
73+
});
74+
});
75+
76+
describe('deleteLinkedAccounts', () => {
77+
const request = {
78+
idToken: 'id-token',
79+
deleteProvider: [ProviderId.GOOGLE]
80+
};
81+
82+
beforeEach(mockFetch.setUp);
83+
afterEach(mockFetch.tearDown);
84+
85+
it('should POST to the correct endpoint', async () => {
86+
const mock = mockEndpoint(Endpoint.SET_ACCOUNT_INFO, {
87+
providerUserInfo: [
88+
{
89+
providerId: ProviderId.GOOGLE,
90+
91+
}
92+
]
93+
});
94+
95+
const response = await deleteLinkedAccounts(mockAuth, request);
96+
expect(response.providerUserInfo[0].providerId).to.eq('google.com');
97+
expect(response.providerUserInfo[0].email).to.eq('[email protected]');
98+
expect(mock.calls[0].request).to.eql(request);
99+
expect(mock.calls[0].method).to.eq('POST');
100+
expect(mock.calls[0].headers).to.eql({
101+
'Content-Type': 'application/json',
102+
'X-Client-Version': 'testSDK/0.0.0'
103+
});
104+
});
105+
106+
it('should handle errors', async () => {
107+
const mock = mockEndpoint(
108+
Endpoint.SET_ACCOUNT_INFO,
109+
{
110+
error: {
111+
code: 400,
112+
message: ServerError.INVALID_PROVIDER_ID,
113+
errors: [
114+
{
115+
message: ServerError.INVALID_PROVIDER_ID
116+
}
117+
]
118+
}
119+
},
120+
400
121+
);
122+
123+
await expect(deleteLinkedAccounts(mockAuth, request)).to.be.rejectedWith(
124+
FirebaseError,
125+
'Firebase: The specified provider ID is invalid. (auth/invalid-provider-id).'
126+
);
127+
expect(mock.calls[0].request).to.eql(request);
128+
});
129+
});
130+
131+
describe('getAccountInfo', () => {
132+
const request = {
133+
idToken: 'id-token'
134+
};
135+
136+
beforeEach(mockFetch.setUp);
137+
afterEach(mockFetch.tearDown);
138+
139+
it('should POST to the correct endpoint', async () => {
140+
const mock = mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
141+
users: [
142+
{
143+
displayName: 'my-name',
144+
145+
}
146+
]
147+
});
148+
149+
const response = await getAccountInfo(mockAuth, request);
150+
expect(response.users[0].displayName).to.eq('my-name');
151+
expect(response.users[0].email).to.eq('[email protected]');
152+
expect(mock.calls[0].request).to.eql(request);
153+
expect(mock.calls[0].method).to.eq('POST');
154+
expect(mock.calls[0].headers).to.eql({
155+
'Content-Type': 'application/json',
156+
'X-Client-Version': 'testSDK/0.0.0'
157+
});
158+
});
159+
160+
it('should handle errors', async () => {
161+
const mock = mockEndpoint(
162+
Endpoint.GET_ACCOUNT_INFO,
163+
{
164+
error: {
165+
code: 400,
166+
message: ServerError.INVALID_ID_TOKEN,
167+
errors: [
168+
{
169+
message: ServerError.INVALID_ID_TOKEN
170+
}
171+
]
172+
}
173+
},
174+
400
175+
);
176+
177+
await expect(getAccountInfo(mockAuth, request)).to.be.rejectedWith(
178+
FirebaseError,
179+
"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)."
180+
);
181+
expect(mock.calls[0].request).to.eql(request);
182+
});
183+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 { Endpoint, HttpMethod, performApiRequest } from '..';
19+
import { Auth } from '../../model/auth';
20+
import { APIMFAInfo } from '../../model/id_token';
21+
22+
export interface DeleteAccountRequest {
23+
idToken: string;
24+
}
25+
26+
export async function deleteAccount(
27+
auth: Auth,
28+
request: DeleteAccountRequest
29+
): Promise<void> {
30+
return performApiRequest<DeleteAccountRequest, void>(
31+
auth,
32+
HttpMethod.POST,
33+
Endpoint.DELETE_ACCOUNT,
34+
request
35+
);
36+
}
37+
38+
export interface ProviderUserInfo {
39+
rawId?: string;
40+
providerId?: string;
41+
email?: string;
42+
displayName?: string;
43+
photoUrl?: string;
44+
phoneNumber?: string;
45+
}
46+
47+
export interface DeleteLinkedAccountsRequest {
48+
idToken: string;
49+
deleteProvider: string[];
50+
}
51+
52+
export interface DeleteLinkedAccountsResponse {
53+
providerUserInfo: ProviderUserInfo[];
54+
}
55+
56+
export async function deleteLinkedAccounts(
57+
auth: Auth,
58+
request: DeleteLinkedAccountsRequest
59+
): Promise<DeleteLinkedAccountsResponse> {
60+
return performApiRequest<
61+
DeleteLinkedAccountsRequest,
62+
DeleteLinkedAccountsResponse
63+
>(auth, HttpMethod.POST, Endpoint.SET_ACCOUNT_INFO, request);
64+
}
65+
66+
export interface APIUserInfo {
67+
localId?: string;
68+
displayName?: string;
69+
photoUrl?: string;
70+
email?: string;
71+
emailVerified?: boolean;
72+
phoneNumber?: string;
73+
lastLoginAt?: number;
74+
createdAt?: number;
75+
tenantId?: string;
76+
passwordHash?: string;
77+
providerUserInfo: ProviderUserInfo[];
78+
mfaInfo?: APIMFAInfo[];
79+
}
80+
81+
export interface GetAccountInfoRequest {
82+
idToken: string;
83+
}
84+
85+
export interface GetAccountInfoResponse {
86+
users: APIUserInfo[];
87+
}
88+
89+
export async function getAccountInfo(
90+
auth: Auth,
91+
request: GetAccountInfoRequest
92+
): Promise<GetAccountInfoResponse> {
93+
return performApiRequest<GetAccountInfoRequest, GetAccountInfoResponse>(
94+
auth,
95+
HttpMethod.POST,
96+
Endpoint.GET_ACCOUNT_INFO,
97+
request
98+
);
99+
}

0 commit comments

Comments
 (0)