17
17
import { expect } from 'chai' ;
18
18
import * as sinon from 'sinon' ;
19
19
import { FirebaseApp } from '@firebase/app' ;
20
+ import * as fetchModule from 'node-fetch' ;
20
21
import { FunctionsErrorCodeCore } from './public-types' ;
21
22
import {
22
23
Provider ,
@@ -32,7 +33,12 @@ import {
32
33
FirebaseAuthInternal ,
33
34
FirebaseAuthInternalName
34
35
} 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' ;
36
42
import { httpsCallable } from './service' ;
37
43
import { FUNCTIONS_TYPE } from './constants' ;
38
44
import { FunctionsError } from './error' ;
@@ -108,32 +114,7 @@ describe('Firebase Functions > Call', () => {
108
114
expect ( result . data ) . to . equal ( 76 ) ;
109
115
} ) ;
110
116
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 () => {
137
118
// // mock auth-internal service
138
119
// const authMock: FirebaseAuthInternal = {
139
120
// getToken: async () => ({ accessToken: 'token' })
@@ -158,6 +139,59 @@ describe('Firebase Functions > Call', () => {
158
139
// stub.restore();
159
140
// });
160
141
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
+
161
195
162
196
it ( 'instance id' , async ( ) => {
163
197
// Should effectively skip this test in environments where messaging doesn't work.
0 commit comments