server_test.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { assertEquals, assertExists } from "@std/assert";
  2. import {
  3. clearToken,
  4. getCryptoString,
  5. makeErrorResponse,
  6. makeSuccessResponse,
  7. setToken,
  8. } from "utils/server.ts";
  9. Deno.test("getCryptoString - produces correct MD5 hash", async () => {
  10. const result = await getCryptoString("hello", "MD5");
  11. assertEquals(result, "5d41402abc4b2a76b9719d911017c592");
  12. });
  13. Deno.test("getCryptoString - produces correct SHA-256 hash", async () => {
  14. const result = await getCryptoString("hello", "SHA-256");
  15. assertEquals(
  16. result,
  17. "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
  18. );
  19. });
  20. Deno.test("getCryptoString - different inputs produce different hashes", async () => {
  21. const hash1 = await getCryptoString("input1", "MD5");
  22. const hash2 = await getCryptoString("input2", "MD5");
  23. assertExists(hash1);
  24. assertExists(hash2);
  25. assertEquals(hash1 !== hash2, true);
  26. });
  27. Deno.test("makeSuccessResponse - returns correct JSON structure with object data", async () => {
  28. const res = makeSuccessResponse({ name: "test", value: 42 });
  29. const body = await res.json();
  30. assertEquals(body.success, true);
  31. assertEquals(body.data, { name: "test", value: 42 });
  32. assertEquals(res.headers.get("Content-Type"), "application/json");
  33. });
  34. Deno.test("makeSuccessResponse - returns correct JSON structure with string data", async () => {
  35. const res = makeSuccessResponse("hello");
  36. const body = await res.json();
  37. assertEquals(body.success, true);
  38. assertEquals(body.data, "hello");
  39. });
  40. Deno.test("makeSuccessResponse - returns correct JSON structure with boolean data", async () => {
  41. const res = makeSuccessResponse(true);
  42. const body = await res.json();
  43. assertEquals(body.success, true);
  44. assertEquals(body.data, true);
  45. });
  46. Deno.test("makeSuccessResponse - returns correct JSON structure with array data", async () => {
  47. const res = makeSuccessResponse([{ id: 1 }, { id: 2 }]);
  48. const body = await res.json();
  49. assertEquals(body.success, true);
  50. assertEquals(body.data, [{ id: 1 }, { id: 2 }]);
  51. });
  52. Deno.test("makeErrorResponse - returns correct JSON structure", async () => {
  53. const res = makeErrorResponse();
  54. const body = await res.json();
  55. assertEquals(body.success, false);
  56. assertEquals(body.data, undefined);
  57. assertEquals(res.headers.get("Content-Type"), "application/json");
  58. });
  59. Deno.test("setToken - sets pd-user-token cookie on response", () => {
  60. const res = new Response(null, { headers: new Headers() });
  61. setToken(res, "test-token-value");
  62. const cookieHeader = res.headers.get("set-cookie");
  63. assertExists(cookieHeader);
  64. assertEquals(cookieHeader!.includes("pd-user-token=test-token-value"), true);
  65. assertEquals(cookieHeader!.includes("Path=/"), true);
  66. });
  67. Deno.test("clearToken - deletes pd-user-token cookie", () => {
  68. const res = new Response(null, { headers: new Headers() });
  69. clearToken(res);
  70. const cookieHeader = res.headers.get("set-cookie");
  71. assertExists(cookieHeader);
  72. assertEquals(cookieHeader!.includes("pd-user-token="), true);
  73. });