GameCenterAuthProviderTests.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2021 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 GameKit
  17. import XCTest
  18. import FirebaseAuth
  19. class GameCenterAuthProviderTests: XCTestCase {
  20. override class func setUp() {
  21. FirebaseApp.configureForTests()
  22. GKLocalPlayer.mock(with: MockLocalPlayer.self)
  23. }
  24. override class func tearDown() {
  25. FirebaseApp.app()?.delete { success in
  26. if success {
  27. print("Shut down app successfully.")
  28. } else {
  29. print("💥 There was a problem when shutting down the app..")
  30. }
  31. }
  32. }
  33. fileprivate static let playerID = "PLAYERID"
  34. fileprivate static let displayName = "DISPLAYNAME"
  35. fileprivate static let publicKeyURL = "PUBLICKEYURL"
  36. fileprivate static let signature = "AAAABBBBCCCC"
  37. fileprivate static let salt = "AAAA"
  38. fileprivate static let timestamp: UInt64 = 12_345_678
  39. class MockLocalPlayer: GKLocalPlayer {
  40. static var _local: MockLocalPlayer!
  41. override class var local: GKLocalPlayer { _local }
  42. override var playerID: String { GameCenterAuthProviderTests.playerID }
  43. override var alias: String { GameCenterAuthProviderTests.displayName }
  44. override var displayName: String { GameCenterAuthProviderTests.displayName }
  45. var _isAuthenticated: Bool = true
  46. override var isAuthenticated: Bool { _isAuthenticated }
  47. var _errorIdentityVerificationSignature: NSError?
  48. override func generateIdentityVerificationSignature(completionHandler: ((URL?, Data?, Data?,
  49. UInt64,
  50. Error?) -> Void)? =
  51. nil) {
  52. let url = URL(string: GameCenterAuthProviderTests.publicKeyURL)
  53. let signature = Data(base64Encoded: GameCenterAuthProviderTests.signature)
  54. let salt = Data(base64Encoded: GameCenterAuthProviderTests.salt)
  55. let timestamp = GameCenterAuthProviderTests.timestamp
  56. if _errorIdentityVerificationSignature != nil {
  57. completionHandler?(nil, nil, nil, 0, _errorIdentityVerificationSignature)
  58. } else {
  59. completionHandler?(url, signature, salt, timestamp, nil)
  60. }
  61. }
  62. }
  63. class MockAuthBackend: AuthBackendImplementationMock {}
  64. // TODO(#10767) - Restore two tests in this file.
  65. func SKIPtestGetCredentialWithLocalPlayer() {
  66. // given
  67. FIRAuthBackend.setBackendImplementation(MockAuthBackend())
  68. MockLocalPlayer._local = MockLocalPlayer()
  69. var cancellables = Set<AnyCancellable>()
  70. let getCredentialExpectation = expectation(description: "Get credential")
  71. // when
  72. GameCenterAuthProvider.getCredential()
  73. .sink { completion in
  74. switch completion {
  75. case .finished:
  76. print("Finished")
  77. case let .failure(error):
  78. XCTFail("💥 Something went wrong: \(error)")
  79. }
  80. } receiveValue: { credential in
  81. do {
  82. XCTAssertTrue(Thread.isMainThread)
  83. let gameCenterCredential =
  84. try XCTUnwrap(credential as? FIRGameCenterAuthCredential)
  85. XCTAssertEqual(gameCenterCredential.displayName, Self.displayName)
  86. XCTAssertEqual(gameCenterCredential.playerID, Self.playerID)
  87. XCTAssertEqual(
  88. gameCenterCredential.publicKeyURL.absoluteString,
  89. Self.publicKeyURL
  90. )
  91. XCTAssertEqual(gameCenterCredential.timestamp, Self.timestamp)
  92. XCTAssertEqual(gameCenterCredential.salt.base64EncodedString(), Self.salt)
  93. XCTAssertEqual(
  94. gameCenterCredential.signature.base64EncodedString(),
  95. Self.signature
  96. )
  97. } catch {
  98. XCTFail("💥 Expect non-nil OAuth credential: \(error)")
  99. }
  100. getCredentialExpectation.fulfill()
  101. }
  102. .store(in: &cancellables)
  103. // then
  104. wait(for: [getCredentialExpectation], timeout: expectationTimeout)
  105. }
  106. func testGetCredentialPlayerNotAuthenticatedWithLocalPlayer() {
  107. // given
  108. FIRAuthBackend.setBackendImplementation(MockAuthBackend())
  109. MockLocalPlayer._local = MockLocalPlayer()
  110. MockLocalPlayer._local._isAuthenticated = false
  111. var cancellables = Set<AnyCancellable>()
  112. let getCredentialExpectation = expectation(description: "Get credential")
  113. // when
  114. GameCenterAuthProvider.getCredential()
  115. .sink { completion in
  116. if case let .failure(error as NSError) = completion {
  117. XCTAssertEqual(error.code, AuthErrorCode.localPlayerNotAuthenticated.rawValue)
  118. getCredentialExpectation.fulfill()
  119. }
  120. } receiveValue: { authDataResult in
  121. XCTFail("💥 result unexpected")
  122. }
  123. .store(in: &cancellables)
  124. // then
  125. wait(for: [getCredentialExpectation], timeout: expectationTimeout)
  126. }
  127. // TODO(#10767) - Restore
  128. func SKIPtestGetCredentialInvalidPlayerWithLocalPlayer() {
  129. // given
  130. FIRAuthBackend.setBackendImplementation(MockAuthBackend())
  131. MockLocalPlayer._local = MockLocalPlayer()
  132. MockLocalPlayer._local
  133. ._errorIdentityVerificationSignature = GKError(.invalidPlayer) as NSError
  134. var cancellables = Set<AnyCancellable>()
  135. let getCredentialExpectation = expectation(description: "Get credential")
  136. // when
  137. GameCenterAuthProvider.getCredential()
  138. .sink { completion in
  139. if case let .failure(error as NSError) = completion {
  140. XCTAssertEqual(error.code, GKError.invalidPlayer.rawValue)
  141. getCredentialExpectation.fulfill()
  142. }
  143. } receiveValue: { authDataResult in
  144. XCTFail("💥 result unexpected")
  145. }
  146. .store(in: &cancellables)
  147. // then
  148. wait(for: [getCredentialExpectation], timeout: expectationTimeout)
  149. }
  150. }