-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmiddleware.test.ts
More file actions
118 lines (100 loc) · 3.17 KB
/
middleware.test.ts
File metadata and controls
118 lines (100 loc) · 3.17 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/** @jest-environment node */
import { NextRequest } from 'next/server';
import { middleware } from './middleware';
describe('middleware /chat redirects', () => {
const originalFetch = global.fetch;
const originalBetterAuthUrl = process.env.BETTER_AUTH_URL;
const originalNextPublicAppUrl = process.env.NEXT_PUBLIC_APP_URL;
beforeEach(() => {
jest.resetAllMocks();
process.env.BETTER_AUTH_URL = 'http://127.0.0.1:3000';
process.env.NEXT_PUBLIC_APP_URL = 'http://127.0.0.1:3000';
});
afterEach(() => {
global.fetch = originalFetch;
if (typeof originalBetterAuthUrl === 'string') {
process.env.BETTER_AUTH_URL = originalBetterAuthUrl;
} else {
delete process.env.BETTER_AUTH_URL;
}
if (typeof originalNextPublicAppUrl === 'string') {
process.env.NEXT_PUBLIC_APP_URL = originalNextPublicAppUrl;
} else {
delete process.env.NEXT_PUBLIC_APP_URL;
}
});
it('redirects unauthenticated /chat requests to /login', async () => {
global.fetch = jest.fn().mockResolvedValueOnce(
new Response(null, {
status: 401,
})
) as typeof fetch;
const response = await middleware(new NextRequest('http://localhost/chat'));
expect(response.status).toBe(307);
expect(response.headers.get('location')).toBe(
'http://127.0.0.1:3000/login'
);
});
it('redirects authenticated /chat requests to /chat/new after profile validation', async () => {
global.fetch = jest
.fn()
.mockResolvedValueOnce(
new Response(
JSON.stringify({
user: {
id: '00000000-0000-4000-8000-000000000010',
},
}),
{
status: 200,
headers: {
'content-type': 'application/json',
},
}
)
)
.mockResolvedValueOnce(
new Response(
JSON.stringify({
role: 'user',
status: 'active',
}),
{
status: 200,
headers: {
'content-type': 'application/json',
},
}
)
) as typeof fetch;
const response = await middleware(new NextRequest('http://localhost/chat'));
expect(response.status).toBe(307);
expect(response.headers.get('location')).toBe(
'http://127.0.0.1:3000/chat/new'
);
});
it('redirects unauthenticated /chat/new requests to /login', async () => {
global.fetch = jest.fn().mockResolvedValueOnce(
new Response(null, {
status: 401,
})
) as typeof fetch;
const response = await middleware(
new NextRequest('http://localhost/chat/new')
);
expect(response.status).toBe(307);
expect(response.headers.get('location')).toBe(
'http://127.0.0.1:3000/login'
);
});
it.each([
'http://localhost/api/internal/data',
'http://localhost/api/admin/encrypt',
])('skips middleware auth proxy checks for %s', async url => {
const fetchMock = jest.fn();
global.fetch = fetchMock as typeof fetch;
const response = await middleware(new NextRequest(url));
expect(fetchMock).not.toHaveBeenCalled();
expect(response.headers.get('location')).toBeNull();
});
});