35
35
#include " firebase/app_check/play_integrity_provider.h"
36
36
#include " firebase/auth.h"
37
37
#include " firebase/database.h"
38
+ #include " firebase/functions.h"
38
39
#include " firebase/internal/platform.h"
39
40
#include " firebase/storage.h"
40
41
#include " firebase/util.h"
@@ -115,6 +116,11 @@ class FirebaseAppCheckTest : public FirebaseTest {
115
116
// Initialize everything needed for Storage tests.
116
117
void InitializeAppAuthStorage ();
117
118
119
+ // Initialize Firebase Functions.
120
+ void InitializeFunctions ();
121
+ // Shut down Firebase Functions.
122
+ void TerminateFunctions ();
123
+
118
124
firebase::database::DatabaseReference CreateWorkingPath (
119
125
bool suppress_cleanup = false );
120
126
@@ -126,6 +132,8 @@ class FirebaseAppCheckTest : public FirebaseTest {
126
132
std::vector<firebase::database::DatabaseReference> cleanup_paths_;
127
133
128
134
firebase::storage::Storage* storage_;
135
+
136
+ firebase::functions::Functions* functions_;
129
137
};
130
138
131
139
// Listens for token changed notifications
@@ -202,6 +210,7 @@ FirebaseAppCheckTest::FirebaseAppCheckTest()
202
210
auth_(nullptr ),
203
211
database_(nullptr ),
204
212
storage_(nullptr ),
213
+ functions_(nullptr ),
205
214
cleanup_paths_() {
206
215
FindFirebaseConfig (FIREBASE_CONFIG_STRING);
207
216
}
@@ -215,6 +224,7 @@ void FirebaseAppCheckTest::TearDown() {
215
224
// Teardown all the products
216
225
TerminateDatabase ();
217
226
TerminateStorage ();
227
+ TerminateFunctions ();
218
228
TerminateAuth ();
219
229
TerminateAppCheck ();
220
230
TerminateApp ();
@@ -349,6 +359,37 @@ void FirebaseAppCheckTest::InitializeAppAuthStorage() {
349
359
InitializeStorage ();
350
360
}
351
361
362
+ void FirebaseAppCheckTest::InitializeFunctions () {
363
+ LogDebug (" Initializing Firebase Functions." );
364
+
365
+ ::firebase::ModuleInitializer initializer;
366
+ initializer.Initialize (
367
+ app_, &functions_, [](::firebase::App* app, void * target) {
368
+ LogDebug (" Attempting to initialize Firebase Functions." );
369
+ ::firebase::InitResult result;
370
+ *reinterpret_cast <firebase::functions::Functions**>(target) =
371
+ firebase::functions::Functions::GetInstance (app, &result);
372
+ return result;
373
+ });
374
+
375
+ WaitForCompletion (initializer.InitializeLastResult (), " InitializeFunctions" );
376
+
377
+ ASSERT_EQ (initializer.InitializeLastResult ().error (), 0 )
378
+ << initializer.InitializeLastResult ().error_message ();
379
+
380
+ LogDebug (" Successfully initialized Firebase Functions." );
381
+ }
382
+
383
+ void FirebaseAppCheckTest::TerminateFunctions () {
384
+ if (functions_) {
385
+ LogDebug (" Shutdown the Functions library." );
386
+ delete functions_;
387
+ functions_ = nullptr ;
388
+ }
389
+
390
+ ProcessEvents (100 );
391
+ }
392
+
352
393
void FirebaseAppCheckTest::SignIn () {
353
394
if (auth_->current_user () != nullptr ) {
354
395
// Already signed in.
@@ -745,4 +786,39 @@ TEST_F(FirebaseAppCheckTest, TestStorageReadFileUnauthenticated) {
745
786
}
746
787
#endif // !FIREBASE_PLATFORM_ANDROID
747
788
789
+ TEST_F (FirebaseAppCheckTest, TestFunctionsSuccess) {
790
+ InitializeAppCheckWithDebug ();
791
+ InitializeApp ();
792
+ InitializeFunctions ();
793
+ firebase::functions::HttpsCallableReference ref;
794
+ ref = functions_->GetHttpsCallable (" addNumbers" );
795
+ firebase::Variant data (firebase::Variant::EmptyMap ());
796
+ data.map ()[" firstNumber" ] = 5 ;
797
+ data.map ()[" secondNumber" ] = 7 ;
798
+ firebase::Future<firebase::functions::HttpsCallableResult> future;
799
+ future = ref.Call (data);
800
+ WaitForCompletion (future, " CallFunction addnumbers" ,
801
+ firebase::functions::kErrorNone );
802
+ firebase::Variant result = future.result ()->data ();
803
+ EXPECT_TRUE (result.is_map ());
804
+ if (result.is_map ()) {
805
+ EXPECT_EQ (result.map ()[" operationResult" ], 12 );
806
+ }
807
+ }
808
+
809
+ TEST_F (FirebaseAppCheckTest, TestFunctionsFailure) {
810
+ // Don't set up AppCheck
811
+ InitializeApp ();
812
+ InitializeFunctions ();
813
+ firebase::functions::HttpsCallableReference ref;
814
+ ref = functions_->GetHttpsCallable (" addNumbers" );
815
+ firebase::Variant data (firebase::Variant::EmptyMap ());
816
+ data.map ()[" firstNumber" ] = 6 ;
817
+ data.map ()[" secondNumber" ] = 8 ;
818
+ firebase::Future<firebase::functions::HttpsCallableResult> future;
819
+ future = ref.Call (data);
820
+ WaitForCompletion (future, " CallFunction addnumbers" ,
821
+ firebase::functions::kErrorUnauthenticated );
822
+ }
823
+
748
824
} // namespace firebase_testapp_automated
0 commit comments