-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyepcodeApi.storage.test.ts
More file actions
180 lines (146 loc) · 6.1 KB
/
Copy pathyepcodeApi.storage.test.ts
File metadata and controls
180 lines (146 loc) · 6.1 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
173
174
175
176
177
178
179
180
import { YepCodeApi, 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 api: YepCodeApi;
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("YepCodeApi", () => {
beforeAll(async () => {
api = new YepCodeApi({ apiHost, apiToken });
});
afterEach(async () => {
await api.deleteObject(testName).catch(() => {});
if (fs.existsSync(downloadedFile)) {
fs.unlinkSync(downloadedFile);
}
});
describe("getObjects", () => {
it("should return a list of storage objects", async () => {
const result: StorageObject[] = await api.getObjects();
expect(Array.isArray(result)).toBe(true);
});
});
describe("createObject", () => {
it("should create a storage object with a File", async () => {
const file: File = new File([readFileSync(testFilePath)], testName);
const result: StorageObject = await api.createObject({
name: 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 api.createObject({
name: testName,
file: fileBlob,
});
expect(result).toBeDefined();
});
it("should create a storage object with a stream", async () => {
const stream = fs.createReadStream(testFilePath);
const result: StorageObject = await api.createObject({
name: testName,
file: stream,
});
expect(result).toBeDefined();
});
});
describe("deleteObject", () => {
it("should delete a storage object", async () => {
const file: File = new File([readFileSync(testFilePath)], testName);
await api.createObject({ name: testName, file });
await api.deleteObject(testName);
});
});
describe("getObject", () => {
it("should get a storage object uploaded as File", async () => {
const file: File = new File([readFileSync(testFilePath)], testName);
await api.createObject({ name: testName, file });
const result: Readable = await api.getObject(testName);
await verifyDownloadedFile(result, downloadedFile, testFilePath);
});
it("should get a storage object uploaded as Blob", async () => {
const blob: Blob = new Blob([readFileSync(testFilePath)]);
await api.createObject({ name: testName, file: blob });
const result: Readable = await api.getObject(testName);
await verifyDownloadedFile(result, downloadedFile, testFilePath);
});
it("should get a storage object uploaded as stream", async () => {
const stream = fs.createReadStream(testFilePath);
await api.createObject({ name: testName, file: stream });
const result: Readable = await api.getObject(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 api.createObject({ name: testName, file });
const result: SignedUrl = await api.createSignedUrl({ path: 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 api.createObject({ name: testName, file });
const result: SignedUrl = await api.createSignedUrl({
path: 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 return content matching the original file when fetched", async () => {
const file: File = new File([readFileSync(testFilePath)], testName);
await api.createObject({ name: testName, file });
const { url } = await api.createSignedUrl({ path: testName });
const response = await fetch(url);
expect(response.ok).toBe(true);
const body = await response.text();
expect(body).toBe(readFileSync(testFilePath, "utf8"));
});
it("should throw a 404 when the file does not exist", async () => {
await expect(
api.createSignedUrl({ path: "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 api.createObject({ name: testName, file });
await expect(
api.createSignedUrl({ path: testName, expiresInSeconds: 999999 })
).rejects.toMatchObject({
name: "YepCodeApiError",
status: 400,
} as Partial<YepCodeApiError>);
});
});
});