FIRMessagingServiceTest.m 14 KB

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