-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.test.ts
100 lines (87 loc) · 2.87 KB
/
mock.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { store as cookieStore } from "@mswjs/cookies";
import { serialize as serializeCookie } from "cookie";
import { setupServer, SetupServer } from "msw/node";
import { http, HttpResponse } from "msw";
import { afterEach } from "vitest";
const sessionCookieName = "MyCookie";
const authorizedSessionCookieValue = "ABCD1234";
const url = "https://my-app.mysite.com:9090";
const cookieString = serializeCookie(
sessionCookieName,
authorizedSessionCookieValue
);
const tokenEndpoint = `${url}/rest/token`;
const restEndpoint = `${url}/rest`;
const pingEndpoint = `${url}/ping`;
/**
* Sets the session cookie in the cookie store so that it is picked up by msw.
*/
function setSessionCookie(
cookieName: string,
cookieValue: string,
url: string
) {
const cookieString = serializeCookie(cookieName, cookieValue);
cookieStore.add(
{ url, credentials: "same-origin" },
{
headers: new Headers({
"Set-Cookie": cookieString,
}),
}
);
}
/**
* Compares the cookie value received vs the authorized cookie value to determine if the user is authenticated.
*/
const isAuthenticatedThroughSessionCookie = (
cookies: Record<string, string>
): boolean => {
const cookieValue = cookies[sessionCookieName];
return cookieValue === authorizedSessionCookieValue;
};
const server: SetupServer = setupServer(
http.get(tokenEndpoint, ({ cookies }) => {
console.log({ cookies });
if (isAuthenticatedThroughSessionCookie(cookies)) {
return HttpResponse.json(
{ data: { token: "mytokenhelloworld" } },
{
status: 200,
}
);
}
return HttpResponse.json(null, { status: 401 });
}),
http.get(restEndpoint, () => {
return HttpResponse.json({ myResponse: "Hello" });
}),
http.get(pingEndpoint, ({ cookies }) => {
console.log({ cookies });
if (isAuthenticatedThroughSessionCookie(cookies)) {
return HttpResponse.text("pong", {
status: 200,
});
}
return HttpResponse.json(null, { status: 401 });
})
);
describe("withJwt", () => {
beforeAll(() => {
setSessionCookie(sessionCookieName, authorizedSessionCookieValue, url);
server.listen();
});
afterAll(() => {
cookieStore.clear();
server.close();
});
it("receives a status code of 200 when hitting the token endpoint when the user is authenticated with the session cookie", async () => {
const response = await fetch(tokenEndpoint, { credentials: "include" });
expect(response.status).toBe(200);
});
// Checks for the same cookie as the test above but this test fails simply because of the order of the handler in the server setup above.
it("receives a status code of 200 when hitting the ping endpoint when the user is authenticated with the session cookie", async () => {
const response = await fetch(pingEndpoint, { credentials: "include" });
expect(response.status).toBe(200);
});
});