1
1
const chai = require ( "chai" ) ;
2
2
const { expect } = chai ;
3
3
const chaiHttp = require ( "chai-http" ) ;
4
+ const sinon = require ( "sinon" ) ;
5
+ const artsQuery = require ( "../../models/arts" ) ;
4
6
5
7
const app = require ( "../../server" ) ;
6
8
const authService = require ( "../../services/authService" ) ;
@@ -13,14 +15,17 @@ const artData = require("../fixtures/arts/arts")();
13
15
14
16
const config = require ( "config" ) ;
15
17
const cookieName = config . get ( "userToken.cookieName" ) ;
18
+ const { addJoinData } = require ( "../../models/users" ) ;
19
+ const joinData = require ( "../fixtures/user/join" ) ;
16
20
17
21
chai . use ( chaiHttp ) ;
18
22
19
23
describe ( "Arts" , function ( ) {
20
24
let jwt ;
25
+ let userId = "" ;
21
26
22
27
beforeEach ( async function ( ) {
23
- const userId = await addUser ( ) ;
28
+ userId = await addUser ( ) ;
24
29
jwt = authService . generateAuthToken ( { userId } ) ;
25
30
await arts . addArt ( artData [ 0 ] , userId ) ;
26
31
} ) ;
@@ -108,6 +113,10 @@ describe("Arts", function () {
108
113
expect ( res . body . arts ) . to . be . a ( "array" ) ;
109
114
expect ( res . body . arts [ 0 ] ) . to . be . a ( "object" ) ;
110
115
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
+ ) ;
111
120
112
121
return done ( ) ;
113
122
} ) ;
@@ -134,4 +143,96 @@ describe("Arts", function () {
134
143
} ) ;
135
144
} ) ;
136
145
} ) ;
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
+ } ) ;
137
238
} ) ;
0 commit comments