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