|  | 
|  | 1 | +/** | 
|  | 2 | + * @license | 
|  | 3 | + * Copyright 2023 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 | +// eslint-disable-next-line import/no-extraneous-dependencies | 
|  | 19 | +import { ActionCodeSettings, Auth, sendSignInLinkToEmail } from '@firebase/auth'; | 
|  | 20 | +import { expect, use } from 'chai'; | 
|  | 21 | +import { | 
|  | 22 | +  getTestInstance, | 
|  | 23 | +  randomEmail | 
|  | 24 | +} from '../../helpers/integration/helpers'; | 
|  | 25 | +import { getEmulatorUrl } from '../../helpers/integration/settings'; | 
|  | 26 | +import chaiAsPromised from 'chai-as-promised'; | 
|  | 27 | +import { FirebaseError } from '@firebase/util'; | 
|  | 28 | + | 
|  | 29 | +use(chaiAsPromised); | 
|  | 30 | + | 
|  | 31 | +// Assumes mobileLinksConfig.domain is set as "HOSTING_DOMAIN" in the test GCP-project. | 
|  | 32 | +describe('Integration test: hosting link validation', () => { | 
|  | 33 | +  let auth: Auth; | 
|  | 34 | +  let email: string; | 
|  | 35 | + | 
|  | 36 | +  const AUTHORIZED_CUSTOM_DOMAIN = "localhost/action_code_return"; | 
|  | 37 | +  const ANDROID_PACKAGE_NAME = "com.google.firebase.test.thin"; | 
|  | 38 | +  const BASE_SETTINGS: ActionCodeSettings = { | 
|  | 39 | +    url: 'http://' + AUTHORIZED_CUSTOM_DOMAIN, | 
|  | 40 | +    handleCodeInApp: true, | 
|  | 41 | +    android: {packageName: ANDROID_PACKAGE_NAME}, | 
|  | 42 | +  }; | 
|  | 43 | +  const VALID_LINK_DOMAIN = "jscore-sandbox.testdomaindonotuse.com"; | 
|  | 44 | +  const INVALID_LINK_DOMAIN = "invalid.testdomaindonotuse.com"; | 
|  | 45 | +  const INVALID_LINK_DOMAIN_ERROR = "auth/invalid-hosting-link-domain"; | 
|  | 46 | +  const TEST_TENANT_ID = 'passpol-tenant-d7hha'; | 
|  | 47 | + | 
|  | 48 | +  beforeEach(function () { | 
|  | 49 | +    auth = getTestInstance(); | 
|  | 50 | +    email = randomEmail(); | 
|  | 51 | + | 
|  | 52 | +    if (getEmulatorUrl()) { | 
|  | 53 | +      this.skip(); | 
|  | 54 | +    } | 
|  | 55 | +  }); | 
|  | 56 | +   | 
|  | 57 | +  it('allows user to sign in with default firebase hosting link', async () => { | 
|  | 58 | +    // Sends email link to user using default hosting link. | 
|  | 59 | +    await sendSignInLinkToEmail( | 
|  | 60 | +      auth, | 
|  | 61 | +      email, | 
|  | 62 | +      BASE_SETTINGS | 
|  | 63 | +    ); | 
|  | 64 | +  }); | 
|  | 65 | + | 
|  | 66 | +  it('allows user to sign in to a tenant with default firebase hosting link', async () => { | 
|  | 67 | +    auth.tenantId = TEST_TENANT_ID; | 
|  | 68 | +    // Sends email link to user using default hosting link. | 
|  | 69 | +    await sendSignInLinkToEmail( | 
|  | 70 | +      auth, | 
|  | 71 | +      email, | 
|  | 72 | +      BASE_SETTINGS | 
|  | 73 | +    ); | 
|  | 74 | +  }); | 
|  | 75 | + | 
|  | 76 | +  it('allows user to sign in with custom firebase hosting link', async () => { | 
|  | 77 | +      // Sends email link to user using custom hosting link. | 
|  | 78 | +      await sendSignInLinkToEmail( | 
|  | 79 | +        auth, | 
|  | 80 | +        email, | 
|  | 81 | +        { | 
|  | 82 | +          ...BASE_SETTINGS, | 
|  | 83 | +          linkDomain: VALID_LINK_DOMAIN | 
|  | 84 | +         | 
|  | 85 | +        } | 
|  | 86 | +      ); | 
|  | 87 | +    });  | 
|  | 88 | + | 
|  | 89 | +    it('allows user to sign in to a tenant with custom firebase hosting link', async () => { | 
|  | 90 | +      // Sends email link to user using custom hosting link. | 
|  | 91 | +      auth.tenantId = TEST_TENANT_ID; | 
|  | 92 | +      await sendSignInLinkToEmail( | 
|  | 93 | +        auth, | 
|  | 94 | +        email, | 
|  | 95 | +        { | 
|  | 96 | +          ...BASE_SETTINGS, | 
|  | 97 | +          linkDomain: VALID_LINK_DOMAIN | 
|  | 98 | +         | 
|  | 99 | +        } | 
|  | 100 | +      ); | 
|  | 101 | +    });  | 
|  | 102 | + | 
|  | 103 | +    it('sign in with invalid firebase hosting link throws exception', async () => { | 
|  | 104 | +      // Throws an exception while sening email link to user using invalid hosting link. | 
|  | 105 | +      await expect(sendSignInLinkToEmail( | 
|  | 106 | +        auth, | 
|  | 107 | +        email, | 
|  | 108 | +        { | 
|  | 109 | +          ...BASE_SETTINGS, | 
|  | 110 | +          linkDomain: INVALID_LINK_DOMAIN | 
|  | 111 | +         | 
|  | 112 | +        } | 
|  | 113 | +      )).to.be.rejectedWith(FirebaseError, new RegExp(".*" + INVALID_LINK_DOMAIN_ERROR + ".*")); | 
|  | 114 | +    });  | 
|  | 115 | +}); | 
0 commit comments