AuthAPNSTokenManagerTests.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.getToken { token, error in
  60. firstCallbackCalled = true
  61. XCTAssertEqual(token?.data, self.data)
  62. XCTAssertEqual(token?.type, .sandbox)
  63. XCTAssertNil(error)
  64. }
  65. XCTAssertFalse(firstCallbackCalled)
  66. // Add second callback, which is yet to be called either.
  67. var secondCallbackCalled = false
  68. manager.getToken { token, error in
  69. secondCallbackCalled = true
  70. XCTAssertEqual(token?.data, self.data)
  71. XCTAssertEqual(token?.type, .sandbox)
  72. XCTAssertNil(error)
  73. }
  74. XCTAssertFalse(secondCallbackCalled)
  75. // Setting nil token shouldn't trigger either callbacks.
  76. manager.token = nil
  77. XCTAssertFalse(firstCallbackCalled)
  78. XCTAssertFalse(secondCallbackCalled)
  79. XCTAssertNil(manager.token)
  80. // Setting a real token should trigger both callbacks.
  81. manager.token = AuthAPNSToken(withData: data!, type: .sandbox)
  82. XCTAssertTrue(firstCallbackCalled)
  83. XCTAssertTrue(secondCallbackCalled)
  84. XCTAssertEqual(manager.token?.data, data)
  85. XCTAssertEqual(manager.token?.type, .sandbox)
  86. // Add third callback, which should be called back immediately.
  87. var thirdCallbackCalled = false
  88. manager.getToken { token, error in
  89. thirdCallbackCalled = true
  90. XCTAssertEqual(token?.data, self.data)
  91. XCTAssertEqual(token?.type, .sandbox)
  92. XCTAssertNil(error)
  93. }
  94. XCTAssertTrue(thirdCallbackCalled)
  95. // In the main thread, Verify the that the fake `registerForRemoteNotifications` was called.
  96. DispatchQueue.main.async {
  97. XCTAssertTrue(self.fakeApplication!.registerCalled)
  98. expectation.fulfill()
  99. }
  100. waitForExpectations(timeout: 5)
  101. }
  102. /** @fn testTimeout
  103. @brief Tests callbacks can be timed out.
  104. */
  105. func testTimeout() throws {
  106. // Set up timeout.
  107. let manager = try XCTUnwrap(manager)
  108. XCTAssertGreaterThan(try XCTUnwrap(manager.timeout), 0)
  109. manager.timeout = kRegistrationTimeout
  110. // Add callback to time out.
  111. let expectation = self.expectation(description: #function)
  112. manager.getToken { token, error in
  113. XCTAssertNil(token)
  114. XCTAssertNil(error)
  115. expectation.fulfill()
  116. }
  117. // Time out.
  118. waitForExpectations(timeout: 2)
  119. // In the main thread, Verify the that the fake `registerForRemoteNotifications` was called.
  120. let expectation2 = self.expectation(description: "registerCalled")
  121. DispatchQueue.main.async {
  122. XCTAssertTrue(self.fakeApplication!.registerCalled)
  123. expectation2.fulfill()
  124. }
  125. // Calling cancel afterwards should have no effect.
  126. manager.cancel(withError: NSError(domain: "dummy", code: 1))
  127. waitForExpectations(timeout: 5)
  128. }
  129. /** @fn testCancel
  130. @brief Tests cancelling the pending callbacks.
  131. */
  132. func testCancel() throws {
  133. // Set up timeout.
  134. let manager = try XCTUnwrap(manager)
  135. XCTAssertGreaterThan(try XCTUnwrap(manager.timeout), 0)
  136. manager.timeout = kRegistrationTimeout
  137. // Add callback to cancel.
  138. var callbackCalled = false
  139. manager.getToken { token, error in
  140. XCTAssertNil(token)
  141. XCTAssertEqual(error as? NSError, self.error as NSError)
  142. XCTAssertFalse(callbackCalled) // verify callback is not called twice
  143. callbackCalled = true
  144. }
  145. XCTAssertFalse(callbackCalled)
  146. // Call cancel.
  147. manager.cancel(withError: error)
  148. // In the main thread, Verify the that the fake `registerForRemoteNotifications` was called.
  149. let expectation2 = expectation(description: "registerCalled")
  150. DispatchQueue.main.async {
  151. XCTAssertTrue(self.fakeApplication!.registerCalled)
  152. expectation2.fulfill()
  153. }
  154. // Calling cancel afterwards should have no effect.
  155. manager.cancel(withError: error)
  156. waitForExpectations(timeout: 5)
  157. }
  158. private class FakeApplication: NSObject, AuthAPNSTokenApplication {
  159. var registerCalled = false
  160. func registerForRemoteNotifications() {
  161. registerCalled = true
  162. }
  163. }
  164. }
  165. #endif