-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,33 @@ | ||
//src/mocks/authentication/auth_handlers.ts | ||
|
||
import { http, HttpResponse } from "msw" | ||
import resolveURL from "../api/fetch.ts"; | ||
import resolveURL from "../../api/fetch"; | ||
|
||
|
||
export const mockCSRFToken: string = "SUPERSECRETCSRFTOKEN" | ||
export const mockUsername: string = "testuser" | ||
export const mockPassword: string = "mockpassword" | ||
|
||
export const auth_handlers = [ | ||
http.post(resolveURL("/auth/csrf"), () => { | ||
return new HttpResponse(null, { | ||
headers: { | ||
'Set-Cookie': mockCSRFToken | ||
} | ||
}) | ||
}), | ||
|
||
http.post(resolveURL("/auth/login"), async ({ cookies, request }) => { | ||
if (!cookies.csrfmiddlewaretoken) return new HttpResponse(null, {status: 403}) | ||
if (cookies.csrfmiddlewaretoken !== mockCSRFToken) return new HttpResponse(null, {status: 403}) | ||
|
||
const data = await request.formData() | ||
if (data.get("username") !== mockUsername || data.get("password") !== mockPassword) return new HttpResponse(null, {status: 403}) | ||
|
||
return new HttpResponse(null, { | ||
headers: { | ||
'Set-Cookie': mockCSRFToken | ||
} | ||
}) | ||
}) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { describe, expect, test } from "vitest" | ||
import { getCSRF, signIn, signOut } from "../../src/api/authentication"; | ||
import { mockCSRFToken, mockUsername, mockPassword } from "../../src/mocks/authentication/auth_handlers"; | ||
|
||
describe("authentication helper functions", () => { | ||
|
||
test("should get csrf token", async () => { | ||
const response: Response = await getCSRF() | ||
expect(response.headers).toBe({ | ||
'Set-Cookie': mockCSRFToken | ||
}) | ||
}) | ||
|
||
test("should be able to sign in", async () => { | ||
const response = await signIn(mockUsername, mockPassword) | ||
expect(response.status).toBe(200) | ||
}) | ||
|
||
test("should be able to sign out", async () => { | ||
const response = await signOut() | ||
expect(response.status).toBe(200) | ||
}) | ||
}) |