FIRMessagingServiceTest.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 <GoogleUtilities/GULUserDefaults.h>
  19. #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
  20. #import "FirebaseMessaging/Sources/FIRMessagingPubSub.h"
  21. #import "FirebaseMessaging/Sources/FIRMessagingTopicsCommon.h"
  22. #import "FirebaseMessaging/Sources/NSError+FIRMessaging.h"
  23. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  24. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenManager.h"
  25. #import "FirebaseMessaging/Tests/UnitTests/FIRMessagingTestUtilities.h"
  26. static NSString *const kFakeToken =
  27. @"fE1e1PZJFSQ:APA91bFAOjp1ahBWn9rTlbjArwBEm_"
  28. @"yUTTzK6dhIvLqzqqCSabaa4TQVM0pGTmF6r7tmMHPe6VYiGMHuCwJFgj5v97xl78sUNMLwuPPhoci8z_"
  29. @"QGlCrTbxCFGzEUfvA3fGpGgIVQU2W6";
  30. static NSString *const kFakeID = @"fE1e1PZJFSQ";
  31. static NSString *const kFIRMessagingTestsServiceSuiteName = @"com.messaging.test_serviceTest";
  32. @interface FIRMessaging ()
  33. @property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
  34. @property(nonatomic, readwrite, strong) NSString *defaultFcmToken;
  35. @end
  36. @interface FIRMessagingPubSub (ExposedForTest)
  37. - (void)updateSubscriptionWithToken:(NSString *)token
  38. topic:(NSString *)topic
  39. options:(NSDictionary *)options
  40. shouldDelete:(BOOL)shouldDelete
  41. handler:(FIRMessagingTopicOperationCompletion)handler;
  42. @end
  43. @interface FIRMessagingServiceTest : XCTestCase {
  44. FIRMessaging *_messaging;
  45. id _mockMessaging;
  46. id _mockPubSub;
  47. id _mockTokenManager;
  48. id _mockInstallations;
  49. FIRMessagingTestUtilities *_testUtil;
  50. }
  51. @end
  52. @implementation FIRMessagingServiceTest
  53. - (void)setUp {
  54. [super setUp];
  55. NSUserDefaults *defaults =
  56. [[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingTestsServiceSuiteName];
  57. _testUtil = [[FIRMessagingTestUtilities alloc] initWithUserDefaults:defaults withRMQManager:NO];
  58. _mockMessaging = _testUtil.mockMessaging;
  59. _messaging = _testUtil.messaging;
  60. _mockTokenManager = _testUtil.mockTokenManager;
  61. _mockInstallations = _testUtil.mockInstallations;
  62. OCMStub([_mockTokenManager defaultFCMToken]).andReturn(kFakeToken);
  63. _mockPubSub = _testUtil.mockPubsub;
  64. }
  65. - (void)tearDown {
  66. [_testUtil cleanupAfterTest:self];
  67. _messaging = nil;
  68. [_mockPubSub stopMocking];
  69. [[[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingTestsServiceSuiteName]
  70. removePersistentDomainForName:kFIRMessagingTestsServiceSuiteName];
  71. [super tearDown];
  72. }
  73. - (void)testSubscribe {
  74. XCTestExpectation *subscribeExpectation =
  75. [self expectationWithDescription:@"Should call subscribe on Pubsub"];
  76. NSString *token = kFakeToken;
  77. NSString *topic = @"/topics/some-random-topic";
  78. [[[_mockPubSub stub] andDo:^(NSInvocation *invocation) {
  79. [subscribeExpectation fulfill];
  80. }] updateSubscriptionWithToken:token
  81. topic:topic
  82. options:OCMOCK_ANY
  83. shouldDelete:NO
  84. handler:OCMOCK_ANY];
  85. [_mockPubSub subscribeWithToken:token
  86. topic:topic
  87. options:nil
  88. handler:^(NSError *error){
  89. // not a nil block
  90. }];
  91. // should call updateSubscription
  92. [self waitForExpectationsWithTimeout:0.1
  93. handler:^(NSError *error) {
  94. XCTAssertNil(error);
  95. [self->_mockPubSub verify];
  96. }];
  97. }
  98. - (void)testUnsubscribe {
  99. XCTestExpectation *subscribeExpectation =
  100. [self expectationWithDescription:@"Should call unsubscribe on Pubsub"];
  101. NSString *token = kFakeToken;
  102. NSString *topic = @"/topics/some-random-topic";
  103. [[[_mockPubSub stub] andDo:^(NSInvocation *invocation) {
  104. [subscribeExpectation fulfill];
  105. }] updateSubscriptionWithToken:[OCMArg isEqual:token]
  106. topic:[OCMArg isEqual:topic]
  107. options:[OCMArg checkWithBlock:^BOOL(id obj) {
  108. if ([obj isKindOfClass:[NSDictionary class]]) {
  109. return [(NSDictionary *)obj count] == 0;
  110. }
  111. return NO;
  112. }]
  113. shouldDelete:YES
  114. handler:OCMOCK_ANY];
  115. [_mockPubSub unsubscribeWithToken:token
  116. topic:topic
  117. options:nil
  118. handler:^(NSError *error){
  119. }];
  120. // should call updateSubscription
  121. [self waitForExpectationsWithTimeout:0.1
  122. handler:^(NSError *error) {
  123. XCTAssertNil(error);
  124. [self->_mockPubSub verify];
  125. }];
  126. }
  127. - (void)testSubscribeWithNoTopicPrefix {
  128. [self mockTokenRequestSuccess];
  129. NSString *topicName = @"topicWithoutPrefix";
  130. NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
  131. OCMExpect([_mockPubSub subscribeToTopic:[OCMArg isEqual:topicNameWithPrefix]
  132. handler:[OCMArg any]]);
  133. [_messaging subscribeToTopic:topicName];
  134. OCMVerifyAll(_mockPubSub);
  135. }
  136. - (void)testSubscribeWithTopicPrefix {
  137. [self mockTokenRequestSuccess];
  138. NSString *topicName = @"/topics/topicWithoutPrefix";
  139. OCMExpect([_mockPubSub subscribeToTopic:[OCMArg isEqual:topicName] handler:[OCMArg any]]);
  140. [_messaging subscribeToTopic:topicName];
  141. OCMVerifyAll(_mockPubSub);
  142. }
  143. - (void)testUnsubscribeWithNoTopicPrefix {
  144. [self mockTokenRequestSuccess];
  145. NSString *topicName = @"topicWithoutPrefix";
  146. NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic:topicName];
  147. OCMExpect([_mockPubSub unsubscribeFromTopic:[OCMArg isEqual:topicNameWithPrefix]
  148. handler:[OCMArg any]]);
  149. [_messaging unsubscribeFromTopic:topicName];
  150. OCMVerifyAll(_mockPubSub);
  151. }
  152. - (void)testUnsubscribeWithTopicPrefix {
  153. [self mockTokenRequestSuccess];
  154. NSString *topicName = @"/topics/topicWithPrefix";
  155. OCMExpect([_mockPubSub unsubscribeFromTopic:[OCMArg isEqual:topicName] handler:[OCMArg any]]);
  156. [_messaging unsubscribeFromTopic:topicName];
  157. OCMVerifyAll(_mockPubSub);
  158. }
  159. - (void)testSubscriptionCompletionHandlerWithSuccess {
  160. [self mockTokenRequestSuccess];
  161. OCMStub([_mockPubSub subscribeToTopic:[OCMArg any]
  162. handler:([OCMArg invokeBlockWithArgs:[NSNull null], nil])]);
  163. XCTestExpectation *subscriptionCompletionExpectation =
  164. [self expectationWithDescription:@"Subscription is complete"];
  165. [_messaging subscribeToTopic:@"news"
  166. completion:^(NSError *error) {
  167. XCTAssertNil(error);
  168. [subscriptionCompletionExpectation fulfill];
  169. }];
  170. [self waitForExpectationsWithTimeout:0.2
  171. handler:^(NSError *_Nullable error){
  172. }];
  173. }
  174. - (void)testUnsubscribeCompletionHandlerWithSuccess {
  175. [self mockTokenRequestSuccess];
  176. OCMStub([_mockPubSub unsubscribeFromTopic:[OCMArg any]
  177. handler:([OCMArg invokeBlockWithArgs:[NSNull null], nil])]);
  178. XCTestExpectation *unsubscriptionCompletionExpectation =
  179. [self expectationWithDescription:@"Unsubscription is complete"];
  180. [_messaging unsubscribeFromTopic:@"news"
  181. completion:^(NSError *_Nullable error) {
  182. XCTAssertNil(error);
  183. [unsubscriptionCompletionExpectation fulfill];
  184. }];
  185. [self waitForExpectationsWithTimeout:0.2
  186. handler:^(NSError *_Nullable error){
  187. }];
  188. }
  189. - (void)testSubscriptionCompletionHandlerWithInvalidTopicName {
  190. [self mockTokenRequestSuccess];
  191. XCTestExpectation *subscriptionCompletionExpectation =
  192. [self expectationWithDescription:@"Subscription is complete"];
  193. [_messaging subscribeToTopic:@"!@#$%^&*()"
  194. completion:^(NSError *_Nullable error) {
  195. XCTAssertNotNil(error);
  196. XCTAssertEqual(error.code, FIRMessagingErrorInvalidTopicName);
  197. [subscriptionCompletionExpectation fulfill];
  198. }];
  199. [self waitForExpectationsWithTimeout:0.2
  200. handler:^(NSError *_Nullable error){
  201. }];
  202. }
  203. - (void)testUnsubscribeCompletionHandlerWithInvalidTopicName {
  204. [self mockTokenRequestSuccess];
  205. XCTestExpectation *unsubscriptionCompletionExpectation =
  206. [self expectationWithDescription:@"Unsubscription is complete"];
  207. [_messaging unsubscribeFromTopic:@"!@#$%^&*()"
  208. completion:^(NSError *error) {
  209. XCTAssertNotNil(error);
  210. XCTAssertEqual(error.code, FIRMessagingErrorInvalidTopicName);
  211. [unsubscriptionCompletionExpectation fulfill];
  212. }];
  213. [self waitForExpectationsWithTimeout:0.2
  214. handler:^(NSError *_Nullable error){
  215. }];
  216. }
  217. - (void)testSubscribeFailedWithInvalidToken {
  218. // Mock get token is failed with FIRMessagingErrorUnknown error.
  219. XCTestExpectation *subscriptionCompletionExpectation =
  220. [self expectationWithDescription:@"Subscription is complete"];
  221. NSString *failureReason = @"Invalid token.";
  222. OCMStub([_mockMessaging
  223. retrieveFCMTokenForSenderID:[OCMArg any]
  224. completion:([OCMArg
  225. invokeBlockWithArgs:
  226. [NSNull null],
  227. [NSError
  228. messagingErrorWithCode:kFIRMessagingErrorCodeUnknown
  229. failureReason:failureReason],
  230. nil])]);
  231. [_messaging subscribeToTopic:@"Apple"
  232. completion:^(NSError *_Nullable error) {
  233. XCTAssertNotNil(error);
  234. XCTAssertEqual(error.code, kFIRMessagingErrorCodeUnknown);
  235. XCTAssertEqualObjects(failureReason, error.localizedFailureReason);
  236. [subscriptionCompletionExpectation fulfill];
  237. }];
  238. [self waitForExpectationsWithTimeout:0.2 handler:nil];
  239. }
  240. - (void)testUnsubscribeFailedWithInvalidToken {
  241. NSString *failureReason = @"Invalid token.";
  242. OCMStub([_mockMessaging
  243. retrieveFCMTokenForSenderID:[OCMArg any]
  244. completion:([OCMArg
  245. invokeBlockWithArgs:
  246. [NSNull null],
  247. [NSError
  248. messagingErrorWithCode:kFIRMessagingErrorCodeUnknown
  249. failureReason:failureReason],
  250. nil])]);
  251. XCTestExpectation *unsubscriptionCompletionExpectation =
  252. [self expectationWithDescription:@"Unsubscription is complete"];
  253. [_messaging unsubscribeFromTopic:@"news"
  254. completion:^(NSError *_Nullable error) {
  255. XCTAssertNotNil(error);
  256. XCTAssertEqual(error.code, kFIRMessagingErrorCodeUnknown);
  257. XCTAssertEqualObjects(failureReason, error.localizedFailureReason);
  258. [unsubscriptionCompletionExpectation fulfill];
  259. }];
  260. [self waitForExpectationsWithTimeout:0.2 handler:nil];
  261. }
  262. - (void)mockTokenRequestSuccess {
  263. OCMStub([_mockInstallations
  264. installationIDWithCompletion:([OCMArg invokeBlockWithArgs:kFakeID, [NSNull null], nil])]);
  265. OCMStub([_mockMessaging
  266. retrieveFCMTokenForSenderID:[OCMArg any]
  267. completion:([OCMArg invokeBlockWithArgs:kFakeToken, [NSNull null], nil])]);
  268. }
  269. @end