Skip to content

Commit 2643598

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

File tree

1 file changed

+164
-78
lines changed

1 file changed

+164
-78
lines changed

Example/Auth/Sample/MainViewController.m

Lines changed: 164 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,11 @@
428428
*/
429429
static NSString *const kAutoAccountLinking = @"Link with Google";
430430

431+
/** @var kAutoPhoneNumberSignIn
432+
@brief The button title for automated account linking.
433+
*/
434+
static NSString *const kAutoPhoneNumberSignIn = @"Sign in With Phone Number";
435+
431436
/** @var kGitHubSignInButtonText
432437
@brief The button title for signing in with github.
433438
*/
@@ -671,7 +676,7 @@ - (void)updateTable {
671676
]],
672677
[StaticContentTableViewSection sectionWithTitle:kPhoneAuthSectionTitle cells:@[
673678
[StaticContentTableViewCell cellWithTitle:kPhoneNumberSignInReCaptchaTitle
674-
action:^{ [weakSelf signInWithPhoneNumberRecaptcha]; }],
679+
action:^{ [weakSelf signInWithPhoneNumberRecaptcha];}],
675680
[StaticContentTableViewCell cellWithTitle:kPhoneNumberSignInTitle
676681
action:^{ [weakSelf signInWithPhoneNumber]; }],
677682
[StaticContentTableViewCell cellWithTitle:kUpdatePhoneNumber
@@ -680,7 +685,7 @@ - (void)updateTable {
680685
action:^{ [weakSelf linkPhoneNumber]; }],
681686
[StaticContentTableViewCell cellWithTitle:kUnlinkPhoneNumber
682687
action:^{
683-
[weakSelf unlinkFromProvider:FIRPhoneAuthProviderID];
688+
[weakSelf unlinkFromProvider:FIRPhoneAuthProviderID completion:nil];
684689
}],
685690
]],
686691
[StaticContentTableViewSection sectionWithTitle:kSectionTitleSignIn cells:@[
@@ -775,15 +780,15 @@ - (void)updateTable {
775780
action:^{ [weakSelf linkWithEmailPassword]; }],
776781
[StaticContentTableViewCell cellWithTitle:kUnlinkFromGoogle
777782
action:^{
778-
[weakSelf unlinkFromProvider:FIRGoogleAuthProviderID];
783+
[weakSelf unlinkFromProvider:FIRGoogleAuthProviderID completion:nil];
779784
}],
780785
[StaticContentTableViewCell cellWithTitle:kUnlinkFromFacebook
781786
action:^{
782-
[weakSelf unlinkFromProvider:FIRFacebookAuthProviderID];
787+
[weakSelf unlinkFromProvider:FIRGoogleAuthProviderID completion:nil];
783788
}],
784789
[StaticContentTableViewCell cellWithTitle:kUnlinkFromEmailPassword
785790
action:^{
786-
[weakSelf unlinkFromProvider:FIREmailAuthProviderID];
791+
[weakSelf unlinkFromProvider:FIRGoogleAuthProviderID completion:nil];
787792
}]
788793
]],
789794
[StaticContentTableViewSection sectionWithTitle:kSectionTitleApp cells:@[
@@ -818,7 +823,9 @@ - (void)updateTable {
818823
[StaticContentTableViewCell cellWithTitle:kAutoSignInAnonymously
819824
action:^{ [weakSelf automatedAnonymousSignIn]; }],
820825
[StaticContentTableViewCell cellWithTitle:kAutoAccountLinking
821-
action:^{ [weakSelf automatedAccountLinking]; }]
826+
action:^{ [weakSelf automatedAccountLinking]; }],
827+
[StaticContentTableViewCell cellWithTitle:kAutoPhoneNumberSignIn
828+
action:^{ [weakSelf automatedPhoneNumberSignIn]; }]
822829
]]
823830
]];
824831
}
@@ -1032,6 +1039,56 @@ - (void)automatedSignInFacebook {
10321039
}];
10331040
}
10341041

