From 54aef651d82ebcc529498e4eba945a5512af4c95 Mon Sep 17 00:00:00 2001 From: Zsika Phillip Date: Mon, 20 Nov 2017 15:56:03 -0800 Subject: Add Auth Result to createUserWithEmail (#477) --- Example/Auth/Sample/MainViewController.m | 44 +++++++++++++++++++++++ Example/Auth/Tests/FIRAuthTests.m | 62 ++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) (limited to 'Example/Auth') diff --git a/Example/Auth/Sample/MainViewController.m b/Example/Auth/Sample/MainViewController.m index 4708b4c..eb45226 100644 --- a/Example/Auth/Sample/MainViewController.m +++ b/Example/Auth/Sample/MainViewController.m @@ -384,6 +384,11 @@ static NSString *const kSectionTitleApp = @"APP"; */ static NSString *const kCreateUserTitle = @"Create User"; +/** @var kCreateUserAuthDataResultTitle + @brief The text of the "Create User (AuthDataResult)" button. + */ +static NSString *const kCreateUserAuthDataResultTitle = @"Create User (AuthDataResult)"; + /** @var kDeleteAppTitle @brief The text of the "Delete App" button. */ @@ -694,6 +699,8 @@ typedef enum { value:nil action:^{ [weakSelf createUser]; } accessibilityID:kCreateUserAccessibilityID], + [StaticContentTableViewCell cellWithTitle:kCreateUserAuthDataResultTitle + action:^{ [weakSelf createUserAuthDataResult]; }], [StaticContentTableViewCell cellWithTitle:kSignInGoogleButtonText action:^{ [weakSelf signInGoogle]; }], [StaticContentTableViewCell cellWithTitle:kSignInGoogleAndRetrieveDataButtonText @@ -2493,6 +2500,43 @@ static NSDictionary *parseURL(NSString *urlString) { }]; } +/** @fn createUserAuthDataResult + @brief Creates a new user. + */ +- (void)createUserAuthDataResult { + [self showTextInputPromptWithMessage:@"Email:" + 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] createUserAndRetrieveDataWithEmail:email + password:password + completion:^(FIRAuthDataResult *_Nullable result, + NSError *_Nullable error) { + if (error) { + [self logFailure:@"create user failed" error:error]; + } else { + [self logSuccess:@"create user succeeded."]; + [self log:result.user.uid]; + } + [self hideSpinner:^{ + [self showTypicalUIForUserUpdateResultsWithTitle:kCreateUserTitle error:error]; + }]; + }]; + }]; + }]; + }]; +} + /** @fn signInWithPhoneNumber @brief Allows sign in with phone number. */ diff --git a/Example/Auth/Tests/FIRAuthTests.m b/Example/Auth/Tests/FIRAuthTests.m index 557f0c7..2842ef3 100644 --- a/Example/Auth/Tests/FIRAuthTests.m +++ b/Example/Auth/Tests/FIRAuthTests.m @@ -1243,6 +1243,68 @@ static const NSTimeInterval kWaitInterval = .5; OCMVerifyAll(_mockBackend); } +/** @fn testCreateUserAndRetrieveDataWithEmailPasswordSuccess + @brief Tests the flow of a successful @c createUserAndRetrieveDataWithEmail:password:completion: + call. + */ +- (void)testCreateUserAndRetrieveDataWithEmailPasswordSuccess { + OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]]) + .andCallBlock2(^(FIRSignUpNewUserRequest *_Nullable request, + FIRSignupNewUserCallback callback) { + XCTAssertEqualObjects(request.APIKey, kAPIKey); + XCTAssertEqualObjects(request.email, kEmail); + XCTAssertEqualObjects(request.password, kFakePassword); + XCTAssertTrue(request.returnSecureToken); + dispatch_async(FIRAuthGlobalWorkQueue(), ^() { + id mockSignUpNewUserResponse = OCMClassMock([FIRSignUpNewUserResponse class]); + [self stubTokensWithMockResponse:mockSignUpNewUserResponse]; + callback(mockSignUpNewUserResponse, nil); + }); + }); + [self expectGetAccountInfo]; + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + [[FIRAuth auth] signOut:NULL]; + [[FIRAuth auth] createUserAndRetrieveDataWithEmail:kEmail + password:kFakePassword + completion:^(FIRAuthDataResult *_Nullable result, + NSError *_Nullable error) { + XCTAssertTrue([NSThread isMainThread]); + [self assertUser:result.user]; + XCTAssertTrue(result.additionalUserInfo.isNewUser); + XCTAssertNil(error); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + [self assertUser:[FIRAuth auth].currentUser]; + OCMVerifyAll(_mockBackend); +} + +/** @fn testCreateUserAndRetrieveDataWithEmailPasswordFailure + @brief Tests the flow of a failed @c createUserAndRetrieveDataWithEmail:password:completion: + call. + */ +- (void)testCreateUserAndRetrieveDataWithEmailPasswordFailure { + NSString *reason = @"Password shouldn't be a common word."; + OCMExpect([_mockBackend signUpNewUser:[OCMArg any] callback:[OCMArg any]]) + .andDispatchError2([FIRAuthErrorUtils weakPasswordErrorWithServerResponseReason:reason]); + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + [[FIRAuth auth] signOut:NULL]; + [[FIRAuth auth] createUserAndRetrieveDataWithEmail:kEmail + password:kFakePassword + completion:^(FIRAuthDataResult *_Nullable result, + NSError *_Nullable error) { + XCTAssertTrue([NSThread isMainThread]); + XCTAssertNil(result); + XCTAssertEqual(error.code, FIRAuthErrorCodeWeakPassword); + XCTAssertNotNil(error.userInfo[NSLocalizedDescriptionKey]); + XCTAssertEqualObjects(error.userInfo[NSLocalizedFailureReasonErrorKey], reason); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + XCTAssertNil([FIRAuth auth].currentUser); + OCMVerifyAll(_mockBackend); +} + /** @fn testCreateUserEmptyPasswordFailure @brief Tests the flow of a failed @c createUserWithEmail:password:completion: call due to an empty password. This error occurs on the client side, so there is no need to fake an RPC -- cgit v1.2.3