@@ -217,7 +217,11 @@ func TestGetComments(t *testing.T) {
217
217
})
218
218
w := httptest .NewRecorder ()
219
219
req := httptest .NewRequest ("GET" , "/v1/comments/stable/wordpress" , nil )
220
- GetComments (w , req )
220
+ params := Params {
221
+ "repo" : "stable" ,
222
+ "chartName" : "wordpress" ,
223
+ }
224
+ GetComments (w , req , params )
221
225
assert .Equal (t , http .StatusOK , w .Code )
222
226
var b body
223
227
json .NewDecoder (w .Body ).Decode (& b )
@@ -263,7 +267,11 @@ func TestCreateComment(t *testing.T) {
263
267
}
264
268
w := httptest .NewRecorder ()
265
269
req := httptest .NewRequest ("POST" , "/v1/comments/stable/wordpress" , bytes .NewBuffer ([]byte (tt .requestBody )))
266
- CreateComment (w , req )
270
+ params := Params {
271
+ "repo" : "stable" ,
272
+ "chartName" : "wordpress" ,
273
+ }
274
+ CreateComment (w , req , params )
267
275
assert .Equal (t , tt .wantCode , w .Code )
268
276
})
269
277
}
@@ -274,7 +282,107 @@ func TestCreateCommentUnauthorized(t *testing.T) {
274
282
dbSession = testutil .NewMockSession (& m )
275
283
w := httptest .NewRecorder ()
276
284
req := httptest .NewRequest ("POST" , "/v1/comments/stable/wordpress" , nil )
277
- CreateComment (w , req )
285
+ params := Params {
286
+ "repo" : "stable" ,
287
+ "chartName" : "wordpress" ,
288
+ }
289
+ CreateComment (w , req , params )
290
+ assert .Equal (t , http .StatusUnauthorized , w .Code )
291
+ }
292
+
293
+ func TestDeleteComment (t * testing.T ) {
294
+ var m mock.Mock
295
+ dbSession = testutil .NewMockSession (& m )
296
+
297
+ currentUser := & user {
ID :
bson .
NewObjectId (),
Name :
"Rick Sanchez" ,
Email :
"[email protected] " }
298
+ oldGetCurrentUser := getCurrentUser
299
+ getCurrentUser = func (_ * http.Request ) (* user , error ) { return currentUser , nil }
300
+ defer func () { getCurrentUser = oldGetCurrentUser }()
301
+
302
+ commentId := getNewObjectID ()
303
+ oldGetNewObjectID := getNewObjectID
304
+ getNewObjectID = func () bson.ObjectId { return commentId }
305
+ defer func () { getNewObjectID = oldGetNewObjectID }()
306
+
307
+ commentTimestamp := getTimestamp ()
308
+ oldGetTimestamp := getTimestamp
309
+ getTimestamp = func () time.Time { return commentTimestamp }
310
+ defer func () { getTimestamp = oldGetTimestamp }()
311
+
312
+ m .On ("One" , & item {}).Return (nil ).Run (func (args mock.Arguments ) {
313
+ * args .Get (0 ).(* item ) = item {ID : "stable/wordpress" , Type : "chart" , Comments : []comment {
314
+ comment {ID : bson .NewObjectId (), Text : "First comment" , CreatedAt : getTimestamp (), Author : currentUser },
315
+ comment {ID : commentId , Text : "Second comment" , CreatedAt : commentTimestamp , Author : currentUser },
316
+ }}
317
+ })
318
+
319
+ tests := []struct {
320
+ name string
321
+ commentId string
322
+ wantCode int
323
+ }{
324
+ {"does not exist" , "5a0e9183833def3853088836" , http .StatusNotFound },
325
+ {"exists" , commentId .Hex (), http .StatusAccepted },
326
+ }
327
+
328
+ for _ , tt := range tests {
329
+ t .Run (tt .name , func (t * testing.T ) {
330
+ if tt .wantCode == http .StatusAccepted {
331
+ m .On ("UpdateId" , "stable/wordpress" , bson.M {"$pull" : bson.M {"comments" : comment {ID : commentId , Text : "Second comment" , Author : currentUser , CreatedAt : commentTimestamp }}})
332
+ }
333
+
334
+ w := httptest .NewRecorder ()
335
+ req := httptest .NewRequest ("DELETE" , "/v1/comments/stable/wordpress" + tt .commentId , nil )
336
+ params := Params {
337
+ "repo" : "stable" ,
338
+ "chartName" : "wordpress" ,
339
+ "commentId" : tt .commentId ,
340
+ }
341
+ DeleteComment (w , req , params )
342
+ assert .Equal (t , tt .wantCode , w .Code )
343
+ })
344
+ }
345
+ }
346
+
347
+ func TestDeleteCommentUnauthorized (t * testing.T ) {
348
+ var m mock.Mock
349
+ dbSession = testutil .NewMockSession (& m )
350
+ w := httptest .NewRecorder ()
351
+ req := httptest .NewRequest ("DELETE" , "/v1/comments/stable/wordpress/5a0e9183833def3853088836" , nil )
352
+ params := Params {
353
+ "repo" : "stable" ,
354
+ "chartName" : "wordpress" ,
355
+ "commentId" : "5a0e9183833def3853088836" ,
356
+ }
357
+ DeleteComment (w , req , params )
358
+ assert .Equal (t , http .StatusUnauthorized , w .Code )
359
+ }
360
+
361
+ func TestDeleteCommentCannotDeleteOtherUsersComments (t * testing.T ) {
362
+ var m mock.Mock
363
+ dbSession = testutil .NewMockSession (& m )
364
+ w := httptest .NewRecorder ()
365
+
366
+ currentUser := & user {
ID :
bson .
NewObjectId (),
Name :
"Rick Sanchez" ,
Email :
"[email protected] " }
367
+ oldGetCurrentUser := getCurrentUser
368
+ getCurrentUser = func (_ * http.Request ) (* user , error ) { return currentUser , nil }
369
+ defer func () { getCurrentUser = oldGetCurrentUser }()
370
+
371
+ commentId := getNewObjectID ()
372
+ m .On ("One" , & item {}).Return (nil ).Run (func (args mock.Arguments ) {
373
+ * args .Get (0 ).(* item ) = item {ID : "stable/wordpress" , Type : "chart" , Comments : []comment {
374
+ comment {ID : bson .NewObjectId (), Author : & user {ID : bson .NewObjectId ()}},
375
+ comment {ID : commentId , Author : & user {ID : bson .NewObjectId ()}},
376
+ }}
377
+ })
378
+
379
+ req := httptest .NewRequest ("DELETE" , "/v1/comments/stable/wordpress/" + commentId .Hex (), nil )
380
+ params := Params {
381
+ "repo" : "stable" ,
382
+ "chartName" : "wordpress" ,
383
+ "commentId" : commentId .Hex (),
384
+ }
385
+ DeleteComment (w , req , params )
278
386
assert .Equal (t , http .StatusUnauthorized , w .Code )
279
387
}
280
388
0 commit comments