From bc2d78439a012bca61fd13dd2df97f4d916174b7 Mon Sep 17 00:00:00 2001 From: Zsika Phillip Date: Wed, 22 Nov 2017 08:57:07 -0800 Subject: Adds AuthDataResult to signInWithEmailPassword Method (#484) * Adds AuthDataResult to signInWithEmail:Password * Addresses comments * Addresses comments * addresses more comments * Fixes broken tests --- Example/Auth/Sample/MainViewController.m | 45 ++++++++++++++++++++++-- Example/Auth/Tests/FIRAuthTests.m | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) (limited to 'Example/Auth') diff --git a/Example/Auth/Sample/MainViewController.m b/Example/Auth/Sample/MainViewController.m index 5831d34..42f0a8e 100644 --- a/Example/Auth/Sample/MainViewController.m +++ b/Example/Auth/Sample/MainViewController.m @@ -106,6 +106,12 @@ static NSString *const kSignInFacebookAndRetrieveDataButtonText = */ static NSString *const kSignInEmailPasswordButtonText = @"Sign in with Email/Password"; +/** @var kSignInEmailPasswordAuthDataResultButtonText + @brief The text of the "Email/Password SignIn (AuthDataResult)" button. + */ +static NSString *const kSignInEmailPasswordAuthDataResultButtonText = + @"Sign in with Email/Password (AuthDataReult)"; + /** @var kSignInWithCustomTokenButtonText @brief The text of the "Sign In (BYOAuth)" button. */ @@ -711,6 +717,8 @@ typedef enum { action:^{ [weakSelf signInFacebookAndRetrieveData]; }], [StaticContentTableViewCell cellWithTitle:kSignInEmailPasswordButtonText action:^{ [weakSelf signInEmailPassword]; }], + [StaticContentTableViewCell cellWithTitle:kSignInEmailPasswordAuthDataResultButtonText + action:^{ [weakSelf signInEmailPasswordAuthDataResult]; }], [StaticContentTableViewCell cellWithTitle:kSignInWithCustomTokenButtonText action:^{ [weakSelf signInWithCustomToken]; }], [StaticContentTableViewCell cellWithTitle:kSignInAnonymouslyButtonText @@ -1650,15 +1658,48 @@ static NSDictionary *parseURL(NSString *urlString) { } FIRAuthCredential *credential = [FIREmailAuthProvider credentialWithEmail:email - password:password]; + password:password]; [self showSpinner:^{ [[AppManager auth] signInWithCredential:credential - completion:^(FIRUser *_Nullable user, NSError *_Nullable error) { + completion:^(FIRUser *_Nullable user, + NSError *_Nullable error) { + [self hideSpinner:^{ + if (error) { + [self logFailure:@"sign-in with Email/Password failed" error:error]; + } else { + [self logSuccess:@"sign-in with Email/Password succeeded."]; + } + [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error]; + }]; + }]; + }]; + }]; + }]; +} + +- (void)signInEmailPasswordAuthDataResult { + [self showTextInputPromptWithMessage:@"Email Address:" + keyboardType:UIKeyboardTypeEmailAddress + completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) { + if (!userPressedOK || !email.length) { + return; + } + [self showTextInputPromptWithMessage:@"Password:" + completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) { + if (!userPressedOK) { + return; + } + [self showSpinner:^{ + [[AppManager auth] signInAndRetrieveDataWithEmail:email + password:password + completion:^(FIRAuthDataResult *_Nullable authResult, + NSError *_Nullable error) { [self hideSpinner:^{ if (error) { [self logFailure:@"sign-in with Email/Password failed" error:error]; } else { [self logSuccess:@"sign-in with Email/Password succeeded."]; + [self log:[NSString stringWithFormat:@"UID: %@",authResult.user.uid]]; } [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error]; }]; diff --git a/Example/Auth/Tests/FIRAuthTests.m b/Example/Auth/Tests/FIRAuthTests.m index 35c8362..770f2c7 100644 --- a/Example/Auth/Tests/FIRAuthTests.m +++ b/Example/Auth/Tests/FIRAuthTests.m @@ -548,6 +548,66 @@ static const NSTimeInterval kWaitInterval = .5; OCMVerifyAll(_mockBackend); } +/** @fn testSignInAndRetrieveDataWithEmailPasswordSuccess + @brief Tests the flow of a successful @c signInAndRetrieveDataWithEmail:password:completion: + call. + */ +- (void)testSignInAndRetrieveDataWithEmailPasswordSuccess { + OCMExpect([_mockBackend verifyPassword:[OCMArg any] callback:[OCMArg any]]) + .andCallBlock2(^(FIRVerifyPasswordRequest *_Nullable request, + FIRVerifyPasswordResponseCallback callback) { + XCTAssertEqualObjects(request.APIKey, kAPIKey); + XCTAssertEqualObjects(request.email, kEmail); + XCTAssertEqualObjects(request.password, kFakePassword); + XCTAssertTrue(request.returnSecureToken); + dispatch_async(FIRAuthGlobalWorkQueue(), ^() { + id mockVerifyPasswordResponse = OCMClassMock([FIRVerifyPasswordResponse class]); + [self stubTokensWithMockResponse:mockVerifyPasswordResponse]; + callback(mockVerifyPasswordResponse, nil); + }); + }); + [self expectGetAccountInfo]; + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + [[FIRAuth auth] signOut:NULL]; + [[FIRAuth auth] signInAndRetrieveDataWithEmail:kEmail + password:kFakePassword + completion:^(FIRAuthDataResult *_Nullable result, + NSError *_Nullable error) { + XCTAssertTrue([NSThread isMainThread]); + [self assertUser:result.user]; + XCTAssertFalse(result.additionalUserInfo.isNewUser); + XCTAssertEqualObjects(result.additionalUserInfo.providerID, FIREmailAuthProviderID); + XCTAssertNil(error); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + [self assertUser:[FIRAuth auth].currentUser]; + OCMVerifyAll(_mockBackend); +} + +/** @fn testSignInAndRetrieveDataWithEmailPasswordFailure + @brief Tests the flow of a failed @c signInAndRetrieveDataWithEmail:password:completion: call. + */ +- (void)testSignInAndRetrieveDataWithEmailPasswordFailure { + OCMExpect([_mockBackend verifyPassword:[OCMArg any] callback:[OCMArg any]]) + .andDispatchError2([FIRAuthErrorUtils wrongPasswordErrorWithMessage:nil]); + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + [[FIRAuth auth] signOut:NULL]; + [[FIRAuth auth] signInAndRetrieveDataWithEmail:kEmail + password:kFakePassword + completion:^(FIRAuthDataResult *_Nullable result, + NSError *_Nullable error) { + XCTAssertTrue([NSThread isMainThread]); + XCTAssertNil(result); + XCTAssertEqual(error.code, FIRAuthErrorCodeWrongPassword); + XCTAssertNotNil(error.userInfo[NSLocalizedDescriptionKey]); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + XCTAssertNil([FIRAuth auth].currentUser); + OCMVerifyAll(_mockBackend); +} + /** @fn testResetPasswordSuccess @brief Tests the flow of a successful @c confirmPasswordResetWithCode:newPassword:completion: call. -- cgit v1.2.3