1042+
/** @fn automatedPhoneNumberSignIn
1043+
@brief Automatically executes the manual test for sign-in with phone number.
1044+
*/
1045+
- (void)automatedPhoneNumberSignIn {
1046+
[self log:@"Automated phone number sign in"];
1047+
FIRAuth *auth = [AppManager auth];
1048+
if (!auth) {
1049+
[self logFailedTest:@"Could not obtain auth object."];
1050+
return;
1051+
}
1052+
[auth signOut:NULL];
1053+
[self log:@"INITIATING AUTOMATED MANUAL TEST FOR PHONE NUMBER SIGN IN:"];
1054+
[self commonPhoneNumberInputWithTitle:@"Phone for automation"
1055+
Completion:^(NSString *_Nullable phone) {
1056+
[self signInWithPhoneNumberRecaptchaWithString:phone completion:^(NSError *error){
1057+
if (error) {
1058+
[self logFailedTest:@"Could not sign in with phone number reCAPTCHA."];
1059+
}
1060+
[self logSuccess:@"sign-in with phone number reCAPTCHA test succeeded."];
1061+
[auth signOut:NULL];
1062+
[self signInWithPhoneNumberRecaptchaWithString:phone completion:^(NSError *error){
1063+
if (error) {
1064+
[self logFailedTest:@"Could not sign in with phone number reCAPTCHA."];
1065+
}
1066+
[self logSuccess:@"second sign-in with phone number reCAPTCHA test succeeded."];
1067+
[self updatePhoneNumberWithString:phone completion:^{
1068+
if (error) {
1069+
[self logFailedTest:@"Could not update phone number."];
1070+
}
1071+
[self logSuccess:@"update phone number test succeeded."];
1072+
[self unlinkFromProvider:FIRPhoneAuthProviderID completion:^ {
1073+
if (error) {
1074+
[self logFailedTest:@"Could not unlink phone number."];
1075+
}
1076+
[self logSuccess:@"unlink phone number test succeeded."];
1077+
[self linkPhoneNumberWithString:phone completion:^{
1078+
if (error) {
1079+
[self logFailedTest:@"Could not link phone number."];
1080+
}
1081+
[self logSuccess:@"link phone number test succeeded."];
1082+
[self log:@"FINISHED AUTOMATED MANUAL TEST FOR PHONE NUMBER SIGN IN."];
1083+
}];
1084+
}];
1085+
}];
1086+
}];
1087+
}];
1088+
}];
1089+
}
1090+
1091+
10351092
/** @fn automatedEmailSignUp
10361093
@brief Automatically executes the manual test for sign-up with email/password.
10371094
*/
@@ -2050,14 +2107,15 @@ - (void)showEmailPasswordDialogWithCompletion:(ShowEmailPasswordDialogCompletion
20502107
@brief Unlinks the current user from the provider with the specified provider ID.
20512108
@param provider The provider ID of the provider to unlink the current user's account from.
20522109
*/
2053-
- (void)unlinkFromProvider:(NSString *)provider {
2110+
- (void)unlinkFromProvider:(NSString *)provider completion:(void(^)(void))completion {
20542111
[[self user] unlinkFromProvider:provider
20552112
completion:^(FIRUser *_Nullable user,
20562113
NSError *_Nullable error) {
20572114
if (error) {
20582115
[self logFailure:@"unlink auth provider failed" error:error];
20592116
} else {
20602117
[self logSuccess:@"unlink auth provider succeeded."];
2118+
if (completion) completion();
20612119
}
20622120
[self showTypicalUIForUserUpdateResultsWithTitle:kUnlinkTitle error:error];
20632121
}];
@@ -2458,32 +2516,41 @@ - (void)signInWithPhoneNumber {
24582516
}];
24592517
}
24602518

2519+
/** @fn signInWithPhoneNumberRecaptchaWithString
2520+
@brief Allows sign in with phone number using reCAPTCHA and completion.
2521+
@param phoneNumber Number pass in string for automation.
2522+
*/
2523+
- (void)signInWithPhoneNumberRecaptchaWithString:(NSString *_Nullable)phoneNumber
2524+
completion:(void(^)(NSError *_Nullable))completion {
2525+
[self showSpinner:^{
2526+
[[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
2527+
UIDelegate:nil
2528+
completion:^(NSString *_Nullable verificationID,
2529+
NSError *_Nullable error) {
2530+
[self hideSpinner:^{
2531+
if (error) {
2532+
[self logFailure:@"failed to send verification code" error:error];
2533+
[self showMessagePrompt:error.localizedDescription];
2534+
return;
2535+
}
2536+
[self logSuccess:@"Code sent"];
2537+
[self commonPhoneNumberInputWithTitle:@"Code"
2538+
Completion:^(NSString *_Nullable verificationCode) {
2539+
[self commontPhoneVerificationWithVerificationID:verificationID
2540+
verificationCode:verificationCode];
2541+
if (completion) completion(nil);
2542+
}];
2543+
}];
2544+
}];
2545+
}];
2546+
}
2547+
24612548
/** @fn signInWithPhoneNumberRecaptcha
24622549
@brief Allows sign in with phone number using reCAPTCHA
24632550
*/
24642551
- (void)signInWithPhoneNumberRecaptcha {
24652552
[self commonPhoneNumberInputWithTitle:@"Phone #" Completion:^(NSString *_Nullable phone) {
2466-
[self showSpinner:^{
2467-
[[AppManager phoneAuthProvider] verifyPhoneNumber:phone
2468-
UIDelegate:nil
2469-
completion:^(NSString *_Nullable verificationID,
2470-
NSError *_Nullable error) {
2471-
[self hideSpinner:^{
2472-
if (error) {
2473-
[self logFailure:@"failed to send verification code" error:error];
2474-
[self showMessagePrompt:error.localizedDescription];
2475-
return;
2476-
}
2477-
[self logSuccess:@"Code sent"];
2478-
2479-
[self commonPhoneNumberInputWithTitle:@"Code"
2480-
Completion:^(NSString *_Nullable verificationCode) {
2481-
[self commontPhoneVerificationWithVerificationID:verificationID
2482-
verificationCode:verificationCode];
2483-
}];
2484-
}];
2485-
}];
2486-
}];
2553+
[self signInWithPhoneNumberRecaptchaWithString:(phone) completion:nil];
24872554
}];
24882555
}
24892556

@@ -2502,7 +2569,6 @@ - (void)commonPhoneNumberInputWithTitle:(NSString *)title
25022569
completion(phoneNumber);
25032570
}];
25042571
}
2505-
25062572
/** @fn commonPhoneNumberInputWithLabel:Completion
25072573
@brief Finishes the phone number verification flow.
25082574
@param verificationID The verificationID from the backend.
@@ -2528,69 +2594,75 @@ - (void)commontPhoneVerificationWithVerificationID:(NSString *)verificationID
25282594
}];
25292595
}
25302596

2531-
/** @fn updatePhoneNumber
2597+
/** @fn updatePhoneNumberWithString
25322598
@brief Allows adding a verified phone number to the currently signed user.
2599+
@param phoneNumber Number pass in string for automation.
25332600
*/
2534-
- (void)updatePhoneNumber {
2535-
[self showTextInputPromptWithMessage:@"Phone #:"
2536-
keyboardType:UIKeyboardTypePhonePad
2537-
completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
2538-
if (!userPressedOK || !phoneNumber.length) {
2539-
return;
2540-
}
2541-
[self showSpinner:^{
2542-
[[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
2543-
UIDelegate:nil
2544-
completion:^(NSString *_Nullable verificationID,
2545-
NSError *_Nullable error) {
2546-
if (error) {
2547-
[self logFailure:@"failed to send verification code" error:error];
2548-
[self showMessagePrompt:error.localizedDescription];
2549-
return;
2550-
}
2551-
[self logSuccess:@"Code sent"];
2552-
2553-
[self showTextInputPromptWithMessage:@"Verification code:"
2554-
keyboardType:UIKeyboardTypeNumberPad
2555-
completionBlock:^(BOOL userPressedOK,
2556-
NSString *_Nullable verificationCode) {
2557-
if (!userPressedOK || !verificationCode.length) {
2558-
return;
2559-
}
2560-
[self showSpinner:^{
2561-
FIRPhoneAuthCredential *credential =
2562-
[[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
2563-
verificationCode:verificationCode];
2564-
[[self user] updatePhoneNumberCredential:credential
2565-
completion:^(NSError *_Nullable error) {
2566-
if (error) {
2567-
[self logFailure:@"update phone number failed" error:error];
2568-
[self showMessagePrompt:error.localizedDescription];
2569-
} else {
2570-
[self logSuccess:@"update phone number succeeded."];
2571-
}
2572-
}];
2573-
}];
2574-
}];
2575-
[self hideSpinner:^{
2576-
[self showTypicalUIForUserUpdateResultsWithTitle:kCreateUserTitle error:error];
2601+
- (void)updatePhoneNumberWithString:(NSString *_Nullable)phoneNumber
2602+
completion:(void(^)(void))completion{
2603+
[self showSpinner:^{
2604+
[[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
2605+
UIDelegate:nil
2606+
completion:^(NSString *_Nullable verificationID,
2607+
NSError *_Nullable error) {
2608+
if (error) {
2609+
[self logFailure:@"failed to send verification code" error:error];
2610+
[self showMessagePrompt:error.localizedDescription];
2611+
return;
2612+
}
2613+
[self logSuccess:@"Code sent"];
2614+
2615+
[self showTextInputPromptWithMessage:@"Verification code:"
2616+
keyboardType:UIKeyboardTypeNumberPad
2617+
completionBlock:^(BOOL userPressedOK,
2618+
NSString *_Nullable verificationCode) {
2619+
if (!userPressedOK || !verificationCode.length) {
2620+
return;
2621+
}
2622+
[self showSpinner:^{
2623+
FIRPhoneAuthCredential *credential =
2624+
[[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
2625+
verificationCode:verificationCode];
2626+
[[self user] updatePhoneNumberCredential:credential
2627+
completion:^(NSError *_Nullable error) {
2628+
if (error) {
2629+
[self logFailure:@"update phone number failed" error:error];
2630+
[self showMessagePrompt:error.localizedDescription];
2631+
} else {
2632+
[self logSuccess:@"update phone number succeeded."];
2633+
if (completion) completion();
2634+
}
25772635
}];
25782636
}];
25792637
}];
2638+
[self hideSpinner:^{
2639+
[self showTypicalUIForUserUpdateResultsWithTitle:kCreateUserTitle error:error];
2640+
}];
25802641
}];
2642+
}];
25812643
}
25822644

2583-
/** @fn linkPhoneNumber
2584-
@brief Allows linking a verified phone number to the currently signed user.
2645+
/** @fn updatePhoneNumber
2646+
@brief Allows adding a verified phone number to the currently signed user via popup prompt.
25852647
*/
2586-
- (void)linkPhoneNumber {
2587-
[self showTextInputPromptWithMessage:@"Phone #:"
2648+
- (void)updatePhoneNumber {
2649+
[self showTextInputPromptWithMessage:@"Update Phone #:"
25882650
keyboardType:UIKeyboardTypePhonePad
25892651
completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
25902652
if (!userPressedOK || !phoneNumber.length) {
25912653
return;
25922654
}
2593-
[self showSpinner:^{
2655+
[self updatePhoneNumberWithString:phoneNumber completion:nil];
2656+
}];
2657+
}
2658+
2659+
/** @fn linkPhoneNumberWithString
2660+
@brief Allows linking a verified phone number to the currently signed user.
2661+
@param phoneNumber Number pass in string for automation.
2662+
*/
2663+
- (void)linkPhoneNumberWithString:(NSString *_Nullable)phoneNumber
2664+
completion:(void(^)(void))completion{
2665+
[self showSpinner:^{
25942666
[[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
25952667
UIDelegate:nil
25962668
completion:^(NSString *_Nullable verificationID,
@@ -2653,13 +2725,27 @@ - (void)linkPhoneNumber {
26532725
return;
26542726
}
26552727
[self logSuccess:@"link phone number succeeded."];
2728+
if (completion) completion();
26562729
}];
26572730
}];
26582731
}];
26592732
}];
26602733
}];
26612734
}];
26622735
}];
2736+
}
2737+
2738+
/** @fn linkPhoneNumber
2739+
@brief Allows linking a verified phone number to the currently signed user.
2740+
*/
2741+
- (void)linkPhoneNumber {
2742+
[self showTextInputPromptWithMessage:@"Phone #:"
2743+
keyboardType:UIKeyboardTypePhonePad
2744+
completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
2745+
if (!userPressedOK || !phoneNumber.length) {
2746+
return;
2747+
}
2748+
[self linkPhoneNumberWithString:phoneNumber completion:nil];
26632749
}];
26642750
}
26652751

0 commit comments

Comments
 (0)