diff options
author | Zsika Phillip <protocol86@users.noreply.github.com> | 2017-11-17 15:55:56 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-17 15:55:56 -0800 |
commit | 5fbe71052de048a15f44476115a49e1958e70691 (patch) | |
tree | fead2d827d2264005dc82dd728b63a84f25dc156 /Example | |
parent | 4fbc96904fe86b3a0cdb91bfa6d96491221eb6b9 (diff) |
Adds AuthDataResult to anonymous sign in (#470)
* Adds AuthDataResult to anonymous sign in
* Fixes typo
* Addresses comments
* addresses comment on PR
Diffstat (limited to 'Example')
-rw-r--r-- | Example/Auth/Sample/MainViewController.m | 25 | ||||
-rw-r--r-- | Example/Auth/Tests/FIRAuthTests.m | 57 |
2 files changed, 80 insertions, 2 deletions
diff --git a/Example/Auth/Sample/MainViewController.m b/Example/Auth/Sample/MainViewController.m index 9a50c18..4708b4c 100644 --- a/Example/Auth/Sample/MainViewController.m +++ b/Example/Auth/Sample/MainViewController.m @@ -116,6 +116,12 @@ static NSString *const kSignInWithCustomTokenButtonText = @"Sign In (BYOAuth)"; */ static NSString *const kSignInAnonymouslyButtonText = @"Sign In Anonymously"; +/** @var kSignInAnonymouslyWithAuthResultButtonText + @brief The text of the "Sign In Anonymously (AuthDataResult)" button. + */ +static NSString *const kSignInAnonymouslyWithAuthResultButtonText = + @"Sign In Anonymously (AuthDataResult)"; + /** @var kSignedInAlertTitle @brief The text of the "Sign In Succeeded" alert. */ @@ -702,6 +708,8 @@ typedef enum { action:^{ [weakSelf signInWithCustomToken]; }], [StaticContentTableViewCell cellWithTitle:kSignInAnonymouslyButtonText action:^{ [weakSelf signInAnonymously]; }], + [StaticContentTableViewCell cellWithTitle:kSignInAnonymouslyWithAuthResultButtonText + action:^{ [weakSelf signInAnonymouslyAuthDataResult]; }], [StaticContentTableViewCell cellWithTitle:kGitHubSignInButtonText action:^{ [weakSelf signInWithGitHub]; }], [StaticContentTableViewCell cellWithTitle:kSignOutButtonText @@ -2781,6 +2789,23 @@ static NSDictionary<NSString *, NSString *> *parseURL(NSString *urlString) { }]; } +/** @fn signInAnonymouslyAuthDataResult + @brief Signs in as an anonymous user, receiving an auth result containing a signed in user upon + success. + */ +- (void)signInAnonymouslyAuthDataResult { + [[AppManager auth] signInAnonymouslyAndRetrieveDataWithCompletion: + ^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) { + if (error) { + [self logFailure:@"sign-in anonymously failed" error:error]; + } else { + [self logSuccess:@"sign-in anonymously succeeded."]; + [self log:[NSString stringWithFormat:@"User ID : %@", authResult.user.uid]]; + } + [self showTypicalUIForUserUpdateResultsWithTitle:kSignInAnonymouslyButtonText error:error]; + }]; +} + /** @fn signInWithGitHub @brief Signs in as a GitHub user. Prompts the user for an access token and uses this access token to create a GitHub (generic) credential for signing in. diff --git a/Example/Auth/Tests/FIRAuthTests.m b/Example/Auth/Tests/FIRAuthTests.m index d9a7461..557f0c7 100644 --- a/Example/Auth/Tests/FIRAuthTests.m +++ b/Example/Auth/Tests/FIRAuthTests.m @@ -1029,7 +1029,7 @@ static const NSTimeInterval kWaitInterval = .5; } /** @fn testSignInAnonymouslySuccess - @brief Tests the flow of a successful @c signInAnonymously:completion: call. + @brief Tests the flow of a successful @c signInAnonymouslyWithCompletion: call. */ - (void)testSignInAnonymouslySuccess { OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]]) @@ -1061,7 +1061,7 @@ static const NSTimeInterval kWaitInterval = .5; } /** @fn testSignInAnonymouslyFailure - @brief Tests the flow of a failed @c signInAnonymously:completion: call. + @brief Tests the flow of a failed @c signInAnonymouslyWithCompletion: call. */ - (void)testSignInAnonymouslyFailure { OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]]) @@ -1081,6 +1081,59 @@ static const NSTimeInterval kWaitInterval = .5; OCMVerifyAll(_mockBackend); } +/** @fn testSignInAnonymouslyAndRetrieveDataSuccess + @brief Tests the flow of a successful @c signInAnonymouslyAndRetrieveDataWithCompletion: call. + */ +- (void)testSignInAnonymouslyAndRetrieveDataSuccess { + OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]]) + .andCallBlock2(^(FIRSignUpNewUserRequest *_Nullable request, + FIRSignupNewUserCallback callback) { + XCTAssertEqualObjects(request.APIKey, kAPIKey); + XCTAssertNil(request.email); + XCTAssertNil(request.password); + XCTAssertTrue(request.returnSecureToken); + dispatch_async(FIRAuthGlobalWorkQueue(), ^() { + id mockSignUpNewUserResponse = OCMClassMock([FIRSignUpNewUserResponse class]); + [self stubTokensWithMockResponse:mockSignUpNewUserResponse]; + callback(mockSignUpNewUserResponse, nil); + }); + }); + [self expectGetAccountInfoAnonymous]; + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + [[FIRAuth auth] signOut:NULL]; + [[FIRAuth auth] signInAnonymouslyAndRetrieveDataWithCompletion: + ^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) { + XCTAssertTrue([NSThread isMainThread]); + [self assertUserAnonymous:result.user]; + XCTAssertNil(error); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + [self assertUserAnonymous:[FIRAuth auth].currentUser]; + OCMVerifyAll(_mockBackend); +} + +/** @fn testSignInAnonymouslyAndRetrieveDataFailure + @brief Tests the flow of a failed @c signInAnonymouslyAndRetrieveDataWithCompletion: call. + */ +- (void)testSignInAnonymouslyAndRetrieveDataFailure { + OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]]) + .andDispatchError2([FIRAuthErrorUtils operationNotAllowedErrorWithMessage:nil]); + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + [[FIRAuth auth] signOut:NULL]; + [[FIRAuth auth] signInAnonymouslyAndRetrieveDataWithCompletion: + ^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) { + XCTAssertTrue([NSThread isMainThread]); + XCTAssertNil(result); + XCTAssertEqual(error.code, FIRAuthErrorCodeOperationNotAllowed); + XCTAssertNotNil(error.userInfo[NSLocalizedDescriptionKey]); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + XCTAssertNil([FIRAuth auth].currentUser); + OCMVerifyAll(_mockBackend); +} + /** @fn testSignInWithCustomTokenSuccess @brief Tests the flow of a successful @c signInWithCustomToken:completion: call. */ |