FIRMessagingServiceTest.m 13 KB

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