FIRMessagingTokenRefreshTests.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. // macOS requests a user password when accessing the Keychain for the first time,
  17. // so the tests may fail. Disable integration tests on macOS so far.
  18. // TODO: Configure the tests to run on macOS without requesting the keychain password.
  19. #if !os(OSX)
  20. import FirebaseCore
  21. import FirebaseMessaging
  22. import XCTest
  23. // This class should be only used once to ensure the test is independent
  24. class fakeAppDelegate: NSObject, MessagingDelegate {
  25. var messaging = Messaging.messaging()
  26. var delegateIsCalled = false
  27. func messaging(_: Messaging, didReceiveRegistrationToken _: String?) {
  28. delegateIsCalled = true
  29. }
  30. }
  31. class FIRMessagingTokenRefreshTests: XCTestCase {
  32. var app: FirebaseApp!
  33. var messaging: Messaging!
  34. override class func setUp() {
  35. if FirebaseApp.app() == nil {
  36. FirebaseApp.configure()
  37. }
  38. }
  39. override func setUpWithError() throws {
  40. messaging = try XCTUnwrap(Messaging.messaging())
  41. // fake APNS Token
  42. messaging.apnsToken = "eb706b132b2f9270faac751e4ceab283f1803b729ac1dd399db3fd2a98bb101b"
  43. .data(using: .utf8)
  44. }
  45. override func tearDown() {
  46. messaging = nil
  47. }
  48. func testDeleteTokenWithTokenRefreshDelegatesAndNotifications() {
  49. let expectation = self.expectation(description: "delegate method and notification are called")
  50. assertTokenWithAuthorizedEntity()
  51. let notificationExpectation = self.expectation(forNotification: NSNotification.Name
  52. .MessagingRegistrationTokenRefreshed,
  53. object: nil,
  54. handler: nil)
  55. let testDelegate = fakeAppDelegate()
  56. messaging.delegate = testDelegate
  57. testDelegate.delegateIsCalled = false
  58. messaging.deleteFCMToken(forSenderID: tokenAuthorizedEntity(), completion: { error in
  59. XCTAssertNil(error)
  60. XCTAssertTrue(testDelegate.delegateIsCalled)
  61. expectation.fulfill()
  62. })
  63. wait(for: [expectation, notificationExpectation], timeout: 5)
  64. }
  65. func testDeleteDefaultTokenWithTokenRefreshDelegatesAndNotifications() {
  66. let expectation = self.expectation(description: "delegate method and notification are called")
  67. assertDefaultToken()
  68. let notificationExpectation = self.expectation(forNotification: NSNotification.Name
  69. .MessagingRegistrationTokenRefreshed,
  70. object: nil,
  71. handler: nil)
  72. let testDelegate = fakeAppDelegate()
  73. messaging?.delegate = testDelegate
  74. testDelegate.delegateIsCalled = false
  75. messaging.deleteToken { error in
  76. XCTAssertNil(error)
  77. XCTAssertTrue(testDelegate.delegateIsCalled)
  78. expectation.fulfill()
  79. }
  80. wait(for: [expectation, notificationExpectation], timeout: 5)
  81. }
  82. func testDeleteDataWithTokenRefreshDelegatesAndNotifications() {
  83. let expectation = self.expectation(description: "delegate method and notification are called")
  84. assertDefaultToken()
  85. let notificationExpectation = self.expectation(forNotification: NSNotification.Name
  86. .MessagingRegistrationTokenRefreshed,
  87. object: nil,
  88. handler: nil)
  89. let testDelegate = fakeAppDelegate()
  90. messaging?.delegate = testDelegate
  91. testDelegate.delegateIsCalled = false
  92. messaging.deleteData { error in
  93. XCTAssertNil(error)
  94. XCTAssertTrue(testDelegate.delegateIsCalled)
  95. expectation.fulfill()
  96. }
  97. wait(for: [expectation, notificationExpectation], timeout: 5)
  98. }
  99. // pragma mark - Helpers
  100. func assertTokenWithAuthorizedEntity() {
  101. let expectation = self.expectation(description: "tokenWithAuthorizedEntity")
  102. messaging.retrieveFCMToken(forSenderID: tokenAuthorizedEntity()) { token, error in
  103. XCTAssertNil(error)
  104. XCTAssertNotNil(token)
  105. expectation.fulfill()
  106. }
  107. wait(for: [expectation], timeout: 5)
  108. }
  109. func assertDefaultToken() {
  110. let expectation = self.expectation(description: "getToken")
  111. messaging.token { token, error in
  112. XCTAssertNil(error)
  113. XCTAssertNotNil(token)
  114. expectation.fulfill()
  115. }
  116. wait(for: [expectation], timeout: 5)
  117. }
  118. func tokenAuthorizedEntity() -> String {
  119. guard let app = FirebaseApp.app() else {
  120. return ""
  121. }
  122. return app.options.gcmSenderID
  123. }
  124. }
  125. #endif // !TARGET_OS_OSX