Skip to content

Commit 5de57cd

Browse files
committed
Add more tests
1 parent 5d900e7 commit 5de57cd

File tree

8 files changed

+128
-34
lines changed

8 files changed

+128
-34
lines changed

packages-exp/auth-exp/src/core/auth/auth_impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const DEFAULT_TOKEN_API_HOST = 'securetoken.googleapis.com';
3838
export const DEFAULT_API_HOST = 'identitytoolkit.googleapis.com';
3939
export const DEFAULT_API_SCHEME = 'https';
4040

41-
class AuthImpl implements Auth {
41+
export class AuthImpl implements Auth {
4242
currentUser: User | null = null;
4343
private operations = Promise.resolve();
4444
private persistenceManager?: PersistenceUserManager;

packages-exp/auth-exp/src/core/strategies/credential.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,65 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { expect, use } from 'chai';
19+
import * as chaiAsPromised from 'chai-as-promised';
20+
import { mockAuth } from '../../../test/mock_auth';
21+
import { MockAuthCredential } from '../../../test/mock_auth_credential';
22+
import * as mockFetch from '../../../test/mock_fetch';
23+
import { APIUserInfo } from '../../api/account_management/account';
24+
import { IdTokenResponse } from '../../model/id_token';
25+
import { ProviderId, SignInMethod } from '../providers';
26+
import { signInWithCredential } from './credential';
27+
import { mockEndpoint } from '../../../test/api/helper';
28+
import { Endpoint } from '../../api';
29+
import { OperationType } from '../../model/user_credential';
30+
31+
use(chaiAsPromised);
32+
33+
describe('core/strategies/signInWithCredential', () => {
34+
const serverUser: APIUserInfo = {
35+
localId: 'local-id',
36+
displayName: 'display-name',
37+
photoUrl: 'photo-url',
38+
email: 'email',
39+
emailVerified: true,
40+
phoneNumber: 'phone-number',
41+
tenantId: 'tenant-id',
42+
createdAt: 123,
43+
lastLoginAt: 456
44+
};
45+
46+
const idTokenResponse: IdTokenResponse = {
47+
idToken: 'my-id-token',
48+
refreshToken: 'my-refresh-token',
49+
expiresIn: '1234',
50+
localId: serverUser.localId!,
51+
kind: 'my-kind'
52+
};
53+
54+
const authCredential = new MockAuthCredential(ProviderId.FIREBASE, SignInMethod.EMAIL_LINK);
55+
56+
beforeEach(() => {
57+
mockFetch.setUp();
58+
authCredential._setIdTokenResponse(idTokenResponse);
59+
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
60+
users: [serverUser]
61+
});
62+
});
63+
afterEach(mockFetch.tearDown);
64+
65+
it('should return a valid user credential', async () => {
66+
const {credential, user, operationType } = await signInWithCredential(mockAuth, authCredential);
67+
expect(credential!.providerId).to.eq(ProviderId.FIREBASE);
68+
expect(credential!.signInMethod).to.eq(SignInMethod.EMAIL_LINK);
69+
expect(user.uid).to.eq('local-id');
70+
expect(user.tenantId).to.eq('tenant-id');
71+
expect(user.displayName).to.eq('display-name');
72+
expect(operationType).to.eq(OperationType.SIGN_IN);
73+
});
74+
75+
it('should update the current user', async () => {
76+
const { user } = await signInWithCredential(mockAuth, authCredential);
77+
expect(mockAuth.currentUser).to.eq(user);
78+
});
79+
});

packages-exp/auth-exp/src/core/user/reload.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { FirebaseError } from '@firebase/util';
2525

