|
| 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 | + |
| 21 | +import { FirebaseError, querystringDecode } from '@firebase/util'; |
| 22 | + |
| 23 | +import { mockAuth } from '../../../test/mock_auth'; |
| 24 | +import * as fetch from '../../../test/mock_fetch'; |
| 25 | +import { ServerError } from '../errors'; |
| 26 | +import { _ENDPOINT, requestStsToken } from './token'; |
| 27 | + |
| 28 | +use(chaiAsPromised); |
| 29 | + |
| 30 | +describe('requestStsToken', () => { |
| 31 | + const { apiKey, tokenApiHost, apiScheme } = mockAuth.config; |
| 32 | + const endpoint = `${apiScheme}://${tokenApiHost}/${_ENDPOINT}?key=${apiKey}`; |
| 33 | + beforeEach(fetch.setUp); |
| 34 | + afterEach(fetch.tearDown); |
| 35 | + |
| 36 | + it('should POST to the correct endpoint', async () => { |
| 37 | + const mock = fetch.mock(endpoint, { |
| 38 | + 'access_token': 'new-access-token', |
| 39 | + 'expires_in': '3600', |
| 40 | + 'refresh_token': 'new-refresh-token' |
| 41 | + }); |
| 42 | + |
| 43 | + const response = await requestStsToken(mockAuth, 'old-refresh-token'); |
| 44 | + expect(response.accessToken).to.eq('new-access-token'); |
| 45 | + expect(response.expiresIn).to.eq('3600'); |
| 46 | + expect(response.refreshToken).to.eq('new-refresh-token'); |
| 47 | + const request = querystringDecode(`?${mock.calls[0].request}`); |
| 48 | + expect(request).to.eql({ |
| 49 | + 'grant_type': 'refresh_token', |
| 50 | + 'refresh_token': 'old-refresh-token' |
| 51 | + }); |
| 52 | + expect(mock.calls[0].method).to.eq('POST'); |
| 53 | + expect(mock.calls[0].headers).to.eql({ |
| 54 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 55 | + 'X-Client-Version': 'testSDK/0.0.0' |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should handle errors', async () => { |
| 60 | + const mock = fetch.mock( |
| 61 | + endpoint, |
| 62 | + { |
| 63 | + error: { |
| 64 | + code: 400, |
| 65 | + message: ServerError.TOKEN_EXPIRED, |
| 66 | + errors: [ |
| 67 | + { |
| 68 | + message: ServerError.TOKEN_EXPIRED |
| 69 | + } |
| 70 | + ] |
| 71 | + } |
| 72 | + }, |
| 73 | + 400 |
| 74 | + ); |
| 75 | + |
| 76 | + await expect(requestStsToken(mockAuth, 'old-token')).to.be.rejectedWith( |
| 77 | + FirebaseError, |
| 78 | + "Firebase: The user's credential is no longer valid. The user must sign in again. (auth/user-token-expired)" |
| 79 | + ); |
| 80 | + const request = querystringDecode(`?${mock.calls[0].request}`); |
| 81 | + expect(request).to.eql({ |
| 82 | + 'grant_type': 'refresh_token', |
| 83 | + 'refresh_token': 'old-token' |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments