diff options
author | 2018-04-03 17:19:51 -0700 | |
---|---|---|
committer | 2018-04-03 17:19:51 -0700 | |
commit | 6566328dc461c38c6000c64996c17883b9895d06 (patch) | |
tree | 1f072d106b95e87fbd9d163194d08c3a76977e07 /Example/Auth/Tests | |
parent | 1be9737fb28deb388cbcfaf6324ebad49683340b (diff) |
Adds copy auth state API (#1018)
* Adds copy auth state API
* improvements
* Addresses comments
Diffstat (limited to 'Example/Auth/Tests')
-rw-r--r-- | Example/Auth/Tests/FIRAuthTests.m | 120 |
1 files changed, 117 insertions, 3 deletions
diff --git a/Example/Auth/Tests/FIRAuthTests.m b/Example/Auth/Tests/FIRAuthTests.m index 914c58b..2e2d2c5 100644 --- a/Example/Auth/Tests/FIRAuthTests.m +++ b/Example/Auth/Tests/FIRAuthTests.m @@ -1773,6 +1773,111 @@ static const NSTimeInterval kWaitInterval = .5; return actionCodeSettings; } +/** @fn testUpdateCurrentUserFailure + @brief Tests the flow of a failed @c updateCurrentUser:completion: + call. + */ +- (void)testUpdateCurrentUserFailure { + NSString *kTestAccessToken = @"fakeAccessToken"; + NSString *kTestAPIKey = @"fakeAPIKey"; + [self waitForSignInWithAccessToken:kTestAccessToken + APIKey:kTestAPIKey + completion:nil]; + NSString *kTestAPIKey2 = @"fakeAPIKey2"; + FIRUser *user2 = [FIRAuth auth].currentUser; + user2.requestConfiguration = [[FIRAuthRequestConfiguration alloc]initWithAPIKey:kTestAPIKey2]; + OCMExpect([_mockBackend getAccountInfo:[OCMArg any] callback:[OCMArg any]]) + .andDispatchError2([FIRAuthErrorUtils invalidAPIKeyError]); + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + [[FIRAuth auth] updateCurrentUser:user2 completion:^(NSError *_Nullable error) { + XCTAssertEqual(error.code, FIRAuthErrorCodeInvalidAPIKey); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + OCMVerifyAll(_mockBackend); +} + +/** @fn testUpdateCurrentUserFailureNetworkError + @brief Tests the flow of a failed @c updateCurrentUser:completion: + call with a network error. + */ +- (void)testUpdateCurrentUserFailureNetworkError { + NSString *kTestAPIKey = @"fakeAPIKey"; + NSString *kTestAccessToken = @"fakeAccessToken"; + [self waitForSignInWithAccessToken:kTestAccessToken + APIKey:kTestAPIKey + completion:nil]; + NSString *kTestAPIKey2 = @"fakeAPIKey2"; + FIRUser *user2 = [FIRAuth auth].currentUser; + user2.requestConfiguration = [[FIRAuthRequestConfiguration alloc]initWithAPIKey:kTestAPIKey2]; + OCMExpect([_mockBackend getAccountInfo:[OCMArg any] callback:[OCMArg any]]) + .andDispatchError2([FIRAuthErrorUtils networkErrorWithUnderlyingError:[NSError new]]); + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + [[FIRAuth auth] updateCurrentUser:user2 completion:^(NSError *_Nullable error) { + XCTAssertEqual(error.code, FIRAuthErrorCodeNetworkError); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + OCMVerifyAll(_mockBackend); +} + +/** @fn testUpdateCurrentUserFailureNUllUser + @brief Tests the flow of a failed @c updateCurrentUser:completion: + call with FIRAuthErrorCodeNullUser. + */ +- (void)testUpdateCurrentUserFailureNUllUser { + NSString *kTestAccessToken = @"fakeAccessToken"; + NSString *kTestAPIKey = @"fakeAPIKey"; + [self waitForSignInWithAccessToken:kTestAccessToken + APIKey:kTestAPIKey + completion:nil]; + FIRUser *fakeNilUser = nil; + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + [[FIRAuth auth] updateCurrentUser:fakeNilUser completion:^(NSError *_Nullable error) { + XCTAssertEqual(error.code, FIRAuthErrorCodeNullUser); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + OCMVerifyAll(_mockBackend); +} + +/** @fn testUpdateCurrentUserSuccess + @brief Tests the flow of a successful @c updateCurrentUser:completion: + call with a network error. + */ +- (void)testUpdateCurrentUserSuccess { + // Sign in with the first user. + [self waitForSignInWithAccessToken:kAccessToken + APIKey:kAPIKey + completion:nil]; + + FIRUser *user1 = [FIRAuth auth].currentUser; + NSString *kTestAPIKey = @"fakeAPIKey"; + user1.requestConfiguration = [[FIRAuthRequestConfiguration alloc]initWithAPIKey:kTestAPIKey]; + [[FIRAuth auth] signOut:nil]; + + NSString *kTestAccessToken2 = @"fakeAccessToken2"; + [self waitForSignInWithAccessToken:kTestAccessToken2 + APIKey:kAPIKey + completion:nil]; + FIRUser *user2 = [FIRAuth auth].currentUser; + + [self expectGetAccountInfoWithAccessToken:kAccessToken]; + + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + // Current user should now be user2. + XCTAssertEqualObjects([FIRAuth auth].currentUser, user2); + [[FIRAuth auth] updateCurrentUser:user1 completion:^(NSError *_Nullable error) { + XCTAssertNil(error); + // Current user should now be user1. + XCTAssertEqualObjects([FIRAuth auth].currentUser, user1); + XCTAssertNotEqualObjects([FIRAuth auth].currentUser, user2); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + OCMVerifyAll(_mockBackend); +} + /** @fn testSignOut @brief Tests the @c signOut: method. */ @@ -1884,7 +1989,7 @@ static const NSTimeInterval kWaitInterval = .5; // Listener should fire for signing in again as the same user with another access token. expectation = [self expectationWithDescription:@"sign-in again"]; shouldHaveUser = YES; - [self waitForSignInWithAccessToken:kNewAccessToken]; + [self waitForSignInWithAccessToken:kNewAccessToken APIKey:nil completion:nil]; // Listener should fire for signing out. expectation = [self expectationWithDescription:@"sign-out"]; @@ -2256,15 +2361,19 @@ static const NSTimeInterval kWaitInterval = .5; @remarks This method also waits for all other pending @c XCTestExpectation instances. */ - (void)waitForSignIn { - [self waitForSignInWithAccessToken:kAccessToken]; + [self waitForSignInWithAccessToken:kAccessToken APIKey:nil completion:nil]; } /** @fn waitForSignInWithAccessToken: @brief Signs in a user to prepare for tests. @param accessToken The access token for the user to have. + @param APIKey Optionally, The API key associated with the user. + @param completion Optionally, The completion invoked at the end of the flow. @remarks This method also waits for all other pending @c XCTestExpectation instances. */ -- (void)waitForSignInWithAccessToken:(NSString *)accessToken { +- (void)waitForSignInWithAccessToken:(NSString *)accessToken + APIKey:(nullable NSString *)APIKey + completion:(nullable FIRAuthResultCallback)completion { OCMExpect([_mockBackend verifyPassword:[OCMArg any] callback:[OCMArg any]]) .andCallBlock2(^(FIRVerifyPasswordRequest *_Nullable request, FIRVerifyPasswordResponseCallback callback) { @@ -2281,7 +2390,12 @@ static const NSTimeInterval kWaitInterval = .5; XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; [[FIRAuth auth] signInWithEmail:kEmail password:kFakePassword completion:^(FIRUser *_Nullable user, NSError *_Nullable error) { + + user.requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:APIKey]; [expectation fulfill]; + if (completion) { + completion(user, error); + } }]; [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; OCMVerifyAll(_mockBackend); |