-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares_test.ts
59 lines (43 loc) · 1.57 KB
/
middlewares_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
import { assertEquals } from "@std/assert";
import { Status, testing } from "@oak/oak";
import { authMiddleware } from "./middlewares.ts";
import helper from "./helpers.ts";
import { getNumericDate } from "djwt";
import { FakeTime } from "@std/testing/time";
Deno.test("Test Auth middleware", async (t) => {
await t.step("inject user id", async () => {
const ctx = testing.createMockContext();
const next = testing.createMockNext();
const payload = {
sub: "7",
exp: getNumericDate(60 * 60),
};
const token = await helper.generateToken(payload);
ctx.request.headers.set("Authorization", `Bearer ${token}`);
await authMiddleware(ctx, next);
assertEquals(ctx.state.user_id, parseInt(payload.sub));
});
await t.step("block unauthorized", async () => {
const ctx = testing.createMockContext();
const next = testing.createMockNext();
await authMiddleware(ctx, next);
assertEquals(ctx.response.status, Status.Unauthorized);
});
const exp = getNumericDate(60 * 60 * 24 * 7);
await t.step("block expired token", async () => {
const ctx = testing.createMockContext();
const next = testing.createMockNext();
// Fake the time after 7 days + 2s
using _ = new FakeTime(
new Date(Date.now() + 60 * 60 * 24 * 7 * 1000 + 2000),
);
const payload = {
sub: "7",
exp,
};
const token = await helper.generateToken(payload);
ctx.request.headers.set("Authorization", `Bearer ${token}`);
await authMiddleware(ctx, next);
assertEquals(ctx.response.status, Status.Unauthorized);
});
});