diff --git a/packages/installations/karma.conf.js b/packages/installations/karma.conf.js index 1699a0681ec..71ad8b9362f 100644 --- a/packages/installations/karma.conf.js +++ b/packages/installations/karma.conf.js @@ -24,6 +24,7 @@ module.exports = function (config) { ...karmaBase, // files to load into karma files, + exclude: ['src/**/*-server-app.test.ts'], // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['mocha'] diff --git a/packages/installations/package.json b/packages/installations/package.json index 78b6dacc6da..48ec9b9a952 100644 --- a/packages/installations/package.json +++ b/packages/installations/package.json @@ -25,9 +25,11 @@ "build:deps": "lerna run --scope @firebase/installations --include-dependencies build", "build:release": "yarn build && yarn typings:public", "dev": "rollup -c -w", - "test": "yarn type-check && yarn test:karma && yarn lint", - "test:ci": "node ../../scripts/run_tests_in_ci.js", - "test:karma": "karma start", + "test": "yarn type-check && yarn test:all && yarn lint", + "test:ci": "node ../../scripts/run_tests_in_ci.js -s test:all", + "test:all": "run-p --npm-path npm test:browser test:node", + "test:browser" : "karma start", + "test:node": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha src/**/*server-app.test.ts --config ../../config/mocharc.node.js", "test:debug": "karma start --browsers=Chrome --auto-watch", "trusted-type-check": "tsec -p tsconfig.json --noEmit", "type-check": "tsc -p . --noEmit", diff --git a/packages/installations/src/api/get-id-server-app.test.ts b/packages/installations/src/api/get-id-server-app.test.ts new file mode 100644 index 00000000000..d16275b29f4 --- /dev/null +++ b/packages/installations/src/api/get-id-server-app.test.ts @@ -0,0 +1,42 @@ +/** + * @license + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect } from 'chai'; +import { getId } from './get-id'; +import { FAKE_INSTALLATIONS_ID, getFakeInstallations, getFakeServerApp } from '../testing/fake-generators'; + +describe('getId-serverapp', () => { + it('getId with firebaseServerApp with authIdToken returns valid id', async() => { + const installationsAuthToken = "fakeToken"; + const serverApp = getFakeServerApp(installationsAuthToken); + const installations = getFakeInstallations(serverApp); + const fid = await getId(installations); + expect(fid).to.equal(FAKE_INSTALLATIONS_ID); + }); + it('getId with firebaseServerApp without authIdToken throws', async() => { + const serverApp = getFakeServerApp(); + const installations = getFakeInstallations(serverApp); + let fails = false; + try { + await getId(installations); + } catch (e) { + console.error(e); + fails = true; + } + expect(fails).to.be.true; + }); +}); diff --git a/packages/installations/src/api/get-token-server-app.test.ts b/packages/installations/src/api/get-token-server-app.test.ts new file mode 100644 index 00000000000..33b3a4d3aed --- /dev/null +++ b/packages/installations/src/api/get-token-server-app.test.ts @@ -0,0 +1,44 @@ +/** + * @license + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect, use } from 'chai'; +import { getToken } from './get-token'; +import { getFakeInstallations, getFakeServerApp } from '../testing/fake-generators'; +import chaiAsPromised from 'chai-as-promised'; + +use(chaiAsPromised); + +describe('getToken-serverapp', () => { + it('getToken with firebaseServerApp with authIdToken returns valid token', async () => { + const installationsAuthToken = "fakeToken.abc123"; + const serverApp = getFakeServerApp(installationsAuthToken); + const installations = getFakeInstallations(serverApp); + const token = await getToken(installations); + expect(token).to.equal(installationsAuthToken); + }); + it('getToken with firebaseServerApp without authIdToken throws', async () => { + const serverApp = getFakeServerApp(); + const installations = getFakeInstallations(serverApp); + let fails = false; + try { + await getToken(installations); + } catch (e) { + fails = true; + } + expect(fails).to.be.true; + }); +}); diff --git a/packages/installations/src/testing/fake-generators.ts b/packages/installations/src/testing/fake-generators.ts index 6309228d72b..a29f0b12eb6 100644 --- a/packages/installations/src/testing/fake-generators.ts +++ b/packages/installations/src/testing/fake-generators.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { FirebaseApp } from '@firebase/app'; +import { FirebaseApp, FirebaseServerApp } from '@firebase/app'; import { Component, ComponentContainer, @@ -27,6 +27,8 @@ import { AppConfig } from '../interfaces/installation-impl'; +export const FAKE_INSTALLATIONS_ID = 'abc123'; + export function getFakeApp(): FirebaseApp { return { name: 'appName', @@ -43,13 +45,29 @@ export function getFakeApp(): FirebaseApp { }; } +export function getFakeServerApp( + installationsAuthToken: string | null = null +): FirebaseServerApp { + const app = getFakeApp() as any; + app.settings = { + automaticDataCollectionEnabled: true + }; + if (installationsAuthToken !== null) { + app.settings.installationsAuthToken = installationsAuthToken; + app.installationsId = FAKE_INSTALLATIONS_ID; + } + return app; +} + export function getFakeAppConfig( customValues: Partial = {} ): AppConfig { return { ...extractAppConfig(getFakeApp()), ...customValues }; } -export function getFakeInstallations(): FirebaseInstallationsImpl { +export function getFakeInstallations( + app?: FirebaseApp +): FirebaseInstallationsImpl { const container = new ComponentContainer('test'); container.addComponent( new Component( @@ -61,9 +79,9 @@ export function getFakeInstallations(): FirebaseInstallationsImpl { ComponentType.PRIVATE ) ); - + const configuredApp: FirebaseApp = app ? app : getFakeApp(); return { - app: getFakeApp(), + app: configuredApp, appConfig: getFakeAppConfig(), heartbeatServiceProvider: container.getProvider('heartbeat'), _delete: () => { diff --git a/packages/installations/src/util/errors.ts b/packages/installations/src/util/errors.ts index ac1efef2b02..a09b7f22ee8 100644 --- a/packages/installations/src/util/errors.ts +++ b/packages/installations/src/util/errors.ts @@ -25,7 +25,7 @@ export const enum ErrorCode { REQUEST_FAILED = 'request-failed', APP_OFFLINE = 'app-offline', DELETE_PENDING_REGISTRATION = 'delete-pending-registration', - SERVER_APP_MISSING_INSTALLATIONS_AUTH_TOKEN = 'server-app-missing-installatoins-auth-token' + SERVER_APP_MISSING_INSTALLATIONS_AUTH_TOKEN = 'server-app-missing-installations-auth-token' } const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {