From 22a943708d156712a97d29dcd11ee903cb9d8494 Mon Sep 17 00:00:00 2001 From: Zsika Phillip Date: Sun, 29 Apr 2018 19:29:48 -0700 Subject: Adds phone auth testing. (disableAppVerification) (#1192) --- Example/Auth/Sample/SettingsViewController.m | 17 ++++++ Example/Auth/Tests/FIRPhoneAuthProviderTests.m | 75 ++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) (limited to 'Example/Auth') diff --git a/Example/Auth/Sample/SettingsViewController.m b/Example/Auth/Sample/SettingsViewController.m index 24a6513..4815f32 100644 --- a/Example/Auth/Sample/SettingsViewController.m +++ b/Example/Auth/Sample/SettingsViewController.m @@ -214,9 +214,26 @@ static NSString *truncatedString(NSString *string, NSUInteger length) { [weakSelf loadTableView]; }], ]], + [StaticContentTableViewSection sectionWithTitle:@"Auth Settings" cells:@[ + [StaticContentTableViewCell cellWithTitle:@"Disable App Verification (Phone)" + value:[AppManager auth].settings. + appVerificationDisabledForTesting ? @"Yes" : @"No" + action:^{ + [weakSelf toggleDisableAppVerification]; + [weakSelf loadTableView]; + }], + ]], ]]; } +/** @fn toggleDisableAppVerification + @brief Toggles the appVerificationDisabledForTesting flag on the current Auth instance. + */ +- (void)toggleDisableAppVerification { + [AppManager auth].settings.appVerificationDisabledForTesting = + ![AppManager auth].settings.appVerificationDisabledForTesting; +} + /** @fn toggleAPIHostWithRequestClassName: @brief Toggles the host name of the server that handles RPCs. @param requestClassName The name of the RPC request class. diff --git a/Example/Auth/Tests/FIRPhoneAuthProviderTests.m b/Example/Auth/Tests/FIRPhoneAuthProviderTests.m index e00fa1f..96432c7 100644 --- a/Example/Auth/Tests/FIRPhoneAuthProviderTests.m +++ b/Example/Auth/Tests/FIRPhoneAuthProviderTests.m @@ -31,6 +31,7 @@ #import "FIRAuthGlobalWorkQueue.h" #import "FIRAuthNotificationManager.h" #import "FIRAuthRequestConfiguration.h" +#import "FIRAuthSettings.h" #import "FIRAuthUIDelegate.h" #import "FIRAuthURLPresenter.h" #import "FIRAuthWebUtils.h" @@ -362,6 +363,80 @@ static const NSTimeInterval kExpectationTimeout = 2; OCMVerifyAll(_mockAppCredentialManager); } +/** @fn testVerifyPhoneNumberInTestMode + @brief Tests a successful invocation of @c verifyPhoneNumber:completion: when app verification + is disabled. + */ +- (void)testVerifyPhoneNumberInTestMode { + // Disable app verification. + FIRAuthSettings *settings = [[FIRAuthSettings alloc] init]; + settings.appVerificationDisabledForTesting = YES; + OCMStub([_mockAuth settings]).andReturn(settings); + OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY]) + .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) { callback(YES); }); + OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]]) + .andCallBlock2(^(FIRSendVerificationCodeRequest *request, + FIRSendVerificationCodeResponseCallback callback) { + XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber); + // Assert that the app credential is nil when in test mode. + XCTAssertNil(request.appCredential); + dispatch_async(FIRAuthGlobalWorkQueue(), ^() { + id mockSendVerificationCodeResponse = OCMClassMock([FIRSendVerificationCodeResponse class]); + OCMStub([mockSendVerificationCodeResponse verificationID]).andReturn(kTestVerificationID); + callback(mockSendVerificationCodeResponse, nil); + }); + }); + + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + [_provider verifyPhoneNumber:kTestPhoneNumber + completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) { + XCTAssertTrue([NSThread isMainThread]); + XCTAssertNil(error); + XCTAssertEqualObjects(verificationID, kTestVerificationID); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + OCMVerifyAll(_mockBackend); + OCMVerifyAll(_mockNotificationManager); + OCMVerifyAll(_mockAppCredentialManager); +} + +/** @fn testVerifyPhoneNumberInTestModeFailure + @brief Tests a failed invocation of @c verifyPhoneNumber:completion: when app verification + is disabled. + */ +- (void)testVerifyPhoneNumberInTestModeFailure { + // Disable app verification. + FIRAuthSettings *settings = [[FIRAuthSettings alloc] init]; + settings.appVerificationDisabledForTesting = YES; + OCMStub([_mockAuth settings]).andReturn(settings); + OCMExpect([_mockNotificationManager checkNotificationForwardingWithCallback:OCMOCK_ANY]) + .andCallBlock1(^(FIRAuthNotificationForwardingCallback callback) { callback(YES); }); + OCMExpect([_mockBackend sendVerificationCode:[OCMArg any] callback:[OCMArg any]]) + .andCallBlock2(^(FIRSendVerificationCodeRequest *request, + FIRSendVerificationCodeResponseCallback callback) { + XCTAssertEqualObjects(request.phoneNumber, kTestPhoneNumber); + // Assert that the app credential is nil when in test mode. + XCTAssertNil(request.appCredential); + dispatch_async(FIRAuthGlobalWorkQueue(), ^() { + callback(nil, [FIRAuthErrorUtils networkErrorWithUnderlyingError:[NSError new]]); + }); + }); + + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; + [_provider verifyPhoneNumber:kTestPhoneNumber + completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) { + XCTAssertTrue([NSThread isMainThread]); + XCTAssertNil(verificationID); + XCTAssertEqual(error.code, FIRAuthErrorCodeNetworkError); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; + OCMVerifyAll(_mockBackend); + OCMVerifyAll(_mockNotificationManager); + OCMVerifyAll(_mockAppCredentialManager); +} + /** @fn testVerifyPhoneNumberUIDelegate @brief Tests a successful invocation of @c verifyPhoneNumber:UIDelegate:completion:. */ -- cgit v1.2.3