FIRCLSPackageReportsOperationTests.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <Foundation/Foundation.h>
  15. #import <XCTest/XCTest.h>
  16. #import "FABMockApplicationIdentifierModel.h"
  17. #import "FIRCLSInternalReport.h"
  18. #import "FIRCLSMockSettings.h"
  19. #import "FIRCLSPackageReportOperation.h"
  20. #import "FIRCLSSettings.h"
  21. #import "FIRCLSTempMockFileManager.h"
  22. NSString *const TestOrgID = @"TestOrgID";
  23. @interface FIRCLSPackageReportsOperationTests : XCTestCase
  24. @property(nonatomic, strong) FIRCLSFileManager *fileManager;
  25. @property(nonatomic, strong) FIRCLSMockSettings *settings;
  26. @property(nonatomic, copy) NSString *packagedPath;
  27. @property(nonatomic, copy) NSString *reportPath;
  28. @end
  29. @implementation FIRCLSPackageReportsOperationTests
  30. - (void)setUp {
  31. [super setUp];
  32. FABMockApplicationIdentifierModel *appIDModel = [[FABMockApplicationIdentifierModel alloc] init];
  33. self.settings = [[FIRCLSMockSettings alloc] initWithFileManager:self.fileManager
  34. appIDModel:appIDModel];
  35. FIRCLSTempMockFileManager *manager = [[FIRCLSTempMockFileManager alloc] init];
  36. self.fileManager = manager;
  37. assert([manager createReportDirectories]);
  38. }
  39. - (void)tearDown {
  40. NSFileManager *fileManager = [NSFileManager defaultManager];
  41. [fileManager removeItemAtPath:self.packagedPath error:nil];
  42. [fileManager removeItemAtPath:self.reportPath error:nil];
  43. [super tearDown];
  44. }
  45. - (void)testBasicPackaging {
  46. // Mock the Organization ID because we will not package reports without one
  47. self.settings.orgID = TestOrgID;
  48. NSFileManager *fileManager = [NSFileManager defaultManager];
  49. NSString *reportPath =
  50. [NSTemporaryDirectory() stringByAppendingPathComponent:@"execution_identifier"];
  51. FIRCLSInternalReport *report =
  52. [[FIRCLSInternalReport alloc] initWithPath:reportPath
  53. executionIdentifier:@"execution_identifier"];
  54. assert(report.identifier);
  55. // create the directory path
  56. assert([self.fileManager createDirectoryAtPath:[report path]]);
  57. // put some test files in it
  58. XCTAssertTrue([fileManager createFileAtPath:[report pathForContentFile:@"file1.txt"]
  59. contents:[@"contents" dataUsingEncoding:NSUTF8StringEncoding]
  60. attributes:nil],
  61. @"");
  62. XCTAssertTrue([fileManager createFileAtPath:[report pathForContentFile:@"file2.txt"]
  63. contents:[@"contents" dataUsingEncoding:NSUTF8StringEncoding]
  64. attributes:nil],
  65. @"");
  66. // and now generate a valid metadata file and put that in place too
  67. NSData *metadataData =
  68. [@"{\"identity\":{\"api_key\":\"my_key\",\"session_id\":\"my_session_id\"}}\n"
  69. dataUsingEncoding:NSUTF8StringEncoding];
  70. XCTAssertTrue([fileManager createFileAtPath:[report metadataPath]
  71. contents:metadataData
  72. attributes:nil],
  73. @"");
  74. // Now, actually run the operation
  75. FIRCLSPackageReportOperation *packageOperation =
  76. [[FIRCLSPackageReportOperation alloc] initWithReport:report
  77. fileManager:self.fileManager
  78. settings:self.settings];
  79. [packageOperation start];
  80. self.packagedPath = [packageOperation finalPath];
  81. self.reportPath = [report path];
  82. // And verify the results
  83. XCTAssertNotNil(self.packagedPath, @"Packaging should succeed");
  84. XCTAssertTrue([fileManager fileExistsAtPath:self.reportPath],
  85. @"The original report directory should not be removed");
  86. XCTAssertTrue([fileManager fileExistsAtPath:self.packagedPath],
  87. @"The multipart mime structure should be present");
  88. }
  89. - (void)testPackagingNoOrgID {
  90. // Mock the Organization ID because we will not package reports without one
  91. self.settings.orgID = nil;
  92. NSFileManager *fileManager = [NSFileManager defaultManager];
  93. NSString *reportPath =
  94. [NSTemporaryDirectory() stringByAppendingPathComponent:@"execution_identifier"];
  95. FIRCLSInternalReport *report =
  96. [[FIRCLSInternalReport alloc] initWithPath:reportPath
  97. executionIdentifier:@"execution_identifier"];
  98. // Now, actually run the operation
  99. FIRCLSPackageReportOperation *packageOperation =
  100. [[FIRCLSPackageReportOperation alloc] initWithReport:report
  101. fileManager:self.fileManager
  102. settings:self.settings];
  103. [packageOperation start];
  104. self.packagedPath = [packageOperation finalPath];
  105. self.reportPath = [report path];
  106. // And verify the results
  107. XCTAssertNil(self.packagedPath, @"Packaging should fail");
  108. XCTAssertFalse([fileManager fileExistsAtPath:self.reportPath],
  109. @"The original report directory should be removed");
  110. XCTAssertFalse([fileManager fileExistsAtPath:self.packagedPath],
  111. @"The multipart mime structure should be present");
  112. }
  113. @end