Skip to content

Add test for verify iOS client #2096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions Example/Auth/EarlGreyTests/FIRVerifyIOSClientTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2018 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <XCTest/XCTest.h>
#import <EarlGrey/EarlGrey.h>

#import <FirebaseCore/FIRApp.h>
#import "FirebaseAuth.h"

static CGFloat const kShortScrollDistance = 100;

static NSTimeInterval const kWaitForElementTimeOut = 15;

@interface FIRVerifyIOSClientTests : XCTestCase
@end

/** Convenience function for EarlGrey tests. */
static id<GREYMatcher> grey_scrollView(void) {
return [GREYMatchers matcherForKindOfClass:[UIScrollView class]];
}

@implementation FIRVerifyIOSClientTests

/** To reset the app so that each test sees the app in a clean state. */
- (void)setUp {
[super setUp];

[self signOut];

[[EarlGrey selectElementWithMatcher:grey_allOf(grey_scrollView(),
grey_kindOfClass([UITableView class]), nil)]
performAction:grey_scrollToContentEdge(kGREYContentEdgeTop)];
}

#pragma mark - Tests

/** Test verify ios client*/
- (void)testVerifyIOSClient {
[[[EarlGrey selectElementWithMatcher:grey_allOf(grey_text(@"Verify iOS client"),
grey_sufficientlyVisible(), nil)]
usingSearchAction:grey_scrollInDirection(kGREYDirectionDown, kShortScrollDistance)
onElementWithMatcher:grey_allOf(grey_scrollView(), grey_kindOfClass([UITableView class]),
nil)] performAction:grey_tap()];

[self waitForElementWithText:@"OK" withDelay:kWaitForElementTimeOut];

[[EarlGrey selectElementWithMatcher:grey_text(@"OK")] performAction:grey_tap()];
}

#pragma mark - Helpers

/** Sign out current account. */
- (void)signOut {
NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];

// Just log the error because we don't want to fail the test if signing out fails.
if (!status) {
NSLog(@"Error signing out: %@", signOutError);
}
}

/** Wait for an element with text to appear. */
- (void)waitForElementWithText:(NSString *)text withDelay:(NSTimeInterval)maxDelay {
GREYCondition *displayed =
[GREYCondition conditionWithName:@"Wait for element"
block:^BOOL {
NSError *error = nil;
[[EarlGrey selectElementWithMatcher:grey_text(text)]
assertWithMatcher:grey_sufficientlyVisible()
error:&error];
return !error;
}];
GREYAssertTrue([displayed waitWithTimeout:maxDelay], @"Failed to wait for element '%@'.", text);
}

@end
6 changes: 3 additions & 3 deletions Example/Auth/EarlGreyTests/FirebaseAuthEarlGreyTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ @interface BasicUITest :XCTestCase
@end

/** Convenience function for EarlGrey tests. */
id<GREYMatcher> grey_scrollView(void) {
static id<GREYMatcher> grey_scrollView(void) {
return [GREYMatchers matcherForKindOfClass:[UIScrollView class]];
}

Expand Down Expand Up @@ -148,8 +148,7 @@ - (void)testSignInWithInvalidBYOAuthToken {

[[EarlGrey selectElementWithMatcher:grey_text(@"Done")] performAction:grey_tap()];

NSString *invalidTokenErrorMessage =
@"The custom token format is incorrect. Please check the documentation.";
NSString *invalidTokenErrorMessage = @"Sign-In Error";

[self waitForElementWithText:invalidTokenErrorMessage withDelay:kWaitForElementTimeOut];

Expand Down Expand Up @@ -182,4 +181,5 @@ - (void)waitForElementWithText:(NSString *)text withDelay:(NSTimeInterval)maxDel
}];
GREYAssertTrue([displayed waitWithTimeout:maxDelay], @"Failed to wait for element '%@'.", text);
}

@end
91 changes: 81 additions & 10 deletions Example/Auth/Sample/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
#import "UserInfoViewController.h"
#import "UserTableViewCell.h"

#import "FIRAuth_Internal.h"
#import "FIRAuthAPNSToken.h"
#import "FIRAuthAPNSTokenManager.h"
#import "FIRAuthAppCredential.h"
#import "FIRAuthAppCredentialManager.h"
#import "FIRAuthBackend.h"
#import "FIRVerifyClientRequest.h"
#import "FIRVerifyClientResponse.h"
#import "FIRSendVerificationCodeRequest.h"

NS_ASSUME_NONNULL_BEGIN

/** @typedef textInputCompletionBlock
Expand Down Expand Up @@ -578,6 +588,11 @@
*/
static NSString *const kPhoneNumberSignInReCaptchaTitle = @"Sign in With Phone Number";

