Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
toyamarinyon committed Jun 16, 2022
1 parent 0ba72f6 commit cc7fa2f
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { test, expect } from "vitest";
import { z } from "zod";
import { createWebCryptSession, WebCryptSessionOption } from ".";

const scheme = z.object({
userId: z.number(),
});
const defaultRequest = new Request("http://loclahost:8989/test");
const password = "IF4B#t69!WlX$uS22blaxDvzJJ%$vEh%";
const option: WebCryptSessionOption = { password, cookie: "session" };

test("no scheme", async () => {
// @ts-ignore we actually want to test this
await expect(createWebCryptSession()).rejects.toThrowError(
"webcrypt-session: Bad usage"
);
});

test("no req", async () => {
await expect(
// @ts-ignore we actually want to test this
createWebCryptSession(scheme)
).rejects.toThrowError("webcrypt-session: Bad usage");
});

test("no option", async () => {
await expect(() =>
// @ts-ignore we actually want to test this
createWebCryptSession(scheme, defaultRequest)
).rejects.toThrowError("webcrypt-session: Bad usage");
});

test("no password", async () => {
await expect(() =>
// @ts-ignore we actually want to test this
createWebCryptSession(scheme, defaultRequest, {})
).rejects.toThrowError("webcrypt-session: Bad usage");
});

test("bad password length", async () => {
await expect(() =>
// @ts-ignore we actually want to test this
createWebCryptSession(scheme, defaultRequest, { password: "a" })
).rejects.toThrowError("webcrypt-session: Bad usage");
});

test("no cookie name", async () => {
await expect(() =>
// @ts-ignore we actually want to test this
createWebCryptSession(scheme, defaultRequest, { password })
).rejects.toThrowError("webcrypt-session: Bad usage");
});

test("session not exists", async () => {
const webCryptSession = await createWebCryptSession(
scheme,
defaultRequest,
option
);
expect(webCryptSession.session.userId).toBeUndefined();
const response = await webCryptSession.response("Hello World");
expect(response.headers.get("set-cookie")).toBeNull();
});

test("session exists", async () => {
const webCryptSession = await createWebCryptSession(
scheme,
new Request("http://loclahost:8989/test", {
headers: {
cookie: "session=Ekvxbb%2F1pRAsZZWq--%2FybF8SeKlgnR%2FKn7eEiFeA%3D%3D",
},
}),
option
);
expect(webCryptSession.session.userId).toBe(1);
});

test("update session", async () => {
const webCryptSession = await createWebCryptSession(
scheme,
new Request("http://loclahost:8989/test"),
option
);
expect(webCryptSession.session.userId).toBeUndefined();
const responseWithoutSession = await webCryptSession.response("Hello World");
expect(responseWithoutSession.headers.get("set-cookie")).toBeNull();

webCryptSession.session.userId = 1;
const responseWithSession = await webCryptSession.response("Hello World");
expect(responseWithSession.headers.get("set-cookie")).not.toBeNull();
});

test("invalid session", async () => {
const webCryptSession = await createWebCryptSession(
scheme,
new Request("http://loclahost:8989/test", {
headers: {
cookie: "session=%7B%22userId%22%3A1%7",
},
}),
option
);
const response = await webCryptSession.response("Hello World");
expect(webCryptSession.session.userId).toBeUndefined();
expect(response.headers.get("set-cookie")).toBeNull();
});
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default defineConfig({
"import.meta.vitest": "undefined",
},
test: {
dir: "src",
includeSource: ["src/**/*.{js,ts}"],
setupFiles: ["./testSetUp.ts"],
},
Expand Down

0 comments on commit cc7fa2f

Please sign in to comment.