FetchSignInMethodsTests.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2020 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. class FetchSignInMethodsTests: XCTestCase {
  19. override class func setUp() {
  20. FirebaseApp.configureForTests()
  21. }
  22. override class func tearDown() {
  23. FirebaseApp.app()?.delete { success in
  24. if success {
  25. print("Shut down app successfully.")
  26. } else {
  27. print("💥 There was a problem when shutting down the app..")
  28. }
  29. }
  30. }
  31. override func setUp() {
  32. do {
  33. try Auth.auth().signOut()
  34. } catch {}
  35. }
  36. static let apiKey = Credentials.apiKey
  37. static let email = "johnnyappleseed@apple.com"
  38. static let emailLinkAuthSignInMethod = "emailLink"
  39. static let facebookAuthSignInMethod = "facebook.com"
  40. static let allSignInMethods = [
  41. FetchSignInMethodsTests.emailLinkAuthSignInMethod,
  42. FetchSignInMethodsTests.facebookAuthSignInMethod,
  43. ]
  44. class MockCreateAuthURIResponse: FIRCreateAuthURIResponse {
  45. override var signinMethods: [String]? { return FetchSignInMethodsTests.allSignInMethods }
  46. }
  47. class MockAuthBackend: AuthBackendImplementationMock {
  48. override func createAuthURI(_ request: FIRCreateAuthURIRequest,
  49. callback: @escaping FIRCreateAuthURIResponseCallback) {
  50. XCTAssertEqual(request.identifier, FetchSignInMethodsTests.email)
  51. XCTAssertNotNil(request.endpoint)
  52. XCTAssertEqual(request.apiKey, FetchSignInMethodsTests.apiKey)
  53. callback(MockCreateAuthURIResponse(), nil)
  54. }
  55. }
  56. func testFetchSignInMethodsForEmail() {
  57. // given
  58. FIRAuthBackend.setBackendImplementation(MockAuthBackend())
  59. var cancellables = Set<AnyCancellable>()
  60. let fetchSignInMethodsExpectation = expectation(description: "Fetched Sign-in methods")
  61. // when
  62. Auth.auth()
  63. .fetchSignInMethods(forEmail: FetchSignInMethodsTests.email)
  64. .sink { completion in
  65. switch completion {
  66. case .finished:
  67. print("Finished")
  68. case let .failure(error):
  69. XCTFail("💥 Something went wrong: \(error)")
  70. }
  71. } receiveValue: { signInMethods in
  72. XCTAssertEqual(signInMethods, FetchSignInMethodsTests.allSignInMethods)
  73. fetchSignInMethodsExpectation.fulfill()
  74. }
  75. .store(in: &cancellables)
  76. // then
  77. wait(for: [fetchSignInMethodsExpectation], timeout: expectationTimeout)
  78. }
  79. }