FIRMessagingExtensionHelperTest.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <XCTest/XCTest.h>
  17. #import "OCMock.h"
  18. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  19. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessagingExtensionHelper.h"
  20. API_AVAILABLE(macos(10.14), ios(10.0))
  21. typedef void (^FIRMessagingContentHandler)(UNNotificationContent *content);
  22. #if TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_WATCH
  23. static NSString *const kFCMPayloadOptionsName = @"fcm_options";
  24. static NSString *const kFCMPayloadOptionsImageURLName = @"image";
  25. static NSString *const kValidImageURL =
  26. @"https://firebasestorage.googleapis.com/v0/b/fcm-ios-f7f9c.appspot.com/o/"
  27. @"chubbyBunny.jpg?alt=media&token=d6c56a57-c007-4b27-b20f-f267cc83e9e5";
  28. @interface FIRMessagingExtensionHelper (ExposedForTest)
  29. - (void)loadAttachmentForURL:(NSURL *)attachmentURL
  30. completionHandler:(void (^)(UNNotificationAttachment *))completionHandler;
  31. - (NSString *)fileExtensionForResponse:(NSURLResponse *)response;
  32. @end
  33. @interface FIRMessagingExtensionHelperTest : XCTestCase {
  34. id _mockExtensionHelper;
  35. id _mockURLResponse;
  36. }
  37. @end
  38. @implementation FIRMessagingExtensionHelperTest
  39. - (void)setUp {
  40. [super setUp];
  41. if (@available(macOS 10.14, iOS 10.0, *)) {
  42. FIRMessagingExtensionHelper *extensionHelper = [FIRMessaging extensionHelper];
  43. _mockExtensionHelper = OCMPartialMock(extensionHelper);
  44. _mockURLResponse = OCMClassMock([NSURLResponse class]);
  45. } else {
  46. // Fallback on earlier versions
  47. }
  48. }
  49. - (void)tearDown {
  50. [_mockExtensionHelper stopMocking];
  51. [_mockURLResponse stopMocking];
  52. }
  53. #ifdef COCOAPODS
  54. // This test requires internet access.
  55. - (void)testModifyNotificationWithValidPayloadData {
  56. if (@available(macOS 10.14, iOS 10.0, *)) {
  57. XCTestExpectation *validPayloadExpectation =
  58. [self expectationWithDescription:@"Test payload is valid."];
  59. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  60. content.userInfo =
  61. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : kValidImageURL}};
  62. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  63. [validPayloadExpectation fulfill];
  64. };
  65. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  66. OCMVerify([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  67. completionHandler:[OCMArg any]]);
  68. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  69. }
  70. }
  71. #endif
  72. - (void)testModifyNotificationWithInvalidPayloadData {
  73. if (@available(macOS 10.14, iOS 10.0, *)) {
  74. XCTestExpectation *validPayloadExpectation =
  75. [self expectationWithDescription:@"Test payload is valid."];
  76. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  77. content.userInfo =
  78. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}};
  79. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  80. [validPayloadExpectation fulfill];
  81. };
  82. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  83. OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  84. completionHandler:[OCMArg any]]);
  85. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  86. }
  87. }
  88. - (void)testModifyNotificationWithEmptyPayloadData {
  89. if (@available(macOS 10.14, iOS 10.0, *)) {
  90. XCTestExpectation *validPayloadExpectation =
  91. [self expectationWithDescription:@"Test payload is valid."];
  92. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  93. content.userInfo =
  94. @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}};
  95. FIRMessagingContentHandler handler = ^(UNNotificationContent *content) {
  96. [validPayloadExpectation fulfill];
  97. };
  98. [_mockExtensionHelper populateNotificationContent:content withContentHandler:handler];
  99. OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any]
  100. completionHandler:[OCMArg any]]);
  101. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  102. }
  103. }
  104. - (void)testModifyNotificationWithValidPayloadDataNoMimeType {
  105. if (@available(macOS 10.14, iOS 10.0, *)) {
  106. NSString *const kValidTestURL = @"test.jpg";
  107. NSString *const kValidTestExtension = @".jpg";
  108. OCMStub([_mockURLResponse suggestedFilename]).andReturn(kValidTestURL);
  109. NSString *const extension = [_mockExtensionHelper fileExtensionForResponse:_mockURLResponse];
  110. XCTAssertTrue([extension isEqualToString:kValidTestExtension]);
  111. }
  112. }
  113. - (void)testModifyNotificationWithInvalidPayloadDataInvalidMimeType {
  114. if (@available(macOS 10.14, iOS 10.0, *)) {
  115. NSString *const kInvalidTestURL = @"test";
  116. NSString *const kInvalidTestExtension = @"";
  117. OCMStub([_mockURLResponse suggestedFilename]).andReturn(kInvalidTestURL);
  118. OCMStub([_mockURLResponse MIMEType]).andReturn(nil);
  119. NSString *const extension = [_mockExtensionHelper fileExtensionForResponse:_mockURLResponse];
  120. XCTAssertTrue([extension isEqualToString:kInvalidTestExtension]);
  121. }
  122. }
  123. - (void)testModifyNotificationWithInvalidPayloadDataValidMimeType {
  124. if (@available(macOS 10.14, iOS 10.0, *)) {
  125. NSString *const kValidMIMETypeTestURL = @"test";
  126. NSString *const kValidMIMETypeTestMIMEType = @"image/jpeg";
  127. NSString *const kValidMIMETypeTestExtension = @".jpeg";
  128. OCMStub([_mockURLResponse suggestedFilename]).andReturn(kValidMIMETypeTestURL);
  129. OCMStub([_mockURLResponse MIMEType]).andReturn(kValidMIMETypeTestMIMEType);
  130. NSString *const extension = [_mockExtensionHelper fileExtensionForResponse:_mockURLResponse];
  131. XCTAssertTrue([extension isEqualToString:kValidMIMETypeTestExtension]);
  132. }
  133. }
  134. @end
  135. #endif