2626
import { mockEndpoint } from '../../../test/api/helper';
2727
import { testUser } from '../../../test/mock_auth';
28-
import * as fetch from '../../../test/mock_fetch';
28+
import * as mockFetch from '../../../test/mock_fetch';
2929
import { Endpoint } from '../../api';
3030
import {
3131
APIUserInfo,
@@ -56,8 +56,8 @@ const BASIC_PROVIDER_USER_INFO: ProviderUserInfo = {
5656
};
5757

5858
describe('core/user/reload', () => {
59-
beforeEach(fetch.setUp);
60-
afterEach(fetch.tearDown);
59+
beforeEach(mockFetch.setUp);
60+
afterEach(mockFetch.tearDown);
6161

6262
it('sets all the new properties', async () => {
6363
const serverUser: APIUserInfo = {

packages-exp/auth-exp/src/core/user/user_credential_impl.test.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,54 @@
1717

1818
import { expect, use } from 'chai';
1919
import * as chaiAsPromised from 'chai-as-promised';
20+
import * as sinon from 'sinon';
21+
import * as sinonChai from 'sinon-chai';
22+
import { mockEndpoint } from '../../../test/api/helper';
2023
import { mockAuth } from '../../../test/mock_auth';
24+
import { MockAuthCredential } from '../../../test/mock_auth_credential';
25+
import * as mockFetch from '../../../test/mock_fetch';
26+
import { Endpoint } from '../../api';
27+
import { APIUserInfo } from '../../api/account_management/account';
2128
import { AuthCredential } from '../../model/auth_credential';
2229
import { IdTokenResponse } from '../../model/id_token';
2330
import { OperationType } from '../../model/user_credential';
2431
import { ProviderId, SignInMethod } from '../providers';
25-
import { AuthCredentialImpl } from '../providers/auth_credential_impl';
2632
import { UserCredentialImpl } from './user_credential_impl';
27-
import { APIUserInfo } from '../../api/account_management/account';
28-
import { mockEndpoint } from '../../../test/api/helper';
29-
import { Endpoint } from '../../api';
3033

3134
use(chaiAsPromised);
35+
use(sinonChai);
3236

3337
describe('core/user/user_credential_impl', () => {
3438
const serverUser: APIUserInfo = {
35-
localId: 'localId',
36-
displayName: 'displayName',
37-
photoUrl: 'photoURL',
39+
localId: 'local-id',
40+
displayName: 'display-name',
41+
photoUrl: 'photo-url',
3842
email: 'email',
3943
emailVerified: true,
40-
phoneNumber: 'phoneNumber',
41-
tenantId: 'tenantId',
44+
phoneNumber: 'phone-number',
45+
tenantId: 'tenant-id',
4246
createdAt: 123,
4347
lastLoginAt: 456
4448
};
4549

4650
beforeEach(() => {
51+
mockFetch.setUp();
4752
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
4853
users: [serverUser]
4954
});
5055
});
56+
afterEach(mockFetch.tearDown);
5157

5258
describe('fromIdTokenResponse', () => {
5359
const idTokenResponse: IdTokenResponse = {
5460
idToken: 'my-id-token',
5561
refreshToken: 'my-refresh-token',
5662
expiresIn: '1234',
57-
localId: 'my-uid',
63+
localId: serverUser.localId!,
5864
kind: 'my-kind'
5965
};
6066

61-
const credential: AuthCredential = new AuthCredentialImpl(
67+
const credential: AuthCredential = new MockAuthCredential(
6268
ProviderId.FIREBASE,
6369
SignInMethod.EMAIL_PASSWORD
6470
);
@@ -72,9 +78,22 @@ describe('core/user/user_credential_impl', () => {
7278
);
7379
expect(userCredential.credential).to.eq(credential);
7480
expect(userCredential.operationType).to.eq(OperationType.SIGN_IN);
75-
expect(userCredential.user.uid).to.eq('my-uid');
81+
expect(userCredential.user.uid).to.eq('local-id');
7682
});
7783

78-
it('should not trigger callbacks', () => {});
84+
it('should not trigger callbacks', async () => {
85+
const cb = sinon.spy();
86+
mockAuth.onAuthStateChanged(cb);
87+
await mockAuth.updateCurrentUser(null);
88+
cb.resetHistory();
89+
90+
await UserCredentialImpl._fromIdTokenResponse(
91+
mockAuth,
92+
credential,
93+
OperationType.SIGN_IN,
94+
idTokenResponse
95+
);
96+
expect(cb).not.to.have.been.called;
97+
});
7998
});
8099
});

packages-exp/auth-exp/src/core/user/user_impl.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ import { Endpoint } from '../../api';
3030
import { IdTokenResponse } from '../../model/id_token';
3131
import { StsTokenManager } from './token_manager';
3232
import { UserImpl } from './user_impl';
33-
import { mockEndpoint } from '../../../test/api/helper';
34-
import { Endpoint } from '../../api';
35-
import { APIUserInfo } from '../../api/account_management/account';
3633

3734
use(sinonChai);
3835
use(chaiAsPromised);
@@ -195,7 +192,7 @@ describe('core/user/user_impl', () => {
195192
idToken: 'my-id-token',
196193
refreshToken: 'my-refresh-token',
197194
expiresIn: '1234',
198-
localId: 'my-uid',
195+
localId: 'local-id',
199196
kind: 'my-kind'
200197
};
201198

@@ -212,19 +209,21 @@ describe('core/user/user_impl', () => {
212209
};
213210

214211
beforeEach(() => {
212+
mockFetch.setUp();
215213
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
216214
users: [serverUser]
217215
});
218216
});
217+
afterEach(mockFetch.tearDown);
219218

220219
it('should initialize a user', async () => {
221220
const user = await UserImpl._fromIdTokenResponse(
222221
mockAuth,
223222
idTokenResponse
224223
);
225224
expect(user.uid).to.eq(idTokenResponse.localId);
226-
expect(user.refreshToken).to.eq('my-refresh-token');
227225
expect(await user.getIdToken()).to.eq('my-id-token');
226+
expect(user.refreshToken).to.eq('my-refresh-token');
228227
});
229228

230229
it('should pull additional user info on the user', async () => {
@@ -239,6 +238,9 @@ describe('core/user/user_impl', () => {
239238
it('should not trigger additional callbacks', async () => {
240239
const cb = sinon.spy();
241240
auth.onAuthStateChanged(cb);
241+
await auth.updateCurrentUser(null);
242+
cb.resetHistory();
243+
242244
await UserImpl._fromIdTokenResponse(mockAuth, idTokenResponse);
243245
expect(cb).not.to.have.been.called;
244246
});

packages-exp/auth-exp/src/core/user/user_impl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ export class UserImpl implements User {
169169

170170
// Initialize the Firebase Auth user.
171171
const user = new UserImpl({
172+
uid: idTokenResponse.localId,
172173
auth,
173-
stsTokenManager,
174-
uid: idTokenResponse.localId
174+
stsTokenManager
175175
});
176176

177177
// Updates the user info and data and resolves with a user instance.

packages-exp/auth-exp/test/mock_auth.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import { StsTokenManager } from '../src/core/user/token_manager';
19+
import { AuthImpl } from '../src/core/auth/auth_impl';
1920
import { UserImpl } from '../src/core/user/user_impl';
2021
import { Auth } from '../src/model/auth';
2122
import { User } from '../src/model/user';

packages-exp/auth-exp/src/core/providers/auth_credential_impl.ts renamed to packages-exp/auth-exp/test/mock_auth_credential.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,34 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { AuthCredential } from '../../model/auth_credential';
19-
import { ProviderId, SignInMethod } from '.';
20-
import { IdTokenResponse } from '../../model/id_token';
21-
import { PhoneOrOauthTokenResponse } from '../../api/authentication/mfa';
22-
import { Auth } from '../../model/auth';
18+
import { PhoneOrOauthTokenResponse } from '../src/api/authentication/mfa';
19+
import { ProviderId, SignInMethod } from "../src/core/providers";
20+
import { Auth } from '../src/model/auth';
21+
import { AuthCredential } from '../src/model/auth_credential';
22+
import { IdTokenResponse } from '../src/model/id_token';
23+
24+
export class MockAuthCredential implements AuthCredential {
25+
response?: PhoneOrOauthTokenResponse;
2326

24-
export class AuthCredentialImpl implements AuthCredential {
2527
constructor(
2628
readonly providerId: ProviderId,
2729
readonly signInMethod: SignInMethod
28-
) {}
30+
) { }
2931

3032
toJSON(): object {
3133
throw new Error('Method not implemented.');
3234
}
3335

34-
_getIdTokenResponse(_auth: Auth): Promise<PhoneOrOauthTokenResponse> {
35-
throw new Error('Method not implemented.');
36+
/**
37+
* For testing purposes only
38+
* @param response
39+
*/
40+
_setIdTokenResponse(response: PhoneOrOauthTokenResponse): void {
41+
this.response = response;
42+
}
43+
44+
async _getIdTokenResponse(_auth: Auth): Promise<PhoneOrOauthTokenResponse> {
45+
return this.response!;
3646
}
3747

3848
_linkToIdToken(_auth: Auth, _idToken: string): Promise<IdTokenResponse> {
@@ -42,4 +52,4 @@ export class AuthCredentialImpl implements AuthCredential {
4252
_matchIdTokenWithUid(_auth: Auth, _uid: string): Promise<IdTokenResponse> {
4353
throw new Error('Method not implemented.');
4454
}
45-
}
55+
}

0 commit comments

Comments
 (0)