PhoneMultiFactorTests.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 FirebaseAuth
  17. import XCTest
  18. let kNoSecondFactorUserEmail = "iosapitests+no_second_factor@gmail.com"
  19. let kNoSecondFactorUserPassword = "aaaaaa"
  20. let kPhoneSecondFactorPhoneNumber = "+16509999999"
  21. let kPhoneSecondFactorVerificationCode = "123456"
  22. let kPhoneSecondFactorDisplayName = "phone1"
  23. let kOneSecondFactorUserEmail = "iosapitests+one_phone_second_factor@gmail.com"
  24. let kOneSecondFactorUserPassword = "aaaaaa"
  25. // TODO: Restore these tests that haven't been built or run for years before Swift conversion.
  26. class PhoneMultiFactorTests: TestsBase {
  27. func SKIPtestEnrollUnenroll() {
  28. let enrollExpectation = expectation(description: "Enroll phone multi factor finished.")
  29. let unenrollExpectation = expectation(description: "Unenroll phone multi factor finished.")
  30. Auth.auth()
  31. .signIn(withEmail: kNoSecondFactorUserEmail,
  32. password: kNoSecondFactorUserPassword) { result, error in
  33. XCTAssertNil(error, "User normal sign in failed. Error: \(error!.localizedDescription)")
  34. // Enroll
  35. guard let user = result?.user else {
  36. XCTFail("No valid user after attempted sign-in.")
  37. return
  38. }
  39. user.multiFactor.getSessionWithCompletion { session, error in
  40. XCTAssertNil(error,
  41. "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
  54. .enroll(with: assertion, displayName: kPhoneSecondFactorDisplayName) { error in
  55. XCTAssertNil(error,
  56. "Phone multi factor enroll failed. Error: \(error!.localizedDescription)")
  57. XCTAssertEqual(
  58. Auth.auth().currentUser?.multiFactor.enrolledFactors.first?.displayName,
  59. kPhoneSecondFactorDisplayName
  60. )
  61. enrollExpectation.fulfill()
  62. // Unenroll
  63. XCTAssertEqual(user, Auth.auth().currentUser)
  64. user.multiFactor
  65. .unenroll(with: (user.multiFactor.enrolledFactors.first)!, completion: { error in
  66. XCTAssertNil(error,
  67. "Phone multi factor unenroll failed. Error: \(error!.localizedDescription)")
  68. XCTAssertEqual(Auth.auth().currentUser?.multiFactor.enrolledFactors.count, 0)
  69. unenrollExpectation.fulfill()
  70. })
  71. }
  72. }
  73. }
  74. }
  75. waitForExpectations(timeout: 30) { error in
  76. XCTAssertNil(error,
  77. "Failed to wait for enroll and unenroll phone multi factor finished. Error: \(error!.localizedDescription)")
  78. }
  79. }
  80. func SKIPtestSignInWithSecondFactor() {
  81. let signInExpectation = expectation(description: "Sign in with phone multi factor finished.")
  82. Auth.auth()
  83. .signIn(withEmail: kOneSecondFactorUserEmail,
  84. password: kOneSecondFactorUserPassword) { result, error in
  85. // SignIn
  86. guard let error = error as? NSError,
  87. error.code == AuthErrorCode.secondFactorRequired.rawValue else {
  88. XCTFail("User sign in returns wrong error. Error: \(error!.localizedDescription)")
  89. return
  90. }
  91. let resolver = error
  92. .userInfo["FIRAuthErrorUserInfoMultiFactorResolverKey"] as! MultiFactorResolver
  93. let hint = resolver.hints.first as! PhoneMultiFactorInfo
  94. PhoneAuthProvider.provider().verifyPhoneNumber(
  95. with: hint,
  96. uiDelegate: nil,
  97. multiFactorSession: resolver.session
  98. ) { verificationId, error in
  99. XCTAssertNil(error,
  100. "Failed to verify phone number. Error: \(error!.localizedDescription)")
  101. let credential = PhoneAuthProvider.provider().credential(
  102. withVerificationID: verificationId!,
  103. verificationCode: kPhoneSecondFactorVerificationCode
  104. )
  105. let assertion = PhoneMultiFactorGenerator.assertion(with: credential)
  106. resolver.resolveSignIn(with: assertion) { authResult, error in
  107. XCTAssertNil(error,
  108. "Failed to sign in with phone multi factor. Error: \(error!.localizedDescription)")
  109. signInExpectation.fulfill()
  110. }
  111. }
  112. }
  113. waitForExpectations(timeout: 300) { error in
  114. XCTAssertNil(error,
  115. "Failed to wait for enroll and unenroll phone multi factor finished. Error: \(error!.localizedDescription)")
  116. }
  117. }
  118. }