FIRMessagingExtensionHelperTest.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 2019 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/GULAppEnvironmentUtil.h>
  19. #import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
  20. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  21. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessagingExtensionHelper.h"
  22. API_AVAILABLE(macos(10.14), ios(10.0), watchos(3.0))
  23. typedef void (^FIRMessagingContentHandler)(UNNotificationContent *content);
  24. #if TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_WATCH
  25. static NSString *const kFCMPayloadOptionsName = @"fcm_options";
  26. static NSString *const kFCMPayloadOptionsImageURLName = @"image";
  27. static NSString *const kValidImageURL =
  28. @"https://firebasestorage.googleapis.com/v0/b/fcm-ios-f7f9c.appspot.com/o/"
  29. @"chubbyBunny.jpg?alt=media&token=d6c56a57-c007-4b27-b20f-f267cc83e9e5";
  30. @interface FIRMessagingExtensionHelper (ExposedForTest)
  31. - (void)loadAttachmentForURL:(NSURL *)attachmentURL
  32. completionHandler:(void (^)(UNNotificationAttachment *))completionHandler;
  33. + (NSString *)bundleIdentifierByRemovingLastPartFrom:(NSString *)bundleIdentifier;
  34. - (NSString *)fileExtensionForResponse:(NSURLResponse *)response;
  35. @end
  36. @interface FIRMessagingExtensionHelperTest : XCTestCase {
  37. id _mockExtensionHelper;
  38. id _mockUtilClass;
  39. id _mockURLResponse;
  40. }
  41. @end
  42. @implementation FIRMessagingExtensionHelperTest
  43. - (void)setUp {
  44. [super setUp];
  45. if (@available(macOS 10.14, iOS 10.0, watchos 3.0, *)) {
  46. FIRMessagingExtensionHelper *extensionHelper = [FIRMessaging extensionHelper];
  47. _mockExtensionHelper = OCMPartialMock(extensionHelper);
  48. _mockUtilClass = OCMClassMock([GULAppEnvironmentUtil class]);
  49. _mockURLResponse = OCMClassMock([NSURLResponse class]);
  50. } else {
  51. // Fallback on earlier versions
  52. }
  53. }
  54. - (void)tearDown {
  55. [_mockExtensionHelper stopMocking];
  56. [_mockUtilClass stopMocking];
  57. [_mockURLResponse stopMocking];
  58. }
  59. #ifdef COCOAPODS
  60. // This test requires internet access.
  61. - (void)testModifyNotificationWithValidPayloadData {
  62. if (@available(macOS 10.14, iOS 10.0, watchos 3.0, *)) {
  63. XCTestExpectation *validPayloadExpectation =
  64. [self expectationWithDescription:@"Test payload is valid."];
  65. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  66. content.userInfo =
  67. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : kValidImageURL}};
  68. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  69. [validPayloadExpectation fulfill];
  70. };
  71. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  72. OCMVerify([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  73. completionHandler:[OCMArg any]]);
  74. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  75. }
  76. }
  77. #endif // COCOAPODS
  78. - (void)testModifyNotificationWithInvalidPayloadData {
  79. if (@available(macOS 10.14, iOS 10.0, watchos 3.0, *)) {
  80. XCTestExpectation *validPayloadExpectation =
  81. [self expectationWithDescription:@"Test payload is valid."];
  82. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  83. content.userInfo =
  84. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}};
  85. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  86. [validPayloadExpectation fulfill];
  87. };
  88. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  89. OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  90. completionHandler:[OCMArg any]]);
  91. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  92. }
  93. }
  94. - (void)testModifyNotificationWithEmptyPayloadData {
  95. if (@available(macOS 10.14, iOS 10.0, watchos 3.0, *)) {
  96. XCTestExpectation *validPayloadExpectation =
  97. [self expectationWithDescription:@"Test payload is valid."];
  98. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  99. content.userInfo =
  100. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}};
  101. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  102. [validPayloadExpectation fulfill];
  103. };
  104. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  105. OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  106. completionHandler:[OCMArg any]]);
  107. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  108. }
  109. }
  110. - (void)testModifyNotificationWithValidPayloadDataNoMimeType {
  111. if (@available(macOS 10.14, iOS 10.0, *)) {
  112. NSString *const kValidTestURL = @"test.jpg";
  113. NSString *const kValidTestExtension = @".jpg";
  114. OCMStub([_mockURLResponse suggestedFilename]).andReturn(kValidTestURL);
  115. NSString *const extension = [_mockExtensionHelper fileExtensionForResponse:_mockURLResponse];
  116. XCTAssertTrue([extension isEqualToString:kValidTestExtension]);
  117. }
  118. }
  119. - (void)testModifyNotificationWithInvalidPayloadDataInvalidMimeType {
  120. if (@available(macOS 10.14, iOS 10.0, *)) {
  121. NSString *const kInvalidTestURL = @"test";
  122. NSString *const kInvalidTestExtension = @"";
  123. OCMStub([_mockURLResponse suggestedFilename]).andReturn(kInvalidTestURL);
  124. OCMStub([_mockURLResponse MIMEType]).andReturn(nil);
  125. NSString *const extension = [_mockExtensionHelper fileExtensionForResponse:_mockURLResponse];
  126. XCTAssertTrue([extension isEqualToString:kInvalidTestExtension]);
  127. }
  128. }
  129. - (void)testModifyNotificationWithInvalidPayloadDataValidMimeType {
  130. if (@available(macOS 10.14, iOS 10.0, *)) {
  131. NSString *const kValidMIMETypeTestURL = @"test";
  132. NSString *const kValidMIMETypeTestMIMEType = @"image/jpeg";
  133. NSString *const kValidMIMETypeTestExtension = @".jpeg";
  134. OCMStub([_mockURLResponse suggestedFilename]).andReturn(kValidMIMETypeTestURL);
  135. OCMStub([_mockURLResponse MIMEType]).andReturn(kValidMIMETypeTestMIMEType);
  136. NSString *const extension = [_mockExtensionHelper fileExtensionForResponse:_mockURLResponse];
  137. XCTAssertTrue([extension isEqualToString:kValidMIMETypeTestExtension]);
  138. }
  139. }
  140. - (void)testDeliveryMetricsLoggingWithEmptyPayload {
  141. OCMStub([_mockUtilClass isAppExtension]).andReturn(YES);
  142. NSDictionary *fakeMessageInfo = @{@"aps" : @{}};
  143. OCMReject([_mockExtensionHelper bundleIdentifierByRemovingLastPartFrom:[OCMArg any]]);
  144. [_mockExtensionHelper exportDeliveryMetricsToBigQueryWithMessageInfo:fakeMessageInfo];
  145. OCMVerifyAll(_mockExtensionHelper);
  146. }
  147. - (void)testDeliveryMetricsLoggingWithInvalidMessageID {
  148. OCMStub([_mockUtilClass isAppExtension]).andReturn(YES);
  149. NSDictionary *fakeMessageInfo = @{
  150. @"aps" : @{@"badge" : @9, @"mutable-content" : @1},
  151. @"fcm_options" : @{@"image" : @"https://google.com"},
  152. @"google.c.fid" : @"fakeFIDForTest",
  153. @"google.c.sender.id" : @123456789
  154. };
  155. OCMReject([_mockExtensionHelper bundleIdentifierByRemovingLastPartFrom:[OCMArg any]]);
  156. [_mockExtensionHelper exportDeliveryMetricsToBigQueryWithMessageInfo:fakeMessageInfo];
  157. OCMVerifyAll(_mockExtensionHelper);
  158. }
  159. - (void)testDeliveryMetricsLoggingWithInvalidFID {
  160. OCMStub([_mockUtilClass isAppExtension]).andReturn(YES);
  161. NSDictionary *fakeMessageInfo = @{
  162. @"aps" : @{@"badge" : @9, @"mutable-content" : @1},
  163. @"fcm_options" : @{@"image" : @"https://google.com"},
  164. @"google.c.sender.id" : @123456789
  165. };
  166. OCMReject([_mockExtensionHelper bundleIdentifierByRemovingLastPartFrom:[OCMArg any]]);
  167. [_mockExtensionHelper exportDeliveryMetricsToBigQueryWithMessageInfo:fakeMessageInfo];
  168. OCMVerifyAll(_mockExtensionHelper);
  169. }
  170. - (void)testDeliveryMetricsLoggingWithDisplayPayload {
  171. OCMStub([_mockUtilClass isAppExtension]).andReturn(YES);
  172. NSDictionary *fakeMessageInfo = @{
  173. @"aps" : @{@"badge" : @9, @"mutable-content" : @1},
  174. @"fcm_options" : @{@"image" : @"https://google.com"},
  175. @"gcm.message_id" : @"1627428480762269",
  176. @"google.c.fid" : @"fakeFIDForTest",
  177. @"google.c.sender.id" : @123456789
  178. };
  179. OCMExpect([_mockExtensionHelper bundleIdentifierByRemovingLastPartFrom:[OCMArg any]]);
  180. [_mockExtensionHelper exportDeliveryMetricsToBigQueryWithMessageInfo:fakeMessageInfo];
  181. OCMVerifyAll(_mockExtensionHelper);
  182. }
  183. - (void)testDeliveryMetricsLoggingWithDataPayload {
  184. OCMStub([_mockUtilClass isAppExtension]).andReturn(NO);
  185. NSDictionary *fakeMessageInfo = @{
  186. @"aps" : @{@"badge" : @9, @"content-available" : @1},
  187. @"fcm_options" : @{@"image" : @"https://google.com"},
  188. @"gcm.message_id" : @"1627428480762269",
  189. @"google.c.fid" : @"fakeFIDForTest",
  190. @"google.c.sender.id" : @123456789
  191. };
  192. OCMReject([_mockExtensionHelper bundleIdentifierByRemovingLastPartFrom:[OCMArg any]]);
  193. [_mockExtensionHelper exportDeliveryMetricsToBigQueryWithMessageInfo:fakeMessageInfo];
  194. OCMVerifyAll(_mockExtensionHelper);
  195. }
  196. @end
  197. #endif // TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_WATCH