|
22 | 22 |
|
23 | 23 | #include "app_framework.h" // NOLINT |
24 | 24 | #include "firebase/app.h" |
| 25 | +#include "firebase/app_check.h" |
| 26 | +#include "firebase/app_check/debug_provider.h" |
25 | 27 | #include "firebase/auth.h" |
26 | 28 | #include "firebase/functions.h" |
27 | 29 | #include "firebase/util.h" |
@@ -49,6 +51,10 @@ using firebase_test_framework::FirebaseTest; |
49 | 51 |
|
50 | 52 | const char kIntegrationTestRootPath[] = "integration_test_data"; |
51 | 53 |
|
| 54 | +// Your Firebase project's Debug token goes here. |
| 55 | +// You can get this from Firebase Console, in the App Check settings. |
| 56 | +const char kAppCheckDebugToken[] = "REPLACE_WITH_APP_CHECK_TOKEN"; |
| 57 | + |
52 | 58 | class FirebaseFunctionsTest : public FirebaseTest { |
53 | 59 | public: |
54 | 60 | FirebaseFunctionsTest(); |
@@ -119,8 +125,17 @@ void FirebaseFunctionsTest::TearDown() { |
119 | 125 | void FirebaseFunctionsTest::Initialize() { |
120 | 126 | if (initialized_) return; |
121 | 127 |
|
| 128 | + LogDebug("Initializing Firebase App Check with Debug Provider"); |
| 129 | + firebase::app_check::DebugAppCheckProviderFactory::GetInstance() |
| 130 | + ->SetDebugToken(kAppCheckDebugToken); |
| 131 | + firebase::app_check::AppCheck::SetAppCheckProviderFactory( |
| 132 | + firebase::app_check::DebugAppCheckProviderFactory::GetInstance()); |
| 133 | + |
122 | 134 | InitializeApp(); |
123 | 135 |
|
| 136 | + // Create the AppCheck instance so it's available for the tests. |
| 137 | + ::firebase::app_check::AppCheck::GetInstance(app_); |
| 138 | + |
124 | 139 | LogDebug("Initializing Firebase Auth and Firebase Functions."); |
125 | 140 |
|
126 | 141 | // 0th element has a reference to this object, the rest have the initializer |
@@ -175,6 +190,17 @@ void FirebaseFunctionsTest::Terminate() { |
175 | 190 | auth_ = nullptr; |
176 | 191 | } |
177 | 192 |
|
| 193 | + if (app_) { |
| 194 | + ::firebase::app_check::AppCheck* app_check = |
| 195 | + ::firebase::app_check::AppCheck::GetInstance(app_); |
| 196 | + if (app_check) { |
| 197 | + LogDebug("Shutdown App Check."); |
| 198 | + delete app_check; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + firebase::app_check::AppCheck::SetAppCheckProviderFactory(nullptr); |
| 203 | + |
178 | 204 | TerminateApp(); |
179 | 205 |
|
180 | 206 | initialized_ = false; |
@@ -380,4 +406,79 @@ TEST_F(FirebaseFunctionsTest, TestFunctionFromURL) { |
380 | 406 | EXPECT_EQ(result.map()["operationResult"], 6); |
381 | 407 | } |
382 | 408 |
|
| 409 | +TEST_F(FirebaseFunctionsTest, TestFunctionWithLimitedUseAppCheckToken) { |
| 410 | + SignIn(); |
| 411 | + |
| 412 | + // addNumbers(5, 7) = 12 |
| 413 | + firebase::Variant data(firebase::Variant::EmptyMap()); |
| 414 | + data.map()["firstNumber"] = 5; |
| 415 | + data.map()["secondNumber"] = 7; |
| 416 | + |
| 417 | + firebase::functions::HttpsCallableOptions options; |
| 418 | + options.limited_use_app_check_token = true; |
| 419 | + |
| 420 | + LogDebug("Calling addNumbers with Limited Use App Check Token"); |
| 421 | + firebase::functions::HttpsCallableReference ref = |
| 422 | + functions_->GetHttpsCallable("addNumbers", options); |
| 423 | + |
| 424 | + firebase::Variant result = |
| 425 | + TestFunctionHelper("addNumbers", ref, &data, firebase::Variant::Null()) |
| 426 | + .result() |
| 427 | + ->data(); |
| 428 | + EXPECT_TRUE(result.is_map()); |
| 429 | + EXPECT_EQ(result.map()["operationResult"], 12); |
| 430 | +} |
| 431 | + |
| 432 | +TEST_F(FirebaseFunctionsTest, TestFunctionFromURLWithLimitedUseAppCheckToken) { |
| 433 | + SignIn(); |
| 434 | + |
| 435 | + // addNumbers(4, 2) = 6 |
| 436 | + firebase::Variant data(firebase::Variant::EmptyMap()); |
| 437 | + data.map()["firstNumber"] = 4; |
| 438 | + data.map()["secondNumber"] = 2; |
| 439 | + |
| 440 | + std::string proj = app_->options().project_id(); |
| 441 | + // V2 functions can still be addressed via the V1 URL schema which handles |
| 442 | + // internal routing |
| 443 | + std::string url = |
| 444 | + "https://us-central1-" + proj + ".cloudfunctions.net/addNumbers"; |
| 445 | + |
| 446 | + firebase::functions::HttpsCallableOptions options; |
| 447 | + options.limited_use_app_check_token = true; |
| 448 | + |
| 449 | + LogDebug("Calling by URL %s with Limited Use App Check Token", url.c_str()); |
| 450 | + firebase::functions::HttpsCallableReference ref = |
| 451 | + functions_->GetHttpsCallableFromURL(url.c_str(), options); |
| 452 | + |
| 453 | + firebase::Variant result = |
| 454 | + TestFunctionHelper(url.c_str(), ref, &data, firebase::Variant::Null()) |
| 455 | + .result() |
| 456 | + ->data(); |
| 457 | + EXPECT_TRUE(result.is_map()); |
| 458 | + EXPECT_EQ(result.map()["operationResult"], 6); |
| 459 | +} |
| 460 | + |
| 461 | +TEST_F(FirebaseFunctionsTest, TestV1FunctionWithLimitedUseAppCheckToken) { |
| 462 | + SignIn(); |
| 463 | + |
| 464 | + // addNumbers(5, 7) = 12 |
| 465 | + firebase::Variant data(firebase::Variant::EmptyMap()); |
| 466 | + data.map()["firstNumber"] = 5; |
| 467 | + data.map()["secondNumber"] = 7; |
| 468 | + |
| 469 | + firebase::functions::HttpsCallableOptions options; |
| 470 | + options.limited_use_app_check_token = true; |
| 471 | + |
| 472 | + LogDebug("Calling addNumbers with Limited Use App Check Token"); |
| 473 | + firebase::functions::HttpsCallableReference ref = |
| 474 | + functions_->GetHttpsCallable("addNumbers", options); |
| 475 | + |
| 476 | + firebase::Variant result = |
| 477 | + TestFunctionHelper("addNumbers", ref, &data, firebase::Variant::Null()) |
| 478 | + .result() |
| 479 | + ->data(); |
| 480 | + EXPECT_TRUE(result.is_map()); |
| 481 | + EXPECT_EQ(result.map()["operationResult"], 12); |
| 482 | +} |
| 483 | + |
383 | 484 | } // namespace firebase_testapp_automated |
0 commit comments