github.com/k0marov/go-socnet@v0.0.0-20220715154813-90d07867c782/features/comments/delivery/http/handlers/handlers_test.go (about) 1 package handlers_test 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "github.com/k0marov/go-socnet/core/general/client_errors" 8 "github.com/k0marov/go-socnet/core/general/core_values" 9 helpers "github.com/k0marov/go-socnet/core/helpers/http_test_helpers" 10 . "github.com/k0marov/go-socnet/core/helpers/test_helpers" 11 "io" 12 "net/http" 13 "net/http/httptest" 14 "testing" 15 16 "github.com/go-chi/chi/v5" 17 "github.com/k0marov/go-socnet/features/comments/delivery/http/handlers" 18 "github.com/k0marov/go-socnet/features/comments/delivery/http/responses" 19 "github.com/k0marov/go-socnet/features/comments/domain/entities" 20 "github.com/k0marov/go-socnet/features/comments/domain/values" 21 post_values "github.com/k0marov/go-socnet/features/posts/domain/values" 22 auth "github.com/k0marov/golang-auth" 23 ) 24 25 func createRequestWithPostId(id post_values.PostId, body io.Reader) *http.Request { 26 return httptest.NewRequest(http.MethodOptions, "/handler-should-not-care?post_id="+id, body) 27 } 28 29 func TestNewGetCommentsHandler(t *testing.T) { 30 post := RandomString() 31 comments := []entities.ContextedComment{RandomContextedComment()} 32 caller := RandomAuthUser() 33 34 helpers.BaseTest401(t, handlers.NewGetCommentsHandler(nil)) 35 t.Run("happy case", func(t *testing.T) { 36 getter := func(postId post_values.PostId, callerId core_values.UserId) ([]entities.ContextedComment, error) { 37 if postId == post && callerId == caller.Id { 38 return comments, nil 39 } 40 panic("unexpected args") 41 } 42 response := httptest.NewRecorder() 43 request := helpers.AddAuthDataToRequest(createRequestWithPostId(post, nil), caller) 44 handlers.NewGetCommentsHandler(getter).ServeHTTP(response, request) 45 AssertJSONData(t, response, responses.NewCommentListResponse(comments)) 46 }) 47 t.Run("error case - post_id is not provided", func(t *testing.T) { 48 response := httptest.NewRecorder() 49 request := helpers.AddAuthDataToRequest(helpers.CreateRequest(nil), caller) 50 handlers.NewGetCommentsHandler(nil).ServeHTTP(response, request) 51 AssertClientError(t, response, client_errors.IdNotProvided) 52 }) 53 helpers.BaseTestServiceErrorHandling(t, func(err error, response *httptest.ResponseRecorder) { 54 getter := func(post_values.PostId, core_values.UserId) ([]entities.ContextedComment, error) { 55 return []entities.ContextedComment{}, err 56 } 57 request := helpers.AddAuthDataToRequest(createRequestWithPostId(post, nil), caller) 58 handlers.NewGetCommentsHandler(getter).ServeHTTP(response, request) 59 }) 60 } 61 62 func TestNewCreateCommentHandler(t *testing.T) { 63 helpers.BaseTest401(t, handlers.NewCreateCommentHandler(nil)) 64 user := RandomAuthUser() 65 post := RandomString() 66 wantNewComment := values.NewCommentValue{ 67 Author: user.Id, 68 Text: RandomString(), 69 Post: post, 70 } 71 createdComment := RandomContextedComment() 72 t.Run("happy case", func(t *testing.T) { 73 creator := func(newComment values.NewCommentValue) (entities.ContextedComment, error) { 74 if newComment == wantNewComment { 75 return createdComment, nil 76 } 77 panic("unexpected args") 78 } 79 response := httptest.NewRecorder() 80 body := bytes.NewBuffer(nil) 81 json.NewEncoder(body).Encode(handlers.NewCommentRequest{Text: wantNewComment.Text}) 82 request := helpers.AddAuthDataToRequest(createRequestWithPostId(post, body), user) 83 handlers.NewCreateCommentHandler(creator).ServeHTTP(response, request) 84 AssertJSONData(t, response, responses.NewCommentResponse(createdComment)) 85 }) 86 t.Run("error case - post id is not provided", func(t *testing.T) { 87 request := helpers.AddAuthDataToRequest(helpers.CreateRequest(nil), user) 88 response := httptest.NewRecorder() 89 handlers.NewCreateCommentHandler(nil).ServeHTTP(response, request) 90 AssertClientError(t, response, client_errors.IdNotProvided) 91 }) 92 helpers.BaseTestServiceErrorHandling(t, func(err error, response *httptest.ResponseRecorder) { 93 creator := func(values.NewCommentValue) (entities.ContextedComment, error) { 94 return entities.ContextedComment{}, err 95 } 96 request := helpers.AddAuthDataToRequest(createRequestWithPostId(post, nil), user) 97 handlers.NewCreateCommentHandler(creator).ServeHTTP(response, request) 98 }) 99 } 100 101 func createRequestWithCommentId(id values.CommentId, caller auth.User) *http.Request { 102 request := helpers.CreateRequest(nil) 103 ctx := chi.NewRouteContext() 104 ctx.URLParams.Add("id", id) 105 request = request.WithContext(context.WithValue(request.Context(), chi.RouteCtxKey, ctx)) 106 return helpers.AddAuthDataToRequest(request, caller) 107 } 108 109 func TestNewToggleLikeCommentHandler(t *testing.T) { 110 helpers.BaseTest401(t, handlers.NewToggleLikeCommentHandler(nil)) 111 112 user := RandomAuthUser() 113 comment := RandomString() 114 t.Run("happy case", func(t *testing.T) { 115 likeToggler := func(commentId values.CommentId, caller core_values.UserId) error { 116 if commentId == comment && caller == user.Id { 117 return nil 118 } 119 panic("unexpected args") 120 } 121 response := httptest.NewRecorder() 122 handlers.NewToggleLikeCommentHandler(likeToggler).ServeHTTP(response, createRequestWithCommentId(comment, user)) 123 AssertStatusCode(t, response, http.StatusOK) 124 }) 125 t.Run("error case - id is not provided", func(t *testing.T) { 126 response := httptest.NewRecorder() 127 handlers.NewToggleLikeCommentHandler(nil).ServeHTTP(response, helpers.AddAuthDataToRequest(helpers.CreateRequest(nil), user)) 128 AssertClientError(t, response, client_errors.IdNotProvided) 129 }) 130 helpers.BaseTestServiceErrorHandling(t, func(err error, response *httptest.ResponseRecorder) { 131 likeToggler := func(values.CommentId, core_values.UserId) error { 132 return err 133 } 134 handlers.NewToggleLikeCommentHandler(likeToggler).ServeHTTP(response, createRequestWithCommentId(comment, user)) 135 }) 136 } 137 138 func TestDeleteCommentHandler(t *testing.T) { 139 helpers.BaseTest401(t, handlers.NewDeleteCommentHandler(nil)) 140 comment := RandomId() 141 caller := RandomAuthUser() 142 t.Run("happy case", func(t *testing.T) { 143 delete := func(commentId values.CommentId, callerId core_values.UserId) error { 144 if commentId == comment && callerId == caller.Id { 145 return nil 146 } 147 panic("unexpected args") 148 } 149 request := createRequestWithCommentId(comment, caller) 150 response := httptest.NewRecorder() 151 handlers.NewDeleteCommentHandler(delete)(response, request) 152 153 AssertStatusCode(t, response, http.StatusOK) 154 }) 155 t.Run("error case - id is not provided", func(t *testing.T) { 156 request := helpers.AddAuthDataToRequest(helpers.CreateRequest(nil), caller) 157 response := httptest.NewRecorder() 158 handlers.NewDeleteCommentHandler(nil)(response, request) 159 AssertClientError(t, response, client_errors.IdNotProvided) 160 }) 161 helpers.BaseTestServiceErrorHandling(t, func(err error, response *httptest.ResponseRecorder) { 162 delete := func(values.CommentId, core_values.UserId) error { 163 return err 164 } 165 request := createRequestWithCommentId(comment, caller) 166 handlers.NewDeleteCommentHandler(delete)(response, request) 167 }) 168 }