|
| 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 | + |
| 21 | +import { |
| 22 | + createUserWithEmailAndPassword, |
| 23 | + EmailAuthProvider, |
| 24 | + reload, |
| 25 | + signInWithCredential, |
| 26 | + signInWithEmailAndPassword, |
| 27 | + updateProfile |
| 28 | +} from '@firebase/auth-exp/index.browser'; |
| 29 | +import { Auth, OperationType, UserCredential } from '@firebase/auth-types-exp'; |
| 30 | +import { FirebaseError } from '@firebase/util'; |
| 31 | + |
| 32 | +import { |
| 33 | + cleanUpTestInstance, |
| 34 | + getTestInstance, |
| 35 | + randomEmail |
| 36 | +} from '../../helpers/integration/helpers'; |
| 37 | + |
| 38 | +use(chaiAsPromised); |
| 39 | + |
| 40 | +describe('Integration test: email/password auth', () => { |
| 41 | + let auth: Auth; |
| 42 | + let email: string; |
| 43 | + beforeEach(() => { |
| 44 | + auth = getTestInstance(); |
| 45 | + email = randomEmail(); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(() => cleanUpTestInstance(auth)); |
| 49 | + |
| 50 | + it('allows user to sign up', async () => { |
| 51 | + const userCred = await createUserWithEmailAndPassword( |
| 52 | + auth, |
| 53 | + email, |
| 54 | + 'password' |
| 55 | + ); |
| 56 | + expect(auth.currentUser).to.eq(userCred.user); |
| 57 | + expect(userCred.operationType).to.eq(OperationType.SIGN_IN); |
| 58 | + |
| 59 | + const user = userCred.user; |
| 60 | + expect(user.isAnonymous).to.be.false; |
| 61 | + expect(user.uid).to.be.a('string'); |
| 62 | + expect(user.email).to.eq(email); |
| 63 | + expect(user.emailVerified).to.be.false; |
| 64 | + }); |
| 65 | + |
| 66 | + it('errors when createUser called twice', async () => { |
| 67 | + await createUserWithEmailAndPassword(auth, email, 'password'); |
| 68 | + await expect( |
| 69 | + createUserWithEmailAndPassword(auth, email, 'password') |
| 70 | + ).to.be.rejectedWith(FirebaseError, 'auth/email-already-in-use'); |
| 71 | + }); |
| 72 | + |
| 73 | + context('with existing user', () => { |
| 74 | + let signUpCred: UserCredential; |
| 75 | + |
| 76 | + beforeEach(async () => { |
| 77 | + signUpCred = await createUserWithEmailAndPassword( |
| 78 | + auth, |
| 79 | + email, |
| 80 | + 'password' |
| 81 | + ); |
| 82 | + await auth.signOut(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('allows the user to sign in with signInWithEmailAndPassword', async () => { |
| 86 | + const signInCred = await signInWithEmailAndPassword( |
| 87 | + auth, |
| 88 | + email, |
| 89 | + 'password' |
| 90 | + ); |
| 91 | + expect(auth.currentUser).to.eq(signInCred.user); |
| 92 | + |
| 93 | + expect(signInCred.operationType).to.eq(OperationType.SIGN_IN); |
| 94 | + expect(signInCred.user.uid).to.eq(signUpCred.user.uid); |
| 95 | + }); |
| 96 | + |
| 97 | + it('allows the user to sign in with signInWithCredential', async () => { |
| 98 | + const credential = EmailAuthProvider.credential(email, 'password'); |
| 99 | + const signInCred = await signInWithCredential(auth, credential); |
| 100 | + expect(auth.currentUser).to.eq(signInCred.user); |
| 101 | + |
| 102 | + expect(signInCred.operationType).to.eq(OperationType.SIGN_IN); |
| 103 | + expect(signInCred.user.uid).to.eq(signUpCred.user.uid); |
| 104 | + }); |
| 105 | + |
| 106 | + it('allows the user to update profile', async () => { |
| 107 | + let { user } = await signInWithEmailAndPassword(auth, email, 'password'); |
| 108 | + await updateProfile(user, { |
| 109 | + displayName: 'Display Name', |
| 110 | + photoURL: 'photo-url' |
| 111 | + }); |
| 112 | + expect(user.displayName).to.eq('Display Name'); |
| 113 | + expect(user.photoURL).to.eq('photo-url'); |
| 114 | + |
| 115 | + await auth.signOut(); |
| 116 | + |
| 117 | + user = (await signInWithEmailAndPassword(auth, email, 'password')).user; |
| 118 | + expect(user.displayName).to.eq('Display Name'); |
| 119 | + expect(user.photoURL).to.eq('photo-url'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('allows the user to delete the account', async () => { |
| 123 | + const { user } = await signInWithEmailAndPassword( |
| 124 | + auth, |
| 125 | + email, |
| 126 | + 'password' |
| 127 | + ); |
| 128 | + await user.delete(); |
| 129 | + |
| 130 | + await expect(reload(user)).to.be.rejectedWith( |
| 131 | + FirebaseError, |
| 132 | + 'auth/user-token-expired' |
| 133 | + ); |
| 134 | + |
| 135 | + expect(auth.currentUser).to.be.null; |
| 136 | + await expect( |
| 137 | + signInWithEmailAndPassword(auth, email, 'password') |
| 138 | + ).to.be.rejectedWith(FirebaseError, 'auth/user-not-found'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('sign in can be called twice successively', async () => { |
| 142 | + const { user: userA } = await signInWithEmailAndPassword( |
| 143 | + auth, |
| 144 | + email, |
| 145 | + 'password' |
| 146 | + ); |
| 147 | + const { user: userB } = await signInWithEmailAndPassword( |
| 148 | + auth, |
| 149 | + email, |
| 150 | + 'password' |
| 151 | + ); |
| 152 | + expect(userA.uid).to.eq(userB.uid); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments