@@ -143,6 +143,18 @@ describe('Users endpoint', async () => {
143143 } ) ;
144144
145145 describe ( `GET ${ USERS_URL } ` , ( ) => {
146+ it ( 'should respond with 401 on request without JWT' , async ( ) => {
147+ const res = await api . get ( USERS_URL ) ;
148+ assertUnauthorizedErrorRes ( res ) ;
149+ } ) ;
150+
151+ it ( 'should respond with 401 on request with non-admin JWT' , async ( ) => {
152+ await createUser ( userData ) ;
153+ const { authorizedApi } = await prepForAuthorizedTest ( userData ) ;
154+ const res = await authorizedApi . get ( USERS_URL ) ;
155+ assertUnauthorizedErrorRes ( res ) ;
156+ } ) ;
157+
146158 it ( 'should respond with users list, on request with admin JWT' , async ( ) => {
147159 await createUser ( adminData ) ;
148160 const dbUser = await createUser ( userData ) ;
@@ -173,14 +185,35 @@ describe('Users endpoint', async () => {
173185 assertNotFoundErrorRes ( res ) ;
174186 } ) ;
175187
176- it ( 'should respond with the found user on request with id or username' , async ( ) => {
177- await createUser ( adminData ) ;
188+ it ( 'should respond with 401 on non-owner request with username' , async ( ) => {
189+ await createUser ( xUserData ) ;
190+ const dbUser = await createUser ( userData ) ;
191+ const { authorizedApi } = await prepForAuthorizedTest ( xUserData ) ;
192+ const res = await authorizedApi . get ( `${ USERS_URL } /${ dbUser . username } ` ) ;
193+ assertUnauthorizedErrorRes ( res ) ;
194+ } ) ;
195+
196+ it ( 'should respond with the found user on request with id for anyone' , async ( ) => {
178197 const dbUser = await createUser ( userData ) ;
198+ const res = await api . get ( `${ USERS_URL } /${ dbUser . id } ` ) ;
199+ const resUser = res . body as User ;
200+ expect ( res . statusCode ) . toBe ( 200 ) ;
201+ expect ( res . type ) . toMatch ( / j s o n / ) ;
202+ expect ( resUser . id ) . toBe ( dbUser . id ) ;
203+ expect ( resUser . isAdmin ) . toStrictEqual ( false ) ;
204+ expect ( resUser . username ) . toBe ( dbUser . username ) ;
205+ expect ( resUser . fullname ) . toBe ( dbUser . fullname ) ;
206+ expect ( resUser . password ) . toBeUndefined ( ) ;
207+ } ) ;
208+
209+ it ( 'should respond with the found user on owner request with id or username' , async ( ) => {
210+ const dbUser = await createUser ( userData ) ;
211+ const { authorizedApi } = await prepForAuthorizedTest ( userData ) ;
179212 for ( const param of [ dbUser . id , dbUser . username ] ) {
180- const res = await api . get ( `${ USERS_URL } /${ param } ` ) ;
213+ const res = await authorizedApi . get ( `${ USERS_URL } /${ param } ` ) ;
181214 const resUser = res . body as User ;
182- expect ( res . type ) . toMatch ( / j s o n / ) ;
183215 expect ( res . statusCode ) . toBe ( 200 ) ;
216+ expect ( res . type ) . toMatch ( / j s o n / ) ;
184217 expect ( resUser . id ) . toBe ( dbUser . id ) ;
185218 expect ( resUser . isAdmin ) . toStrictEqual ( false ) ;
186219 expect ( resUser . username ) . toBe ( dbUser . username ) ;
0 commit comments