-
Notifications
You must be signed in to change notification settings - Fork 945
Add integration tests for phone, plus address some issues caught by them #3478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
packages-exp/auth-exp/test/integration/flows/phone.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { expect, use } from 'chai'; | ||
import * as chaiAsPromised from 'chai-as-promised'; | ||
|
||
import { | ||
linkWithPhoneNumber, | ||
PhoneAuthProvider, | ||
reauthenticateWithPhoneNumber, | ||
RecaptchaVerifier, | ||
signInAnonymously, | ||
signInWithPhoneNumber, | ||
unlink, | ||
updatePhoneNumber | ||
} from '@firebase/auth-exp/index.browser'; | ||
import { | ||
Auth, | ||
OperationType, | ||
ProviderId, | ||
UserCredential | ||
} from '@firebase/auth-types-exp'; | ||
import { FirebaseError } from '@firebase/util'; | ||
|
||
import { | ||
cleanUpTestInstance, | ||
getTestInstance | ||
} from '../../helpers/integration/helpers'; | ||
|
||
use(chaiAsPromised); | ||
|
||
// NOTE: These tests don't use a real phone number. In order to run these tests | ||
// you must whitelist the following phone numbers as "testing" numbers in the | ||
// auth console | ||
// https://console.firebase.google.com/u/0/project/_/authentication/providers | ||
// • +1 (555) 555-1000, SMS code 123456 | ||
// • +1 (555) 555-2000, SMS code 654321 | ||
|
||
const PHONE_A = { | ||
number: '+15555551000', | ||
code: '123456' | ||
}; | ||
|
||
const PHONE_B = { | ||
number: '+15555552000', | ||
code: '654321' | ||
}; | ||
|
||
describe('Integration test: phone auth', () => { | ||
let auth: Auth; | ||
let verifier: RecaptchaVerifier; | ||
let fakeRecaptchaContainer: HTMLElement; | ||
|
||
beforeEach(() => { | ||
auth = getTestInstance(); | ||
fakeRecaptchaContainer = document.createElement('div'); | ||
document.body.appendChild(fakeRecaptchaContainer); | ||
verifier = new RecaptchaVerifier(fakeRecaptchaContainer, undefined, auth); | ||
}); | ||
|
||
afterEach(async () => { | ||
await cleanUpTestInstance(auth); | ||
document.body.removeChild(fakeRecaptchaContainer); | ||
}); | ||
|
||
it('allows user to sign up', async () => { | ||
const cr = await signInWithPhoneNumber(auth, PHONE_A.number, verifier); | ||
const userCred = await cr.confirm(PHONE_A.code); | ||
|
||
expect(auth.currentUser).to.eq(userCred.user); | ||
expect(userCred.operationType).to.eq(OperationType.SIGN_IN); | ||
|
||
const user = userCred.user; | ||
expect(user.isAnonymous).to.be.false; | ||
expect(user.uid).to.be.a('string'); | ||
expect(user.phoneNumber).to.eq(PHONE_A.number); | ||
}); | ||
|
||
it('anonymous users can link (and unlink) phone number', async () => { | ||
const { user } = await signInAnonymously(auth); | ||
const { uid: anonId } = user; | ||
|
||
const cr = await linkWithPhoneNumber(user, PHONE_A.number, verifier); | ||
const linkResult = await cr.confirm(PHONE_A.code); | ||
expect(linkResult.operationType).to.eq(OperationType.LINK); | ||
expect(linkResult.user.uid).to.eq(user.uid); | ||
expect(linkResult.user.phoneNumber).to.eq(PHONE_A.number); | ||
|
||
await unlink(user, ProviderId.PHONE); | ||
expect(auth.currentUser!.uid).to.eq(anonId); | ||
expect(auth.currentUser!.isAnonymous).to.be.true; | ||
expect(auth.currentUser!.phoneNumber).to.be.null; | ||
}); | ||
|
||
context('with already-created user', () => { | ||
let signUpCred: UserCredential; | ||
|
||
function resetVerifier(): void { | ||
verifier.clear(); | ||
verifier = new RecaptchaVerifier(fakeRecaptchaContainer, undefined, auth); | ||
} | ||
|
||
beforeEach(async () => { | ||
const cr = await signInWithPhoneNumber(auth, PHONE_A.number, verifier); | ||
signUpCred = await cr.confirm(PHONE_A.code); | ||
resetVerifier(); | ||
await auth.signOut(); | ||
}); | ||
|
||
it('allows the user to sign in again', async () => { | ||
const cr = await signInWithPhoneNumber(auth, PHONE_A.number, verifier); | ||
const signInCred = await cr.confirm(PHONE_A.code); | ||
|
||
expect(signInCred.user.uid).to.eq(signUpCred.user.uid); | ||
}); | ||
|
||
it('allows the user to update their phone number', async () => { | ||
let cr = await signInWithPhoneNumber(auth, PHONE_A.number, verifier); | ||
const { user } = await cr.confirm(PHONE_A.code); | ||
|
||
resetVerifier(); | ||
|
||
const provider = new PhoneAuthProvider(auth); | ||
const verificationId = await provider.verifyPhoneNumber( | ||
PHONE_B.number, | ||
verifier | ||
); | ||
|
||
await updatePhoneNumber( | ||
user, | ||
PhoneAuthProvider.credential(verificationId, PHONE_B.code) | ||
); | ||
expect(user.phoneNumber).to.eq(PHONE_B.number); | ||
|
||
await auth.signOut(); | ||
resetVerifier(); | ||
|
||
cr = await signInWithPhoneNumber(auth, PHONE_B.number, verifier); | ||
const { user: secondSignIn } = await cr.confirm(PHONE_B.code); | ||
expect(secondSignIn.uid).to.eq(user.uid); | ||
}); | ||
|
||
it('allows the user to reauthenticate with phone number', async () => { | ||
let cr = await signInWithPhoneNumber(auth, PHONE_A.number, verifier); | ||
const { user } = await cr.confirm(PHONE_A.code); | ||
const oldToken = user.refreshToken; | ||
|
||
resetVerifier(); | ||
|
||
cr = await reauthenticateWithPhoneNumber(user, PHONE_A.number, verifier); | ||
await cr.confirm(PHONE_A.code); | ||
|
||
expect(user.refreshToken).not.to.eq(oldToken); | ||
}); | ||
|
||
it('prevents reauthentication with wrong phone number', async () => { | ||
let cr = await signInWithPhoneNumber(auth, PHONE_A.number, verifier); | ||
const { user } = await cr.confirm(PHONE_A.code); | ||
|
||
resetVerifier(); | ||
|
||
cr = await reauthenticateWithPhoneNumber(user, PHONE_B.number, verifier); | ||
await expect(cr.confirm(PHONE_B.code)).to.be.rejectedWith( | ||
FirebaseError, | ||
'auth/user-mismatch' | ||
); | ||
|
||
// We need to manually delete PHONE_B number since a failed | ||
// reauthenticateWithPhoneNumber does not trigger a state change | ||
resetVerifier(); | ||
cr = await signInWithPhoneNumber(auth, PHONE_B.number, verifier); | ||
const { user: otherUser } = await cr.confirm(PHONE_B.code); | ||
await otherUser.delete(); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.