PhoneMultiFactorTests.swift 5.0 KB

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