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
|
const dotenv = require("dotenv");
const path = require("path");
const fs = require("fs");
dotenv.config();
// TODO: write unittest to check if FileStorage.ts is working
// TODO: write unitest to check if env vars are defined
if (!process.env.STORAGE_PROVIDER) process.env.STORAGE_PROVIDER = "file";
// TODO:nodejs path.join trailing slash windows compatible
if (process.env.STORAGE_PROVIDER === "file") {
if (process.env.STORAGE_LOCATION) {
if (!process.env.STORAGE_LOCATION.startsWith("/")) {
process.env.STORAGE_LOCATION = path.join(__dirname, "..", process.env.STORAGE_LOCATION, "/");
}
} else {
process.env.STORAGE_LOCATION = path.join(__dirname, "..", "files", "/");
}
if (!fs.existsSync(process.env.STORAGE_LOCATION)) fs.mkdirSync(process.env.STORAGE_LOCATION, { recursive: true });
}
const { CDNServer } = require("../dist/Server");
const { Config } = require("@fosscord/util");
const supertest = require("supertest");
const request = supertest("http://localhost:3003");
const server = new CDNServer({ port: Number(process.env.PORT) || 3003 });
beforeAll(async () => {
await server.start();
return server;
});
afterAll(() => {
return server.stop();
});
describe("/ping", () => {
describe("GET", () => {
describe("without signature specified", () => {
test("route should respond with 200", async () => {
let response = await request.get("/ping");
expect(response.text).toBe("pong");
});
});
});
});
describe("/attachments", () => {
describe("POST", () => {
describe("without signature specified", () => {
test("route should respond with 400", async () => {
const response = await request.post("/attachments/123456789");
expect(response.statusCode).toBe(400);
});
});
describe("with signature specified, without file specified", () => {
test("route should respond with 400", async () => {
const response = await request.post("/attachments/123456789").set({ signature: Config.get().security.requestSignature });
expect(response.statusCode).toBe(400);
});
});
describe("with signature specified, with file specified ", () => {
test("route should respond with Content-type: application/json, 200 and res.body.url", async () => {
const response = await request
.post("/attachments/123456789")
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
expect(response.statusCode).toBe(200);
expect(response.headers["content-type"]).toEqual(expect.stringContaining("json"));
expect(response.body.url).toBeDefined();
});
});
});
describe("GET", () => {
describe("getting uploaded image by url returned by POST /attachments", () => {
test("route should respond with 200", async () => {
let response = await request
.post("/attachments/123456789")
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
request.get(response.body.url.replace("http://localhost:3003", "")).then((x) => {
expect(x.statusCode).toBe(200);
});
});
});
});
describe("DELETE", () => {
describe("deleting uploaded image by url returned by POST /attachments", () => {
test("route should respond with res.body.success", async () => {
let response = await request
.post("/attachments/123456789")
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
request.delete(response.body.url.replace("http://localhost:3003", "")).then((x) => {
expect(x.body.success).toBeDefined();
});
});
});
});
});
describe("/avatars", () => {
describe("POST", () => {
describe("without signature specified", () => {
test("route should respond with 400", async () => {
const response = await request.post("/avatars/123456789");
expect(response.statusCode).toBe(400);
});
});
describe("with signature specified, without file specified", () => {
test("route should respond with 400", async () => {
const response = await request.post("/avatars/123456789").set({ signature: Config.get().security.requestSignature });
expect(response.statusCode).toBe(400);
});
});
describe("with signature specified, with file specified ", () => {
test("route should respond with Content-type: application/json, 200 and res.body.url", async () => {
const response = await request
.post("/avatars/123456789")
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
expect(response.statusCode).toBe(200);
expect(response.headers["content-type"]).toEqual(expect.stringContaining("json"));
expect(response.body.url).toBeDefined();
});
});
});
describe("GET", () => {
describe("getting uploaded image by url returned by POST /avatars", () => {
test("route should respond with 200", async () => {
let response = await request
.post("/avatars/123456789")
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
request.get(response.body.url.replace("http://localhost:3003", "")).then((x) => {
expect(x.statusCode).toBe(200);
});
});
});
});
describe("DELETE", () => {
describe("deleting uploaded image by url returned by POST /avatars", () => {
test("route should respond with res.body.success", async () => {
let response = await request
.post("/avatars/123456789")
.set({ signature: Config.get().security.requestSignature })
.attach("file", __dirname + "/antman.jpg");
request.delete(response.body.url.replace("http://localhost:3003", "")).then((x) => {
expect(x.body.success).toBeDefined();
});
});
});
});
});
describe("/external", () => {
describe("POST", () => {
describe("without signature specified", () => {
test("route should respond with 400", async () => {
const response = await request.post("/external");
expect(response.statusCode).toBe(400);
});
});
describe("with signature specified, without file specified", () => {
test("route should respond with 400", async () => {
const response = await request.post("/external").set({ signature: Config.get().security.requestSignature });
expect(response.statusCode).toBe(400);
});
});
describe("with signature specified, with file specified ", () => {
test("route should respond with Content-type: application/json, 200 and res.body.url", async () => {
const response = await request.post("/external").set({ signature: Config.get().security.requestSignature }).send({
url: "https://i.ytimg.com/vi_webp/TiXzhQr5AUc/mqdefault.webp"
});
expect(response.statusCode).toBe(200);
expect(response.headers["content-type"]).toEqual(expect.stringContaining("json"));
expect(response.body.id).toBeDefined();
});
});
describe("with signature specified, with falsy url specified ", () => {
test("route should respond with 400", async () => {
const response = await request.post("/external").set({ signature: Config.get().security.requestSignature }).send({
url: "notavalidurl.123"
});
expect(response.statusCode).toBe(400);
});
});
});
describe("GET", () => {
describe("getting uploaded image by url returned by POST /avatars", () => {
test("route should respond with 200", async () => {
let response = await request.post("/external").set({ signature: Config.get().security.requestSignature }).send({
url: "https://i.ytimg.com/vi_webp/TiXzhQr5AUc/mqdefault.webp"
});
request.get(`external/${response.body.id}`).then((x) => {
expect(x.statusCode).toBe(200);
});
});
});
});
});
|