@@ -447,3 +447,90 @@ func TestPostController_Update(t *testing.T) {
447
447
})
448
448
}
449
449
}
450
+
451
+ func TestPostController_Delete (t * testing.T ) {
452
+ tests := []struct {
453
+ name string
454
+ userID string
455
+ postID string
456
+ prepareMockPost func (ctx context.Context , post * mock.MockPost )
457
+ wantErr bool
458
+ wantCode int
459
+ }{
460
+ {
461
+ name : "正しく投稿を削除できる" ,
462
+ userID : "user-id" ,
463
+ postID : "1" ,
464
+ prepareMockPost : func (ctx context.Context , post * mock.MockPost ) {
465
+ post .EXPECT ().Delete (ctx , & entity.Post {
466
+ ID : 1 ,
467
+ UserID : "user-id" ,
468
+ }).Return (nil )
469
+ },
470
+ wantErr : false ,
471
+ wantCode : 200 ,
472
+ },
473
+ {
474
+ name : "存在しないポストならばErrNotFound" ,
475
+ userID : "user-id" ,
476
+ postID : "100" ,
477
+ prepareMockPost : func (ctx context.Context , post * mock.MockPost ) {
478
+ post .EXPECT ().Delete (ctx , & entity.Post {
479
+ ID : 100 ,
480
+ UserID : "user-id" ,
481
+ }).Return (entity .NewErrorNotFound ("post" ))
482
+ },
483
+ wantErr : true ,
484
+ wantCode : http .StatusForbidden ,
485
+ },
486
+ {
487
+ name : "ユーザーに削除権限がないならばErrIsNotAuthor" ,
488
+ userID : "other-user-id" ,
489
+ postID : "1" ,
490
+ prepareMockPost : func (ctx context.Context , post * mock.MockPost ) {
491
+ post .EXPECT ().Delete (ctx , & entity.Post {
492
+ ID : 1 ,
493
+ UserID : "other-user-id" ,
494
+ }).Return (entity .ErrIsNotAuthor )
495
+ },
496
+ wantErr : true ,
497
+ wantCode : http .StatusForbidden ,
498
+ },
499
+ }
500
+ for _ , tt := range tests {
501
+ t .Run (tt .name , func (t * testing.T ) {
502
+ e := echo .New ()
503
+ req := httptest .NewRequest ("DELETE" , "/" , nil )
504
+ req .Header .Set (echo .HeaderContentType , echo .MIMEApplicationJSON )
505
+ rec := httptest .NewRecorder ()
506
+ c := e .NewContext (req , rec )
507
+ c .SetParamNames ("postID" )
508
+ c .SetParamValues (tt .postID )
509
+ c .Set ("userID" , tt .userID )
510
+
511
+ ctx := context .Background ()
512
+ ctrl := gomock .NewController (t )
513
+ defer ctrl .Finish ()
514
+ postRepo := mock .NewMockPost (ctrl )
515
+ tt .prepareMockPost (ctx , postRepo )
516
+ userRepo := mock .NewMockUser (ctrl )
517
+
518
+ con := NewPostController (usecase .NewPostUsecase (postRepo , userRepo ))
519
+ err := con .Delete (c )
520
+
521
+ if (err != nil ) != tt .wantErr {
522
+ t .Errorf ("error = %v, wantErr = %v" , err , tt .wantErr )
523
+ }
524
+
525
+ if he , ok := err .(* echo.HTTPError ); ok {
526
+ if he .Code != tt .wantCode {
527
+ t .Errorf ("code = %d, want = %d" , he .Code , tt .wantCode )
528
+ }
529
+ } else {
530
+ if rec .Code != tt .wantCode {
531
+ t .Errorf ("code = %d, want = %d" , rec .Code , tt .wantCode )
532
+ }
533
+ }
534
+ })
535
+ }
536
+ }
0 commit comments