AnonymousAuthTests.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 AnonymousAuthTests: XCTestCase {
  19. static let apiKey = Credentials.apiKey
  20. static let accessTokenTimeToLive: TimeInterval = 60 * 60
  21. static let refreshToken = "REFRESH_TOKEN"
  22. static let accessToken = "ACCESS_TOKEN"
  23. static let localID = "LOCAL_ID"
  24. class MockSignUpNewUserResponse: FIRSignUpNewUserResponse {
  25. override var idToken: String { return AnonymousAuthTests.accessToken }
  26. override var refreshToken: String { return AnonymousAuthTests.refreshToken }
  27. override var approximateExpirationDate: Date {
  28. Date(timeIntervalSinceNow: AnonymousAuthTests.accessTokenTimeToLive)
  29. }
  30. }
  31. class MockGetAccountInfoResponseUser: FIRGetAccountInfoResponseUser {
  32. override var localID: String { return AnonymousAuthTests.localID }
  33. }
  34. class MockGetAccountInfoResponse: FIRGetAccountInfoResponse {
  35. override var users: [FIRGetAccountInfoResponseUser] {
  36. return [MockGetAccountInfoResponseUser(dictionary: [:])]
  37. }
  38. }
  39. class MockAuthBackend: AuthBackendImplementationMock {
  40. override func signUpNewUser(_ request: FIRSignUpNewUserRequest,
  41. callback: @escaping FIRSignupNewUserCallback) {
  42. XCTAssertEqual(request.apiKey, AnonymousAuthTests.apiKey)
  43. XCTAssertNil(request.email)
  44. XCTAssertNil(request.password)
  45. XCTAssertTrue(request.returnSecureToken)
  46. let response = MockSignUpNewUserResponse()
  47. callback(response, nil)
  48. }
  49. override func getAccountInfo(_ request: FIRGetAccountInfoRequest,
  50. callback: @escaping FIRGetAccountInfoResponseCallback) {
  51. XCTAssertEqual(request.apiKey, AnonymousAuthTests.apiKey)
  52. XCTAssertEqual(request.accessToken, AnonymousAuthTests.accessToken)
  53. let response = MockGetAccountInfoResponse()
  54. callback(response, nil)
  55. }
  56. }
  57. // MARK: - Test case setup
  58. override class func setUp() {
  59. FIRAuthBackend.setBackendImplementation(MockAuthBackend())
  60. }
  61. // MARK: - Test method setup
  62. var app: FirebaseApp?
  63. var auth: Auth?
  64. override func setUp() {
  65. self.app = FirebaseApp.appForAuthUnitTestsWithName(name: "app1")
  66. guard let app = app else { fatalError() }
  67. auth = Auth.auth(app: app)
  68. }
  69. func testSignInAnonymouslySuccessfully() {
  70. // given
  71. var cancellables = Set<AnyCancellable>()
  72. let userSignedInExpectation = expectation(description: "Signed in anonymously")
  73. // when
  74. auth?.signInAnonymously()
  75. .sink { completion in
  76. switch completion {
  77. case .finished:
  78. print("Finished")
  79. case let .failure(error):
  80. XCTFail("💥 Something went wrong: \(error)")
  81. }
  82. } receiveValue: { authDataResult in
  83. XCTAssertNotNil(authDataResult.user)
  84. XCTAssertEqual(authDataResult.user.uid, AnonymousAuthTests.localID)
  85. XCTAssertNil(authDataResult.user.displayName)
  86. XCTAssertTrue(authDataResult.user.isAnonymous)
  87. XCTAssertEqual(authDataResult.user.providerData.count, 0)
  88. XCTAssertNotNil(authDataResult.additionalUserInfo)
  89. XCTAssertTrue((authDataResult.additionalUserInfo?.isNewUser) != nil)
  90. XCTAssertNil(authDataResult.additionalUserInfo?.username)
  91. XCTAssertNil(authDataResult.additionalUserInfo?.profile)
  92. userSignedInExpectation.fulfill()
  93. }
  94. .store(in: &cancellables)
  95. // then
  96. wait(for: [userSignedInExpectation], timeout: expectationTimeout)
  97. }
  98. }