Skip to content

Commit d8bcaf0

Browse files
committed
Adds automated test for manual phone sign-in
1 parent 2643598 commit d8bcaf0

File tree

1 file changed

+118
-97
lines changed

1 file changed

+118
-97
lines changed

Example/Auth/Sample/MainViewController.m

Lines changed: 118 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@
429429
static NSString *const kAutoAccountLinking = @"Link with Google";
430430

431431
/** @var kAutoPhoneNumberSignIn
432-
@brief The button title for automated account linking.
432+
@brief The button title for automated Phone Number sign-in.
433433
*/
434434
static NSString *const kAutoPhoneNumberSignIn = @"Sign in With Phone Number";
435435

@@ -676,16 +676,16 @@ - (void)updateTable {
676676
]],
677677
[StaticContentTableViewSection sectionWithTitle:kPhoneAuthSectionTitle cells:@[
678678
[StaticContentTableViewCell cellWithTitle:kPhoneNumberSignInReCaptchaTitle
679-
action:^{ [weakSelf signInWithPhoneNumberRecaptcha];}],
679+
action:^{ [weakSelf signInWithPhoneNumberPrompt]; }],
680680
[StaticContentTableViewCell cellWithTitle:kPhoneNumberSignInTitle
681681
action:^{ [weakSelf signInWithPhoneNumber]; }],
682682
[StaticContentTableViewCell cellWithTitle:kUpdatePhoneNumber
683-
action:^{ [weakSelf updatePhoneNumber]; }],
683+
action:^{ [weakSelf updatePhoneNumberPrompt]; }],
684684
[StaticContentTableViewCell cellWithTitle:kLinkPhoneNumber
685-
action:^{ [weakSelf linkPhoneNumber]; }],
685+
action:^{ [weakSelf linkPhoneNumberPrompt]; }],
686686
[StaticContentTableViewCell cellWithTitle:kUnlinkPhoneNumber
687687
action:^{
688-
[weakSelf unlinkFromProvider:FIRPhoneAuthProviderID completion:nil];
688+
[weakSelf unlinkFromProvider:FIRPhoneAuthProviderID completion:nil];
689689
}],
690690
]],
691691
[StaticContentTableViewSection sectionWithTitle:kSectionTitleSignIn cells:@[
@@ -1053,28 +1053,28 @@ - (void)automatedPhoneNumberSignIn {
10531053
[self log:@"INITIATING AUTOMATED MANUAL TEST FOR PHONE NUMBER SIGN IN:"];
10541054
[self commonPhoneNumberInputWithTitle:@"Phone for automation"
10551055
Completion:^(NSString *_Nullable phone) {
1056-
[self signInWithPhoneNumberRecaptchaWithString:phone completion:^(NSError *error){
1056+
[self signInWithPhoneNumber:phone completion:^(NSError *error) {
10571057
if (error) {
10581058
[self logFailedTest:@"Could not sign in with phone number reCAPTCHA."];
10591059
}
10601060
[self logSuccess:@"sign-in with phone number reCAPTCHA test succeeded."];
10611061
[auth signOut:NULL];
1062-
[self signInWithPhoneNumberRecaptchaWithString:phone completion:^(NSError *error){
1062+
[self signInWithPhoneNumber:phone completion:^(NSError *error) {
10631063
if (error) {
10641064
[self logFailedTest:@"Could not sign in with phone number reCAPTCHA."];
10651065
}
10661066
[self logSuccess:@"second sign-in with phone number reCAPTCHA test succeeded."];
1067-
[self updatePhoneNumberWithString:phone completion:^{
1067+
[self updatePhoneNumber:phone completion:^(NSError *error) {
10681068
if (error) {
10691069
[self logFailedTest:@"Could not update phone number."];
10701070
}
10711071
[self logSuccess:@"update phone number test succeeded."];
1072-
[self unlinkFromProvider:FIRPhoneAuthProviderID completion:^ {
1072+
[self unlinkFromProvider:FIRPhoneAuthProviderID completion:^(NSError *error) {
10731073
if (error) {
10741074
[self logFailedTest:@"Could not unlink phone number."];
10751075
}
10761076
[self logSuccess:@"unlink phone number test succeeded."];
1077-
[self linkPhoneNumberWithString:phone completion:^{
1077+
[self linkPhoneNumber:phone completion:^(NSError *error) {
10781078
if (error) {
10791079
[self logFailedTest:@"Could not link phone number."];
10801080
}
@@ -2106,16 +2106,21 @@ - (void)showEmailPasswordDialogWithCompletion:(ShowEmailPasswordDialogCompletion
21062106
/** @fn unlinkFromProvider:
21072107
@brief Unlinks the current user from the provider with the specified provider ID.
21082108
@param provider The provider ID of the provider to unlink the current user's account from.
2109+
@completion A completion block to be executed after the provider is unlinked.
21092110
*/
2110-
- (void)unlinkFromProvider:(NSString *)provider completion:(void(^)(void))completion {
2111+
- (void)unlinkFromProvider:(NSString *)provider
2112+
completion:(void(^)(NSError *_Nullable))completion {
21112113
[[self user] unlinkFromProvider:provider
21122114
completion:^(FIRUser *_Nullable user,
21132115
NSError *_Nullable error) {
21142116
if (error) {
21152117
[self logFailure:@"unlink auth provider failed" error:error];
2118+
completion(error);
21162119
} else {
21172120
[self logSuccess:@"unlink auth provider succeeded."];
2118-
if (completion) completion();
2121+
if (completion) {
2122+
completion(nil);
2123+
}
21192124
}
21202125
[self showTypicalUIForUserUpdateResultsWithTitle:kUnlinkTitle error:error];
21212126
}];
@@ -2516,12 +2521,13 @@ - (void)signInWithPhoneNumber {
25162521
}];
25172522
}
25182523

2519-
/** @fn signInWithPhoneNumberRecaptchaWithString
2520-
@brief Allows sign in with phone number using reCAPTCHA and completion.
2521-
@param phoneNumber Number pass in string for automation.
2524+
/** @fn signInWithPhoneNumber
2525+
@brief Allows sign in with phone number using reCAPTCHA.
2526+
@param phoneNumber Number pass in string.
2527+
@completion A completion block to be executed after successful phone number sign in.
25222528
*/
2523-
- (void)signInWithPhoneNumberRecaptchaWithString:(NSString *_Nullable)phoneNumber
2524-
completion:(void(^)(NSError *_Nullable))completion {
2529+
- (void)signInWithPhoneNumber:(NSString *_Nullable)phoneNumber
2530+
completion:(void(^)(NSError *_Nullable))completion {
25252531
[self showSpinner:^{
25262532
[[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
25272533
UIDelegate:nil
@@ -2531,26 +2537,30 @@ - (void)signInWithPhoneNumberRecaptchaWithString:(NSString *_Nullable)phoneNumbe
25312537
if (error) {
25322538
[self logFailure:@"failed to send verification code" error:error];
25332539
[self showMessagePrompt:error.localizedDescription];
2540+
completion(error);
25342541
return;
25352542
}
25362543
[self logSuccess:@"Code sent"];
25372544
[self commonPhoneNumberInputWithTitle:@"Code"
25382545
Completion:^(NSString *_Nullable verificationCode) {
25392546
[self commontPhoneVerificationWithVerificationID:verificationID
25402547
verificationCode:verificationCode];
2541-
if (completion) completion(nil);
2548+
if (completion) {
2549+
completion(nil);
2550+
}
25422551
}];
25432552
}];
25442553
}];
25452554
}];
25462555
}
25472556

2548-
/** @fn signInWithPhoneNumberRecaptcha
2549-
@brief Allows sign in with phone number using reCAPTCHA
2557+
/** @fn signInWithPhoneNumberPrompt
2558+
@brief Allows sign in with phone number via popup prompt.
25502559
*/
2551-
- (void)signInWithPhoneNumberRecaptcha {
2552-
[self commonPhoneNumberInputWithTitle:@"Phone #" Completion:^(NSString *_Nullable phone) {
2553-
[self signInWithPhoneNumberRecaptchaWithString:(phone) completion:nil];
2560+
- (void)signInWithPhoneNumberPrompt {
2561+
[self commonPhoneNumberInputWithTitle:@"Phone #"
2562+
Completion:^(NSString *_Nullable phone) {
2563+
[self signInWithPhoneNumber:phone completion:nil];
25542564
}];
25552565
}
25562566

@@ -2594,12 +2604,13 @@ - (void)commontPhoneVerificationWithVerificationID:(NSString *)verificationID
25942604
}];
25952605
}
25962606

2597-
/** @fn updatePhoneNumberWithString
2607+
/** @fn updatePhoneNumber
25982608
@brief Allows adding a verified phone number to the currently signed user.
2599-
@param phoneNumber Number pass in string for automation.
2609+
@param phoneNumber Number pass in string.
2610+
@completion A completion block to be executed after phone number is updated.
26002611
*/
2601-
- (void)updatePhoneNumberWithString:(NSString *_Nullable)phoneNumber
2602-
completion:(void(^)(void))completion{
2612+
- (void)updatePhoneNumber:(NSString *_Nullable)phoneNumber
2613+
completion:(void(^)(NSError *_Nullable))completion{
26032614
[self showSpinner:^{
26042615
[[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
26052616
UIDelegate:nil
@@ -2608,6 +2619,7 @@ - (void)updatePhoneNumberWithString:(NSString *_Nullable)phoneNumber
26082619
if (error) {
26092620
[self logFailure:@"failed to send verification code" error:error];
26102621
[self showMessagePrompt:error.localizedDescription];
2622+
completion(error);
26112623
return;
26122624
}
26132625
[self logSuccess:@"Code sent"];
@@ -2628,9 +2640,12 @@ - (void)updatePhoneNumberWithString:(NSString *_Nullable)phoneNumber
26282640
if (error) {
26292641
[self logFailure:@"update phone number failed" error:error];
26302642
[self showMessagePrompt:error.localizedDescription];
2643+
completion(error);
26312644
} else {
26322645
[self logSuccess:@"update phone number succeeded."];
2633-
if (completion) completion();
2646+
if (completion) {
2647+
completion(nil);
2648+
}
26342649
}
26352650
}];
26362651
}];
@@ -2645,107 +2660,113 @@ - (void)updatePhoneNumberWithString:(NSString *_Nullable)phoneNumber
26452660
/** @fn updatePhoneNumber
26462661
@brief Allows adding a verified phone number to the currently signed user via popup prompt.
26472662
*/
2648-
- (void)updatePhoneNumber {
2663+
- (void)updatePhoneNumberPrompt {
26492664
[self showTextInputPromptWithMessage:@"Update Phone #:"
26502665
keyboardType:UIKeyboardTypePhonePad
26512666
completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
26522667
if (!userPressedOK || !phoneNumber.length) {
26532668
return;
26542669
}
2655-
[self updatePhoneNumberWithString:phoneNumber completion:nil];
2670+
[self updatePhoneNumber:phoneNumber completion:nil];
26562671
}];
26572672
}
26582673

2659-
/** @fn linkPhoneNumberWithString
2674+
/** @fn linkPhoneNumber
26602675
@brief Allows linking a verified phone number to the currently signed user.
2661-
@param phoneNumber Number pass in string for automation.
2676+
@param phoneNumber Number pass in string.
2677+
@completion A completion block to be executed after linking phone number.
26622678
*/
2663-
- (void)linkPhoneNumberWithString:(NSString *_Nullable)phoneNumber
2664-
completion:(void(^)(void))completion{
2665-
[self showSpinner:^{
2666-
[[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
2667-
UIDelegate:nil
2668-
completion:^(NSString *_Nullable verificationID,
2669-
NSError *_Nullable error) {
2670-
[self hideSpinner:^{
2671-
if (error) {
2672-
[self logFailure:@"failed to send verification code" error:error];
2673-
[self showMessagePrompt:error.localizedDescription];
2679+
- (void)linkPhoneNumber:(NSString *_Nullable)phoneNumber
2680+
completion:(void(^)(NSError *_Nullable))completion{
2681+
[self showSpinner:^{
2682+
[[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
2683+
UIDelegate:nil
2684+
completion:^(NSString *_Nullable verificationID,
2685+
NSError *_Nullable error) {
2686+
[self hideSpinner:^{
2687+
if (error) {
2688+
[self logFailure:@"failed to send verification code" error:error];
2689+
[self showMessagePrompt:error.localizedDescription];
2690+
completion(error);
2691+
return;
2692+
}
2693+
[self logSuccess:@"Code sent"];
2694+
2695+
[self showTextInputPromptWithMessage:@"Verification code:"
2696+
keyboardType:UIKeyboardTypeNumberPad
2697+
completionBlock:^(BOOL userPressedOK,
2698+
NSString *_Nullable verificationCode) {
2699+
if (!userPressedOK || !verificationCode.length) {
26742700
return;
26752701
}
2676-
[self logSuccess:@"Code sent"];
2677-
2678-
[self showTextInputPromptWithMessage:@"Verification code:"
2679-
keyboardType:UIKeyboardTypeNumberPad
2680-
completionBlock:^(BOOL userPressedOK,
2681-
NSString *_Nullable verificationCode) {
2682-
if (!userPressedOK || !verificationCode.length) {
2683-
return;
2684-
}
2685-
[self showSpinner:^{
2686-
FIRPhoneAuthCredential *credential =
2687-
[[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
2688-
verificationCode:verificationCode];
2689-
[[self user] linkWithCredential:credential
2690-
completion:^(FIRUser *_Nullable user,
2691-
NSError *_Nullable error) {
2692-
[self hideSpinner:^{
2693-
if (error) {
2694-
if (error.code == FIRAuthErrorCodeCredentialAlreadyInUse) {
2695-
[self showMessagePromptWithTitle:@"Phone number is already linked to "
2696-
@"another user"
2697-
message:@"Tap Ok to sign in with that user now."
2698-
showCancelButton:YES
2699-
completion:^(BOOL userPressedOK,
2700-
NSString *_Nullable userInput) {
2701-
if (userPressedOK) {
2702-
// If FIRAuthErrorCodeCredentialAlreadyInUse error, sign in with the
2703-
// provided credential.
2704-
[self showSpinner:^{
2705-
FIRPhoneAuthCredential *credential =
2706-
error.userInfo[FIRAuthUpdatedCredentialKey];
2707-
[[AppManager auth] signInWithCredential:credential
2708-
completion:^(FIRUser *_Nullable user,
2709-
NSError *_Nullable error) {
2710-
[self hideSpinner:^{
2711-
if (error) {
2712-
[self logFailure:@"failed to verify phone number" error:error];
2713-
[self showMessagePrompt:error.localizedDescription];
2714-
return;
2715-
}
2716-
}];
2702+
[self showSpinner:^{
2703+
FIRPhoneAuthCredential *credential =
2704+
[[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
2705+
verificationCode:verificationCode];
2706+
[[self user] linkWithCredential:credential
2707+
completion:^(FIRUser *_Nullable user,
2708+
NSError *_Nullable error) {
2709+
[self hideSpinner:^{
2710+
if (error) {
2711+
if (error.code == FIRAuthErrorCodeCredentialAlreadyInUse) {
2712+
[self showMessagePromptWithTitle:@"Phone number is already linked to "
2713+
@"another user"
2714+
message:@"Tap Ok to sign in with that user now."
2715+
showCancelButton:YES
2716+
completion:^(BOOL userPressedOK,
2717+
NSString *_Nullable userInput) {
2718+
if (userPressedOK) {
2719+
// If FIRAuthErrorCodeCredentialAlreadyInUse error, sign in with the
2720+
// provided credential.
2721+
[self showSpinner:^{
2722+
FIRPhoneAuthCredential *credential =
2723+
error.userInfo[FIRAuthUpdatedCredentialKey];
2724+
[[AppManager auth] signInWithCredential:credential
2725+
completion:^(FIRUser *_Nullable user,
2726+
NSError *_Nullable error) {
2727+
[self hideSpinner:^{
2728+
if (error) {
2729+
[self logFailure:@"failed to verify phone number" error:error];
2730+
[self showMessagePrompt:error.localizedDescription];
2731+
completion(error);
2732+
return;
2733+
}
27172734
}];
27182735
}];
2719-
}
2720-
}];
2721-
} else {
2722-
[self logFailure:@"link phone number failed" error:error];
2723-
[self showMessagePrompt:error.localizedDescription];
2724-
}
2725-
return;
2736+
}];
2737+
}
2738+
}];
2739+
} else {
2740+
[self logFailure:@"link phone number failed" error:error];
2741+
[self showMessagePrompt:error.localizedDescription];
27262742
}
2727-
[self logSuccess:@"link phone number succeeded."];
2728-
if (completion) completion();
2729-
}];
2743+
completion(error);
2744+
return;
2745+
}
2746+
[self logSuccess:@"link phone number succeeded."];
2747+
if (completion) {
2748+
completion(nil);
2749+
}
27302750
}];
27312751
}];
27322752
}];
27332753
}];
27342754
}];
27352755
}];
2756+
}];
27362757
}
27372758

27382759
/** @fn linkPhoneNumber
2739-
@brief Allows linking a verified phone number to the currently signed user.
2760+
@brief Allows linking a verified phone number to the currently signed user via popup prompt.
27402761
*/
2741-
- (void)linkPhoneNumber {
2762+
- (void)linkPhoneNumberPrompt {
27422763
[self showTextInputPromptWithMessage:@"Phone #:"
27432764
keyboardType:UIKeyboardTypePhonePad
27442765
completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
27452766
if (!userPressedOK || !phoneNumber.length) {
27462767
return;
27472768
}
2748-
[self linkPhoneNumberWithString:phoneNumber completion:nil];
2769+
[self linkPhoneNumber:phoneNumber completion:nil];
27492770
}];
27502771
}
27512772

0 commit comments

Comments
 (0)