From 4dc63f8d7cbf60417b88c6a77839ea32656627b9 Mon Sep 17 00:00:00 2001 From: Gil Date: Thu, 22 Feb 2018 08:26:22 -0800 Subject: Fix Firestore tests for M22 (#834) * Add FIRFirestoreTests to the Firestore Xcode project * Avoid waitForExpectations:timeout: This API was added in Xcode 8.3, but we still build production releases with Xcode 8.2. waitForExpectationsWithTimeout:handler: is available from Xcode 7.2. * Add AppForUnitTesting Add a utility for constructing a Firebase App for testing. * Handle the nil UID from FIRAuth * Avoid running CMake tests twice * Only build app_testing on Apple platforms * Revise test.sh messages --- Firestore/core/CMakeLists.txt | 1 + .../auth/firebase_credentials_provider_apple.h | 5 +- .../auth/firebase_credentials_provider_apple.mm | 3 +- Firestore/core/src/firebase/firestore/auth/user.h | 20 ++++-- .../test/firebase/firestore/auth/CMakeLists.txt | 1 + .../auth/firebase_credentials_provider_test.mm | 71 +++++++++------------- .../firebase/firestore/testutil/CMakeLists.txt | 35 +++++++++++ .../test/firebase/firestore/testutil/app_testing.h | 43 +++++++++++++ .../firebase/firestore/testutil/app_testing.mm | 48 +++++++++++++++ 9 files changed, 174 insertions(+), 53 deletions(-) create mode 100644 Firestore/core/test/firebase/firestore/testutil/CMakeLists.txt create mode 100644 Firestore/core/test/firebase/firestore/testutil/app_testing.h create mode 100644 Firestore/core/test/firebase/firestore/testutil/app_testing.mm (limited to 'Firestore/core') diff --git a/Firestore/core/CMakeLists.txt b/Firestore/core/CMakeLists.txt index e70647d..5bf8c5e 100644 --- a/Firestore/core/CMakeLists.txt +++ b/Firestore/core/CMakeLists.txt @@ -20,6 +20,7 @@ add_subdirectory(src/firebase/firestore/model) add_subdirectory(src/firebase/firestore/remote) add_subdirectory(src/firebase/firestore/util) +add_subdirectory(test/firebase/firestore/testutil) add_subdirectory(test/firebase/firestore) add_subdirectory(test/firebase/firestore/auth) add_subdirectory(test/firebase/firestore/core) diff --git a/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.h b/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.h index 65c4c65..66c3c87 100644 --- a/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.h +++ b/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.h @@ -26,6 +26,7 @@ #include #include // NOLINT(build/c++11) +#include #include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h" #include "Firestore/core/src/firebase/firestore/auth/user.h" @@ -76,8 +77,8 @@ class FirebaseCredentialsProvider : public CredentialsProvider { * avoid races between notifications arriving and C++ object destruction. */ struct Contents { - Contents(FIRApp* app, const absl::string_view uid) - : app(app), current_user(uid), mutex() { + Contents(FIRApp* app, User&& user) + : app(app), current_user(std::move(user)), mutex() { } const FIRApp* app; diff --git a/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.mm b/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.mm index f463958..fe3cb24 100644 --- a/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.mm +++ b/Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.mm @@ -28,8 +28,7 @@ namespace firestore { namespace auth { FirebaseCredentialsProvider::FirebaseCredentialsProvider(FIRApp* app) - : contents_( - std::make_shared(app, util::MakeStringView([app getUID]))) { + : contents_(std::make_shared(app, User::FromUid([app getUID]))) { std::weak_ptr weak_contents = contents_; auth_listener_handle_ = [[NSNotificationCenter defaultCenter] diff --git a/Firestore/core/src/firebase/firestore/auth/user.h b/Firestore/core/src/firebase/firestore/auth/user.h index cc7b66d..3918c61 100644 --- a/Firestore/core/src/firebase/firestore/auth/user.h +++ b/Firestore/core/src/firebase/firestore/auth/user.h @@ -43,12 +43,6 @@ class User { /** Construct an authenticated user with the given UID. */ explicit User(const absl::string_view uid); -#if defined(__OBJC__) - explicit User(NSString* uid) - : User(firebase::firestore::util::MakeStringView(uid)) { - } -#endif // defined(__OBJC__) - const std::string& uid() const { return uid_; } @@ -62,6 +56,20 @@ class User { /** Returns an unauthenticated instance. */ static const User& Unauthenticated(); +#if defined(__OBJC__) + /** + * Returns an authenticated user if uid is non-nil, otherwise an + * unauthenticated user. + */ + static User FromUid(NSString* _Nullable uid) { + if (uid == nil) { + return Unauthenticated(); + } else { + return User(util::MakeStringView(uid)); + } + } +#endif // defined(__OBJC__) + User& operator=(const User& other) = default; friend bool operator==(const User& lhs, const User& rhs); diff --git a/Firestore/core/test/firebase/firestore/auth/CMakeLists.txt b/Firestore/core/test/firebase/firestore/auth/CMakeLists.txt index f470bd7..a6aa2ef 100644 --- a/Firestore/core/test/firebase/firestore/auth/CMakeLists.txt +++ b/Firestore/core/test/firebase/firestore/auth/CMakeLists.txt @@ -30,5 +30,6 @@ if(APPLE) firebase_credentials_provider_test.mm DEPENDS firebase_firestore_auth_apple + firebase_firestore_testutil ) endif(APPLE) diff --git a/Firestore/core/test/firebase/firestore/auth/firebase_credentials_provider_test.mm b/Firestore/core/test/firebase/firestore/auth/firebase_credentials_provider_test.mm index e98d3d8..a6ccc4a 100644 --- a/Firestore/core/test/firebase/firestore/auth/firebase_credentials_provider_test.mm +++ b/Firestore/core/test/firebase/firestore/auth/firebase_credentials_provider_test.mm @@ -21,6 +21,7 @@ #import #include "Firestore/core/src/firebase/firestore/util/string_apple.h" +#include "Firestore/core/test/firebase/firestore/testutil/app_testing.h" #include "gtest/gtest.h" @@ -28,67 +29,51 @@ namespace firebase { namespace firestore { namespace auth { -// TODO(zxu123): Make this an integration test and get infos from environment. -// Set a .plist file here to enable the test-case. -static NSString* const kPlist = @""; - -class FirebaseCredentialsProviderTest : public ::testing::Test { - protected: - void SetUp() override { - app_ready_ = false; - if (![kPlist hasSuffix:@".plist"]) { - return; - } - - static dispatch_once_t once_token; - dispatch_once(&once_token, ^{ - FIROptions* options = [[FIROptions alloc] initWithContentsOfFile:kPlist]; - [FIRApp configureWithOptions:options]; - }); +FIRApp* AppWithFakeUid(NSString* _Nullable uid) { + FIRApp* app = testutil::AppForUnitTesting(); + app.getUIDImplementation = ^NSString* { + return uid; + }; + return app; +} - // Set getUID implementation. - FIRApp* default_app = [FIRApp defaultApp]; - default_app.getUIDImplementation = ^NSString* { - return @"I'm a fake uid."; - }; - app_ready_ = true; - } +TEST(FirebaseCredentialsProviderTest, GetTokenUnauthenticated) { + FIRApp* app = AppWithFakeUid(nil); - bool app_ready_; -}; + FirebaseCredentialsProvider credentials_provider(app); + credentials_provider.GetToken( + /*force_refresh=*/true, [](Token token, const absl::string_view error) { + EXPECT_EQ("", token.token()); + const User& user = token.user(); + EXPECT_EQ("", user.uid()); + EXPECT_FALSE(user.is_authenticated()); + EXPECT_EQ("", error) << error; + }); +} -// Set kPlist above before enable. -TEST_F(FirebaseCredentialsProviderTest, GetToken) { - if (!app_ready_) { - return; - } +TEST(FirebaseCredentialsProviderTest, GetToken) { + FIRApp* app = AppWithFakeUid(@"fake uid"); - FirebaseCredentialsProvider credentials_provider([FIRApp defaultApp]); + FirebaseCredentialsProvider credentials_provider(app); credentials_provider.GetToken( /*force_refresh=*/true, [](Token token, const absl::string_view error) { EXPECT_EQ("", token.token()); const User& user = token.user(); - EXPECT_EQ("I'm a fake uid.", user.uid()); + EXPECT_EQ("fake uid", user.uid()); EXPECT_TRUE(user.is_authenticated()); EXPECT_EQ("", error) << error; }); } -// Set kPlist above before enable. -TEST_F(FirebaseCredentialsProviderTest, SetListener) { - if (!app_ready_) { - return; - } +TEST(FirebaseCredentialsProviderTest, SetListener) { + FIRApp* app = AppWithFakeUid(@"fake uid"); - FirebaseCredentialsProvider credentials_provider([FIRApp defaultApp]); + FirebaseCredentialsProvider credentials_provider(app); credentials_provider.SetUserChangeListener([](User user) { - EXPECT_EQ("I'm a fake uid.", user.uid()); + EXPECT_EQ("fake uid", user.uid()); EXPECT_TRUE(user.is_authenticated()); }); - // TODO(wilhuff): We should wait for the above expectations to actually happen - // before continuing. - credentials_provider.SetUserChangeListener(nullptr); } diff --git a/Firestore/core/test/firebase/firestore/testutil/CMakeLists.txt b/Firestore/core/test/firebase/firestore/testutil/CMakeLists.txt new file mode 100644 index 0000000..b9fcd47 --- /dev/null +++ b/Firestore/core/test/firebase/firestore/testutil/CMakeLists.txt @@ -0,0 +1,35 @@ +# Copyright 2018 Google +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cc_library( + firebase_firestore_testutil_apple + SOURCES + app_testing.h + app_testing.mm + DEPENDS + FirebaseCore + absl_strings +) + +if(APPLE) + list(APPEND TESTUTIL_DEPENDS firebase_firestore_testutil_apple) +endif() + +add_library( + firebase_firestore_testutil INTERFACE +) +target_link_libraries( + firebase_firestore_testutil INTERFACE + ${TESTUTIL_DEPENDS} +) diff --git a/Firestore/core/test/firebase/firestore/testutil/app_testing.h b/Firestore/core/test/firebase/firestore/testutil/app_testing.h new file mode 100644 index 0000000..d18ba4f --- /dev/null +++ b/Firestore/core/test/firebase/firestore/testutil/app_testing.h @@ -0,0 +1,43 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FIRESTORE_CORE_TEST_FIREBASE_FIRESTORE_TESTUTIL_APP_TESTING_H_ +#define FIRESTORE_CORE_TEST_FIREBASE_FIRESTORE_TESTUTIL_APP_TESTING_H_ + +#include "absl/strings/string_view.h" + +#if __OBJC__ + +@class FIRApp; + +namespace firebase { +namespace firestore { +namespace testutil { + +/** Creates a set of default Firebase Options for testing. */ +FIROptions* OptionsForUnitTesting( + const absl::string_view project_id = "project_id"); + +/** Creates a new Firebase App for testing. */ +FIRApp* AppForUnitTesting(const absl::string_view project_id = "project_id"); + +} // namespace testutil +} // namespace firestore +} // namespace firebase + +#endif // __OBJC__ + +#endif // FIRESTORE_CORE_TEST_FIREBASE_FIRESTORE_TESTUTIL_APP_TESTING_H_ diff --git a/Firestore/core/test/firebase/firestore/testutil/app_testing.mm b/Firestore/core/test/firebase/firestore/testutil/app_testing.mm new file mode 100644 index 0000000..21db0db --- /dev/null +++ b/Firestore/core/test/firebase/firestore/testutil/app_testing.mm @@ -0,0 +1,48 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import +#import + +#include "Firestore/core/src/firebase/firestore/util/string_apple.h" +#include "Firestore/core/test/firebase/firestore/testutil/app_testing.h" + +namespace firebase { +namespace firestore { +namespace testutil { + +FIROptions* OptionsForUnitTesting(const absl::string_view project_id) { + FIROptions* options = + [[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123ab" + GCMSenderID:@"gcm_sender_id"]; + options.projectID = util::WrapNSString(project_id); + return options; +} + +FIRApp* AppForUnitTesting(const absl::string_view project_id) { + static int counter = 0; + + NSString* appName = + [NSString stringWithFormat:@"app_for_unit_testing_%d", counter++]; + FIROptions* options = OptionsForUnitTesting(project_id); + [FIRApp configureWithName:appName options:options]; + + return [FIRApp appNamed:appName]; +} + +} // namespace testutil +} // namespace firestore +} // namespace firebase -- cgit v1.2.3