|
| 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, useFakeTimers } from 'sinon'; |
| 22 | +import { |
| 23 | + DEFAULT_API_TIMEOUT_MS, |
| 24 | + Endpoint, |
| 25 | + HttpMethod, |
| 26 | + performApiRequest |
| 27 | +} from '.'; |
| 28 | +import { mockEndpoint } from '../../test/api/helper'; |
| 29 | +import { mockAuth } from '../../test/mock_auth'; |
| 30 | +import * as mockFetch from '../../test/mock_fetch'; |
| 31 | +import { ServerError } from './errors'; |
| 32 | +import { AuthErrorCode } from '../core/errors'; |
| 33 | + |
| 34 | +use(chaiAsPromised); |
| 35 | + |
| 36 | +describe('performApiRequest', () => { |
| 37 | + const request = { |
| 38 | + requestKey: 'request-value' |
| 39 | + }; |
| 40 | + |
| 41 | + const serverResponse = { |
| 42 | + responseKey: 'response-value' |
| 43 | + }; |
| 44 | + |
| 45 | + context('with regular requests', () => { |
| 46 | + beforeEach(mockFetch.setUp); |
| 47 | + afterEach(mockFetch.tearDown); |
| 48 | + |
| 49 | + it('should set the correct request, method and HTTP Headers', async () => { |
| 50 | + const mock = mockEndpoint(Endpoint.SIGN_UP, serverResponse); |
| 51 | + const response = await performApiRequest< |
| 52 | + typeof request, |
| 53 | + typeof serverResponse |
| 54 | + >(mockAuth, HttpMethod.POST, Endpoint.SIGN_UP, request); |
| 55 | + expect(response).to.eql(serverResponse); |
| 56 | + expect(mock.calls.length).to.eq(1); |
| 57 | + expect(mock.calls[0].method).to.eq(HttpMethod.POST); |
| 58 | + expect(mock.calls[0].request).to.eql(request); |
| 59 | + expect(mock.calls[0].headers).to.eql({ |
| 60 | + 'Content-Type': 'application/json', |
| 61 | + 'X-Client-Version': 'testSDK/0.0.0' |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should translate server errors to auth errors', async () => { |
| 66 | + const mock = mockEndpoint( |
| 67 | + Endpoint.SIGN_UP, |
| 68 | + { |
| 69 | + error: { |
| 70 | + code: 400, |
| 71 | + message: ServerError.EMAIL_EXISTS, |
| 72 | + errors: [ |
| 73 | + { |
| 74 | + message: ServerError.EMAIL_EXISTS |
| 75 | + } |
| 76 | + ] |
| 77 | + } |
| 78 | + }, |
| 79 | + 400 |
| 80 | + ); |
| 81 | + const promise = performApiRequest<typeof request, typeof serverResponse>( |
| 82 | + mockAuth, |
| 83 | + HttpMethod.POST, |
| 84 | + Endpoint.SIGN_UP, |
| 85 | + request |
| 86 | + ); |
| 87 | + await expect(promise).to.be.rejectedWith( |
| 88 | + FirebaseError, |
| 89 | + 'Firebase: The email address is already in use by another account. (auth/email-already-in-use).' |
| 90 | + ); |
| 91 | + expect(mock.calls[0].request).to.eql(request); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should handle unknown server errors', async () => { |
| 95 | + const mock = mockEndpoint( |
| 96 | + Endpoint.SIGN_UP, |
| 97 | + { |
| 98 | + error: { |
| 99 | + code: 400, |
| 100 | + message: 'Awesome error', |
| 101 | + errors: [ |
| 102 | + { |
| 103 | + message: 'Awesome error' |
| 104 | + } |
| 105 | + ] |
| 106 | + } |
| 107 | + }, |
| 108 | + 400 |
| 109 | + ); |
| 110 | + const promise = performApiRequest<typeof request, typeof serverResponse>( |
| 111 | + mockAuth, |
| 112 | + HttpMethod.POST, |
| 113 | + Endpoint.SIGN_UP, |
| 114 | + request |
| 115 | + ); |
| 116 | + await expect(promise).to.be.rejectedWith( |
| 117 | + FirebaseError, |
| 118 | + 'Firebase: An internal AuthError has occurred. (auth/internal-error).' |
| 119 | + ); |
| 120 | + expect(mock.calls[0].request).to.eql(request); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should support custom error handling per endpoint', async () => { |
| 124 | + const mock = mockEndpoint( |
| 125 | + Endpoint.SIGN_UP, |
| 126 | + { |
| 127 | + error: { |
| 128 | + code: 400, |
| 129 | + message: ServerError.EMAIL_EXISTS, |
| 130 | + errors: [ |
| 131 | + { |
| 132 | + message: ServerError.EMAIL_EXISTS |
| 133 | + } |
| 134 | + ] |
| 135 | + } |
| 136 | + }, |
| 137 | + 400 |
| 138 | + ); |
| 139 | + const promise = performApiRequest<typeof request, typeof serverResponse>( |
| 140 | + mockAuth, |
| 141 | + HttpMethod.POST, |
| 142 | + Endpoint.SIGN_UP, |
| 143 | + request, |
| 144 | + { |
| 145 | + [ServerError.EMAIL_EXISTS]: AuthErrorCode.ARGUMENT_ERROR |
| 146 | + } |
| 147 | + ); |
| 148 | + await expect(promise).to.be.rejectedWith( |
| 149 | + FirebaseError, |
| 150 | + 'Firebase: Error (auth/argument-error).' |
| 151 | + ); |
| 152 | + expect(mock.calls[0].request).to.eql(request); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + context('with network issues', () => { |
| 157 | + let fetchStub: SinonStub; |
| 158 | + |
| 159 | + beforeEach(() => { |
| 160 | + fetchStub = stub(self, 'fetch'); |
| 161 | + }); |
| 162 | + |
| 163 | + afterEach(() => { |
| 164 | + fetchStub.restore(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should handle timeouts', async () => { |
| 168 | + const clock = useFakeTimers(); |
| 169 | + fetchStub.callsFake(() => { |
| 170 | + return new Promise<never>(() => null); |
| 171 | + }); |
| 172 | + const promise = performApiRequest<typeof request, never>( |
| 173 | + mockAuth, |
| 174 | + HttpMethod.POST, |
| 175 | + Endpoint.SIGN_UP, |
| 176 | + request |
| 177 | + ); |
| 178 | + clock.tick(DEFAULT_API_TIMEOUT_MS + 1); |
| 179 | + await expect(promise).to.be.rejectedWith( |
| 180 | + FirebaseError, |
| 181 | + 'Firebase: The operation has timed out. (auth/timeout).' |
| 182 | + ); |
| 183 | + clock.restore(); |
| 184 | + }); |
| 185 | + |
| 186 | + it('should handle network failure', async () => { |
| 187 | + fetchStub.callsFake(() => { |
| 188 | + return new Promise<never>((_, reject) => |
| 189 | + reject(new Error('network error')) |
| 190 | + ); |
| 191 | + }); |
| 192 | + const promise = performApiRequest<typeof request, never>( |
| 193 | + mockAuth, |
| 194 | + HttpMethod.POST, |
| 195 | + Endpoint.SIGN_UP, |
| 196 | + request |
| 197 | + ); |
| 198 | + await expect(promise).to.be.rejectedWith( |
| 199 | + FirebaseError, |
| 200 | + 'Firebase: A network AuthError (such as timeout]: interrupted connection or unreachable host) has occurred. (auth/network-request-failed).' |
| 201 | + ); |
| 202 | + }); |
| 203 | + }); |
| 204 | +}); |
0 commit comments