Skip to content

Commit 80d5e96

Browse files
committed
♻️ Use helper function (#2446)
1 parent 4c14843 commit 80d5e96

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/test/lib/utils/authorship.test.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,29 @@ describe('validateAuthFormWithFallback', () => {
255255
vi.restoreAllMocks();
256256
});
257257

258+
/**
259+
* Creates a mock HTTP Request object for testing authentication endpoints.
260+
*
261+
* @param username - The username to include in the request body
262+
* @param password - The password to include in the request body
263+
* @returns A Request object with POST method, form-encoded body containing credentials, and appropriate headers
264+
*
265+
* @example
266+
* ```typescript
267+
* const request = createMockRequest('john_doe', 'secret123');
268+
* // Creates a POST request to localhost with username and password in the body
269+
* ```
270+
*/
271+
function createMockRequest(username: string, password: string): Request {
272+
return new Request('http://localhost', {
273+
method: 'POST',
274+
body: new URLSearchParams({ username, password }).toString(),
275+
headers: {
276+
'Content-Type': 'application/x-www-form-urlencoded',
277+
},
278+
});
279+
}
280+
258281
describe('Auth form for successful cases', () => {
259282
test('expect to validate valid form data successfully', async () => {
260283
const mockForm = {
@@ -270,13 +293,7 @@ describe('validateAuthFormWithFallback', () => {
270293
vi.mocked(superValidate).mockResolvedValueOnce(mockForm);
271294
vi.mocked(zod).mockReturnValue({} as any);
272295

273-
const mockRequest = new Request('http://localhost', {
274-
method: 'POST',
275-
body: new URLSearchParams({ username: 'testuser', password: 'TestPass123' }).toString(),
276-
headers: {
277-
'Content-Type': 'application/x-www-form-urlencoded',
278-
},
279-
});
296+
const mockRequest = createMockRequest('testuser', 'TestPass123');
280297

281298
const result = await validateAuthFormWithFallback(mockRequest);
282299

@@ -303,13 +320,7 @@ describe('validateAuthFormWithFallback', () => {
303320
vi.mocked(superValidate).mockResolvedValueOnce(mockForm);
304321
vi.mocked(zod).mockReturnValue({} as any);
305322

306-
const mockRequest = new Request('http://localhost', {
307-
method: 'POST',
308-
body: new URLSearchParams({ username: 'ab', password: '123' }).toString(),
309-
headers: {
310-
'Content-Type': 'application/x-www-form-urlencoded',
311-
},
312-
});
323+
const mockRequest = createMockRequest('ab', '123');
313324

314325
const result = await validateAuthFormWithFallback(mockRequest);
315326

@@ -323,13 +334,7 @@ describe('validateAuthFormWithFallback', () => {
323334
.mockRejectedValueOnce(new Error('Failed to create strategy with zod adapter'));
324335
vi.mocked(zod).mockReturnValue({} as any);
325336

326-
const mockRequest = new Request('http://localhost', {
327-
method: 'POST',
328-
body: new URLSearchParams({ username: 'testuser', password: 'TestPass123' }).toString(),
329-
headers: {
330-
'Content-Type': 'application/x-www-form-urlencoded',
331-
},
332-
});
337+
const mockRequest = createMockRequest('testuser', 'TestPass123');
333338

334339
const result = await validateAuthFormWithFallback(mockRequest);
335340

0 commit comments

Comments
 (0)