PhoneAuthProviderTests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import Combine
  16. import XCTest
  17. import FirebaseAuth
  18. import FirebaseAuthTestingSupport
  19. class PhoneAuthProviderTests: XCTestCase {
  20. fileprivate static let phoneNumber = "55555555"
  21. fileprivate static let invalidPhoneNumber = "555+!*55555"
  22. fileprivate static let verificationID = "verificationID"
  23. func testVerifyEmptyPhoneNumber() {
  24. // given
  25. let provider = PhoneAuthProviderFake()
  26. var cancellables = Set<AnyCancellable>()
  27. provider.verifyPhoneNumberHandler = { completion in
  28. completion(nil, FIRAuthErrorUtils.missingPhoneNumberError(withMessage: ""))
  29. }
  30. // Empty phone number is checked on the client side so no backend RPC is mocked.
  31. let expectation = self.expectation(description: #function)
  32. // When
  33. provider.verifyPhoneNumber("", uiDelegate: nil)
  34. .sink { completion in
  35. if case let .failure(error as NSError) = completion {
  36. XCTAssertEqual(error.code, AuthErrorCode.missingPhoneNumber.rawValue)
  37. expectation.fulfill()
  38. }
  39. } receiveValue: { verificationID in
  40. XCTFail("💥 result unexpected")
  41. }
  42. .store(in: &cancellables)
  43. // then
  44. wait(for: [expectation], timeout: expectationTimeout)
  45. }
  46. func testVerifyInvalidPhoneNumber() {
  47. // given
  48. let provider = PhoneAuthProviderFake()
  49. var cancellables = Set<AnyCancellable>()
  50. provider.verifyPhoneNumberHandler = { completion in
  51. completion(nil, FIRAuthErrorUtils.invalidPhoneNumberError(withMessage: ""))
  52. }
  53. let expectation = self.expectation(description: #function)
  54. // When
  55. provider.verifyPhoneNumber(Self.invalidPhoneNumber, uiDelegate: nil)
  56. .sink { completion in
  57. if case let .failure(error as NSError) = completion {
  58. XCTAssertEqual(error.code, AuthErrorCode.invalidPhoneNumber.rawValue)
  59. expectation.fulfill()
  60. }
  61. } receiveValue: { verificationID in
  62. XCTFail("💥 result unexpected")
  63. }
  64. .store(in: &cancellables)
  65. // then
  66. wait(for: [expectation], timeout: expectationTimeout)
  67. }
  68. func testVerifyPhoneNumber() {
  69. // given
  70. let provider = PhoneAuthProviderFake()
  71. var cancellables = Set<AnyCancellable>()
  72. provider.verifyPhoneNumberHandler = { completion in
  73. completion(Self.verificationID, nil)
  74. }
  75. let expectation = self.expectation(description: #function)
  76. // When
  77. provider.verifyPhoneNumber(Self.phoneNumber, uiDelegate: nil)
  78. .sink { completion in
  79. switch completion {
  80. case .finished:
  81. print("Finished")
  82. case let .failure(error):
  83. XCTFail("💥 Something went wrong: \(error)")
  84. }
  85. } receiveValue: { verificationID in
  86. XCTAssertEqual(verificationID, Self.verificationID)
  87. expectation.fulfill()
  88. }
  89. .store(in: &cancellables)
  90. // then
  91. wait(for: [expectation], timeout: expectationTimeout)
  92. }
  93. func testVerifyPhoneNumberInTestModeFailure() {
  94. // given
  95. let provider = PhoneAuthProviderFake()
  96. var cancellables = Set<AnyCancellable>()
  97. provider.verifyPhoneNumberHandler = { completion in
  98. let underlyingError = NSError(domain: "Test Error", code: 1, userInfo: nil)
  99. completion(nil, FIRAuthErrorUtils.networkError(withUnderlyingError: underlyingError))
  100. }
  101. let expectation = self.expectation(description: #function)
  102. // When
  103. provider.verifyPhoneNumber(Self.phoneNumber, uiDelegate: nil)
  104. .sink { completion in
  105. if case let .failure(error as NSError) = completion {
  106. XCTAssertEqual(error.code, AuthErrorCode.networkError.rawValue)
  107. expectation.fulfill()
  108. }
  109. } receiveValue: { verificationID in
  110. XCTFail("💥 result unexpected")
  111. }
  112. .store(in: &cancellables)
  113. // then
  114. wait(for: [expectation], timeout: expectationTimeout)
  115. }
  116. }