UpdateCurrentUserTests.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Combine
  15. import FirebaseAuth
  16. import Foundation
  17. import XCTest
  18. class UpdateCurrentUserTests: 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. static let oAuthSessionID = "sessionID"
  46. static let oAuthRequestURI = "requestURI"
  47. static let googleID = "GOOGLE_ID"
  48. static let googleDisplayName = "Google Doe"
  49. static let googleEmail = "user@gmail.com"
  50. static let customToken = "CUSTOM_TOKEN"
  51. class MockVerifyPasswordResponse: FIRVerifyPasswordResponse {
  52. override var idToken: String { return UpdateCurrentUserTests.accessToken }
  53. override var refreshToken: String { return UpdateCurrentUserTests.refreshToken }
  54. override var approximateExpirationDate: Date {
  55. Date(timeIntervalSinceNow: UpdateCurrentUserTests.accessTokenTimeToLive)
  56. }
  57. }
  58. class MockGetAccountInfoResponseUser: FIRGetAccountInfoResponseUser {
  59. override var localID: String? { return UpdateCurrentUserTests.localID }
  60. override var displayName: String { return UpdateCurrentUserTests.displayName }
  61. override var email: String? { return UpdateCurrentUserTests.email }
  62. override var passwordHash: String? { return UpdateCurrentUserTests.passwordHash }
  63. }
  64. class MockGetAccountInfoResponse: FIRGetAccountInfoResponse {
  65. override var users: [FIRGetAccountInfoResponseUser] {
  66. return [MockGetAccountInfoResponseUser(dictionary: [:])]
  67. }
  68. }
  69. class MockAuthBackend: AuthBackendImplementationMock {
  70. override func verifyPassword(_ request: FIRVerifyPasswordRequest,
  71. callback: @escaping FIRVerifyPasswordResponseCallback) {
  72. callback(MockVerifyPasswordResponse(), nil)
  73. }
  74. override func getAccountInfo(_ request: FIRGetAccountInfoRequest,
  75. callback: @escaping FIRGetAccountInfoResponseCallback) {
  76. XCTAssertEqual(request.apiKey, SignInWithCustomTokenTests.apiKey)
  77. XCTAssertEqual(request.accessToken, SignInWithCustomTokenTests.accessToken)
  78. let response = MockGetAccountInfoResponse()
  79. callback(response, nil)
  80. }
  81. }
  82. func waitForSignIn(with accessToken: String, apiKey: String) {
  83. let userSignedInExpectation = expectation(description: "User signed in")
  84. let cancellable = Auth.auth()
  85. .signIn(withEmail: UpdateCurrentUserTests.email, password: UpdateCurrentUserTests.password)
  86. .sink { completion in
  87. switch completion {
  88. case .finished:
  89. print("Finished")
  90. case let .failure(error):
  91. XCTFail("💥 Something went wrong: \(error)")
  92. }
  93. } receiveValue: { authDataResult in
  94. userSignedInExpectation.fulfill()
  95. }
  96. wait(for: [userSignedInExpectation], timeout: expectationTimeout)
  97. XCTAssertNotNil(Auth.auth().currentUser)
  98. cancellable.cancel()
  99. }
  100. func testUpdateCurrentUser() {
  101. // given
  102. FIRAuthBackend.setBackendImplementation(MockAuthBackend())
  103. var cancellables = Set<AnyCancellable>()
  104. let userUpdated = expectation(description: "Current user updated")
  105. // when
  106. waitForSignIn(with: UpdateCurrentUserTests.accessToken, apiKey: UpdateCurrentUserTests.apiKey)
  107. guard let user1 = Auth.auth().currentUser else {
  108. XCTFail("Current user unexpectedly was nil")
  109. return
  110. }
  111. let accessToken2 = "fakeAccessToken2"
  112. waitForSignIn(with: accessToken2, apiKey: UpdateCurrentUserTests.apiKey)
  113. guard let user2 = Auth.auth().currentUser else {
  114. XCTFail("Current user unexpectedly was nil")
  115. return
  116. }
  117. XCTAssertEqual(Auth.auth().currentUser, user2)
  118. Auth.auth()
  119. .updateCurrentUser(user1)
  120. .sink { completion in
  121. switch completion {
  122. case .finished:
  123. print("Finished")
  124. case let .failure(error):
  125. XCTFail("💥 Something went wrong: \(error)")
  126. }
  127. } receiveValue: {
  128. XCTAssertEqual(Auth.auth().currentUser, user1)
  129. XCTAssertNotEqual(Auth.auth().currentUser, user2)
  130. userUpdated.fulfill()
  131. }
  132. .store(in: &cancellables)
  133. // then
  134. wait(for: [userUpdated], timeout: expectationTimeout)
  135. }
  136. }