27
27
let kOneSecondFactorUserPassword = " aaaaaa "
28
28
29
29
class PhoneMultiFactorTests : FIRAuthApiTestsBase {
30
-
31
30
func testEnrollUnenroll( ) {
32
- let enrollExpectation = self . expectation ( description: " Enroll phone multi factor finished. " )
33
- let unenrollExpectation = self . expectation ( description: " Unenroll phone multi factor finished. " )
34
- Auth . auth ( ) . signIn ( withEmail: kNoSecondFactorUserEmail, password: kNoSecondFactorUserPassword) { ( result, error) in
31
+ let enrollExpectation = expectation ( description: " Enroll phone multi factor finished. " )
32
+ let unenrollExpectation = expectation ( description: " Unenroll phone multi factor finished. " )
33
+ Auth . auth ( ) . signIn ( withEmail: kNoSecondFactorUserEmail, password: kNoSecondFactorUserPassword) { result, error in
35
34
XCTAssertNil ( error, " User normal sign in failed. Error: \( error!. localizedDescription) " )
36
35
37
36
// Enroll
38
37
guard let user = result? . user else {
39
38
XCTFail ( " No valid user after attempted sign-in. " )
40
39
}
41
- user. multiFactor. getSessionWithCompletion ( { ( session, error) in
40
+ user. multiFactor. getSessionWithCompletion { session, error in
42
41
XCTAssertNil ( error, " Get multi factor session failed. Error: \( error!. localizedDescription) " )
43
42
PhoneAuthProvider . provider ( ) . verifyPhoneNumber (
44
43
kPhoneSecondFactorPhoneNumber,
45
44
uiDelegate: nil ,
46
- multiFactorSession: session) { ( verificationId, error) in
47
- XCTAssertNil ( error, " Verify phone number failed. Error: \( error!. localizedDescription) " )
48
- let credential = PhoneAuthProvider . provider ( ) . credential (
49
- withVerificationID: verificationId!,
50
- verificationCode: kPhoneSecondFactorVerificationCode)
51
- let assertion = PhoneMultiFactorGenerator . assertion ( with: credential) ;
52
- user? . multiFactor. enroll ( with: assertion, displayName: kPhoneSecondFactorDisplayName) { ( error) in
53
- XCTAssertNil ( error, " Phone multi factor enroll failed. Error: \( error!. localizedDescription) " )
54
- XCTAssertEqual ( Auth . auth ( ) . currentUser? . multiFactor. enrolledFactors. first? . displayName, kPhoneSecondFactorDisplayName)
55
- enrollExpectation. fulfill ( )
45
+ multiFactorSession: session
46
+ ) { verificationId, error in
47
+ XCTAssertNil ( error, " Verify phone number failed. Error: \( error!. localizedDescription) " )
48
+ let credential = PhoneAuthProvider . provider ( ) . credential (
49
+ withVerificationID: verificationId!,
50
+ verificationCode: kPhoneSecondFactorVerificationCode
51
+ )
52
+ let assertion = PhoneMultiFactorGenerator . assertion ( with: credential)
53
+ user? . multiFactor. enroll ( with: assertion, displayName: kPhoneSecondFactorDisplayName) { error in
54
+ XCTAssertNil ( error, " Phone multi factor enroll failed. Error: \( error!. localizedDescription) " )
55
+ XCTAssertEqual ( Auth . auth ( ) . currentUser? . multiFactor. enrolledFactors. first? . displayName, kPhoneSecondFactorDisplayName)
56
+ enrollExpectation. fulfill ( )
56
57
57
- // Unenroll
58
- user = Auth . auth ( ) . currentUser
59
- user? . multiFactor. unenroll ( with: ( user? . multiFactor. enrolledFactors. first) !, completion: { ( error) in
60
- XCTAssertNil ( error, " Phone multi factor unenroll failed. Error: \( error!. localizedDescription) " )
61
- XCTAssertEqual ( Auth . auth ( ) . currentUser? . multiFactor. enrolledFactors. count, 0 )
62
- unenrollExpectation. fulfill ( )
63
- } )
64
- }
58
+ // Unenroll
59
+ user = Auth . auth ( ) . currentUser
60
+ user? . multiFactor. unenroll ( with: ( user? . multiFactor. enrolledFactors. first) !, completion: { error in
61
+ XCTAssertNil ( error, " Phone multi factor unenroll failed. Error: \( error!. localizedDescription) " )
62
+ XCTAssertEqual ( Auth . auth ( ) . currentUser? . multiFactor. enrolledFactors. count, 0 )
63
+ unenrollExpectation. fulfill ( )
64
+ } )
65
+ }
65
66
}
66
- } )
67
+ }
67
68
}
68
69
69
- self . waitForExpectations ( timeout: 30 ) { error in
70
+ waitForExpectations ( timeout: 30 ) { error in
70
71
XCTAssertNil ( error, " Failed to wait for enroll and unenroll phone multi factor finished. Error: \( error!. localizedDescription) " )
71
72
}
72
73
}
73
74
74
75
func testSignInWithSecondFactor( ) {
75
- let signInExpectation = self . expectation ( description: " Sign in with phone multi factor finished. " )
76
- Auth . auth ( ) . signIn ( withEmail: kOneSecondFactorUserEmail, password: kOneSecondFactorUserPassword) { ( result, error) in
76
+ let signInExpectation = expectation ( description: " Sign in with phone multi factor finished. " )
77
+ Auth . auth ( ) . signIn ( withEmail: kOneSecondFactorUserEmail, password: kOneSecondFactorUserPassword) { result, error in
77
78
// SignIn
78
79
guard let error = error, error. code == AuthErrorCode . secondFactorRequired. rawValue else {
79
80
XCTFail ( " User sign in returns wrong error. Error: \( error!. localizedDescription) " )
@@ -83,20 +84,22 @@ class PhoneMultiFactorTests: FIRAuthApiTestsBase {
83
84
PhoneAuthProvider . provider ( ) . verifyPhoneNumber (
84
85
with: hint,
85
86
uiDelegate: nil ,
86
- multiFactorSession: resolver. session) { ( verificationId, error) in
87
- XCTAssertNil ( error, " Failed to verify phone number. Error: \( error!. localizedDescription) " )
88
- let credential = PhoneAuthProvider . provider ( ) . credential (
89
- withVerificationID: verificationId!,
90
- verificationCode: kPhoneSecondFactorVerificationCode)
91
- let assertion = PhoneMultiFactorGenerator . assertion ( with: credential) ;
92
- resolver. resolveSignIn ( with: assertion) { ( authResult, error) in
93
- XCTAssertNil ( error, " Failed to sign in with phone multi factor. Error: \( error!. localizedDescription) " )
94
- signInExpectation. fulfill ( )
95
- }
87
+ multiFactorSession: resolver. session
88
+ ) { verificationId, error in
89
+ XCTAssertNil ( error, " Failed to verify phone number. Error: \( error!. localizedDescription) " )
90
+ let credential = PhoneAuthProvider . provider ( ) . credential (
91
+ withVerificationID: verificationId!,
92
+ verificationCode: kPhoneSecondFactorVerificationCode
93
+ )
94
+ let assertion = PhoneMultiFactorGenerator . assertion ( with: credential)
95
+ resolver. resolveSignIn ( with: assertion) { authResult, error in
96
+ XCTAssertNil ( error, " Failed to sign in with phone multi factor. Error: \( error!. localizedDescription) " )
97
+ signInExpectation. fulfill ( )
98
+ }
96
99
}
97
100
}
98
101
99
- self . waitForExpectations ( timeout: 300 ) { error in
102
+ waitForExpectations ( timeout: 300 ) { error in
100
103
XCTAssertNil ( error, " Failed to wait for enroll and unenroll phone multi factor finished. Error: \( error!. localizedDescription) " )
101
104
}
102
105
}
0 commit comments