-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauthHandlers.test.ts
More file actions
198 lines (174 loc) · 6.29 KB
/
authHandlers.test.ts
File metadata and controls
198 lines (174 loc) · 6.29 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Tests for mock auth MSW handlers.
*
* These tests verify the in-memory better-auth mock endpoints
* that enable sign-up / sign-in / session / sign-out flows in the
* MSW (browser & test) environment.
*
* @vitest-environment node
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
import { setupServer } from 'msw/node';
import { createAuthHandlers, resetAuthState } from '../mocks/authHandlers';
const BASE_URL = 'http://localhost/api/v1/auth';
const handlers = createAuthHandlers('/api/v1/auth');
const server = setupServer(...handlers);
beforeAll(() => server.listen({ onUnhandledRequest: 'bypass' }));
afterAll(() => server.close());
beforeEach(() => resetAuthState());
describe('Mock Auth Handlers', () => {
it('should register a new user via sign-up', async () => {
const res = await fetch(`${BASE_URL}/sign-up/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'Alice',
email: 'alice@example.com',
password: 'secret123',
}),
});
expect(res.ok).toBe(true);
const body = await res.json();
expect(body.user).toBeDefined();
expect(body.user.name).toBe('Alice');
expect(body.user.email).toBe('alice@example.com');
expect(body.session).toBeDefined();
expect(body.session.token).toBeTruthy();
// Password must never be exposed
expect(body.user.password).toBeUndefined();
});
it('should reject duplicate sign-up', async () => {
// Register a user first
await fetch(`${BASE_URL}/sign-up/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Alice', email: 'alice@example.com', password: 'secret123' }),
});
const res = await fetch(`${BASE_URL}/sign-up/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'Alice',
email: 'alice@example.com',
password: 'secret123',
}),
});
expect(res.status).toBe(409);
const body = await res.json();
expect(body.message).toMatch(/already exists/i);
});
it('should reject sign-up without email', async () => {
const res = await fetch(`${BASE_URL}/sign-up/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ password: 'secret123' }),
});
expect(res.status).toBe(400);
});
it('should sign in with correct credentials', async () => {
// Register user first
await fetch(`${BASE_URL}/sign-up/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Alice', email: 'alice@example.com', password: 'secret123' }),
});
const res = await fetch(`${BASE_URL}/sign-in/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'alice@example.com',
password: 'secret123',
}),
});
expect(res.ok).toBe(true);
const body = await res.json();
expect(body.user.email).toBe('alice@example.com');
expect(body.session.token).toBeTruthy();
});
it('should reject sign-in with wrong password', async () => {
const res = await fetch(`${BASE_URL}/sign-in/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'alice@example.com',
password: 'wrong',
}),
});
expect(res.status).toBe(401);
});
it('should return current session after sign-in', async () => {
// Register and sign in
await fetch(`${BASE_URL}/sign-up/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Alice', email: 'alice@example.com', password: 'secret123' }),
});
await fetch(`${BASE_URL}/sign-in/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'alice@example.com',
password: 'secret123',
}),
});
const res = await fetch(`${BASE_URL}/get-session`);
expect(res.ok).toBe(true);
const body = await res.json();
expect(body.user.email).toBe('alice@example.com');
expect(body.session.token).toBeTruthy();
});
it('should clear session on sign-out', async () => {
const res = await fetch(`${BASE_URL}/sign-out`, { method: 'POST' });
expect(res.ok).toBe(true);
const sessionRes = await fetch(`${BASE_URL}/get-session`);
expect(sessionRes.status).toBe(401);
});
it('should handle forget-password', async () => {
const res = await fetch(`${BASE_URL}/forget-password`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: 'alice@example.com' }),
});
expect(res.ok).toBe(true);
});
it('should handle reset-password', async () => {
const res = await fetch(`${BASE_URL}/reset-password`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token: 'tok', newPassword: 'newpass' }),
});
expect(res.ok).toBe(true);
});
it('should update user when authenticated', async () => {
// Register and sign in first
await fetch(`${BASE_URL}/sign-up/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Alice', email: 'alice@example.com', password: 'secret123' }),
});
await fetch(`${BASE_URL}/sign-in/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'alice@example.com',
password: 'secret123',
}),
});
const res = await fetch(`${BASE_URL}/update-user`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Alice Updated' }),
});
expect(res.ok).toBe(true);
const body = await res.json();
expect(body.user.name).toBe('Alice Updated');
});
it('should reject update-user when not authenticated', async () => {
const res = await fetch(`${BASE_URL}/update-user`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Nope' }),
});
expect(res.status).toBe(401);
});
});