|
| 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 * as chaiAsPromised from 'chai-as-promised'; |
| 19 | +import * as sinonChai from 'sinon-chai'; |
| 20 | +import { expect, use } from 'chai'; |
| 21 | +import { testAuth, TestAuth } from '../../test/helpers/mock_auth'; |
| 22 | +import { SingletonInstantiator } from '../core/util/instantiator'; |
| 23 | +import { AuthEventType, PopupRedirectResolver } from '../model/popup_redirect'; |
| 24 | +import { cordovaPopupRedirectResolver } from './popup_redirect'; |
| 25 | +import { GoogleAuthProvider } from '../core/providers/google'; |
| 26 | +import { FirebaseError } from '@firebase/util'; |
| 27 | + |
| 28 | +use(chaiAsPromised); |
| 29 | +use(sinonChai); |
| 30 | + |
| 31 | +describe('platform_cordova/popup_redirect', () => { |
| 32 | + let auth: TestAuth; |
| 33 | + let resolver: PopupRedirectResolver; |
| 34 | + |
| 35 | + beforeEach(async () => { |
| 36 | + auth = await testAuth(); |
| 37 | + attachExpectedPlugins(); |
| 38 | + resolver = new (cordovaPopupRedirectResolver as SingletonInstantiator<PopupRedirectResolver>)(); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('_openRedirect plugin checks', () => { |
| 42 | + // TODO: Rest of the tests go here |
| 43 | + it('does not reject if all plugins installed', () => { |
| 44 | + expect(() => |
| 45 | + resolver._openRedirect( |
| 46 | + auth, |
| 47 | + new GoogleAuthProvider(), |
| 48 | + AuthEventType.SIGN_IN_VIA_REDIRECT |
| 49 | + ) |
| 50 | + ).not.to.throw; |
| 51 | + }); |
| 52 | + |
| 53 | + it('rejects if universal links is missing', () => { |
| 54 | + removeProp(window, 'universalLinks'); |
| 55 | + expect(() => |
| 56 | + resolver._openRedirect( |
| 57 | + auth, |
| 58 | + new GoogleAuthProvider(), |
| 59 | + AuthEventType.SIGN_IN_VIA_REDIRECT |
| 60 | + ) |
| 61 | + ) |
| 62 | + .to.throw(FirebaseError, 'auth/invalid-cordova-configuration') |
| 63 | + .that.has.deep.property('customData', { |
| 64 | + appName: 'test-app', |
| 65 | + missingPlugin: 'cordova-universal-links-plugin-fix' |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('rejects if build info is missing', () => { |
| 70 | + removeProp(window.BuildInfo, 'packageName'); |
| 71 | + expect(() => |
| 72 | + resolver._openRedirect( |
| 73 | + auth, |
| 74 | + new GoogleAuthProvider(), |
| 75 | + AuthEventType.SIGN_IN_VIA_REDIRECT |
| 76 | + ) |
| 77 | + ) |
| 78 | + .to.throw(FirebaseError, 'auth/invalid-cordova-configuration') |
| 79 | + .that.has.deep.property('customData', { |
| 80 | + appName: 'test-app', |
| 81 | + missingPlugin: 'cordova-plugin-buildInfo' |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('rejects if browsertab openUrl is missing', () => { |
| 86 | + removeProp(window.cordova.plugins.browsertab, 'openUrl'); |
| 87 | + expect(() => |
| 88 | + resolver._openRedirect( |
| 89 | + auth, |
| 90 | + new GoogleAuthProvider(), |
| 91 | + AuthEventType.SIGN_IN_VIA_REDIRECT |
| 92 | + ) |
| 93 | + ) |
| 94 | + .to.throw(FirebaseError, 'auth/invalid-cordova-configuration') |
| 95 | + .that.has.deep.property('customData', { |
| 96 | + appName: 'test-app', |
| 97 | + missingPlugin: 'cordova-plugin-browsertab' |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('rejects if InAppBrowser is missing', () => { |
| 102 | + removeProp(window.cordova.InAppBrowser, 'open'); |
| 103 | + expect(() => |
| 104 | + resolver._openRedirect( |
| 105 | + auth, |
| 106 | + new GoogleAuthProvider(), |
| 107 | + AuthEventType.SIGN_IN_VIA_REDIRECT |
| 108 | + ) |
| 109 | + ) |
| 110 | + .to.throw(FirebaseError, 'auth/invalid-cordova-configuration') |
| 111 | + .that.has.deep.property('customData', { |
| 112 | + appName: 'test-app', |
| 113 | + missingPlugin: 'cordova-plugin-inappbrowser' |
| 114 | + }); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +function attachExpectedPlugins(): void { |
| 120 | + // Eventually these will be replaced with full mocks |
| 121 | + const win = (window as unknown) as Record<string, unknown>; |
| 122 | + win.cordova = { |
| 123 | + plugins: { |
| 124 | + browsertab: { |
| 125 | + isAvailable: () => {}, |
| 126 | + openUrl: () => {} |
| 127 | + } |
| 128 | + }, |
| 129 | + InAppBrowser: { |
| 130 | + open: () => {} |
| 131 | + } |
| 132 | + }; |
| 133 | + win.universalLinks = { |
| 134 | + subscribe: () => {} |
| 135 | + }; |
| 136 | + win.BuildInfo = { |
| 137 | + packageName: 'com.example.name.package' |
| 138 | + }; |
| 139 | +} |
| 140 | + |
| 141 | +function removeProp(obj: unknown, prop: string): void { |
| 142 | + delete (obj as Record<string, unknown>)[prop]; |
| 143 | +} |
0 commit comments