@@ -2,21 +2,31 @@ import { test } from "tap";
22import { getTestInstance } from "../helpers/test-instance.js" ;
33import { createApp } from "../../src/app/app.js" ;
44
5+ /**
6+ * Make a magic link request to the app
7+ * @param {Object } app - Hono app instance
8+ * @param {string } email - Email address for the magic link
9+ * @returns {Promise<Response> } The response from the request
10+ */
11+ async function makeMagicLinkRequest ( app , email ) {
12+ const formData = new URLSearchParams ( ) ;
13+ formData . append ( "email" , email ) ;
14+
15+ return app . request ( "/login/magic-link" , {
16+ method : "POST" ,
17+ headers : {
18+ "Content-Type" : "application/x-www-form-urlencoded" ,
19+ } ,
20+ body : formData . toString ( ) ,
21+ } ) ;
22+ }
23+
524test ( "magic links feature tests" , async ( t ) => {
625 t . test ( "user can request magic link" , async ( t ) => {
726 const testInstance = await getTestInstance ( ) ;
827 const app = createApp ( testInstance . auth ) ;
928
10- const formData = new URLSearchParams ( ) ;
11- formData . append ( "email" , "magic@example.com" ) ;
12-
13- const res = await app . request ( "/login/magic-link" , {
14- method : "POST" ,
15- headers : {
16- "Content-Type" : "application/x-www-form-urlencoded" ,
17- } ,
18- body : formData . toString ( ) ,
19- } ) ;
29+ const res = await makeMagicLinkRequest ( app , "magic@example.com" ) ;
2030
2131 t . equal ( res . status , 302 , "redirects after requesting magic link" ) ;
2232 t . match (
@@ -30,16 +40,7 @@ test("magic links feature tests", async (t) => {
3040 const testInstance = await getTestInstance ( ) ;
3141 const app = createApp ( testInstance . auth ) ;
3242
33- const formData = new URLSearchParams ( ) ;
34- formData . append ( "email" , "nonexistent@example.com" ) ;
35-
36- const res = await app . request ( "/login/magic-link" , {
37- method : "POST" ,
38- headers : {
39- "Content-Type" : "application/x-www-form-urlencoded" ,
40- } ,
41- body : formData . toString ( ) ,
42- } ) ;
43+ const res = await makeMagicLinkRequest ( app , "nonexistent@example.com" ) ;
4344
4445 // Should still show success to prevent email enumeration
4546 t . equal ( res . status , 302 , "redirects after requesting magic link" ) ;
0 commit comments