PhoneMultiFactorTests.swift 4.9 KB

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