/** @var kVerifyIOSClientTitle
@brief The title for button to verify iOS client.
*/
static NSString *const kVerifyIOSClientTitle = @"Verify iOS client";

/** @var kIsNewUserToggleTitle
@brief The title for button to enable new or existing user toggle.
*/
Expand Down Expand Up @@ -741,6 +756,8 @@ - (void)updateTable {
action:^{
[weakSelf unlinkFromProvider:FIRPhoneAuthProviderID completion:nil];
}],
[StaticContentTableViewCell cellWithTitle:kVerifyIOSClientTitle
action:^{ [weakSelf verifyIOSClient]; }],
]],
[StaticContentTableViewSection sectionWithTitle:kSectionTitleSignIn cells:@[
[StaticContentTableViewCell cellWithTitle:kSwitchToInMemoryUserTitle
Expand Down Expand Up @@ -1605,17 +1622,19 @@ - (void)removeIDTokenListener {
@param string The string to add to the console.
*/
- (void)log:(NSString *)string {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *date = [dateFormatter stringFromDate:[NSDate date]];
if (!_consoleString) {
_consoleString = [NSMutableString string];
}
[_consoleString appendString:[NSString stringWithFormat:@"%@ %@\n", date, string]];
_consoleTextView.text = _consoleString;
dispatch_async(dispatch_get_main_queue(), ^{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *date = [dateFormatter stringFromDate:[NSDate date]];
if (!_consoleString) {
_consoleString = [NSMutableString string];
}
[_consoleString appendString:[NSString stringWithFormat:@"%@ %@\n", date, string]];
_consoleTextView.text = _consoleString;

CGRect targetRect = CGRectMake(0, _consoleTextView.contentSize.height - 1, 1, 1);
[_consoleTextView scrollRectToVisible:targetRect animated:YES];
CGRect targetRect = CGRectMake(0, _consoleTextView.contentSize.height - 1, 1, 1);
[_consoleTextView scrollRectToVisible:targetRect animated:YES];
});
}

/** @fn logSuccess:
Expand Down Expand Up @@ -2885,6 +2904,58 @@ - (void)signInWithPhoneNumber:(NSString *_Nullable)phoneNumber
}];
}

/** @fn verifyIOSClient
@brief Trigger verify iOS client by sending a verification code to the test number.
*/
- (void)verifyIOSClient {
[[AppManager auth].tokenManager getTokenWithCallback:^(FIRAuthAPNSToken *_Nullable token,
NSError *_Nullable error) {
if (!token) {
[self logFailure:@"Verify iOS client failed." error:error];
return;
}
FIRVerifyClientRequest *request =
[[FIRVerifyClientRequest alloc] initWithAppToken:token.string
isSandbox:token.type == FIRAuthAPNSTokenTypeSandbox
requestConfiguration:[AppManager auth].requestConfiguration];
[FIRAuthBackend verifyClient:request callback:^(FIRVerifyClientResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
[self logFailure:@"Verify iOS client failed." error:error];
return;
}
NSTimeInterval timeout = [response.suggestedTimeOutDate timeIntervalSinceNow];
[[AppManager auth].appCredentialManager
didStartVerificationWithReceipt:response.receipt
timeout:timeout
callback:^(FIRAuthAppCredential *credential) {
if (!credential.secret) {
[self logFailure:@"Failed to receive remote notification to verify app identity."
error:error];
return;
}
NSString *testPhoneNumber = @"+16509964692";
FIRSendVerificationCodeRequest *request =
[[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:testPhoneNumber
appCredential:credential
reCAPTCHAToken:nil
requestConfiguration:[AppManager auth].requestConfiguration];
[FIRAuthBackend sendVerificationCode:request
callback:^(FIRSendVerificationCodeResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
[self logFailure:@"Verify iOS client failed." error:error];
return;
} else {
[self logSuccess:@"Verify iOS client succeeded."];
[self showMessagePrompt:@"Verify iOS client succeed."];
}
}];
}];
}];
}];
}

/** @fn signInWithPhoneNumberWithPrompt
@brief Allows sign in with phone number via popup prompt.
*/
Expand Down
8 changes: 6 additions & 2 deletions Example/Firebase.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
0672F2F31EBBA7D900818E87 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 0672F2F11EBBA7D900818E87 /* GoogleService-Info.plist */; };
069428831EC3B38C00F7BC69 /* 1mb.dat in Resources */ = {isa = PBXBuildFile; fileRef = 069428801EC3B35A00F7BC69 /* 1mb.dat */; };
06C24A061EC39BCB005208CA /* FIRStorageIntegrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 06121ECA1EC39A0B0008D70E /* FIRStorageIntegrationTests.m */; };
409E1130219FA260000E6CFC /* FIRVerifyIOSClientTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 409E112F219FA260000E6CFC /* FIRVerifyIOSClientTests.m */; };
7E9485421F578AC4005A3939 /* FIRAuthURLPresenterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7E94853F1F578A9D005A3939 /* FIRAuthURLPresenterTests.m */; };
7EE21F7A1FE89193009B1370 /* FIREmailLinkRequestTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EE21F791FE89193009B1370 /* FIREmailLinkRequestTests.m */; };
7EE21F7C1FE8919E009B1370 /* FIREmailLinkSignInResponseTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EE21F7B1FE8919D009B1370 /* FIREmailLinkSignInResponseTests.m */; };
Expand Down Expand Up @@ -947,6 +948,7 @@
069428801EC3B35A00F7BC69 /* 1mb.dat */ = {isa = PBXFileReference; lastKnownFileType = file; path = 1mb.dat; sourceTree = "<group>"; };
0697B1201EC13D8A00542174 /* Base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Base64.h; sourceTree = "<group>"; };
0697B1211EC13D8A00542174 /* Base64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Base64.m; sourceTree = "<group>"; };
409E112F219FA260000E6CFC /* FIRVerifyIOSClientTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRVerifyIOSClientTests.m; sourceTree = "<group>"; };
6003F58D195388D20070C39A /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
6003F58F195388D20070C39A /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
6003F591195388D20070C39A /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -2017,6 +2019,7 @@
isa = PBXGroup;
children = (
DE26D1FA1F70333E004AE1D3 /* FirebaseAuthEarlGreyTests.m */,
409E112F219FA260000E6CFC /* FIRVerifyIOSClientTests.m */,
DE26D1FB1F70333E004AE1D3 /* Info.plist */,
);
path = EarlGreyTests;
Expand Down Expand Up @@ -4181,6 +4184,7 @@
buildActionMask = 2147483647;
files = (
DE26D2771F705CB5004AE1D3 /* FirebaseAuthEarlGreyTests.m in Sources */,
409E1130219FA260000E6CFC /* FIRVerifyIOSClientTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -6033,7 +6037,7 @@
"\"${PODS_ROOT}/Headers/Public\"",
"\"${PODS_ROOT}/Headers/Public/FirebaseInstanceID\"",
"\"${PODS_ROOT}/Headers/Public/GoogleSignIn\"",
"\"${PODS_ROOT}/../../Firebase/Auth/Source\"",
"\"${PODS_ROOT}/../../Firebase/Auth/Source\"/**",
);
INFOPLIST_FILE = Auth/Sample/Application.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
Expand Down Expand Up @@ -6072,7 +6076,7 @@
"\"${PODS_ROOT}/Headers/Public\"",
"\"${PODS_ROOT}/Headers/Public/FirebaseInstanceID\"",
"\"${PODS_ROOT}/Headers/Public/GoogleSignIn\"",
"\"${PODS_ROOT}/../../Firebase/Auth/Source\"",
"\"${PODS_ROOT}/../../Firebase/Auth/Source\"/**",
);
INFOPLIST_FILE = Auth/Sample/Application.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,25 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "DE26D25C1F7049F1004AE1D3"
BuildableName = "Auth_ApiTests.xctest"
BlueprintName = "Auth_ApiTests"
BlueprintIdentifier = "DE26D26C1F705C35004AE1D3"
BuildableName = "Auth_EarlGreyTests.xctest"
BlueprintName = "Auth_EarlGreyTests"
ReferencedContainer = "container:Firebase.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "DE26D26C1F705C35004AE1D3"
BuildableName = "Auth_EarlGreyTests.xctest"
BlueprintName = "Auth_EarlGreyTests"
BlueprintIdentifier = "DE26D25C1F7049F1004AE1D3"
BuildableName = "Auth_ApiTests.xctest"
BlueprintName = "Auth_ApiTests"
ReferencedContainer = "container:Firebase.xcodeproj">
</BuildableReference>
</TestableReference>
Expand All @@ -66,7 +65,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
1 change: 0 additions & 1 deletion Firebase/Auth/Source/Public/FIRPhoneAuthProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ extern NSString *const FIRPhoneAuthProviderID NS_SWIFT_NAME(PhoneAuthProviderID)
*/
extern NSString *const _Nonnull FIRPhoneAuthSignInMethod NS_SWIFT_NAME(PhoneAuthSignInMethod);


/** @typedef FIRVerificationResultCallback
@brief The type of block invoked when a request to send a verification code has finished.

Expand Down