FIRMessagingTokenManagerTest.m 5.6 KB

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