|
| 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 | +import * as sinon from 'sinon'; |
| 21 | +import * as sinonChai from 'sinon-chai'; |
| 22 | + |
| 23 | +import { ApplicationVerifier, OperationType } from '@firebase/auth-types-exp'; |
| 24 | +import { FirebaseError } from '@firebase/util'; |
| 25 | + |
| 26 | +import { mockEndpoint } from '../../../test/api/helper'; |
| 27 | +import { testAuth } from '../../../test/mock_auth'; |
| 28 | +import * as fetch from '../../../test/mock_fetch'; |
| 29 | +import { Endpoint } from '../../api'; |
| 30 | +import { Auth } from '../../model/auth'; |
| 31 | +import { IdTokenResponse } from '../../model/id_token'; |
| 32 | +import { RecaptchaVerifier } from '../../platform_browser/recaptcha/recaptcha_verifier'; |
| 33 | +import { _verifyPhoneNumber, signInWithPhoneNumber } from './phone'; |
| 34 | + |
| 35 | +use(chaiAsPromised); |
| 36 | +use(sinonChai); |
| 37 | + |
| 38 | +describe('core/strategies/phone', () => { |
| 39 | + let auth: Auth; |
| 40 | + let verifier: ApplicationVerifier; |
| 41 | + let sendCodeEndpoint: fetch.Route; |
| 42 | + |
| 43 | + beforeEach(async () => { |
| 44 | + auth = await testAuth(); |
| 45 | + fetch.setUp(); |
| 46 | + |
| 47 | + sendCodeEndpoint = mockEndpoint(Endpoint.SEND_VERIFICATION_CODE, { |
| 48 | + sessionInfo: 'session-info' |
| 49 | + }); |
| 50 | + |
| 51 | + verifier = new RecaptchaVerifier(document.createElement('div'), {}, auth); |
| 52 | + sinon.stub(verifier, 'verify').returns(Promise.resolve('recaptcha-token')); |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + fetch.tearDown(); |
| 57 | + sinon.restore(); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('signInWithPhoneNumber', () => { |
| 61 | + it('calls verify phone number', async () => { |
| 62 | + await signInWithPhoneNumber(auth, '+15105550000', verifier); |
| 63 | + |
| 64 | + expect(sendCodeEndpoint.calls[0].request).to.eql({ |
| 65 | + recaptchaToken: 'recaptcha-token', |
| 66 | + phoneNumber: '+15105550000' |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + context('ConfirmationResult', () => { |
| 71 | + it('result contains verification id baked in', async () => { |
| 72 | + const result = await signInWithPhoneNumber(auth, 'number', verifier); |
| 73 | + expect(result.verificationId).to.eq('session-info'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('calling #confirm finishes the sign in flow', async () => { |
| 77 | + const idTokenResponse: IdTokenResponse = { |
| 78 | + idToken: 'my-id-token', |
| 79 | + refreshToken: 'my-refresh-token', |
| 80 | + expiresIn: '1234', |
| 81 | + localId: 'uid', |
| 82 | + kind: 'my-kind' |
| 83 | + }; |
| 84 | + |
| 85 | + // This endpoint is called from within the callback, in |
| 86 | + // signInWithCredential |
| 87 | + const signInEndpoint = mockEndpoint( |
| 88 | + Endpoint.SIGN_IN_WITH_PHONE_NUMBER, |
| 89 | + idTokenResponse |
| 90 | + ); |
| 91 | + mockEndpoint(Endpoint.GET_ACCOUNT_INFO, { |
| 92 | + users: [{ localId: 'uid' }] |
| 93 | + }); |
| 94 | + |
| 95 | + const result = await signInWithPhoneNumber(auth, 'number', verifier); |
| 96 | + const userCred = await result.confirm('6789'); |
| 97 | + expect(userCred.user.uid).to.eq('uid'); |
| 98 | + expect(userCred.operationType).to.eq(OperationType.SIGN_IN); |
| 99 | + expect(signInEndpoint.calls[0].request).to.eql({ |
| 100 | + sessionInfo: 'session-info', |
| 101 | + code: '6789' |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('_verifyPhoneNumber', () => { |
| 108 | + it('works with a string phone number', async () => { |
| 109 | + await _verifyPhoneNumber(auth, 'number', verifier); |
| 110 | + expect(sendCodeEndpoint.calls[0].request).to.eql({ |
| 111 | + recaptchaToken: 'recaptcha-token', |
| 112 | + phoneNumber: 'number' |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('works with an options object', async () => { |
| 117 | + await _verifyPhoneNumber( |
| 118 | + auth, |
| 119 | + { |
| 120 | + phoneNumber: 'number' |
| 121 | + }, |
| 122 | + verifier |
| 123 | + ); |
| 124 | + expect(sendCodeEndpoint.calls[0].request).to.eql({ |
| 125 | + recaptchaToken: 'recaptcha-token', |
| 126 | + phoneNumber: 'number' |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('throws if the verifier does not return a string', async () => { |
| 131 | + (verifier.verify as sinon.SinonStub).returns(Promise.resolve(123)); |
| 132 | + await expect( |
| 133 | + _verifyPhoneNumber(auth, 'number', verifier) |
| 134 | + ).to.be.rejectedWith( |
| 135 | + FirebaseError, |
| 136 | + 'Firebase: Error (auth/argument-error)' |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + it('throws if the verifier type is not recaptcha', async () => { |
| 141 | + const mutVerifier: { |
| 142 | + -readonly [K in keyof ApplicationVerifier]: ApplicationVerifier[K]; |
| 143 | + } = verifier; |
| 144 | + mutVerifier.type = 'not-recaptcha-thats-for-sure'; |
| 145 | + await expect( |
| 146 | + _verifyPhoneNumber(auth, 'number', mutVerifier) |
| 147 | + ).to.be.rejectedWith( |
| 148 | + FirebaseError, |
| 149 | + 'Firebase: Error (auth/argument-error)' |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + it('resets the verifer after successful verification', async () => { |
| 154 | + sinon.spy(verifier, 'reset'); |
| 155 | + expect(await _verifyPhoneNumber(auth, 'number', verifier)).to.eq( |
| 156 | + 'session-info' |
| 157 | + ); |
| 158 | + expect(verifier.reset).to.have.been.called; |
| 159 | + }); |
| 160 | + |
| 161 | + it('resets the verifer after a failed verification', async () => { |
| 162 | + sinon.spy(verifier, 'reset'); |
| 163 | + (verifier.verify as sinon.SinonStub).returns(Promise.resolve(123)); |
| 164 | + |
| 165 | + await expect(_verifyPhoneNumber(auth, 'number', verifier)).to.be.rejected; |
| 166 | + expect(verifier.reset).to.have.been.called; |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments