|
| 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 { FirebaseError } from '@firebase/util'; |
| 19 | +import { expect, use } from 'chai'; |
| 20 | +import * as chaiAsPromised from 'chai-as-promised'; |
| 21 | +import { SinonStub, stub } from 'sinon'; |
| 22 | +import { mockEndpoint } from '../../../test/api/helper'; |
| 23 | +import { mockAuth } from '../../../test/mock_auth'; |
| 24 | +import * as mockFetch from '../../../test/mock_fetch'; |
| 25 | +import { Endpoint } from '../../api'; |
| 26 | +import { ServerError } from '../../api/errors'; |
| 27 | +import { ProviderId } from '../providers'; |
| 28 | +import * as location from '../util/location'; |
| 29 | +import { fetchSignInMethodsForEmail } from './email'; |
| 30 | + |
| 31 | +use(chaiAsPromised); |
| 32 | + |
| 33 | +describe('fetchSignInMethodsForEmail', () => { |
| 34 | + const email = '[email protected]'; |
| 35 | + const expectedSignInMethods = [ProviderId.PASSWORD, ProviderId.GOOGLE]; |
| 36 | + |
| 37 | + beforeEach(mockFetch.setUp); |
| 38 | + afterEach(mockFetch.tearDown); |
| 39 | + |
| 40 | + it('should return the sign in methods', async () => { |
| 41 | + const mock = mockEndpoint(Endpoint.CREATE_AUTH_URI, { |
| 42 | + signinMethods: expectedSignInMethods |
| 43 | + }); |
| 44 | + const response = await fetchSignInMethodsForEmail(mockAuth, email); |
| 45 | + expect(response).to.eql(expectedSignInMethods); |
| 46 | + expect(mock.calls[0].request).to.eql({ |
| 47 | + identifier: email, |
| 48 | + continueUri: location.getCurrentUrl() |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + context('on non standard platforms', () => { |
| 53 | + let locationStub: SinonStub; |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + locationStub = stub(location, 'isHttpOrHttps'); |
| 57 | + locationStub.callsFake(() => false); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + locationStub.restore(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should use localhost for the continueUri', async () => { |
| 65 | + const mock = mockEndpoint(Endpoint.CREATE_AUTH_URI, { |
| 66 | + signinMethods: expectedSignInMethods |
| 67 | + }); |
| 68 | + const response = await fetchSignInMethodsForEmail(mockAuth, email); |
| 69 | + expect(response).to.eql(expectedSignInMethods); |
| 70 | + expect(mock.calls[0].request).to.eql({ |
| 71 | + identifier: email, |
| 72 | + continueUri: 'http://localhost' |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should surface errors', async () => { |
| 78 | + const mock = mockEndpoint( |
| 79 | + Endpoint.CREATE_AUTH_URI, |
| 80 | + { |
| 81 | + error: { |
| 82 | + code: 400, |
| 83 | + message: ServerError.INVALID_EMAIL |
| 84 | + } |
| 85 | + }, |
| 86 | + 400 |
| 87 | + ); |
| 88 | + await expect( |
| 89 | + fetchSignInMethodsForEmail(mockAuth, email) |
| 90 | + ).to.be.rejectedWith( |
| 91 | + FirebaseError, |
| 92 | + 'Firebase: The email address is badly formatted. (auth/invalid-email).' |
| 93 | + ); |
| 94 | + expect(mock.calls.length).to.eq(1); |
| 95 | + }); |
| 96 | +}); |
0 commit comments