summary refs log tree commit diff
diff options
context:
space:
mode:
authorxnacly <matteogropp@protonmail.com>2021-08-18 18:13:41 +0200
committerxnacly <matteogropp@protonmail.com>2021-08-18 18:13:41 +0200
commit533a501427518e6c95f18577780f9d6baf70aa56 (patch)
tree04379d135d9807419b1109289d5c3e5c8c3f507b
parentmoved import (diff)
downloadserver-533a501427518e6c95f18577780f9d6baf70aa56.tar.xz
added unittests for attachments [post]
-rw-r--r--cdn/tests/antman.jpgbin0 -> 259112 bytes
-rw-r--r--cdn/tests/start.test.js79
2 files changed, 74 insertions, 5 deletions
diff --git a/cdn/tests/antman.jpg b/cdn/tests/antman.jpg
new file mode 100644
index 00000000..56af9063
--- /dev/null
+++ b/cdn/tests/antman.jpg
Binary files differdiff --git a/cdn/tests/start.test.js b/cdn/tests/start.test.js
index 348405de..5cfc27a8 100644
--- a/cdn/tests/start.test.js
+++ b/cdn/tests/start.test.js
@@ -1,5 +1,23 @@
+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 { db } = require("@fosscord/util");
+const { db, Config } = require("@fosscord/util");
 const supertest = require("supertest");
 const request = supertest("http://localhost:3003");
 const server = new CDNServer({ port: Number(process.env.PORT) || 3003 });
@@ -14,9 +32,60 @@ afterAll(() => {
 	return server.stop();
 });
 
-describe("GET /ping", () => {
-	test("should return pong", async () => {
-		const response = await request.get("/ping");
-		expect(response.text).toBe("pong");
+let attachment_url = "";
+
+describe("/ping", () => {
+	describe("GET", () => {
+		test("route should return pong", async () => {
+			const response = await request.get("/ping").set({ signature: Config.get().security.requestSignature });
+			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("without file specified and with signature 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 file specified and with signature specified", () => {
+			test("route should respond with Content-type application/json and 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();
+				attachment_url = response.body.url;
+			});
+		});
+	});
+	// 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");
+	// 			console.warn(response.body.url.replace("http://localhost:3003", ""));
+	// 			response = request.get(response.body.url.replace("http://localhost:3003", ""));
+	// 			expect(response.statusCode).toBe(400);
+	// 		});
+	// 	});
+	// });
+	describe("DELETE", () => {
+		describe("without signature specified", () => {});
 	});
 });