1
- import { jest } from ' @jest/globals' ;
1
+ import { jest } from " @jest/globals" ;
2
2
3
3
// Mock Next.js modules
4
- jest . mock ( ' next/server' , ( ) => ( {
4
+ jest . mock ( " next/server" , ( ) => ( {
5
5
NextResponse : {
6
6
json : jest . fn ( ( data , options ) => {
7
7
return {
@@ -15,7 +15,7 @@ jest.mock('next/server', () => ({
15
15
} ,
16
16
} ) ) ;
17
17
18
- jest . mock ( ' @/db/drizzle' , ( ) => ( {
18
+ jest . mock ( " @/db/drizzle" , ( ) => ( {
19
19
db : {
20
20
query : {
21
21
users : {
@@ -25,41 +25,41 @@ jest.mock('@/db/drizzle', () => ({
25
25
} ,
26
26
} ) ) ;
27
27
28
- jest . mock ( ' @/lib/password' , ( ) => ( {
28
+ jest . mock ( " @/lib/password" , ( ) => ( {
29
29
verifyPassword : jest . fn ( ) ,
30
30
} ) ) ;
31
31
32
- jest . mock ( ' @/lib/server/session' , ( ) => ( {
33
- generateSessionToken : jest . fn ( ) . mockReturnValue ( ' mock-session-token' ) ,
34
- createSession : jest . fn ( ) . mockResolvedValue ( {
35
- token : ' mock-session-token' ,
36
- expiresAt : new Date ( Date . now ( ) + 86400000 )
32
+ jest . mock ( " @/lib/server/session" , ( ) => ( {
33
+ generateSessionToken : jest . fn ( ) . mockReturnValue ( " mock-session-token" ) ,
34
+ createSession : jest . fn ( ) . mockResolvedValue ( {
35
+ token : " mock-session-token" ,
36
+ expiresAt : new Date ( Date . now ( ) + 86400000 ) ,
37
37
} ) ,
38
38
} ) ) ;
39
39
40
- jest . mock ( ' @/lib/server/cookies' , ( ) => ( {
40
+ jest . mock ( " @/lib/server/cookies" , ( ) => ( {
41
41
setSessionTokenCookie : jest . fn ( ) ,
42
42
} ) ) ;
43
43
44
- import { NextResponse } from ' next/server' ;
45
- import { db } from ' @/db/drizzle' ;
46
- import { verifyPassword } from ' @/lib/password' ;
47
- import { generateSessionToken , createSession } from ' @/lib/server/session' ;
48
- import { setSessionTokenCookie } from ' @/lib/server/cookies' ;
49
- import { POST } from ' @/app/api/auth/login/route' ;
44
+ import { NextResponse } from " next/server" ;
45
+ import { db } from " @/db/drizzle" ;
46
+ import { verifyPassword } from " @/lib/password" ;
47
+ import { generateSessionToken , createSession } from " @/lib/server/session" ;
48
+ import { setSessionTokenCookie } from " @/lib/server/cookies" ;
49
+ import { POST } from " @/app/api/auth/login/route" ;
50
50
51
- describe ( ' POST /api/login' , ( ) => {
51
+ describe ( " POST /api/login" , ( ) => {
52
52
beforeEach ( ( ) => {
53
53
jest . clearAllMocks ( ) ;
54
54
} ) ;
55
55
56
- it ( ' should return user data on valid credentials' , async ( ) => {
56
+ it ( " should return user data on valid credentials" , async ( ) => {
57
57
// Mock user data
58
58
const mockUser = {
59
- id : '1' ,
60
-
61
- name : ' Test User' ,
62
- hashedPassword : ' hashed_password' ,
59
+ id : "1" ,
60
+
61
+ name : " Test User" ,
62
+ hashedPassword : " hashed_password" ,
63
63
} ;
64
64
65
65
// Set up mocks
@@ -69,8 +69,8 @@ describe('POST /api/login', () => {
69
69
// Create mock request
70
70
const request = {
71
71
json : jest . fn ( ) . mockResolvedValue ( {
72
-
73
- password : ' password123' ,
72
+
73
+ password : " password123" ,
74
74
} ) ,
75
75
} as unknown as Request ;
76
76
@@ -79,27 +79,30 @@ describe('POST /api/login', () => {
79
79
80
80
// Assertions
81
81
expect ( db . query . users . findFirst ) . toHaveBeenCalled ( ) ;
82
- expect ( verifyPassword ) . toHaveBeenCalledWith ( 'hashed_password' , 'password123' ) ;
82
+ expect ( verifyPassword ) . toHaveBeenCalledWith (
83
+ "hashed_password" ,
84
+ "password123" ,
85
+ ) ;
83
86
expect ( generateSessionToken ) . toHaveBeenCalled ( ) ;
84
87
expect ( createSession ) . toHaveBeenCalled ( ) ;
85
88
expect ( setSessionTokenCookie ) . toHaveBeenCalled ( ) ;
86
-
89
+
87
90
expect ( response . data ) . toEqual ( {
88
- _id : '1' ,
89
-
90
- name : ' Test User' ,
91
+ _id : "1" ,
92
+
93
+ name : " Test User" ,
91
94
} ) ;
92
95
} ) ;
93
96
94
- it ( ' should return 401 if user not found' , async ( ) => {
97
+ it ( " should return 401 if user not found" , async ( ) => {
95
98
// Set up mocks
96
99
( db . query . users . findFirst as jest . Mock ) . mockResolvedValue ( null ) ;
97
100
98
101
// Create mock request
99
102
const request = {
100
103
json : jest . fn ( ) . mockResolvedValue ( {
101
-
102
- password : ' password123' ,
104
+
105
+ password : " password123" ,
103
106
} ) ,
104
107
} as unknown as Request ;
105
108
@@ -108,16 +111,16 @@ describe('POST /api/login', () => {
108
111
109
112
// Assertions
110
113
expect ( response . status ) . toBe ( 401 ) ;
111
- expect ( response . data ) . toEqual ( { error : ' Invalid credentials' } ) ;
114
+ expect ( response . data ) . toEqual ( { error : " Invalid credentials" } ) ;
112
115
} ) ;
113
116
114
- it ( ' should return 401 on invalid password' , async ( ) => {
117
+ it ( " should return 401 on invalid password" , async ( ) => {
115
118
// Mock user data
116
119
const mockUser = {
117
- id : '1' ,
118
-
119
- name : ' Test User' ,
120
- hashedPassword : ' hashed_password' ,
120
+ id : "1" ,
121
+
122
+ name : " Test User" ,
123
+ hashedPassword : " hashed_password" ,
121
124
} ;
122
125
123
126
// Set up mocks
@@ -127,8 +130,8 @@ describe('POST /api/login', () => {
127
130
// Create mock request
128
131
const request = {
129
132
json : jest . fn ( ) . mockResolvedValue ( {
130
-
131
- password : ' wrong_password' ,
133
+
134
+ password : " wrong_password" ,
132
135
} ) ,
133
136
} as unknown as Request ;
134
137
@@ -138,20 +141,20 @@ describe('POST /api/login', () => {
138
141
// Assertions
139
142
expect ( verifyPassword ) . toHaveBeenCalled ( ) ;
140
143
expect ( response . status ) . toBe ( 401 ) ;
141
- expect ( response . data ) . toEqual ( { error : ' Invalid credentials' } ) ;
144
+ expect ( response . data ) . toEqual ( { error : " Invalid credentials" } ) ;
142
145
} ) ;
143
146
144
- it ( ' should return 400 on invalid request' , async ( ) => {
147
+ it ( " should return 400 on invalid request" , async ( ) => {
145
148
// Create mock request that throws an error when trying to parse JSON
146
149
const request = {
147
- json : jest . fn ( ) . mockRejectedValue ( new Error ( ' Invalid JSON' ) ) ,
150
+ json : jest . fn ( ) . mockRejectedValue ( new Error ( " Invalid JSON" ) ) ,
148
151
} as unknown as Request ;
149
152
150
153
// Call the handler
151
154
const response = await POST ( request ) ;
152
155
153
156
// Assertions
154
157
expect ( response . status ) . toBe ( 400 ) ;
155
- expect ( response . data ) . toEqual ( { error : ' Invalid request' } ) ;
158
+ expect ( response . data ) . toEqual ( { error : " Invalid request" } ) ;
156
159
} ) ;
157
- } ) ;
160
+ } ) ;
0 commit comments