Skip to content

Commit 10348f8

Browse files
committed
Added server / node tests to Installations.
1 parent acda705 commit 10348f8

File tree

6 files changed

+115
-8
lines changed

6 files changed

+115
-8
lines changed

packages/installations/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = function (config) {
2424
...karmaBase,
2525
// files to load into karma
2626
files,
27+
exclude: ['src/**/*-server-app.test.ts'],
2728
// frameworks to use
2829
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
2930
frameworks: ['mocha']

packages/installations/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
"build:deps": "lerna run --scope @firebase/installations --include-dependencies build",
2626
"build:release": "yarn build && yarn typings:public",
2727
"dev": "rollup -c -w",
28-
"test": "yarn type-check && yarn test:karma && yarn lint",
29-
"test:ci": "node ../../scripts/run_tests_in_ci.js",
30-
"test:karma": "karma start",
28+
"test": "yarn type-check && yarn test:all && yarn lint",
29+
"test:ci": "node ../../scripts/run_tests_in_ci.js -s test:all",
30+
"test:all": "run-p --npm-path npm test:browser test:node",
31+
"test:browser" : "karma start",
32+
"test:node": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha src/**/*server-app.test.ts --config ../../config/mocharc.node.js",
3133
"test:debug": "karma start --browsers=Chrome --auto-watch",
3234
"trusted-type-check": "tsec -p tsconfig.json --noEmit",
3335
"type-check": "tsc -p . --noEmit",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license
3+
* Copyright 2024 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 } from 'chai';
19+
import { getId } from './get-id';
20+
import { FAKE_INSTALLATIONS_ID, getFakeInstallations, getFakeServerApp } from '../testing/fake-generators';
21+
22+
describe('getId-serverapp', () => {
23+
it('getId with firebaseServerApp with authIdToken returns valid id', async() => {
24+
const installationsAuthToken = "fakeToken";
25+
const serverApp = getFakeServerApp(installationsAuthToken);
26+
const installations = getFakeInstallations(serverApp);
27+
const fid = await getId(installations);
28+
expect(fid).to.equal(FAKE_INSTALLATIONS_ID);
29+
});
30+
it('getId with firebaseServerApp without authIdToken throws', async() => {
31+
const serverApp = getFakeServerApp();
32+
const installations = getFakeInstallations(serverApp);
33+
let fails = false;
34+
try {
35+
await getId(installations);
36+
} catch (e) {
37+
console.error(e);
38+
fails = true;
39+
}
40+
expect(fails).to.be.true;
41+
});
42+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license
3+
* Copyright 2024 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 { getToken } from './get-token';
20+
import { getFakeInstallations, getFakeServerApp } from '../testing/fake-generators';
21+
import chaiAsPromised from 'chai-as-promised';
22+
23+
use(chaiAsPromised);
24+
25+
describe('getToken-serverapp', () => {
26+
it('getToken with firebaseServerApp with authIdToken returns valid token', async () => {
27+
const installationsAuthToken = "fakeToken.abc123";
28+
const serverApp = getFakeServerApp(installationsAuthToken);
29+
const installations = getFakeInstallations(serverApp);
30+
const token = await getToken(installations);
31+
expect(token).to.equal(installationsAuthToken);
32+
});
33+
it('getToken with firebaseServerApp without authIdToken throws', async () => {
34+
const serverApp = getFakeServerApp();
35+
const installations = getFakeInstallations(serverApp);
36+
let fails = false;
37+
try {
38+
await getToken(installations);
39+
} catch (e) {
40+
fails = true;
41+
}
42+
expect(fails).to.be.true;
43+
});
44+
});

packages/installations/src/testing/fake-generators.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseApp } from '@firebase/app';
18+
import { FirebaseApp, FirebaseServerApp } from '@firebase/app';
1919
import {
2020
Component,
2121
ComponentContainer,
@@ -27,6 +27,8 @@ import {
2727
AppConfig
2828
} from '../interfaces/installation-impl';
2929

30+
export const FAKE_INSTALLATIONS_ID = 'abc123';
31+
3032
export function getFakeApp(): FirebaseApp {
3133
return {
3234
name: 'appName',
@@ -43,13 +45,29 @@ export function getFakeApp(): FirebaseApp {
4345
};
4446
}
4547

48+
export function getFakeServerApp(
49+
installationsAuthToken: string | null = null
50+
): FirebaseServerApp {
51+
const app = getFakeApp() as any;
52+
app.settings = {
53+
automaticDataCollectionEnabled: true
54+
};
55+
if (installationsAuthToken !== null) {
56+
app.settings.installationsAuthToken = installationsAuthToken;
57+
app.installationsId = FAKE_INSTALLATIONS_ID;
58+
}
59+
return app;
60+
}
61+
4662
export function getFakeAppConfig(
4763
customValues: Partial<AppConfig> = {}
4864
): AppConfig {
4965
return { ...extractAppConfig(getFakeApp()), ...customValues };
5066
}
5167

52-
export function getFakeInstallations(): FirebaseInstallationsImpl {
68+
export function getFakeInstallations(
69+
app?: FirebaseApp
70+
): FirebaseInstallationsImpl {
5371
const container = new ComponentContainer('test');
5472
container.addComponent(
5573
new Component(
@@ -61,9 +79,9 @@ export function getFakeInstallations(): FirebaseInstallationsImpl {
6179
ComponentType.PRIVATE
6280
)
6381
);
64-
82+
const configuredApp: FirebaseApp = app ? app : getFakeApp();
6583
return {
66-
app: getFakeApp(),
84+
app: configuredApp,
6785
appConfig: getFakeAppConfig(),
6886
heartbeatServiceProvider: container.getProvider('heartbeat'),
6987
_delete: () => {

packages/installations/src/util/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const enum ErrorCode {
2525
REQUEST_FAILED = 'request-failed',
2626
APP_OFFLINE = 'app-offline',
2727
DELETE_PENDING_REGISTRATION = 'delete-pending-registration',
28-
SERVER_APP_MISSING_INSTALLATIONS_AUTH_TOKEN = 'server-app-missing-installatoins-auth-token'
28+
SERVER_APP_MISSING_INSTALLATIONS_AUTH_TOKEN = 'server-app-missing-installations-auth-token'
2929
}
3030

3131
const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {

0 commit comments

Comments
 (0)