-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstorage.service.spec.ts
315 lines (270 loc) · 9.38 KB
/
storage.service.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { INestApplication } from "@nestjs/common";
import { Test } from "@nestjs/testing";
import { Server } from "http";
import { FirebaseModule, StorageService } from "../../index";
import { FirebaseEmulatorEnv } from "../../../e2e/firebase-emulator-env";
describe("Firebase Storage", () => {
let server: Server;
let app: INestApplication;
let storageService: StorageService;
beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [
FirebaseModule.forRoot({
appName: "storage-test",
apiKey: FirebaseEmulatorEnv.apiKey,
projectId: FirebaseEmulatorEnv.projectId,
storageBucket: FirebaseEmulatorEnv.storageBucket,
emulator: {
storage: {
host: FirebaseEmulatorEnv.storageHost,
port: FirebaseEmulatorEnv.storagePort,
},
},
}),
],
}).compile();
app = module.createNestApplication();
server = app.getHttpServer();
await app.init();
storageService = module.get<StorageService>(StorageService);
});
afterAll(async () => {
return await app.close();
});
const uint8ArrayPNG = new Uint8Array([
137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 8, 0,
0, 0, 8, 8, 2, 0, 0, 0, 75, 109, 41, 220, 0, 0, 0, 34, 73, 68, 65, 84, 8,
215, 99, 120, 173, 168, 135, 21, 49, 0, 241, 255, 15, 90, 104, 8, 33, 129,
83, 7, 97, 163, 136, 214, 129, 93, 2, 43, 2, 0, 181, 31, 90, 179, 225, 252,
176, 37, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130,
]);
// Create a Reference
it("should create a reference on storage root", () => {
const storageReference = storageService.ref();
expect(storageReference.toString()).toEqual(
`gs://${FirebaseEmulatorEnv.storageBucket}/`,
);
expect(storageReference.bucket).toEqual(FirebaseEmulatorEnv.storageBucket);
expect(storageReference.name).toMatchInlineSnapshot(`""`);
});
it("should create a reference on a folder", () => {
const storageReference = storageService.ref("cats");
expect(storageReference.toString()).toEqual(
`gs://${FirebaseEmulatorEnv.storageBucket}/cats`,
);
expect(storageReference.name).toMatchInlineSnapshot(`"cats"`);
});
// Upload Files
it(`should upload uint8Array`, async () => {
const fileName = Date.now().toString() + ".png";
await storageService
.uploadBytes(fileName, uint8ArrayPNG, {
contentType: "image/png",
customMetadata: {
firebaseStorageDownloadTokens: "custom-token-for-test",
},
})
.then((snapshot) => {
expect(snapshot.metadata.contentType).toMatchInlineSnapshot(
`"image/png"`,
);
});
const downloadUrl = await storageService.getDownloadURL(fileName);
expect(downloadUrl).toEqual(
`http://${FirebaseEmulatorEnv.storageHost}:${FirebaseEmulatorEnv.storagePort}/v0/b/default-bucket/o/${fileName}?alt=media&token=custom-token-for-test`,
);
});
it(`should upload string`, async () => {
const fileName = Date.now().toString() + ".txt";
await storageService
.uploadString(fileName, "text content", "raw", {
contentType: "text/plain",
customMetadata: {
firebaseStorageDownloadTokens: "custom-token-for-test",
},
})
.then((snapshot) => {
expect(snapshot.metadata.contentType).toMatchInlineSnapshot(
`"text/plain"`,
);
});
const downloadUrl = await storageService.getDownloadURL(fileName);
expect(downloadUrl).toEqual(
`http://${FirebaseEmulatorEnv.storageHost}:${FirebaseEmulatorEnv.storagePort}/v0/b/${FirebaseEmulatorEnv.storageBucket}/o/${fileName}?alt=media&token=custom-token-for-test`,
);
});
it(`should upload Bytes Resumable`, (done) => {
const fileName = Date.now().toString() + ".png";
let strEvents = "";
let sep = "";
const uploadTask = storageService.uploadBytesResumable(
fileName,
uint8ArrayPNG,
{
contentType: "image/png",
customMetadata: {
firebaseStorageDownloadTokens: "custom-token-for-test",
},
},
);
uploadTask.on(
"state_changed",
(snapshot) => {
const progress =
(snapshot.bytesTransferred / snapshot.totalBytes) * 100 + "%";
strEvents += sep + snapshot.state + " - " + progress;
sep = "\n";
},
() => {},
() => {
expect(strEvents).toMatchInlineSnapshot(`
"running - 0%
running - 100%"
`);
done();
},
);
});
// Download Files
it(`should fail unknown path`, async () => {
await storageService.getDownloadURL("unknown").catch((error) => {
expect(error).toMatchInlineSnapshot(
`[FirebaseError: Firebase Storage: Object 'unknown' does not exist. (storage/object-not-found)]`,
);
});
});
it(`should getDownloadURL (from emulator imported dataset)`, async () => {
expect(
await storageService.getDownloadURL("cats/snowflake.jpg"),
).toMatchInlineSnapshot(
`"http://localhost:9199/v0/b/default-bucket/o/cats%2Fsnowflake.jpg?alt=media&token=1f6f9332-150b-4a91-80e7-8732ac84265e"`,
);
});
it(`should getBytes`, async () => {
const buffer = await storageService.getBytes("cats/snowflake.jpg");
expect(buffer.byteLength).toEqual(101863);
});
it(`should getStream`, async () => {
const fileName = Date.now().toString() + ".txt";
await storageService.uploadString(fileName, "text content", "raw", {
contentType: "text/plain",
});
const stream = await storageService.getStream(fileName);
const chunks = [];
stream.on("data", function (chunk) {
chunks.push(chunk);
});
return await new Promise((fulfill) => stream.on("end", fulfill)).then(
() => {
expect(Buffer.concat(chunks).toString()).toMatchInlineSnapshot(
`"text content"`,
);
},
);
});
it(`should getMetadata`, async () => {
const metadata = await storageService.getMetadata("cats/snowflake.jpg");
const keys = ["contentType", "fullPath", "name", "size", "type"];
const filtered = keys.reduce(
(obj, key) => ({ ...obj, [key]: metadata[key] }),
{},
);
expect(filtered).toMatchInlineSnapshot(`
{
"contentType": "image/jpeg",
"fullPath": "cats/snowflake.jpg",
"name": "snowflake.jpg",
"size": 101863,
"type": "file",
}
`);
});
it(`should updateMetadata`, async () => {
const fileName = Date.now().toString() + ".txt";
await storageService.uploadString(fileName, "text content", "raw", {
contentType: "text/plain",
});
const metadataBefore = await storageService.getMetadata(fileName);
const keys = ["contentLanguage", "contentType", "size", "type"];
const filteredBefore = keys.reduce(
(obj, key) => ({ ...obj, [key]: metadataBefore[key] }),
{},
);
expect(filteredBefore).toMatchInlineSnapshot(`
{
"contentLanguage": undefined,
"contentType": "text/plain",
"size": 12,
"type": "file",
}
`);
await storageService.updateMetadata(fileName, { contentLanguage: "en-US" });
const metadataAfter = await storageService.getMetadata(fileName);
const filteredAfter = keys.reduce(
(obj, key) => ({ ...obj, [key]: metadataAfter[key] }),
{},
);
expect(filteredAfter).toMatchInlineSnapshot(`
{
"contentLanguage": "en-US",
"contentType": "text/plain",
"size": 12,
"type": "file",
}
`);
});
// Delete Files
it(`should deleteObject`, async () => {
const fileName = Date.now().toString() + ".txt";
await storageService.uploadString(fileName, "text content", "raw", {
contentType: "text/plain",
customMetadata: {
firebaseStorageDownloadTokens: "custom-token-for-test",
},
});
expect(await storageService.getDownloadURL(fileName)).toEqual(
`http://${FirebaseEmulatorEnv.storageHost}:${FirebaseEmulatorEnv.storagePort}/v0/b/${FirebaseEmulatorEnv.storageBucket}/o/${fileName}?alt=media&token=custom-token-for-test`,
);
await storageService.deleteObject(fileName);
await storageService.getDownloadURL(fileName).catch((error) => {
expect(error).toMatchInlineSnapshot(
`[FirebaseError: Firebase Storage: Object '${fileName}' does not exist. (storage/object-not-found)]`,
);
});
});
it(`should ListAll`, async () => {
expect((await storageService.listAll("cats")).items.map((ref) => ref.name))
.toMatchInlineSnapshot(`
[
"jellybean.jpg",
"marshmallow.jpg",
"minnie.jpg",
"puffin.jpg",
"snowflake.jpg",
]
`);
});
it(`should ListAll`, async () => {
const firstPage = await storageService.list("cats", { maxResults: 3 });
expect(firstPage.items.map((ref) => ref.name)).toMatchInlineSnapshot(`
[
"jellybean.jpg",
"marshmallow.jpg",
"minnie.jpg",
]
`);
expect(firstPage.nextPageToken).toMatchInlineSnapshot(`"cats/puffin.jpg"`);
const secondPage = await storageService.list("cats", {
maxResults: 3,
pageToken: firstPage.nextPageToken,
});
expect(secondPage.items.map((ref) => ref.name)).toMatchInlineSnapshot(`
[
"puffin.jpg",
"snowflake.jpg",
]
`);
expect(secondPage.nextPageToken).toMatchInlineSnapshot(`undefined`);
});
});