| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import { assertEquals, assertExists } from "@std/assert";
- import {
- clearToken,
- getCryptoString,
- makeErrorResponse,
- makeSuccessResponse,
- setToken,
- } from "utils/server.ts";
- Deno.test("getCryptoString - produces correct MD5 hash", async () => {
- const result = await getCryptoString("hello", "MD5");
- assertEquals(result, "5d41402abc4b2a76b9719d911017c592");
- });
- Deno.test("getCryptoString - produces correct SHA-256 hash", async () => {
- const result = await getCryptoString("hello", "SHA-256");
- assertEquals(
- result,
- "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
- );
- });
- Deno.test("getCryptoString - different inputs produce different hashes", async () => {
- const hash1 = await getCryptoString("input1", "MD5");
- const hash2 = await getCryptoString("input2", "MD5");
- assertExists(hash1);
- assertExists(hash2);
- assertEquals(hash1 !== hash2, true);
- });
- Deno.test("makeSuccessResponse - returns correct JSON structure with object data", async () => {
- const res = makeSuccessResponse({ name: "test", value: 42 });
- const body = await res.json();
- assertEquals(body.success, true);
- assertEquals(body.data, { name: "test", value: 42 });
- assertEquals(res.headers.get("Content-Type"), "application/json");
- });
- Deno.test("makeSuccessResponse - returns correct JSON structure with string data", async () => {
- const res = makeSuccessResponse("hello");
- const body = await res.json();
- assertEquals(body.success, true);
- assertEquals(body.data, "hello");
- });
- Deno.test("makeSuccessResponse - returns correct JSON structure with boolean data", async () => {
- const res = makeSuccessResponse(true);
- const body = await res.json();
- assertEquals(body.success, true);
- assertEquals(body.data, true);
- });
- Deno.test("makeSuccessResponse - returns correct JSON structure with array data", async () => {
- const res = makeSuccessResponse([{ id: 1 }, { id: 2 }]);
- const body = await res.json();
- assertEquals(body.success, true);
- assertEquals(body.data, [{ id: 1 }, { id: 2 }]);
- });
- Deno.test("makeErrorResponse - returns correct JSON structure", async () => {
- const res = makeErrorResponse();
- const body = await res.json();
- assertEquals(body.success, false);
- assertEquals(body.data, undefined);
- assertEquals(res.headers.get("Content-Type"), "application/json");
- });
- Deno.test("setToken - sets pd-user-token cookie on response", () => {
- const res = new Response(null, { headers: new Headers() });
- setToken(res, "test-token-value");
- const cookieHeader = res.headers.get("set-cookie");
- assertExists(cookieHeader);
- assertEquals(cookieHeader!.includes("pd-user-token=test-token-value"), true);
- assertEquals(cookieHeader!.includes("Path=/"), true);
- });
- Deno.test("clearToken - deletes pd-user-token cookie", () => {
- const res = new Response(null, { headers: new Headers() });
- clearToken(res);
- const cookieHeader = res.headers.get("set-cookie");
- assertExists(cookieHeader);
- assertEquals(cookieHeader!.includes("pd-user-token="), true);
- });
|