|
| 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 { |
| 19 | + OperationType, |
| 20 | + ProviderId, |
| 21 | + SignInMethod |
| 22 | +} from '@firebase/auth-types-exp'; |
| 23 | +import { expect } from 'chai'; |
| 24 | +import { mockEndpoint } from '../../../test/api/helper'; |
| 25 | +import { testAuth, testUser } from '../../../test/mock_auth'; |
| 26 | +import * as mockFetch from '../../../test/mock_fetch'; |
| 27 | +import { Endpoint } from '../../api'; |
| 28 | +import { APIUserInfo } from '../../api/account_management/account'; |
| 29 | +import { Auth } from '../../model/auth'; |
| 30 | +import { signInAnonymously } from './anonymous'; |
| 31 | + |
| 32 | +describe('core/strategies/anonymous', () => { |
| 33 | + let auth: Auth; |
| 34 | + const serverUser: APIUserInfo = { |
| 35 | + localId: 'local-id' |
| 36 | + }; |
| 37 | + |
| 38 | + beforeEach(async () => { |
| 39 | + auth = await testAuth(); |
| 40 | + mockFetch.setUp(); |
| 41 | + mockEndpoint(Endpoint.SIGN_UP, { |
| 42 | + idToken: 'id-token', |
| 43 | + refreshToken: 'refresh-token', |
| 44 | + expiresIn: '1234', |
| 45 | + localId: serverUser.localId! |
| 46 | + }); |
| 47 | + mockEndpoint(Endpoint.GET_ACCOUNT_INFO, { |
| 48 | + users: [serverUser] |
| 49 | + }); |
| 50 | + }); |
| 51 | + afterEach(mockFetch.tearDown); |
| 52 | + |
| 53 | + describe('signInAnonymously', () => { |
| 54 | + it('should sign in an anonymous user', async () => { |
| 55 | + const { credential, user, operationType } = await signInAnonymously( |
| 56 | + auth |
| 57 | + ); |
| 58 | + expect(credential?.providerId).to.eq(ProviderId.ANONYMOUS); |
| 59 | + expect(credential?.signInMethod).to.eq(SignInMethod.ANONYMOUS); |
| 60 | + expect(operationType).to.eq(OperationType.SIGN_IN); |
| 61 | + expect(user.uid).to.eq(serverUser.localId); |
| 62 | + expect(user.isAnonymous).to.be.true; |
| 63 | + }); |
| 64 | + |
| 65 | + context('already signed in anonymously', () => { |
| 66 | + it('should return the current user', async () => { |
| 67 | + const userCredential = await signInAnonymously(auth); |
| 68 | + expect(userCredential.user.isAnonymous).to.be.true; |
| 69 | + |
| 70 | + const { credential, user, operationType } = await signInAnonymously( |
| 71 | + auth |
| 72 | + ); |
| 73 | + expect(credential?.providerId).to.eq(ProviderId.ANONYMOUS); |
| 74 | + expect(credential?.signInMethod).to.eq(SignInMethod.ANONYMOUS); |
| 75 | + expect(operationType).to.eq(OperationType.SIGN_IN); |
| 76 | + expect(user.uid).to.eq(userCredential.user.uid); |
| 77 | + expect(user.isAnonymous).to.be.true; |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + context('already signed in with a non-anonymous account', () => { |
| 82 | + it('should sign in as a new user user', async () => { |
| 83 | + const fakeUser = testUser(auth, 'other-uid'); |
| 84 | + await auth.updateCurrentUser(fakeUser); |
| 85 | + expect(fakeUser.isAnonymous).to.be.false; |
| 86 | + |
| 87 | + const { credential, user, operationType } = await signInAnonymously( |
| 88 | + auth |
| 89 | + ); |
| 90 | + expect(credential?.providerId).to.eq(ProviderId.ANONYMOUS); |
| 91 | + expect(credential?.signInMethod).to.eq(SignInMethod.ANONYMOUS); |
| 92 | + expect(operationType).to.eq(OperationType.SIGN_IN); |
| 93 | + expect(user.uid).to.not.eq(fakeUser.uid); |
| 94 | + expect(user.isAnonymous).to.be.true; |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments