From dad0506df40f89c0b1fe9d2ae0d69fbe4035d29f Mon Sep 17 00:00:00 2001 From: xnacly Date: Sun, 29 Aug 2021 17:11:01 +0200 Subject: added /avatars unittests [cdn] --- cdn/tests/server.test.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'cdn/tests/server.test.js') diff --git a/cdn/tests/server.test.js b/cdn/tests/server.test.js index 13046034..6f06c2b6 100644 --- a/cdn/tests/server.test.js +++ b/cdn/tests/server.test.js @@ -67,7 +67,6 @@ describe("/attachments", () => { expect(response.statusCode).toBe(200); expect(response.headers["content-type"]).toEqual(expect.stringContaining("json")); expect(response.body.url).toBeDefined(); - attachment_url = response.body.url; }); }); }); @@ -98,3 +97,59 @@ describe("/attachments", () => { }); }); }); + +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(); + }); + }); + }); + }); +}); -- cgit 1.5.1 From 6ceb710df97da772b5f570f78bbd6b7ae455721c Mon Sep 17 00:00:00 2001 From: xnacly Date: Sun, 29 Aug 2021 18:05:46 +0200 Subject: added /external unit tests [cdn] --- cdn/tests/cdn_endpoints.test.js | 211 ++++++++++++++++++++++++++++++++++++++++ cdn/tests/server.test.js | 155 ----------------------------- 2 files changed, 211 insertions(+), 155 deletions(-) create mode 100644 cdn/tests/cdn_endpoints.test.js delete mode 100644 cdn/tests/server.test.js (limited to 'cdn/tests/server.test.js') diff --git a/cdn/tests/cdn_endpoints.test.js b/cdn/tests/cdn_endpoints.test.js new file mode 100644 index 00000000..a133d0dd --- /dev/null +++ b/cdn/tests/cdn_endpoints.test.js @@ -0,0 +1,211 @@ +const dotenv = require("dotenv"); +const path = require("path"); +const fse = require("fs-extra"); +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", "/"); + } + fse.ensureDirSync(process.env.STORAGE_LOCATION); +} +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); + }); + }); + }); + }); +}); diff --git a/cdn/tests/server.test.js b/cdn/tests/server.test.js deleted file mode 100644 index 6f06c2b6..00000000 --- a/cdn/tests/server.test.js +++ /dev/null @@ -1,155 +0,0 @@ -const dotenv = require("dotenv"); -const path = require("path"); -const fse = require("fs-extra"); -dotenv.config(); - -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", "/"); - } - fse.ensureDirSync(process.env.STORAGE_LOCATION); -} - -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(); - }); - }); - }); - }); -}); -- cgit 1.5.1