PhoneMultiFactorTests.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import XCTest
  17. let kNoSecondFactorUserEmail = "iosapitests+no_second_factor@gmail.com"
  18. let kNoSecondFactorUserPassword = "aaaaaa"
  19. let kPhoneSecondFactorPhoneNumber = "+16509999999"
  20. let kPhoneSecondFactorVerificationCode = "123456"
  21. let kPhoneSecondFactorDisplayName = "phone1"
  22. let kOneSecondFactorUserEmail = "iosapitests+one_phone_second_factor@gmail.com"
  23. let kOneSecondFactorUserPassword = "aaaaaa"
  24. class PhoneMultiFactorTests: FIRAuthApiTestsBase {
  25. func testEnrollUnenroll() {
  26. let enrollExpectation = expectation(description: "Enroll phone multi factor finished.")
  27. let unenrollExpectation = expectation(description: "Unenroll phone multi factor finished.")
  28. Auth.auth()
  29. .signIn(withEmail: kNoSecondFactorUserEmail,
  30. password: kNoSecondFactorUserPassword) { result, error in
  31. XCTAssertNil(error, "User normal sign in failed. Error: \(error!.localizedDescription)")
  32. // Enroll
  33. guard let user = result?.user else {
  34. XCTFail("No valid user after attempted sign-in.")
  35. }
  36. user.multiFactor.getSessionWithCompletion { session, error in
  37. XCTAssertNil(error,
  38. "Get multi factor session failed. Error: \(error!.localizedDescription)")
  39. PhoneAuthProvider.provider().verifyPhoneNumber(
  40. kPhoneSecondFactorPhoneNumber,
  41. uiDelegate: nil,
  42. multiFactorSession: session
  43. ) { verificationId, error in
  44. XCTAssertNil(error, "Verify phone number failed. Error: \(error!.localizedDescription)")
  45. let credential = PhoneAuthProvider.provider().credential(
  46. withVerificationID: verificationId!,
  47. verificationCode: kPhoneSecondFactorVerificationCode
  48. )
  49. let assertion = PhoneMultiFactorGenerator.assertion(with: credential)
  50. user?.multiFactor
  51. .enroll(with: assertion, displayName: kPhoneSecondFactorDisplayName) { error in
  52. XCTAssertNil(error,
  53. "Phone multi factor enroll failed. Error: \(error!.localizedDescription)")
  54. XCTAssertEqual(Auth.auth().currentUser?.multiFactor.enrolledFactors.first?
  55. .displayName,
  56. kPhoneSecondFactorDisplayName)
  57. enrollExpectation.fulfill()
  58. // Unenroll
  59. user = Auth.auth().currentUser
  60. user?.multiFactor
  61. .unenroll(with: (user?.multiFactor.enrolledFactors.first)!, completion: { error in
  62. XCTAssertNil(error,
  63. "Phone multi factor unenroll failed. Error: \(error!.localizedDescription)")
  64. XCTAssertEqual(Auth.auth().currentUser?.multiFactor.enrolledFactors.count, 0)
  65. unenrollExpectation.fulfill()
  66. })
  67. }
  68. }
  69. }
  70. }
  71. waitForExpectations(timeout: 30) { error in
  72. XCTAssertNil(error,
  73. "Failed to wait for enroll and unenroll phone multi factor finished. Error: \(error!.localizedDescription)")
  74. }
  75. }
  76. func testSignInWithSecondFactor() {
  77. let signInExpectation = expectation(description: "Sign in with phone multi factor finished.")
  78. Auth.auth()
  79. .signIn(withEmail: kOneSecondFactorUserEmail,
  80. password: kOneSecondFactorUserPassword) { result, error in
  81. // SignIn
  82. guard let error = error, error.code == AuthErrorCode.secondFactorRequired.rawValue else {
  83. XCTFail("User sign in returns wrong error. Error: \(error!.localizedDescription)")
  84. }
  85. let resolver = error
  86. .userInfo["FIRAuthErrorUserInfoMultiFactorResolverKey"] as! MultiFactorResolver
  87. let hint = resolver.hints.first as! PhoneMultiFactorInfo
  88. PhoneAuthProvider.provider().verifyPhoneNumber(
  89. with: hint,
  90. uiDelegate: nil,
  91. multiFactorSession: resolver.session
  92. ) { verificationId, error in
  93. XCTAssertNil(error,
  94. "Failed to verify phone number. Error: \(error!.localizedDescription)")
  95. let credential = PhoneAuthProvider.provider().credential(
  96. withVerificationID: verificationId!,
  97. verificationCode: kPhoneSecondFactorVerificationCode
  98. )
  99. let assertion = PhoneMultiFactorGenerator.assertion(with: credential)
  100. resolver.resolveSignIn(with: assertion) { authResult, error in
  101. XCTAssertNil(error,
  102. "Failed to sign in with phone multi factor. Error: \(error!.localizedDescription)")
  103. signInExpectation.fulfill()
  104. }
  105. }
  106. }
  107. waitForExpectations(timeout: 300) { error in
  108. XCTAssertNil(error,
  109. "Failed to wait for enroll and unenroll phone multi factor finished. Error: \(error!.localizedDescription)")
  110. }
  111. }
  112. }