summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--fixtures.js (renamed from test.js)0
-rw-r--r--test/root.js91
-rw-r--r--tests/root.spec.js35
3 files changed, 35 insertions, 91 deletions
diff --git a/test.js b/fixtures.js
index bfc6b3c..bfc6b3c 100644
--- a/test.js
+++ b/fixtures.js
diff --git a/test/root.js b/test/root.js
deleted file mode 100644
index d5af081..0000000
--- a/test/root.js
+++ /dev/null
@@ -1,91 +0,0 @@
-import test from "ava";
-import supertest from "supertest";
-
-import app from "../main.js";
-
-const request = supertest(app);
-
-test("get / when not logged should redirect to /login", async function(t) {
- await request.get("/")
- .expect("Content-Type", /text\/plain/)
- .expect(302, /Redirecting to \/login$/);
- t.pass();
-});
-test("get /login when not logged in should display the login page", async function(t) {
- await request.get("/login")
- .expect("Content-Type", /text\/html/)
- .expect(200, /<form action="\/login" method="post">/);
- t.pass();
-});
-test("get /logout when not logged in should redirect to the root", async function(t) {
- await request.get("/logout")
- .expect("Content-Type", /text\/plain/)
- .expect(302, /Redirecting to \/$/);
- t.pass();
-});
-test("post /login with valid credentials should set a cookie and redirect to the games list", async function(t) {
- await request.post("/login")
- .send("username=Alice&password=Alice42!")
- .expect("Content-Type", /text\/plain/)
- .expect("set-cookie", /JDMSessionId=/)
- .expect(302, /Redirecting to \/games$/);
- t.pass();
-});
-test("get / when logged in should redirect to the /games page", async function(t) {
- const authResponse = await request.post("/login")
- .send("username=Alice&password=Alice42!")
- .expect("Content-Type", /text\/plain/)
- .expect("set-cookie", /JDMSessionId=/)
- .expect(302, /Redirecting to \/games$/);
- let cookie = authResponse.get("Set-Cookie");
- await request.get("/")
- .set("Cookie", cookie)
- .expect("Content-Type", /text\/plain/)
- .expect(302, /Redirecting to \/games$/);
- t.pass();
-});
-test("get /login when already logged in should redirect to the games page", async function(t) {
- const authResponse = await request.post("/login")
- .send("username=Alice&password=Alice42!")
- .expect("Content-Type", /text\/plain/)
- .expect("set-cookie", /JDMSessionId=/)
- .expect(302, /Redirecting to \/games$/);
- let cookie = authResponse.get("Set-Cookie");
- await request.get("/login")
- .set("Cookie", cookie)
- .expect("Content-Type", /text\/plain/)
- .expect(302, /Redirecting to \/games$/);
- t.pass();
-});
-test("get /logout when logged in should delog and redirect to the root", async function(t) {
- const authResponse = await request.post("/login")
- .send("username=Alice&password=Alice42!")
- .expect("Content-Type", /text\/plain/)
- .expect("set-cookie", /JDMSessionId=/)
- .expect(302, /Redirecting to \/games$/);
- let cookie = authResponse.get("Set-Cookie");
- await request.get("/logout")
- .set("cookie", cookie)
- .expect("content-type", /text\/plain/)
- .expect("set-cookie", /JDMSessionId=;/)
- .expect(302, /Redirecting to \/$/);
- t.pass();
-});
-test("get / with an now invalid cookie should redirect to the /login page", async function(t) {
- const authResponse = await request.post("/login")
- .send("username=Alice&password=Alice42!")
- .expect("Content-Type", /text\/plain/)
- .expect("set-cookie", /JDMSessionId=/)
- .expect(302, /Redirecting to \/games$/);
- let cookie = authResponse.get("Set-Cookie");
- await request.get("/logout")
- .set("cookie", cookie)
- .expect("content-type", /text\/plain/)
- .expect("set-cookie", /JDMSessionId=;/)
- .expect(302, /Redirecting to \/$/);
- await request.get("/")
- .set("Cookie", cookie)
- .expect("Content-Type", /text\/plain/)
- .expect(302, /Redirecting to \/login$/);
- t.pass();
-});
diff --git a/tests/root.spec.js b/tests/root.spec.js
new file mode 100644
index 0000000..ffceda5
--- /dev/null
+++ b/tests/root.spec.js
@@ -0,0 +1,35 @@
+import { beforeEach, describe, it } from "vitest";
+import supertest from "supertest";
+
+import app from "../main.js";
+
+const request = supertest(app);
+
+describe.concurrent("Root handlers tests", async function() {
+ describe.concurrent("When not logged in", async function() {
+ it("/", async function() { request.get("/").expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/login$/); });
+ it("/login", async function() { request.get("/login").expect("Content-Type", /text\/html/).expect(200, /<form action="\/login" method="post">/); });
+ it("/logout", async function() { request.get("/logout").expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/$/); });
+ });
+
+ describe.concurrent("With valid credentials", async function() {
+ beforeEach(async function(ctx) {
+ const authResponse = await request.post("/login")
+ .send("username=Alice&password=Alice42!")
+ .expect("Content-Type", /text\/plain/)
+ .expect("set-cookie", /JDMSessionId=/)
+ .expect(302, /Redirecting to \/games$/);
+ ctx.cookie = authResponse.get("Set-Cookie");
+ });
+ it("/", async function(ctx) { request.get("/").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/games$/); });
+ it("/login", async function(ctx) { request.get("/login").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/games$/); });
+ describe("logout", async function(ctx) {
+ it("/logout", async function() { request.get("/logout").set("cookie", ctx.cookie).expect("content-type", /text\/plain/).expect("set-cookie", /JDMSessionId=;/).expect(302, /Redirecting to \/$/); });
+ describe.concurrent("all handlers with the now invalid cookie", async function() {
+ it("/", async function() { request.get("/").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/login$/); });
+ it("/", async function() { request.get("/login").set("Cookie", ctx.cookie).expect("Content-Type", /text\/html/).expect(200, /<form action="\/login" method="post">/); });
+ it("/", async function() { request.get("/logout").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/$/); });
+ });
+ });
+ });
+});