AuthAPNSTokenManagerTests.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2023 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. #if os(iOS)
  15. import Foundation
  16. import XCTest
  17. @testable import FirebaseAuth
  18. import FirebaseCoreInternal
  19. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  20. class AuthAPNSTokenManagerTests: XCTestCase {
  21. private var fakeApplication: FakeApplication?
  22. var manager: AuthAPNSTokenManager?
  23. let data = "qwerty".data(using: .utf8)
  24. let kRegistrationTimeout = 0.5
  25. let error = NSError(domain: "dummy", code: 1)
  26. override func setUp() {
  27. fakeApplication = FakeApplication()
  28. manager = AuthAPNSTokenManager(withApplication: fakeApplication!)
  29. }
  30. /** @fn testSetToken
  31. @brief Tests setting and getting the `token` property.
  32. */
  33. func testSetToken() throws {
  34. XCTAssertNil(manager?.token)
  35. manager?.token = AuthAPNSToken(withData: data!, type: .prod)
  36. let managerToken = try XCTUnwrap(manager?.token)
  37. XCTAssertEqual(managerToken.data, data)
  38. XCTAssertEqual(managerToken.type, .prod)
  39. manager?.token = nil
  40. XCTAssertNil(manager?.token)
  41. }
  42. /** @fn testDetectTokenType
  43. @brief Tests automatic detection of token type.
  44. */
  45. func testDetectTokenType() throws {
  46. XCTAssertNil(manager?.token)
  47. manager?.token = AuthAPNSToken(withData: data!, type: .unknown)
  48. let managerToken = try XCTUnwrap(manager?.token)
  49. XCTAssertEqual(managerToken.data, data)
  50. XCTAssertNotEqual(managerToken.type, .unknown)
  51. }
  52. /** @fn testCallback
  53. @brief Tests callbacks are called.
  54. */
  55. func testCallback() throws {
  56. let expectation = self.expectation(description: #function)
  57. XCTAssertFalse(fakeApplication!.registerCalled)
  58. let firstCallbackCalled = UnfairLock(false)
  59. let manager = try XCTUnwrap(manager)
  60. manager.getTokenInternal { result in
  61. firstCallbackCalled.withLock { $0 = true }
  62. switch result {
  63. case let .success(token):
  64. XCTAssertEqual(token.data, self.data)
  65. XCTAssertEqual(token.type, .sandbox)
  66. case let .failure(error):
  67. XCTFail("Unexpected error: \(error)")
  68. }
  69. }
  70. XCTAssertFalse(firstCallbackCalled.value())
  71. // Add second callback, which is yet to be called either.
  72. let secondCallbackCalled = UnfairLock(false)
  73. manager.getTokenInternal { result in
  74. secondCallbackCalled.withLock { $0 = true }
  75. switch result {
  76. case let .success(token):
  77. XCTAssertEqual(token.data, self.data)
  78. XCTAssertEqual(token.type, .sandbox)
  79. case let .failure(error):
  80. XCTFail("Unexpected error: \(error)")
  81. }
  82. }
  83. XCTAssertFalse(secondCallbackCalled.value())
  84. // Setting nil token shouldn't trigger either callbacks.
  85. manager.token = nil
  86. XCTAssertFalse(firstCallbackCalled.value())
  87. XCTAssertFalse(secondCallbackCalled.value())
  88. XCTAssertNil(manager.token)
  89. // Setting a real token should trigger both callbacks.
  90. manager.token = AuthAPNSToken(withData: data!, type: .sandbox)
  91. XCTAssertTrue(firstCallbackCalled.value())
  92. XCTAssertTrue(secondCallbackCalled.value())
  93. XCTAssertEqual(manager.token?.data, data)
  94. XCTAssertEqual(manager.token?.type, .sandbox)
  95. // Add third callback, which should be called back immediately.
  96. let thirdCallbackCalled = UnfairLock(false)
  97. manager.getTokenInternal { result in
  98. thirdCallbackCalled.withLock { $0 = true }
  99. switch result {
  100. case let .success(token):
  101. XCTAssertEqual(token.data, self.data)
  102. XCTAssertEqual(token.type, .sandbox)
  103. case let .failure(error):
  104. XCTFail("Unexpected error: \(error)")
  105. }
  106. }
  107. XCTAssertTrue(thirdCallbackCalled.value())
  108. // In the main thread, Verify the that the fake `registerForRemoteNotifications` was called.
  109. DispatchQueue.main.async {
  110. XCTAssertTrue(self.fakeApplication!.registerCalled)
  111. expectation.fulfill()
  112. }
  113. waitForExpectations(timeout: 5)
  114. }
  115. /** @fn testTimeout
  116. @brief Tests callbacks can be timed out.
  117. */
  118. func testTimeout() throws {
  119. // Set up timeout.
  120. manager = AuthAPNSTokenManager(
  121. withApplication: fakeApplication!,
  122. timeout: kRegistrationTimeout
  123. )
  124. let manager = try XCTUnwrap(manager)
  125. XCTAssertGreaterThan(try XCTUnwrap(manager.timeout), 0)
  126. // Add callback to time out.
  127. let expectation = self.expectation(description: #function)
  128. manager.getTokenInternal { result in
  129. switch result {
  130. case let .success(token):
  131. XCTFail("Unexpected success: \(token)")
  132. case let .failure(error):
  133. XCTAssertEqual(
  134. error as NSError,
  135. AuthErrorUtils.missingAppTokenError(underlyingError: nil) as NSError
  136. )
  137. }
  138. expectation.fulfill()
  139. }
  140. // Time out.
  141. waitForExpectations(timeout: 2)
  142. // In the main thread, Verify the that the fake `registerForRemoteNotifications` was called.
  143. let expectation2 = self.expectation(description: "registerCalled")
  144. DispatchQueue.main.async {
  145. XCTAssertTrue(self.fakeApplication!.registerCalled)
  146. expectation2.fulfill()
  147. }
  148. // Calling cancel afterwards should have no effect.
  149. manager.cancel(withError: NSError(domain: "dummy", code: 1))
  150. waitForExpectations(timeout: 5)
  151. }
  152. /** @fn testCancel
  153. @brief Tests cancelling the pending callbacks.
  154. */
  155. func testCancel() throws {
  156. // Set up timeout.
  157. manager = AuthAPNSTokenManager(
  158. withApplication: fakeApplication!,
  159. timeout: kRegistrationTimeout
  160. )
  161. let manager = try XCTUnwrap(manager)
  162. XCTAssertGreaterThan(try XCTUnwrap(manager.timeout), 0)
  163. // Add callback to cancel.
  164. let callbackCalled = UnfairLock(false)
  165. manager.getTokenInternal { result in
  166. switch result {
  167. case let .success(token):
  168. XCTFail("Unexpected success: \(token)")
  169. case let .failure(error):
  170. XCTAssertEqual(error as NSError, self.error as NSError)
  171. }
  172. XCTAssertFalse(callbackCalled.value()) // verify callback is not called twice
  173. callbackCalled.withLock { $0 = true }
  174. }
  175. XCTAssertFalse(callbackCalled.value())
  176. // Call cancel.
  177. manager.cancel(withError: error)
  178. // In the main thread, Verify the that the fake `registerForRemoteNotifications` was called.
  179. let expectation2 = expectation(description: "registerCalled")
  180. DispatchQueue.main.async {
  181. XCTAssertTrue(self.fakeApplication!.registerCalled)
  182. expectation2.fulfill()
  183. }
  184. // Calling cancel afterwards should have no effect.
  185. manager.cancel(withError: error)
  186. waitForExpectations(timeout: 5)
  187. }
  188. private class FakeApplication: NSObject, AuthAPNSTokenApplication {
  189. var registerCalled = false
  190. func registerForRemoteNotifications() {
  191. registerCalled = true
  192. }
  193. }
  194. }
  195. #endif