EmailPasswordAuthTests.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 EmailPasswordAuthTests: 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 accessTokenTimeToLive: TimeInterval = 60 * 60
  38. static let refreshToken = "REFRESH_TOKEN"
  39. static let accessToken = "ACCESS_TOKEN"
  40. static let email = "johnnyappleseed@apple.com"
  41. static let password = "secret"
  42. static let localID = "LOCAL_ID"
  43. static let displayName = "Johnny Appleseed"
  44. static let passwordHash = "UkVEQUNURUQ="
  45. class MockSignUpNewUserResponse: FIRSignUpNewUserResponse {
  46. override var idToken: String { return EmailPasswordAuthTests.accessToken }
  47. override var refreshToken: String { return EmailPasswordAuthTests.refreshToken }
  48. override var approximateExpirationDate: Date {
  49. Date(timeIntervalSinceNow: EmailPasswordAuthTests.accessTokenTimeToLive)
  50. }
  51. }
  52. class MockGetAccountInfoResponseUser: FIRGetAccountInfoResponseUser {
  53. override var localID: String { return EmailPasswordAuthTests.localID }
  54. override var email: String { return EmailPasswordAuthTests.email }
  55. override var displayName: String { return EmailPasswordAuthTests.displayName }
  56. }
  57. class MockGetAccountInfoResponse: FIRGetAccountInfoResponse {
  58. override var users: [FIRGetAccountInfoResponseUser] {
  59. return [MockGetAccountInfoResponseUser(dictionary: [:])]
  60. }
  61. }
  62. class MockVerifyPasswordResponse: FIRVerifyPasswordResponse {
  63. override var localID: String { return EmailPasswordAuthTests.localID }
  64. override var email: String { return EmailPasswordAuthTests.email }
  65. override var displayName: String { return EmailPasswordAuthTests.displayName }
  66. override var idToken: String { return EmailPasswordAuthTests.accessToken }
  67. override var approximateExpirationDate: Date {
  68. Date(timeIntervalSinceNow: EmailPasswordAuthTests.accessTokenTimeToLive)
  69. }
  70. override var refreshToken: String { return EmailPasswordAuthTests.refreshToken }
  71. }
  72. class MockAuthBackend: AuthBackendImplementationMock {
  73. override func signUpNewUser(_ request: FIRSignUpNewUserRequest,
  74. callback: @escaping FIRSignupNewUserCallback) {
  75. XCTAssertEqual(request.apiKey, EmailPasswordAuthTests.apiKey)
  76. XCTAssertEqual(request.email, EmailPasswordAuthTests.email)
  77. XCTAssertEqual(request.password, EmailPasswordAuthTests.password)
  78. XCTAssertTrue(request.returnSecureToken)
  79. let response = MockSignUpNewUserResponse()
  80. callback(response, nil)
  81. }
  82. override func getAccountInfo(_ request: FIRGetAccountInfoRequest,
  83. callback: @escaping FIRGetAccountInfoResponseCallback) {
  84. XCTAssertEqual(request.apiKey, EmailPasswordAuthTests.apiKey)
  85. XCTAssertEqual(request.accessToken, EmailPasswordAuthTests.accessToken)
  86. let response = MockGetAccountInfoResponse()
  87. callback(response, nil)
  88. }
  89. override func verifyPassword(_ request: FIRVerifyPasswordRequest,
  90. callback: @escaping FIRVerifyPasswordResponseCallback) {
  91. let response = MockVerifyPasswordResponse()
  92. callback(response, nil)
  93. }
  94. }
  95. func testCreateUserWithEmailAndPassword() {
  96. // given
  97. FIRAuthBackend.setBackendImplementation(MockAuthBackend())
  98. var cancellables = Set<AnyCancellable>()
  99. let userCreatedExpectation = expectation(description: "User created")
  100. // when
  101. Auth.auth()
  102. .createUser(
  103. withEmail: EmailPasswordAuthTests.email,
  104. password: EmailPasswordAuthTests.password
  105. )
  106. .sink { completion in
  107. switch completion {
  108. case .finished:
  109. print("Finished")
  110. case let .failure(error):
  111. XCTFail("💥 Something went wrong: \(error)")
  112. }
  113. } receiveValue: { authDataResult in
  114. let user = authDataResult.user
  115. XCTAssertNotNil(user)
  116. XCTAssertEqual(user.uid, EmailPasswordAuthTests.localID)
  117. XCTAssertEqual(user.displayName, EmailPasswordAuthTests.displayName)
  118. XCTAssertEqual(user.email, EmailPasswordAuthTests.email)
  119. XCTAssertFalse(user.isAnonymous)
  120. XCTAssertEqual(user.providerData.count, 0)
  121. userCreatedExpectation.fulfill()
  122. }
  123. .store(in: &cancellables)
  124. // then
  125. wait(for: [userCreatedExpectation], timeout: expectationTimeout)
  126. }
  127. func testSignInUserWithEmailAndPassword() {
  128. // given
  129. FIRAuthBackend.setBackendImplementation(MockAuthBackend())
  130. var cancellables = Set<AnyCancellable>()
  131. let userSignInExpectation = expectation(description: "User signed in")
  132. // when
  133. Auth.auth()
  134. .signIn(withEmail: EmailPasswordAuthTests.email, password: EmailPasswordAuthTests.password)
  135. .sink { completion in
  136. switch completion {
  137. case .finished:
  138. print("Finished")
  139. case let .failure(error):
  140. XCTFail("💥 Something went wrong: \(error)")
  141. }
  142. } receiveValue: { authDataResult in
  143. XCTAssertNotNil(authDataResult.user)
  144. XCTAssertEqual(authDataResult.user.email, EmailPasswordAuthTests.email)
  145. userSignInExpectation.fulfill()
  146. }
  147. .store(in: &cancellables)
  148. // then
  149. wait(for: [userSignInExpectation], timeout: expectationTimeout)
  150. }
  151. }