AuthAPNSTokenManagerTests.swift 7.1 KB

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