@@ -9,6 +9,7 @@ import { createUserWithSession } from '../../testHelpers';
9
9
import { eq , sql } from 'drizzle-orm' ;
10
10
import { sha256 } from 'hono/utils/crypto' ;
11
11
import { qrs } from '../../qr/schema' ;
12
+ import { adminMeta } from '../../admin/schema' ;
12
13
13
14
const sessionIds : Partial < Record < Role , string > > = { } ;
14
15
const userIds : Partial < Record < Role , string > > = { } ;
@@ -29,6 +30,7 @@ beforeAll(async () => {
29
30
await db . execute ( sql `TRUNCATE ${ users } CASCADE` ) ;
30
31
await db . execute ( sql `TRUNCATE ${ profiles } CASCADE` ) ;
31
32
await db . execute ( sql `TRUNCATE ${ qrs } CASCADE` ) ;
33
+ await db . execute ( sql `TRUNCATE ${ adminMeta } CASCADE` ) ;
32
34
33
35
for ( const role of roles ) {
34
36
const toCreate = {
@@ -56,6 +58,8 @@ beforeAll(async () => {
56
58
} ) ;
57
59
}
58
60
}
61
+
62
+ await db . insert ( adminMeta ) . values ( { mealNumber : 0 , showCategories : false } ) ;
59
63
} ) ;
60
64
61
65
describe ( 'Profiles module > PUT /' , ( ) => {
@@ -107,7 +111,7 @@ describe('Profiles module > PUT /', () => {
107
111
} ) ;
108
112
} ) ;
109
113
110
- describe ( 'Profile smodule > PUT /meals' , ( ) => {
114
+ describe ( 'Profile module > PUT /meals' , ( ) => {
111
115
test ( 'volunteer can update meals' , async ( ) => {
112
116
// { userId: string }
113
117
let res = await baseRoute . meal . $put (
@@ -155,6 +159,61 @@ describe('Profile smodule > PUT /meals', () => {
155
159
} ) ;
156
160
} ) ;
157
161
162
+ describe ( 'Profile module > DELETE /meals' , ( ) => {
163
+ test ( 'admin can update meals' , async ( ) => {
164
+ await db
165
+ . update ( profiles )
166
+ . set ( { meals : [ true , false , false ] } )
167
+ . where ( eq ( profiles . id , userIds . hacker ! ) ) ;
168
+
169
+ // { userId: string }
170
+ let res = await baseRoute . meal . $delete (
171
+ {
172
+ json : {
173
+ userId : expectedUsers . hacker ! . id ,
174
+ mealNum : 0
175
+ }
176
+ } ,
177
+ {
178
+ headers : {
179
+ Cookie : `auth_session=${ sessionIds . admin } `
180
+ }
181
+ }
182
+ ) ;
183
+
184
+ let userInDb = await db
185
+ . select ( )
186
+ . from ( profiles )
187
+ . where ( eq ( profiles . id , userIds . hacker ! ) ) ;
188
+
189
+ expect ( res . status ) . toBe ( 200 ) ;
190
+ expect ( userInDb [ 0 ] ! . meals [ 0 ] ) . toBeFalse ( ) ;
191
+ } ) ;
192
+
193
+ test ( 'only volunteer can update meals' , async ( ) => {
194
+ let res = await baseRoute . meal . $delete (
195
+ {
196
+ json : {
197
+ userId : expectedUsers . hacker ! . id ,
198
+ mealNum : 0
199
+ }
200
+ } ,
201
+ {
202
+ headers : {
203
+ Cookie : `auth_session=${ sessionIds . volunteer } `
204
+ }
205
+ }
206
+ ) ;
207
+
208
+ // @ts -expect-error code is from middleware
209
+ expect ( res . status ) . toBe ( 403 ) ;
210
+ expect ( res . text ( ) ) . resolves . toBe (
211
+ // @ts -ignore error message is from middleware
212
+ 'You do not have access to DELETE /api/profile/meal'
213
+ ) ;
214
+ } ) ;
215
+ } ) ;
216
+
158
217
describe . skip ( 'Profiles module > GET/POST /cv' , ( ) => {
159
218
test . skip ( 'can upload & download own cv' , async ( ) => {
160
219
let res = await baseRoute . cv . $get ( undefined , {
0 commit comments