-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyepcodeStorage.test.ts
More file actions
172 lines (138 loc) · 5.89 KB
/
Copy pathyepcodeStorage.test.ts
File metadata and controls
172 lines (138 loc) · 5.89 KB
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
import { YepCodeStorage } from "../../src/storage";
import { YepCodeApiError } from "../../src/api/yepcodeApi";
import { SignedUrl, StorageObject } from "../../src/api/types";
import fs, { createWriteStream, readFileSync } from "fs";
import path from "path";
import { Readable } from "stream";
const testName = "test-run-sdk.txt";
const testFilePath = path.join(__dirname, testName);
const downloadedFile = path.join(__dirname, "./downloaded_test.json");
const apiHost = process.env.YEPCODE_API_HOST;
const apiToken = process.env.YEPCODE_API_TOKEN;
let storage: YepCodeStorage;
const verifyDownloadedFile = async (
result: Readable,
downloadedFile: string,
testFilePath: string
) => {
const fileStream = createWriteStream(downloadedFile);
await new Promise<void>((resolve, reject) => {
result.pipe(fileStream).on("finish", resolve).on("error", reject);
});
expect(fs.existsSync(downloadedFile)).toBe(true);
const downloadedContent = readFileSync(downloadedFile, "utf8");
const originalContent = readFileSync(testFilePath, "utf8");
expect(downloadedContent).toBe(originalContent);
};
describe.skip("YepCodeStorage", () => {
beforeAll(async () => {
storage = new YepCodeStorage({ apiHost, apiToken });
});
afterEach(async () => {
await storage.delete(testName).catch(() => {});
if (fs.existsSync(downloadedFile)) {
fs.unlinkSync(downloadedFile);
}
});
describe("getObjects", () => {
it("should return a list of storage objects", async () => {
const result: StorageObject[] = await storage.list();
expect(Array.isArray(result)).toBe(true);
});
it("should return a list of storage objects with a prefix", async () => {
const file: File = new File([readFileSync(testFilePath)], testName);
await storage.upload(testName, file);
const result: StorageObject[] = await storage.list({
prefix: "test-run",
});
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(1);
expect(result[0].name).toBe(testName);
});
});
describe("createObject", () => {
it("should create a storage object with a File", async () => {
const file: File = new File([readFileSync(testFilePath)], testName);
const result: StorageObject = await storage.upload(testName, file);
expect(result).toBeDefined();
});
it("should create a storage object with a Blob", async () => {
const fileBlob: Blob = new Blob([readFileSync(testFilePath)]);
const result: StorageObject = await storage.upload(testName, fileBlob);
expect(result).toBeDefined();
});
it("should create a storage object with a stream", async () => {
const stream = fs.createReadStream(testFilePath);
const result: StorageObject = await storage.upload(testName, stream);
expect(result).toBeDefined();
});
});
describe("deleteObject", () => {
it("should delete a storage object", async () => {
const file: File = new File([readFileSync(testFilePath)], testName);
await storage.upload(testName, file);
await storage.delete(testName);
});
});
describe("getObject", () => {
it("should get a storage object uploaded as File", async () => {
const file: File = new File([readFileSync(testFilePath)], testName);
await storage.upload(testName, file);
const result: Readable = await storage.download(testName);
await verifyDownloadedFile(result, downloadedFile, testFilePath);
});
it("should get a storage object uploaded as Blob", async () => {
const blob: Blob = new Blob([readFileSync(testFilePath)]);
await storage.upload(testName, blob);
const result: Readable = await storage.download(testName);
await verifyDownloadedFile(result, downloadedFile, testFilePath);
});
it("should get a storage object uploaded as stream", async () => {
const stream = fs.createReadStream(testFilePath);
await storage.upload(testName, stream);
const result: Readable = await storage.download(testName);
await verifyDownloadedFile(result, downloadedFile, testFilePath);
});
});
describe("createSignedUrl", () => {
it("should return a signed url with the default expiry", async () => {
const file: File = new File([readFileSync(testFilePath)], testName);
await storage.upload(testName, file);
const result: SignedUrl = await storage.createSignedUrl(testName);
expect(typeof result.url).toBe("string");
expect(result.url.length).toBeGreaterThan(0);
expect(result.path).toBe(testName);
const expiresAt = new Date(result.expiresAt).getTime();
const expectedExpiry = Date.now() + 3600 * 1000;
expect(Math.abs(expiresAt - expectedExpiry)).toBeLessThan(60 * 1000);
});
it("should return a signed url with a custom expiry", async () => {
const file: File = new File([readFileSync(testFilePath)], testName);
await storage.upload(testName, file);
const result: SignedUrl = await storage.createSignedUrl(testName, {
expiresInSeconds: 60,
});
const expiresAt = new Date(result.expiresAt).getTime();
const expectedExpiry = Date.now() + 60 * 1000;
expect(Math.abs(expiresAt - expectedExpiry)).toBeLessThan(30 * 1000);
});
it("should throw a 404 when the file does not exist", async () => {
await expect(
storage.createSignedUrl("does-not-exist.txt")
).rejects.toMatchObject({
name: "YepCodeApiError",
status: 404,
} as Partial<YepCodeApiError>);
});
it("should throw a 400 when expiresInSeconds is out of range", async () => {
const file: File = new File([readFileSync(testFilePath)], testName);
await storage.upload(testName, file);
await expect(
storage.createSignedUrl(testName, { expiresInSeconds: 999999 })
).rejects.toMatchObject({
name: "YepCodeApiError",
status: 400,
} as Partial<YepCodeApiError>);
});
});
});