FIRStorageTests.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2017 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 "FIRStorageTestHelpers.h"
  15. #import "FIRStorageReference.h"
  16. #import "FIRStorageReference_Private.h"
  17. #import "FIRStorage_Private.h"
  18. @interface FIRStorageTests : XCTestCase
  19. @property(strong, nonatomic) id app;
  20. @end
  21. @implementation FIRStorageTests
  22. - (void)setUp {
  23. [super setUp];
  24. id mockOptions = OCMClassMock([FIROptions class]);
  25. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  26. self.app = OCMClassMock([FIRApp class]);
  27. OCMStub([self.app name]).andReturn(kFIRStorageAppName);
  28. OCMStub([(FIRApp *)self.app options]).andReturn(mockOptions);
  29. }
  30. - (void)tearDown {
  31. self.app = nil;
  32. [super tearDown];
  33. }
  34. - (void)testBucketNotEnforced {
  35. FIROptions * mockOptions = OCMClassMock([FIROptions class]);
  36. OCMStub([mockOptions storageBucket]).andReturn(@"");
  37. FIRApp *app = OCMClassMock([FIRApp class]);
  38. OCMStub([app name]).andReturn(kFIRStorageAppName);
  39. OCMStub([(FIRApp *)app options]).andReturn(mockOptions);
  40. FIRStorage *storage = [FIRStorage storageForApp:app];
  41. [storage referenceForURL:@"gs://benwu-test1.storage.firebase.com/child"];
  42. [storage referenceForURL:@"gs://benwu-test2.storage.firebase.com/child"];
  43. }
  44. - (void)testBucketEnforced {
  45. FIRStorage *storage = [FIRStorage storageForApp:self.app
  46. URL:@"gs://benwu-test1.storage.firebase.com"];
  47. [storage referenceForURL:@"gs://benwu-test1.storage.firebase.com/child"];
  48. storage = [FIRStorage storageForApp:self.app URL:@"gs://benwu-test1.storage.firebase.com/"];
  49. [storage referenceForURL:@"gs://benwu-test1.storage.firebase.com/child"];
  50. XCTAssertThrows([storage referenceForURL:@"gs://benwu-test2.storage.firebase.com/child"]);
  51. }
  52. - (void) testInitWithCustomUrl {
  53. FIRStorage *storage = [FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com"];
  54. XCTAssertEqualObjects(@"gs://foo-bar.appspot.com/", [[storage reference] description]);
  55. storage = [FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com/"];
  56. XCTAssertEqualObjects(@"gs://foo-bar.appspot.com/", [[storage reference] description]);
  57. }
  58. - (void) testInitWithWrongScheme {
  59. XCTAssertThrows([FIRStorage storageForApp:self.app URL:@"http://foo-bar.appspot.com"]);
  60. }
  61. - (void) testInitWithNoScheme {
  62. XCTAssertThrows([FIRStorage storageForApp:self.app URL:@"foo-bar.appspot.com"]);
  63. }
  64. - (void) testInitWithNilURL {
  65. XCTAssertThrows([FIRStorage storageForApp:self.app URL:nil]);
  66. }
  67. - (void) testInitWithPath {
  68. XCTAssertThrows([FIRStorage storageForApp:self.app URL:@"gs://foo-bar.appspot.com/child"]);
  69. }
  70. - (void) testInitWithDefaultAndCustomUrl {
  71. FIRStorage *customInstance = [FIRStorage storageForApp:self.app
  72. URL:@"gs://foo-bar.appspot.com"];
  73. FIRStorage *defaultInstance = [FIRStorage storageForApp:self.app];
  74. XCTAssertEqualObjects(@"gs://foo-bar.appspot.com/", [[customInstance reference] description]);
  75. XCTAssertEqualObjects(@"gs://bucket/", [[defaultInstance reference] description]);
  76. }
  77. - (void)testStorageDefaultApp {
  78. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  79. XCTAssertEqualObjects(storage.app.name, ((FIRApp *)self.app).name);
  80. XCTAssertNotNil(storage.fetcherServiceForApp);
  81. }
  82. - (void)testStorageCustomApp {
  83. id mockOptions = OCMClassMock([FIROptions class]);
  84. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  85. id secondApp = OCMClassMock([FIRApp class]);
  86. OCMStub([secondApp name]).andReturn(@"secondApp");
  87. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  88. FIRStorage *storage = [FIRStorage storageForApp:secondApp];
  89. XCTAssertNotEqual(storage.app.name, ((FIRApp *)self.app).name);
  90. XCTAssertNotNil(storage.fetcherServiceForApp);
  91. XCTAssertNotEqualObjects(storage.fetcherServiceForApp,
  92. [FIRStorage storageForApp:self.app].fetcherServiceForApp);
  93. }
  94. - (void)testStorageNoBucketInConfig {
  95. id mockOptions = OCMClassMock([FIROptions class]);
  96. OCMStub([mockOptions storageBucket]).andReturn(nil);
  97. id secondApp = OCMClassMock([FIRApp class]);
  98. OCMStub([secondApp name]).andReturn(@"secondApp");
  99. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  100. XCTAssertThrows([FIRStorage storageForApp:secondApp]);
  101. }
  102. - (void)testStorageEmptyBucketInConfig {
  103. id mockOptions = OCMClassMock([FIROptions class]);
  104. OCMStub([mockOptions storageBucket]).andReturn(@"");
  105. id secondApp = OCMClassMock([FIRApp class]);
  106. OCMStub([secondApp name]).andReturn(@"secondApp");
  107. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  108. FIRStorage *storage = [FIRStorage storageForApp:secondApp];
  109. FIRStorageReference *storageRef = [storage referenceForURL:@"gs://bucket/path/to/object"];
  110. XCTAssertEqualObjects(storageRef.bucket, @"bucket");
  111. }
  112. - (void)testStorageWrongBucketInConfig {
  113. id mockOptions = OCMClassMock([FIROptions class]);
  114. OCMStub([mockOptions storageBucket]).andReturn(@"notMyBucket");
  115. id secondApp = OCMClassMock([FIRApp class]);
  116. OCMStub([secondApp name]).andReturn(@"secondApp");
  117. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  118. FIRStorage *storage = [FIRStorage storageForApp:secondApp];
  119. XCTAssertEqualObjects([(FIRApp *)secondApp options].storageBucket, @"notMyBucket");
  120. XCTAssertThrows([storage referenceForURL:@"gs://bucket/path/to/object"]);
  121. }
  122. - (void)testRefDefaultApp {
  123. FIRStorageReference *convenienceRef =
  124. [[FIRStorage storageForApp:self.app] referenceForURL:@"gs://bucket/path/to/object"];
  125. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  126. FIRStorageReference *builtRef =
  127. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:self.app] path:path];
  128. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  129. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  130. }
  131. - (void)testRefCustomApp {
  132. id mockOptions = OCMClassMock([FIROptions class]);
  133. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  134. id secondApp = OCMClassMock([FIRApp class]);
  135. OCMStub([secondApp name]).andReturn(@"secondApp");
  136. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  137. FIRStorageReference *convenienceRef =
  138. [[FIRStorage storageForApp:secondApp] referenceForURL:@"gs://bucket/path/to/object"];
  139. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  140. FIRStorageReference *builtRef =
  141. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:secondApp] path:path];
  142. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  143. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  144. }
  145. - (void)testRootRefDefaultApp {
  146. FIRStorageReference *convenienceRef = [[FIRStorage storageForApp:self.app] reference];
  147. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:nil];
  148. FIRStorageReference *builtRef =
  149. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:self.app] path:path];
  150. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  151. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  152. }
  153. - (void)testRefWithPathDefaultApp {
  154. FIRStorageReference *convenienceRef =
  155. [[FIRStorage storageForApp:self.app] referenceWithPath:@"path/to/object"];
  156. FIRStoragePath *path = [[FIRStoragePath alloc] initWithBucket:@"bucket" object:@"path/to/object"];
  157. FIRStorageReference *builtRef =
  158. [[FIRStorageReference alloc] initWithStorage:[FIRStorage storageForApp:self.app] path:path];
  159. XCTAssertEqualObjects([convenienceRef description], [builtRef description]);
  160. XCTAssertEqualObjects(convenienceRef.storage.app, builtRef.storage.app);
  161. }
  162. - (void)testEqual {
  163. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  164. FIRStorage *copy = [storage copy];
  165. XCTAssertEqualObjects(storage.app.name, copy.app.name);
  166. }
  167. - (void)testNotEqual {
  168. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  169. id mockOptions = OCMClassMock([FIROptions class]);
  170. OCMStub([mockOptions storageBucket]).andReturn(@"bucket");
  171. id secondApp = OCMClassMock([FIRApp class]);
  172. OCMStub([secondApp name]).andReturn(@"secondApp");
  173. OCMStub([(FIRApp *)secondApp options]).andReturn(mockOptions);
  174. FIRStorage *secondStorage = [FIRStorage storageForApp:secondApp];
  175. XCTAssertNotEqualObjects(storage, secondStorage);
  176. }
  177. - (void)testHash {
  178. FIRStorage *storage = [FIRStorage storageForApp:self.app];
  179. FIRStorage *copy = [storage copy];
  180. XCTAssertEqual([storage hash], [copy hash]);
  181. }
  182. @end