SignInWithCustomTokenTests.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 SignInWithCustomTokenTests: 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 MockVerifyCustomTokenResponse: FIRVerifyCustomTokenResponse {
  52. override var idToken: String { return SignInWithCustomTokenTests.accessToken }
  53. override var refreshToken: String { return SignInWithCustomTokenTests.refreshToken }
  54. override var approximateExpirationDate: Date {
  55. Date(timeIntervalSinceNow: SignInWithCustomTokenTests.accessTokenTimeToLive)
  56. }
  57. }
  58. class MockGetAccountInfoResponseUser: FIRGetAccountInfoResponseUser {
  59. override var localID: String? { return SignInWithCustomTokenTests.localID }
  60. override var displayName: String { return SignInWithCustomTokenTests.displayName }
  61. override var email: String? { return SignInWithCustomTokenTests.email }
  62. override var passwordHash: String? { return SignInWithCustomTokenTests.passwordHash }
  63. }
  64. class MockGetAccountInfoResponse: FIRGetAccountInfoResponse {
  65. override var users: [FIRGetAccountInfoResponseUser] {
  66. return [MockGetAccountInfoResponseUser(dictionary: [:])]
  67. }
  68. }
  69. class MockAuthBackend: AuthBackendImplementationMock {
  70. override func verifyCustomToken(_ request: FIRVerifyCustomTokenRequest,
  71. callback: @escaping FIRVerifyCustomTokenResponseCallback) {
  72. XCTAssertEqual(request.apiKey, SignInWithCustomTokenTests.apiKey)
  73. XCTAssertEqual(request.token, SignInWithCustomTokenTests.customToken)
  74. XCTAssertTrue(request.returnSecureToken)
  75. callback(MockVerifyCustomTokenResponse(), nil)
  76. }
  77. override func getAccountInfo(_ request: FIRGetAccountInfoRequest,
  78. callback: @escaping FIRGetAccountInfoResponseCallback) {
  79. XCTAssertEqual(request.apiKey, SignInWithCustomTokenTests.apiKey)
  80. XCTAssertEqual(request.accessToken, SignInWithCustomTokenTests.accessToken)
  81. let response = MockGetAccountInfoResponse()
  82. callback(response, nil)
  83. }
  84. }
  85. func testSignInWithCustomToken() {
  86. // given
  87. FIRAuthBackend.setBackendImplementation(MockAuthBackend())
  88. var cancellables = Set<AnyCancellable>()
  89. let userSignInExpectation = expectation(description: "User signed in")
  90. // when
  91. Auth.auth()
  92. .signIn(withCustomToken: SignInWithCustomTokenTests.customToken)
  93. .sink { completion in
  94. switch completion {
  95. case .finished:
  96. print("Finished")
  97. case let .failure(error):
  98. XCTFail("💥 Something went wrong: \(error)")
  99. }
  100. } receiveValue: { authDataResult in
  101. let user = authDataResult.user
  102. XCTAssertNotNil(user)
  103. XCTAssertEqual(user.uid, SignInWithCustomTokenTests.localID)
  104. XCTAssertEqual(user.displayName, SignInWithCustomTokenTests.displayName)
  105. XCTAssertEqual(user.email, SignInWithCustomTokenTests.email)
  106. XCTAssertFalse(user.isAnonymous)
  107. XCTAssertEqual(user.providerData.count, 0)
  108. userSignInExpectation.fulfill()
  109. }
  110. .store(in: &cancellables)
  111. // then
  112. wait(for: [userSignInExpectation], timeout: expectationTimeout)
  113. }
  114. }