diff options
author | Zsika Phillip <protocol86@users.noreply.github.com> | 2018-04-03 17:19:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-03 17:19:51 -0700 |
commit | 6566328dc461c38c6000c64996c17883b9895d06 (patch) | |
tree | 1f072d106b95e87fbd9d163194d08c3a76977e07 /Example | |
parent | 1be9737fb28deb388cbcfaf6324ebad49683340b (diff) |
Adds copy auth state API (#1018)
* Adds copy auth state API
* improvements
* Addresses comments
Diffstat (limited to 'Example')
-rw-r--r-- | Example/Auth/Sample/MainViewController.m | 31 | ||||
-rw-r--r-- | Example/Auth/Tests/FIRAuthTests.m | 120 |
2 files changed, 148 insertions, 3 deletions
diff --git a/Example/Auth/Sample/MainViewController.m b/Example/Auth/Sample/MainViewController.m index 18f1e02..e343508 100644 --- a/Example/Auth/Sample/MainViewController.m +++ b/Example/Auth/Sample/MainViewController.m @@ -429,6 +429,11 @@ static NSString *const kRemoveIDTokenListenerTitle = @"Remove Last ID Token Chan */ static NSString *const kSectionTitleApp = @"APP"; +/** @var kUpdateCurrentUserFromSavedTitle + @brief The text of the "Upgrade to saved user" button. + */ +static NSString *const kUpdateCurrentUserFromSavedTitle = @"Upgrade to saved user"; + /** @var kCreateUserTitle @brief The text of the "Create User" button. */ @@ -745,6 +750,9 @@ typedef enum { }], ]], [StaticContentTableViewSection sectionWithTitle:kSectionTitleSignIn cells:@[ + [StaticContentTableViewCell cellWithTitle:kUpdateCurrentUserFromSavedTitle + value:nil + action:^{ [weakSelf updateToSavedUser]; }], [StaticContentTableViewCell cellWithTitle:kCreateUserTitle value:nil action:^{ [weakSelf createUser]; } @@ -2744,6 +2752,29 @@ static NSDictionary<NSString *, NSString *> *parseURL(NSString *urlString) { }]; } +/** @fn updateToSavedUser + @brief updates the current user to the saved user. + */ +- (void)updateToSavedUser { + if(![AppManager auth].currentUser) { + NSLog(@"You must be signed in to perform this action"); + return; + } + + if (!_userInMemory) { + NSLog(@"You need an in memory user to perform this action"); + return; + } + + [[AppManager auth] updateCurrentUser:_userInMemory completion:^(NSError *_Nullable error) { + if (error) { + [self showMessagePrompt: + [NSString stringWithFormat:@"An error Occurred: %@", error.localizedDescription]]; + return; + } + }]; +} + /** @fn createUser @brief Creates a new user. */ 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); |