@@ -3,7 +3,7 @@ import * as appHandler from "./route";
3
3
import { expect , test } from "@jest/globals" ;
4
4
// import { authMock } from "@/test/authMock";
5
5
import { validateSession , invalidateSession } from "@/test/util/authMockUtils" ;
6
- import { fillDbMockWithManyUnclaimedItems } from "@/test/util/dbMockUtils" ;
6
+ import { fillDbMockWithManyItems } from "@/test/util/dbMockUtils" ;
7
7
import { dbMock } from "@/test/dbMock" ;
8
8
9
9
test ( "Should return 401 for invalid session" , async ( ) => {
@@ -36,18 +36,155 @@ test("Should give correct database queries", async () => {
36
36
await testApiHandler ( {
37
37
appHandler,
38
38
async test ( { fetch } ) {
39
- await fillDbMockWithManyUnclaimedItems ( 3 ) ;
39
+ await fillDbMockWithManyItems ( 3 ) ;
40
40
validateSession ( "ADMIN" ) ;
41
41
42
42
const res = await fetch ( { method : "GET" } ) ;
43
43
await expect ( res . status ) . toBe ( 200 ) ;
44
44
45
45
// Check that the response json was written correctly
46
46
const expectedRet = {
47
- unclaimedItems : await dbMock . unclaimedItem . findMany ( ) ,
47
+ items : await dbMock . item . findMany ( ) ,
48
48
} ;
49
49
const json = await res . json ( ) ;
50
50
await expect ( json ) . toEqual ( JSON . parse ( JSON . stringify ( expectedRet ) ) ) ; // Needed to stringify and parse because the expiration field would cause an error because Date != ISOstring
51
51
} ,
52
52
} ) ;
53
53
} ) ;
54
+
55
+ test ( "Should return 400 on invalid expirationDateAfter" , async ( ) => {
56
+ await testApiHandler ( {
57
+ appHandler,
58
+ requestPatcher ( request ) {
59
+ request . nextUrl . searchParams . set ( "expirationDateAfter" , "foo" ) ;
60
+ request . nextUrl . searchParams . set (
61
+ "expirationDateBefore" ,
62
+ "2025-02-10T20:21:11+00:00"
63
+ ) ;
64
+ } ,
65
+ async test ( { fetch } ) {
66
+ await fillDbMockWithManyItems ( 3 ) ;
67
+ validateSession ( "ADMIN" ) ;
68
+
69
+ const res = await fetch ( { method : "GET" } ) ;
70
+ await expect ( res . status ) . toBe ( 400 ) ;
71
+ await expect ( res . json ( ) ) . resolves . toStrictEqual ( {
72
+ message : "expirationDateAfter must be a valid ISO-8601 timestamp" ,
73
+ } ) ;
74
+ } ,
75
+ } ) ;
76
+ } ) ;
77
+
78
+ test ( "Should return 400 on invalid expirationDateBefore" , async ( ) => {
79
+ await testApiHandler ( {
80
+ appHandler,
81
+ requestPatcher ( request ) {
82
+ request . nextUrl . searchParams . set (
83
+ "expirationDateAfter" ,
84
+ "2025-02-10T20:21:11+00:00"
85
+ ) ;
86
+ request . nextUrl . searchParams . set ( "expirationDateBefore" , "foo" ) ;
87
+ } ,
88
+ async test ( { fetch } ) {
89
+ await fillDbMockWithManyItems ( 3 ) ;
90
+ validateSession ( "ADMIN" ) ;
91
+
92
+ const res = await fetch ( { method : "GET" } ) ;
93
+ await expect ( res . status ) . toBe ( 400 ) ;
94
+ await expect ( res . json ( ) ) . resolves . toStrictEqual ( {
95
+ message : "expirationDateBefore must be a valid ISO-8601 timestamp" ,
96
+ } ) ;
97
+ } ,
98
+ } ) ;
99
+ } ) ;
100
+
101
+ test ( "Should be successful when both expirationDateBefore, expirationDateAfter valid" , async ( ) => {
102
+ await testApiHandler ( {
103
+ appHandler,
104
+ requestPatcher ( request ) {
105
+ request . nextUrl . searchParams . set (
106
+ "expirationDateAfter" ,
107
+ "2025-02-10T00:00:00.000Z"
108
+ ) ;
109
+ request . nextUrl . searchParams . set (
110
+ "expirationDateBefore" ,
111
+ "2025-02-14T00:00:00.000Z"
112
+ ) ;
113
+ } ,
114
+ async test ( { fetch } ) {
115
+ await fillDbMockWithManyItems ( 3 , [
116
+ new Date ( "2025-02-11" ) ,
117
+ new Date ( "2025-02-12" ) ,
118
+ new Date ( "2025-02-13" ) ,
119
+ ] ) ;
120
+ validateSession ( "ADMIN" ) ;
121
+
122
+ const res = await fetch ( { method : "GET" } ) ;
123
+ await expect ( res . status ) . toBe ( 200 ) ;
124
+
125
+ const expectedRet = {
126
+ items : await dbMock . item . findMany ( ) ,
127
+ } ;
128
+ const json = await res . json ( ) ;
129
+ await expect ( json ) . toEqual ( JSON . parse ( JSON . stringify ( expectedRet ) ) ) ;
130
+ } ,
131
+ } ) ;
132
+ } ) ;
133
+
134
+ test ( "Should be successful when expirationDateBefore valid, expirationDateAfter missing" , async ( ) => {
135
+ await testApiHandler ( {
136
+ appHandler,
137
+ requestPatcher ( request ) {
138
+ request . nextUrl . searchParams . set (
139
+ "expirationDateBefore" ,
140
+ "2025-02-14T00:00:00.000Z"
141
+ ) ;
142
+ } ,
143
+ async test ( { fetch } ) {
144
+ await fillDbMockWithManyItems ( 3 , [
145
+ new Date ( "2025-02-11" ) ,
146
+ new Date ( "2025-02-12" ) ,
147
+ new Date ( "2025-02-13" ) ,
148
+ ] ) ;
149
+ validateSession ( "ADMIN" ) ;
150
+
151
+ const res = await fetch ( { method : "GET" } ) ;
152
+ await expect ( res . status ) . toBe ( 200 ) ;
153
+
154
+ const expectedRet = {
155
+ items : await dbMock . item . findMany ( ) ,
156
+ } ;
157
+ const json = await res . json ( ) ;
158
+ await expect ( json ) . toEqual ( JSON . parse ( JSON . stringify ( expectedRet ) ) ) ;
159
+ } ,
160
+ } ) ;
161
+ } ) ;
162
+
163
+ test ( "Should be successful when expirationDateBefore missing, expirationDateAfter valid" , async ( ) => {
164
+ await testApiHandler ( {
165
+ appHandler,
166
+ requestPatcher ( request ) {
167
+ request . nextUrl . searchParams . set (
168
+ "expirationDateAfter" ,
169
+ "2025-02-10T00:00:00.000Z"
170
+ ) ;
171
+ } ,
172
+ async test ( { fetch } ) {
173
+ await fillDbMockWithManyItems ( 3 , [
174
+ new Date ( "2025-02-11" ) ,
175
+ new Date ( "2025-02-12" ) ,
176
+ new Date ( "2025-02-13" ) ,
177
+ ] ) ;
178
+ validateSession ( "ADMIN" ) ;
179
+
180
+ const res = await fetch ( { method : "GET" } ) ;
181
+ await expect ( res . status ) . toBe ( 200 ) ;
182
+
183
+ const expectedRet = {
184
+ items : await dbMock . item . findMany ( ) ,
185
+ } ;
186
+ const json = await res . json ( ) ;
187
+ await expect ( json ) . toEqual ( JSON . parse ( JSON . stringify ( expectedRet ) ) ) ;
188
+ } ,
189
+ } ) ;
190
+ } ) ;
0 commit comments