FIRMessagingTokenManagerTest.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2017 Google
  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. #import <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FirebaseMessaging/Sources/Token/FIRMessagingAuthService.h"
  19. #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinPreferences.h"
  20. #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinStore.h"
  21. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenManager.h"
  22. #import "FirebaseMessaging/Tests/UnitTests/FIRMessagingTestUtilities.h"
  23. @interface FIRMessaging (ExposedForTest)
  24. @property(nonatomic, readwrite, strong) FIRMessagingTokenManager *tokenManager;
  25. @end
  26. @interface FIRMessagingTokenManager (ExposedForTest)
  27. - (void)didDeleteFCMScopedTokensForCheckin:(FIRMessagingCheckinPreferences *)checkin;
  28. - (void)resetCredentialsIfNeeded;
  29. @end
  30. @interface FIRMessagingAuthService (ExposedForTest)
  31. @property(nonatomic, readwrite, strong) FIRMessagingCheckinStore *checkinStore;
  32. @end
  33. @interface FIRMessagingTokenManagerTest : XCTestCase {
  34. FIRMessaging *_messaging;
  35. id _mockMessaging;
  36. id _mockPubSub;
  37. id _mockTokenManager;
  38. id _mockInstallations;
  39. id _mockCheckinStore;
  40. id _mockAuthService;
  41. FIRMessagingTestUtilities *_testUtil;
  42. }
  43. @end
  44. @implementation FIRMessagingTokenManagerTest
  45. - (void)setUp {
  46. [super setUp];
  47. // Create the messaging instance with all the necessary dependencies.
  48. NSUserDefaults *defaults =
  49. [[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingDefaultsTestDomain];
  50. _testUtil = [[FIRMessagingTestUtilities alloc] initWithUserDefaults:defaults withRMQManager:NO];
  51. _mockMessaging = _testUtil.mockMessaging;
  52. _messaging = _testUtil.messaging;
  53. _mockTokenManager = _testUtil.mockTokenManager;
  54. _mockAuthService = OCMPartialMock(_messaging.tokenManager.authService);
  55. _mockCheckinStore = OCMPartialMock(_messaging.tokenManager.authService.checkinStore);
  56. }
  57. - (void)tearDown {
  58. [_mockCheckinStore stopMocking];
  59. [_mockAuthService stopMocking];
  60. [_testUtil cleanupAfterTest:self];
  61. _messaging = nil;
  62. [[[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingDefaultsTestDomain]
  63. removePersistentDomainForName:kFIRMessagingDefaultsTestDomain];
  64. [super tearDown];
  65. }
  66. - (void)testTokenChangeMethod {
  67. NSString *oldToken = nil;
  68. NSString *newToken = @"new_token";
  69. XCTAssertTrue([_messaging.tokenManager hasTokenChangedFromOldToken:oldToken toNewToken:newToken]);
  70. oldToken = @"old_token";
  71. newToken = nil;
  72. XCTAssertTrue([_messaging.tokenManager hasTokenChangedFromOldToken:oldToken toNewToken:newToken]);
  73. oldToken = @"old_token";
  74. newToken = @"new_token";
  75. XCTAssertTrue([_messaging.tokenManager hasTokenChangedFromOldToken:oldToken toNewToken:newToken]);
  76. oldToken = @"The_same_token";
  77. newToken = @"The_same_token";
  78. XCTAssertFalse([_messaging.tokenManager hasTokenChangedFromOldToken:oldToken
  79. toNewToken:newToken]);
  80. oldToken = nil;
  81. newToken = nil;
  82. XCTAssertFalse([_messaging.tokenManager hasTokenChangedFromOldToken:oldToken
  83. toNewToken:newToken]);
  84. oldToken = @"";
  85. newToken = @"";
  86. XCTAssertFalse([_messaging.tokenManager hasTokenChangedFromOldToken:oldToken
  87. toNewToken:newToken]);
  88. oldToken = nil;
  89. newToken = @"";
  90. XCTAssertFalse([_messaging.tokenManager hasTokenChangedFromOldToken:oldToken
  91. toNewToken:newToken]);
  92. oldToken = @"";
  93. newToken = nil;
  94. XCTAssertFalse([_messaging.tokenManager hasTokenChangedFromOldToken:oldToken
  95. toNewToken:newToken]);
  96. }
  97. - (void)testResetCredentialsWithNoCachedCheckin {
  98. id completionArg = [OCMArg invokeBlockWithArgs:[NSNull null], nil];
  99. OCMReject([_mockCheckinStore removeCheckinPreferencesWithHandler:completionArg]);
  100. // Always setting up stub after expect.
  101. OCMStub([_mockAuthService checkinPreferences]).andReturn(nil);
  102. [_messaging.tokenManager resetCredentialsIfNeeded];
  103. OCMVerifyAll(_mockCheckinStore);
  104. }
  105. - (void)testResetCredentialsWithoutFreshInstall {
  106. id completionArg = [OCMArg invokeBlockWithArgs:[NSNull null], nil];
  107. OCMReject([_mockCheckinStore removeCheckinPreferencesWithHandler:completionArg]);
  108. // Always setting up stub after expect.
  109. OCMStub([_mockAuthService hasCheckinPlist]).andReturn(YES);
  110. [_messaging.tokenManager resetCredentialsIfNeeded];
  111. OCMVerifyAll(_mockCheckinStore);
  112. }
  113. - (void)testResetCredentialsWithFreshInstall {
  114. FIRMessagingCheckinPreferences *checkinPreferences =
  115. [[FIRMessagingCheckinPreferences alloc] initWithDeviceID:@"test-auth-id"
  116. secretToken:@"test-secret"];
  117. // Expect checkin is removed if it's a fresh install.
  118. id completionArg = [OCMArg invokeBlockWithArgs:[NSNull null], nil];
  119. OCMExpect([_mockCheckinStore removeCheckinPreferencesWithHandler:completionArg]);
  120. // Always setting up stub after expect.
  121. OCMStub([_mockAuthService checkinPreferences]).andReturn(checkinPreferences);
  122. // Plist file doesn't exist, meaning this is a fresh install.
  123. OCMStub([_mockCheckinStore hasCheckinPlist]).andReturn(NO);
  124. // Expect reset operation but do nothing to avoid flakes due to delayed operation queue.
  125. OCMExpect(
  126. [_mockTokenManager didDeleteFCMScopedTokensForCheckin:[OCMArg isEqual:checkinPreferences]])
  127. .andDo(nil);
  128. [_messaging.tokenManager resetCredentialsIfNeeded];
  129. OCMVerifyAll(_mockCheckinStore);
  130. }
  131. @end