Skip to content

Commit e19a985

Browse files
committed
Add more tests
1 parent 16976a0 commit e19a985

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

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

+37-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { OperationType, ProviderId, SignInMethod } from '@firebase/auth-types-exp';
18+
import {
19+
OperationType,
20+
ProviderId,
21+
SignInMethod
22+
} from '@firebase/auth-types-exp';
1923
import { expect } from 'chai';
2024
import { mockEndpoint } from '../../../test/api/helper';
21-
import { mockAuth } from '../../../test/mock_auth';
25+
import { mockAuth, testUser } from '../../../test/mock_auth';
2226
import * as mockFetch from '../../../test/mock_fetch';
2327
import { Endpoint } from '../../api';
2428
import { APIUserInfo } from '../../api/account_management/account';
@@ -55,8 +59,37 @@ describe('core/strategies/anonymous', () => {
5559
expect(user.isAnonymous).to.be.true;
5660
});
5761

58-
context('already signed in anonymousl', () => {});
62+
context('already signed in anonymously', () => {
63+
it('should return the current user', async () => {
64+
const userCredential = await signInAnonymously(mockAuth);
65+
expect(userCredential.user.isAnonymous).to.be.true;
5966

60-
context('already signed in with a non-anonymous account', () => {});
67+
const { credential, user, operationType } = await signInAnonymously(
68+
mockAuth
69+
);
70+
expect(credential?.providerId).to.eq(ProviderId.ANONYMOUS);
71+
expect(credential?.signInMethod).to.eq(SignInMethod.ANONYMOUS);
72+
expect(operationType).to.eq(OperationType.SIGN_IN);
73+
expect(user.uid).to.eq(userCredential.user.uid);
74+
expect(user.isAnonymous).to.be.true;
75+
});
76+
});
77+
78+
context('already signed in with a non-anonymous account', () => {
79+
it('should sign in as a new user user', async () => {
80+
const fakeUser = testUser('other-uid');
81+
await mockAuth.updateCurrentUser(fakeUser);
82+
expect(fakeUser.isAnonymous).to.be.false;
83+
84+
const { credential, user, operationType } = await signInAnonymously(
85+
mockAuth
86+
);
87+
expect(credential?.providerId).to.eq(ProviderId.ANONYMOUS);
88+
expect(credential?.signInMethod).to.eq(SignInMethod.ANONYMOUS);
89+
expect(operationType).to.eq(OperationType.SIGN_IN);
90+
expect(user.uid).to.not.eq(fakeUser.uid);
91+
expect(user.isAnonymous).to.be.true;
92+
});
93+
});
6194
});
6295
});

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { OperationType, UserCredential } from '@firebase/auth-types-exp';
18+
import { OperationType, UserCredential, ProviderId } from '@firebase/auth-types-exp';
1919
import { Auth } from '../../model/auth';
2020
import { AuthCredential } from '../../model/auth_credential';
2121
import { IdTokenResponse } from '../../model/id_token';
@@ -35,7 +35,7 @@ export class UserCredentialImpl implements UserCredential {
3535
operationType: OperationType,
3636
idTokenResponse: IdTokenResponse
3737
): Promise<UserCredential> {
38-
const user = await UserImpl._fromIdTokenResponse(auth, idTokenResponse);
38+
const user = await UserImpl._fromIdTokenResponse(auth, idTokenResponse, credential?.providerId === ProviderId.ANONYMOUS);
3939
const userCred = new UserCredentialImpl(user, credential, operationType);
4040
// TODO: handle additional user info
4141
// updateAdditionalUserInfoFromIdTokenResponse(userCred, idTokenResponse);

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface UserParameters {
3535
email?: string;
3636
phoneNumber?: string;
3737
photoURL?: string;
38+
isAnonymous?: boolean;
3839
}
3940

4041
function assertStringOrUndefined(
@@ -65,7 +66,7 @@ export class UserImpl implements User {
6566
email: string | null;
6667
phoneNumber: string | null;
6768
photoURL: string | null;
68-
isAnonymous = false;
69+
isAnonymous: boolean = false;
6970

7071
constructor({ uid, auth, stsTokenManager, ...opt }: UserParameters) {
7172
this.uid = uid;
@@ -75,6 +76,7 @@ export class UserImpl implements User {
7576
this.email = opt.email || null;
7677
this.phoneNumber = opt.phoneNumber || null;
7778
this.photoURL = opt.photoURL || null;
79+
this.isAnonymous = opt.isAnonymous || false;
7880
}
7981

8082
async getIdToken(forceRefresh?: boolean): Promise<string> {
@@ -161,7 +163,8 @@ export class UserImpl implements User {
161163
*/
162164
static async _fromIdTokenResponse(
163165
auth: Auth,
164-
idTokenResponse: IdTokenResponse
166+
idTokenResponse: IdTokenResponse,
167+
isAnonymous: boolean = false
165168
): Promise<User> {
166169
const stsTokenManager = new StsTokenManager();
167170
stsTokenManager.updateFromServerResponse(idTokenResponse);
@@ -170,7 +173,8 @@ export class UserImpl implements User {
170173
const user = new UserImpl({
171174
uid: idTokenResponse.localId,
172175
auth,
173-
stsTokenManager
176+
stsTokenManager,
177+
isAnonymous
174178
});
175179

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

0 commit comments

Comments
 (0)