2021-08-18 18:13:41 +02:00
|
|
|
const dotenv = require("dotenv");
|
|
|
|
const path = require("path");
|
|
|
|
const fse = require("fs-extra");
|
|
|
|
dotenv.config();
|
|
|
|
|
2021-08-29 18:05:46 +02:00
|
|
|
// TODO: write unittest to check if FileStorage.ts is working
|
|
|
|
// TODO: write unitest to check if env vars are defined
|
|
|
|
|
2021-08-18 18:13:41 +02:00
|
|
|
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("/")) {
|
2021-10-20 02:14:48 +02:00
|
|
|
process.env.STORAGE_LOCATION = path.join(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
process.env.STORAGE_LOCATION,
|
|
|
|
"/"
|
|
|
|
);
|
2021-08-18 18:13:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
process.env.STORAGE_LOCATION = path.join(__dirname, "..", "files", "/");
|
|
|
|
}
|
|
|
|
fse.ensureDirSync(process.env.STORAGE_LOCATION);
|
|
|
|
}
|
2021-08-18 13:57:23 +02:00
|
|
|
const { CDNServer } = require("../dist/Server");
|
2021-08-29 17:00:03 +02:00
|
|
|
const { Config } = require("@fosscord/util");
|
2021-08-18 13:57:23 +02:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2021-08-18 18:13:41 +02:00
|
|
|
describe("/ping", () => {
|
|
|
|
describe("GET", () => {
|
2021-08-18 18:46:53 +02:00
|
|
|
describe("without signature specified", () => {
|
|
|
|
test("route should respond with 200", async () => {
|
|
|
|
let response = await request.get("/ping");
|
|
|
|
expect(response.text).toBe("pong");
|
|
|
|
});
|
2021-08-18 18:13:41 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2021-08-18 18:46:53 +02:00
|
|
|
describe("with signature specified, without file specified", () => {
|
2021-08-18 18:13:41 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2021-08-18 18:46:53 +02:00
|
|
|
describe("with signature specified, with file specified ", () => {
|
|
|
|
test("route should respond with Content-type: application/json, 200 and res.body.url", async () => {
|
2021-08-18 18:13:41 +02:00
|
|
|
const response = await request
|
|
|
|
.post("/attachments/123456789")
|
|
|
|
.set({ signature: Config.get().security.requestSignature })
|
|
|
|
.attach("file", __dirname + "/antman.jpg");
|
|
|
|
expect(response.statusCode).toBe(200);
|
2021-10-20 02:14:48 +02:00
|
|
|
expect(response.headers["content-type"]).toEqual(
|
|
|
|
expect.stringContaining("json")
|
|
|
|
);
|
2021-08-18 18:13:41 +02:00
|
|
|
expect(response.body.url).toBeDefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-08-18 18:46:53 +02:00
|
|
|
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");
|
2021-10-20 02:14:48 +02:00
|
|
|
request
|
|
|
|
.get(response.body.url.replace("http://localhost:3003", ""))
|
|
|
|
.then((x) => {
|
|
|
|
expect(x.statusCode).toBe(200);
|
|
|
|
});
|
2021-08-18 18:46:53 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-08-18 18:13:41 +02:00
|
|
|
describe("DELETE", () => {
|
2021-08-18 19:29:16 +02:00
|
|
|
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");
|
2021-10-20 02:14:48 +02:00
|
|
|
request
|
|
|
|
.delete(
|
|
|
|
response.body.url.replace("http://localhost:3003", "")
|
|
|
|
)
|
|
|
|
.then((x) => {
|
|
|
|
expect(x.body.success).toBeDefined();
|
|
|
|
});
|
2021-08-18 19:29:16 +02:00
|
|
|
});
|
|
|
|
});
|
2021-08-18 13:57:23 +02:00
|
|
|
});
|
|
|
|
});
|
2021-08-29 17:11:01 +02:00
|
|
|
|
|
|
|
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);
|
2021-10-20 02:14:48 +02:00
|
|
|
expect(response.headers["content-type"]).toEqual(
|
|
|
|
expect.stringContaining("json")
|
|
|
|
);
|
2021-08-29 17:11:01 +02:00
|
|
|
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");
|
2021-10-20 02:14:48 +02:00
|
|
|
request
|
|
|
|
.get(response.body.url.replace("http://localhost:3003", ""))
|
|
|
|
.then((x) => {
|
|
|
|
expect(x.statusCode).toBe(200);
|
|
|
|
});
|
2021-08-29 17:11:01 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
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");
|
2021-10-20 02:14:48 +02:00
|
|
|
request
|
|
|
|
.delete(
|
|
|
|
response.body.url.replace("http://localhost:3003", "")
|
|
|
|
)
|
|
|
|
.then((x) => {
|
|
|
|
expect(x.body.success).toBeDefined();
|
|
|
|
});
|
2021-08-29 17:11:01 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-08-29 18:05:46 +02:00
|
|
|
|
|
|
|
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 })
|
2021-10-20 02:14:48 +02:00
|
|
|
.send({
|
|
|
|
url: "https://i.ytimg.com/vi_webp/TiXzhQr5AUc/mqdefault.webp",
|
|
|
|
});
|
2021-08-29 18:05:46 +02:00
|
|
|
expect(response.statusCode).toBe(200);
|
2021-10-20 02:14:48 +02:00
|
|
|
expect(response.headers["content-type"]).toEqual(
|
|
|
|
expect.stringContaining("json")
|
|
|
|
);
|
2021-08-29 18:05:46 +02:00
|
|
|
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 })
|
2021-10-20 02:14:48 +02:00
|
|
|
.send({
|
|
|
|
url: "https://i.ytimg.com/vi_webp/TiXzhQr5AUc/mqdefault.webp",
|
|
|
|
});
|
2021-08-29 18:05:46 +02:00
|
|
|
request.get(`external/${response.body.id}`).then((x) => {
|
|
|
|
expect(x.statusCode).toBe(200);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|