3535#include " firebase/app_check/play_integrity_provider.h"
3636#include " firebase/auth.h"
3737#include " firebase/database.h"
38+ #include " firebase/functions.h"
3839#include " firebase/internal/platform.h"
3940#include " firebase/storage.h"
4041#include " firebase/util.h"
@@ -115,6 +116,11 @@ class FirebaseAppCheckTest : public FirebaseTest {
115116 // Initialize everything needed for Storage tests.
116117 void InitializeAppAuthStorage ();
117118
119+ // Initialize Firebase Functions.
120+ void InitializeFunctions ();
121+ // Shut down Firebase Functions.
122+ void TerminateFunctions ();
123+
118124 firebase::database::DatabaseReference CreateWorkingPath (
119125 bool suppress_cleanup = false );
120126
@@ -126,6 +132,8 @@ class FirebaseAppCheckTest : public FirebaseTest {
126132 std::vector<firebase::database::DatabaseReference> cleanup_paths_;
127133
128134 firebase::storage::Storage* storage_;
135+
136+ firebase::functions::Functions* functions_;
129137};
130138
131139// Listens for token changed notifications
@@ -202,6 +210,7 @@ FirebaseAppCheckTest::FirebaseAppCheckTest()
202210 auth_(nullptr ),
203211 database_(nullptr ),
204212 storage_(nullptr ),
213+ functions_(nullptr ),
205214 cleanup_paths_() {
206215 FindFirebaseConfig (FIREBASE_CONFIG_STRING);
207216}
@@ -215,6 +224,7 @@ void FirebaseAppCheckTest::TearDown() {
215224 // Teardown all the products
216225 TerminateDatabase ();
217226 TerminateStorage ();
227+ TerminateFunctions ();
218228 TerminateAuth ();
219229 TerminateAppCheck ();
220230 TerminateApp ();
@@ -349,6 +359,37 @@ void FirebaseAppCheckTest::InitializeAppAuthStorage() {
349359 InitializeStorage ();
350360}
351361
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+
352393void FirebaseAppCheckTest::SignIn () {
353394 if (auth_->current_user () != nullptr ) {
354395 // Already signed in.
@@ -745,4 +786,39 @@ TEST_F(FirebaseAppCheckTest, TestStorageReadFileUnauthenticated) {
745786}
746787#endif // !FIREBASE_PLATFORM_ANDROID
747788
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+
748824} // namespace firebase_testapp_automated
0 commit comments