Skip to content

Commit 6c54002

Browse files
ssbushiavolkovi
authored andcommitted
add test, works as a unit test, broken karma
1 parent 8faaddc commit 6c54002

File tree

3 files changed

+107
-27
lines changed

3 files changed

+107
-27
lines changed

packages/functions/src/callable.test.ts

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { expect } from 'chai';
1818
import * as sinon from 'sinon';
1919
import { FirebaseApp } from '@firebase/app';
20+
import * as fetchModule from 'node-fetch';
2021
import { FunctionsErrorCodeCore } from './public-types';
2122
import {
2223
Provider,
@@ -32,7 +33,12 @@ import {
3233
FirebaseAuthInternal,
3334
FirebaseAuthInternalName
3435
} from '@firebase/auth-interop-types';
35-
import { makeFakeApp, createTestService } from '../test/utils';
36+
37+
import {
38+
FirebaseAppCheckInternal,
39+
AppCheckInternalComponentName,
40+
} from '@firebase/app-check-interop-types';
41+
import { makeFakeApp, createTestService, createTestServiceWithFetchMock} from '../test/utils';
3642
import { httpsCallable } from './service';
3743
import { FUNCTIONS_TYPE } from './constants';
3844
import { FunctionsError } from './error';
@@ -108,32 +114,7 @@ describe('Firebase Functions > Call', () => {
108114
expect(result.data).to.equal(76);
109115
});
110116

111-
it('token', async () => {
112-
// mock auth-internal service
113-
const authMock: FirebaseAuthInternal = {
114-
getToken: async () => ({ accessToken: 'token' })
115-
} as unknown as FirebaseAuthInternal;
116-
const authProvider = new Provider<FirebaseAuthInternalName>(
117-
'auth-internal',
118-
new ComponentContainer('test')
119-
);
120-
authProvider.setComponent(
121-
new Component('auth-internal', () => authMock, ComponentType.PRIVATE)
122-
);
123-
124-
const functions = createTestService(app, region, authProvider);
125-
126-
// Stub out the internals to get an auth token.
127-
const stub = sinon.stub(authMock, 'getToken').callThrough();
128-
const func = httpsCallable(functions, 'tokenTest');
129-
const result = await func({});
130-
expect(result.data).to.deep.equal({});
131-
132-
expect(stub.callCount).to.equal(1);
133-
stub.restore();
134-
});
135-
136-
// it('appcheck token', async () => {
117+
// it('token', async () => {
137118
// // mock auth-internal service
138119
// const authMock: FirebaseAuthInternal = {
139120
// getToken: async () => ({ accessToken: 'token' })
@@ -158,6 +139,59 @@ describe('Firebase Functions > Call', () => {
158139
// stub.restore();
159140
// });
160141

142+
// eslint-disable-next-line no-restricted-properties -- Here's a description
143+
it('appcheck token', async () => {
144+
// mock auth-internal service
145+
const authMock: FirebaseAuthInternal = {
146+
getToken: async () => ({ accessToken: 'token' })
147+
} as unknown as FirebaseAuthInternal;
148+
const authProvider = new Provider<FirebaseAuthInternalName>(
149+
'auth-internal',
150+
new ComponentContainer('test')
151+
);
152+
authProvider.setComponent(
153+
new Component('auth-internal', () => authMock, ComponentType.PRIVATE)
154+
);
155+
// mock app-check-internal service
156+
const appCheckMock: FirebaseAppCheckInternal = {
157+
getToken: async () => ({ accessToken: 'token' }),
158+
getLimitedUseToken: async () => ({ accessToken: 'limited-token' })
159+
} as unknown as FirebaseAppCheckInternal;
160+
const appCheckProvider = new Provider<AppCheckInternalComponentName>(
161+
'app-check-internal',
162+
new ComponentContainer('test')
163+
);
164+
appCheckProvider.setComponent(
165+
new Component('app-check-internal', () => appCheckMock, ComponentType.PRIVATE)
166+
);
167+
const messagingProvider = new Provider<MessagingInternalComponentName>(
168+
'messaging-internal',
169+
new ComponentContainer('test')
170+
);
171+
const fetchStub = sinon
172+
.stub(fetchModule, 'default').callThrough();
173+
// .returns(Promise.resolve(new fetchModule.Response(JSON.stringify({data: "hi"}), { status: 200 })));
174+
175+
const functions = createTestServiceWithFetchMock(app, fetchStub, region, authProvider, messagingProvider, appCheckProvider);
176+
177+
// Stub out the internals to get an auth token.
178+
const authStub = sinon.stub(authMock, 'getToken').callThrough();
179+
const appCheckStub = sinon.stub(appCheckMock, 'getToken').callThrough();
180+
const appCheckLimitedUseStub = sinon.stub(appCheckMock, 'getLimitedUseToken').callThrough();
181+
const func = httpsCallable(functions, 'tokenTest');
182+
const result = await func({});
183+
expect(result.data).to.deep.equal({});
184+
185+
expect(authStub.callCount).to.equal(1);
186+
expect(appCheckStub.callCount).to.equal(1);
187+
expect(appCheckLimitedUseStub.callCount).to.equal(1);
188+
expect(fetchStub.called).to.be.true;
189+
authStub.restore();
190+
appCheckStub.restore();
191+
appCheckLimitedUseStub.restore();
192+
fetchStub.restore();
193+
});
194+
161195

162196
it('instance id', async () => {
163197
// Should effectively skip this test in environments where messaging doesn't work.

packages/functions/src/service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ async function postJSON(
224224
headers
225225
});
226226
} catch (e) {
227+
console.log("error!");
227228
// This could be an unhandled error on the backend, or it could be a
228229
// network error. There's no way to know, since an unhandled error on the
229230
// backend will fail to set the proper CORS header, and thus will be
@@ -233,6 +234,8 @@ async function postJSON(
233234
json: null
234235
};
235236
}
237+
console.log("!!!!");
238+
console.log(response);
236239
let json: HttpResponseBody | null = null;
237240
try {
238241
json = await response.json();
@@ -315,6 +318,8 @@ async function callAtURL(
315318
);
316319
}
317320

321+
console.log(response);
322+
318323
// Check for an error status, regardless of http status.
319324
const error = _errorForResponse(response.status, response.json);
320325
if (error) {

packages/functions/test/utils.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,44 @@ export function createTestService(
7979
}
8080
return functions;
8181
}
82+
83+
84+
export function createTestServiceWithFetchMock(
85+
app: FirebaseApp,
86+
fetchMock: any,
87+
region?: string,
88+
authProvider = new Provider<FirebaseAuthInternalName>(
89+
'auth-internal',
90+
new ComponentContainer('test')
91+
),
92+
messagingProvider = new Provider<MessagingInternalComponentName>(
93+
'messaging-internal',
94+
new ComponentContainer('test')
95+
),
96+
appCheckProvider = new Provider<AppCheckInternalComponentName>(
97+
'app-check-internal',
98+
new ComponentContainer('test')
99+
),
100+
): FunctionsService {
101+
console.log(typeof window);
102+
console.log(fetchMock);
103+
104+
const functions = new FunctionsService(
105+
app,
106+
authProvider,
107+
messagingProvider,
108+
appCheckProvider,
109+
region,
110+
fetchMock,
111+
);
112+
const useEmulator = !!process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN;
113+
if (useEmulator) {
114+
const url = new URL(process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN!);
115+
connectFunctionsEmulator(
116+
functions,
117+
url.hostname,
118+
Number.parseInt(url.port, 10)
119+
);
120+
}
121+
return functions;
122+
}

0 commit comments

Comments
 (0)