11const chai = require ( "chai" ) ;
22const { expect } = chai ;
33const chaiHttp = require ( "chai-http" ) ;
4+ const sinon = require ( "sinon" ) ;
5+ const artsQuery = require ( "../../models/arts" ) ;
46
57const app = require ( "../../server" ) ;
68const authService = require ( "../../services/authService" ) ;
@@ -13,14 +15,17 @@ const artData = require("../fixtures/arts/arts")();
1315
1416const config = require ( "config" ) ;
1517const cookieName = config . get ( "userToken.cookieName" ) ;
18+ const { addJoinData } = require ( "../../models/users" ) ;
19+ const joinData = require ( "../fixtures/user/join" ) ;
1620
1721chai . use ( chaiHttp ) ;
1822
1923describe ( "Arts" , function ( ) {
2024 let jwt ;
25+ let userId = "" ;
2126
2227 beforeEach ( async function ( ) {
23- const userId = await addUser ( ) ;
28+ userId = await addUser ( ) ;
2429 jwt = authService . generateAuthToken ( { userId } ) ;
2530 await arts . addArt ( artData [ 0 ] , userId ) ;
2631 } ) ;
@@ -108,6 +113,10 @@ describe("Arts", function () {
108113 expect ( res . body . arts ) . to . be . a ( "array" ) ;
109114 expect ( res . body . arts [ 0 ] ) . to . be . a ( "object" ) ;
110115 expect ( res . body . arts [ 0 ] . title ) . to . equal ( artData [ 0 ] . title ) ;
116+ expect ( res ) . to . have . header (
117+ "X-Deprecation-Warning" ,
118+ "WARNING: This endpoint is deprecated and will be removed in the future. Please use /arts/:userId to get the art details."
119+ ) ;
111120
112121 return done ( ) ;
113122 } ) ;
@@ -134,4 +143,96 @@ describe("Arts", function () {
134143 } ) ;
135144 } ) ;
136145 } ) ;
146+
147+ describe ( "GET /arts/:userId" , function ( ) {
148+ beforeEach ( async function ( ) {
149+ await addJoinData ( joinData ( userId ) [ 0 ] ) ;
150+ } ) ;
151+
152+ it ( "Should get all the arts of the user" , function ( done ) {
153+ chai
154+ . request ( app )
155+ . get ( `/arts/${ userId } ?dev=true` )
156+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
157+ . end ( ( err , res ) => {
158+ if ( err ) {
159+ return done ( err ) ;
160+ }
161+ expect ( res ) . to . have . status ( 200 ) ;
162+ expect ( res . body ) . to . be . a ( "object" ) ;
163+ expect ( res . body . message ) . to . equal ( `User Arts of userId ${ userId } returned successfully` ) ;
164+ expect ( res . body . arts ) . to . be . a ( "array" ) ;
165+ expect ( res . body . arts [ 0 ] ) . to . be . a ( "object" ) ;
166+ expect ( res . body . arts [ 0 ] . title ) . to . equal ( artData [ 0 ] . title ) ;
167+
168+ return done ( ) ;
169+ } ) ;
170+ } ) ;
171+
172+ it ( "Should return 401, for Unauthenticated User" , function ( done ) {
173+ chai
174+ . request ( app )
175+ . get ( `/arts/${ userId } ?dev=true` )
176+ . end ( ( err , res ) => {
177+ if ( err ) {
178+ return done ( err ) ;
179+ }
180+
181+ expect ( res ) . to . have . status ( 401 ) ;
182+ expect ( res . body ) . to . be . a ( "object" ) ;
183+ expect ( res . body ) . to . deep . equal ( {
184+ statusCode : 401 ,
185+ error : "Unauthorized" ,
186+ message : "Unauthenticated User" ,
187+ } ) ;
188+
189+ return done ( ) ;
190+ } ) ;
191+ } ) ;
192+
193+ it ( "Should return 204 No Content if no arts are found" , function ( done ) {
194+ sinon . stub ( artsQuery , "fetchUserArts" ) . resolves ( [ ] ) ;
195+
196+ chai
197+ . request ( app )
198+ . get ( `/arts/${ userId } ?dev=true` )
199+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
200+ . end ( ( err , res ) => {
201+ artsQuery . fetchUserArts . restore ( ) ;
202+
203+ if ( err ) {
204+ return done ( err ) ;
205+ }
206+
207+ expect ( res ) . to . have . status ( 204 ) ;
208+ expect ( res . body ) . to . deep . equal ( { } ) ;
209+ return done ( ) ;
210+ } ) ;
211+ } ) ;
212+
213+ it ( "Should return 500 Internal Server Error if there is an exception" , function ( done ) {
214+ sinon . stub ( artsQuery , "fetchUserArts" ) . throws ( new Error ( "Database error" ) ) ;
215+
216+ chai
217+ . request ( app )
218+ . get ( `/arts/${ userId } ?dev=true` )
219+ . set ( "cookie" , `${ cookieName } =${ jwt } ` )
220+ . end ( ( err , res ) => {
221+ artsQuery . fetchUserArts . restore ( ) ;
222+
223+ if ( err ) {
224+ return done ( err ) ;
225+ }
226+
227+ expect ( res ) . to . have . status ( 500 ) ;
228+ expect ( res . body ) . to . deep . equal ( {
229+ statusCode : 500 ,
230+ error : "Internal Server Error" ,
231+ message : "An internal server error occurred" ,
232+ } ) ;
233+
234+ return done ( ) ;
235+ } ) ;
236+ } ) ;
237+ } ) ;
137238} ) ;
0 commit comments