|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 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 { FirebaseError } from '@firebase/util'; |
| 24 | + |
| 25 | +import { endpointUrl, mockEndpoint } from '../../../test/helpers/api/helper'; |
| 26 | +import { testAuth, TestAuth, testUser } from '../../../test/helpers/mock_auth'; |
| 27 | +import * as fetch from '../../../test/helpers/mock_fetch'; |
| 28 | +import { Endpoint } from '../../api'; |
| 29 | +import { User } from '../../model/user'; |
| 30 | +import { _castAuth } from './auth_impl'; |
| 31 | +import { useAuthEmulator } from './emulator'; |
| 32 | + |
| 33 | +use(sinonChai); |
| 34 | +use(chaiAsPromised); |
| 35 | + |
| 36 | +describe('core/auth/emulator', () => { |
| 37 | + let auth: TestAuth; |
| 38 | + let user: User; |
| 39 | + let normalEndpoint: fetch.Route; |
| 40 | + let emulatorEndpoint: fetch.Route; |
| 41 | + |
| 42 | + beforeEach(async () => { |
| 43 | + auth = await testAuth(); |
| 44 | + user = testUser(_castAuth(auth), 'uid', 'email', true); |
| 45 | + fetch.setUp(); |
| 46 | + normalEndpoint = mockEndpoint(Endpoint.DELETE_ACCOUNT, {}); |
| 47 | + emulatorEndpoint = fetch.mock( |
| 48 | + `http://localhost:2020/${endpointUrl(Endpoint.DELETE_ACCOUNT).replace( |
| 49 | + /^.*:\/\//, |
| 50 | + '' |
| 51 | + )}`, |
| 52 | + {} |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + fetch.tearDown(); |
| 58 | + sinon.restore(); |
| 59 | + |
| 60 | + // The DOM persists through tests; remove the banner if it is attached |
| 61 | + const banner = |
| 62 | + typeof document !== 'undefined' |
| 63 | + ? document.querySelector('.firebase-emulator-warning') |
| 64 | + : null; |
| 65 | + if (banner) { |
| 66 | + banner.parentElement?.removeChild(banner); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + context('useAuthEmulator', () => { |
| 71 | + it('fails if a network request has already been made', async () => { |
| 72 | + await user.delete(); |
| 73 | + expect(() => useAuthEmulator(auth, 'http://localhost:2020')).to.throw( |
| 74 | + FirebaseError, |
| 75 | + 'auth/emulator-config-failed' |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it('updates the endpoint appropriately', async () => { |
| 80 | + useAuthEmulator(auth, 'http://localhost:2020'); |
| 81 | + await user.delete(); |
| 82 | + expect(normalEndpoint.calls.length).to.eq(0); |
| 83 | + expect(emulatorEndpoint.calls.length).to.eq(1); |
| 84 | + }); |
| 85 | + |
| 86 | + it('checks the scheme properly', () => { |
| 87 | + expect(() => useAuthEmulator(auth, 'http://localhost:2020')).not.to.throw; |
| 88 | + delete auth.config.emulator; |
| 89 | + expect(() => useAuthEmulator(auth, 'https://localhost:2020')).not.to |
| 90 | + .throw; |
| 91 | + delete auth.config.emulator; |
| 92 | + expect(() => useAuthEmulator(auth, 'ssh://localhost:2020')).to.throw( |
| 93 | + FirebaseError, |
| 94 | + 'auth/invalid-emulator-scheme' |
| 95 | + ); |
| 96 | + delete auth.config.emulator; |
| 97 | + expect(() => useAuthEmulator(auth, 'localhost:2020')).to.throw( |
| 98 | + FirebaseError, |
| 99 | + 'auth/invalid-emulator-scheme' |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it('attaches a banner to the DOM', () => { |
| 104 | + useAuthEmulator(auth, 'http://localhost:2020'); |
| 105 | + if (typeof document !== 'undefined') { |
| 106 | + const el = document.querySelector('.firebase-emulator-warning')!; |
| 107 | + expect(el).not.to.be.null; |
| 108 | + expect(el.textContent).to.eq( |
| 109 | + 'Running in emulator mode. ' + |
| 110 | + 'Do not use with production credentials.' |
| 111 | + ); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it('logs out a warning to the console', () => { |
| 116 | + sinon.stub(console, 'info'); |
| 117 | + useAuthEmulator(auth, 'http://localhost:2020'); |
| 118 | + expect(console.info).to.have.been.calledWith( |
| 119 | + 'WARNING: You are using the Auth Emulator,' + |
| 120 | + ' which is intended for local testing only. Do not use with' + |
| 121 | + ' production credentials.' |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + it('logs out the warning but has no banner if disableBanner true', () => { |
| 126 | + sinon.stub(console, 'info'); |
| 127 | + useAuthEmulator(auth, 'http://localhost:2020', { disableWarnings: true }); |
| 128 | + expect(console.info).to.have.been.calledWith( |
| 129 | + 'WARNING: You are using the Auth Emulator,' + |
| 130 | + ' which is intended for local testing only. Do not use with' + |
| 131 | + ' production credentials.' |
| 132 | + ); |
| 133 | + if (typeof document !== 'undefined') { |
| 134 | + expect(document.querySelector('.firebase-emulator-warning')).to.be.null; |
| 135 | + } |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + context('toJSON', () => { |
| 140 | + it('works when theres no current user', () => { |
| 141 | + expect(JSON.stringify(auth)).to.eq( |
| 142 | + '{"apiKey":"test-api-key","authDomain":"localhost","appName":"test-app"}' |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + it('also stringifies the current user', () => { |
| 147 | + auth.currentUser = ({ |
| 148 | + toJSON: (): object => ({ foo: 'bar' }) |
| 149 | + } as unknown) as User; |
| 150 | + expect(JSON.stringify(auth)).to.eq( |
| 151 | + '{"apiKey":"test-api-key","authDomain":"localhost",' + |
| 152 | + '"appName":"test-app","currentUser":{"foo":"bar"}}' |
| 153 | + ); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments