Skip to content

Commit

Permalink
Added server / node tests to Installations.
Browse files Browse the repository at this point in the history
  • Loading branch information
DellaBitta committed Nov 4, 2024
1 parent acda705 commit 10348f8
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/installations/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
8 changes: 5 additions & 3 deletions packages/installations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
42 changes: 42 additions & 0 deletions packages/installations/src/api/get-id-server-app.test.ts
Original file line number Diff line number Diff line change
@@ -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;
});
});
44 changes: 44 additions & 0 deletions packages/installations/src/api/get-token-server-app.test.ts
Original file line number Diff line number Diff line change
@@ -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;
});
});
26 changes: 22 additions & 4 deletions packages/installations/src/testing/fake-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { FirebaseApp } from '@firebase/app';
import { FirebaseApp, FirebaseServerApp } from '@firebase/app';
import {
Component,
ComponentContainer,
Expand All @@ -27,6 +27,8 @@ import {
AppConfig
} from '../interfaces/installation-impl';

export const FAKE_INSTALLATIONS_ID = 'abc123';

export function getFakeApp(): FirebaseApp {
return {
name: 'appName',
Expand All @@ -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> = {}
): AppConfig {
return { ...extractAppConfig(getFakeApp()), ...customValues };
}

export function getFakeInstallations(): FirebaseInstallationsImpl {
export function getFakeInstallations(
app?: FirebaseApp
): FirebaseInstallationsImpl {
const container = new ComponentContainer('test');
container.addComponent(
new Component(
Expand All @@ -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: () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/installations/src/util/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {
Expand Down

0 comments on commit 10348f8

Please sign in to comment.