h4-projekt-gruppe-0-sm/backend/test/test_authentication.ts
2025-03-12 15:36:15 +01:00

40 lines
1.1 KiB
TypeScript

import { assertEquals, assertMatch } from "jsr:@std/assert";
import axios from "npm:axios";
const url = `http://127.0.0.1:8080`;
// const url = `http://10.135.51.114:8080`;
const name = "Maksim";
const email = `mash.skp_${Math.floor(Math.random() * 100000)}@edu.mercantec.dk`;
const password = "Merc1234";
Deno.test("test", async () => {
const registerRes = await axios.post(
`${url}/api/users/register`,
{ name, email, password },
{ responseType: "json" },
)
.then((res) => res.data);
assertEquals(registerRes, { ok: true });
const loginRes = await axios.post(
`${url}/api/sessions/login`,
{ email, password },
{ responseType: "json" },
).then((res) => res.data);
assertEquals(loginRes.ok, true);
assertMatch(loginRes.token, /^[0-9a-zA-Z]+$/);
const token = loginRes.token;
const sessionUserRes = await axios.post(
`${url}/api/sessions/login`,
{ email, password },
{ responseType: "json" },
).then((res) => res.data);
assertEquals(sessionUserRes.ok, true);
});