Skip to content

Commit

Permalink
PR review feedback:
Browse files Browse the repository at this point in the history
- Updated 'POST /posts' endpoint to handle missing data properly.
- Improved tests for 'POST /posts' and 'DELETE /posts/{id}' endpoints.
  • Loading branch information
AlDu2407 committed Jun 1, 2024
1 parent 27e0dec commit 3e2fc04
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 56 deletions.
25 changes: 25 additions & 0 deletions admin-app/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package admin_app

import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/matheusgomes28/urchin/common"
Expand Down Expand Up @@ -51,6 +53,13 @@ func postPostHandler(database database.Database) func(*gin.Context) {
return
}

err = checkRequiredData(add_post_request)
if err != nil {
log.Error().Msgf("failed to add post required data is missing: %v", err)
c.JSON(http.StatusBadRequest, common.ErrorRes("missing required data", err))
return
}

id, err := database.AddPost(
add_post_request.Title,
add_post_request.Excerpt,
Expand Down Expand Up @@ -126,3 +135,19 @@ func deletePostHandler(database database.Database) func(*gin.Context) {
})
}
}

func checkRequiredData(addPostRequest AddPostRequest) error {
if strings.TrimSpace(addPostRequest.Title) == "" {
return fmt.Errorf("missing required data 'Title'")
}

if strings.TrimSpace(addPostRequest.Excerpt) == "" {
return fmt.Errorf("missing required data 'Excerpt'")
}

if strings.TrimSpace(addPostRequest.Content) == "" {
return fmt.Errorf("missing required data 'Content'")
}

return nil
}
77 changes: 21 additions & 56 deletions tests/admin_app_tests/endpoint_tests/posts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ import (
"github.com/stretchr/testify/assert"
)

type postRequest struct {
Title string `json:"title"`
Excerpt string `json:"excerpt"`
Content string `json:"content"`
}

type postResponse struct {
Id int `json:"id"`
}

var app_settings = common.AppSettings{
DatabaseAddress: "localhost",
DatabasePort: 3006,
Expand All @@ -34,37 +24,6 @@ var app_settings = common.AppSettings{
ImageDirectory: "../../../images",
}

func TestIndexPing(t *testing.T) {
databaseMock := mocks.DatabaseMock{
AddPostHandler: func(string, string, string) (int, error) {
return 0, nil
},
}

r := admin_app.SetupRoutes(app_settings, databaseMock)
w := httptest.NewRecorder()

request := postRequest{
Title: "",
Excerpt: "",
Content: "",
}
request_body, err := json.Marshal(request)
assert.Nil(t, err)

req, _ := http.NewRequest("POST", "/posts", bytes.NewReader(request_body))
req.Header.Add("content-type", "application/json")
r.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)

var response postResponse
err = json.Unmarshal(w.Body.Bytes(), &response)
assert.Nil(t, err)

assert.Equal(t, response.Id, 0)
}

func TestPostPostSuccess(t *testing.T) {
databaseMock := mocks.DatabaseMock{
AddPostHandler: func(string, string, string) (int, error) {
Expand All @@ -86,7 +45,7 @@ func TestPostPostSuccess(t *testing.T) {

router.ServeHTTP(responseRecorder, request)

assert.Equal(t, 200, responseRecorder.Code)
assert.Equal(t, http.StatusOK, responseRecorder.Code)
var response admin_app.PostIdResponse
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err)
Expand Down Expand Up @@ -114,12 +73,14 @@ func TestPostPostWithoutTitle(t *testing.T) {

router.ServeHTTP(responseRecorder, request)

assert.Equal(t, 200, responseRecorder.Code)
var response admin_app.PostIdResponse
assert.Equal(t, http.StatusBadRequest, responseRecorder.Code)
var response common.ErrorResponse
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err)

assert.NotNil(t, response.Id)
assert.Equal(t, response.Msg, "missing required data")
assert.NotNil(t, response.Err)
assert.Contains(t, response.Err, "'Title'")
}

func TestPostPostWithoutExcerpt(t *testing.T) {
Expand All @@ -142,12 +103,14 @@ func TestPostPostWithoutExcerpt(t *testing.T) {

router.ServeHTTP(responseRecorder, request)

assert.Equal(t, 200, responseRecorder.Code)
var response admin_app.PostIdResponse
assert.Equal(t, http.StatusBadRequest, responseRecorder.Code)
var response common.ErrorResponse
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err)

assert.NotNil(t, response.Id)
assert.Equal(t, response.Msg, "missing required data")
assert.NotNil(t, response.Err)
assert.Contains(t, response.Err, "'Excerpt'")
}

func TestPostPostWithoutContent(t *testing.T) {
Expand All @@ -170,12 +133,14 @@ func TestPostPostWithoutContent(t *testing.T) {

router.ServeHTTP(responseRecorder, request)

assert.Equal(t, 200, responseRecorder.Code)
var response admin_app.PostIdResponse
assert.Equal(t, http.StatusBadRequest, responseRecorder.Code)
var response common.ErrorResponse
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err)

assert.NotNil(t, response.Id)
assert.Equal(t, response.Msg, "missing required data")
assert.NotNil(t, response.Err)
assert.Contains(t, response.Err, "'Content'")
}

func TestPostPostNoBody(t *testing.T) {
Expand All @@ -188,7 +153,7 @@ func TestPostPostNoBody(t *testing.T) {

router.ServeHTTP(responseRecorder, request)

assert.Equal(t, 400, responseRecorder.Code)
assert.Equal(t, http.StatusBadRequest, responseRecorder.Code)
var response common.ErrorResponse
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err)
Expand All @@ -207,7 +172,7 @@ func TestPostPostInvalidPostRequest(t *testing.T) {

router.ServeHTTP(responseRecorder, request)

assert.Equal(t, 400, responseRecorder.Code)
assert.Equal(t, http.StatusBadRequest, responseRecorder.Code)
var response common.ErrorResponse
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err)
Expand Down Expand Up @@ -237,7 +202,7 @@ func TestPostPostFailedSave(t *testing.T) {

router.ServeHTTP(responseRecorder, request)

assert.Equal(t, 400, responseRecorder.Code)
assert.Equal(t, http.StatusBadRequest, responseRecorder.Code)
var response common.ErrorResponse
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err)
Expand All @@ -260,7 +225,7 @@ func TestDeletePostSuccess(t *testing.T) {

router.ServeHTTP(responseRecorder, request)

assert.Equal(t, 200, responseRecorder.Code)
assert.Equal(t, http.StatusOK, responseRecorder.Code)
var response admin_app.PostIdResponse
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err)
Expand All @@ -282,7 +247,7 @@ func TestDeletePostFailedDelete(t *testing.T) {

router.ServeHTTP(responseRecorder, request)

assert.Equal(t, 400, responseRecorder.Code)
assert.Equal(t, http.StatusBadRequest, responseRecorder.Code)
var response common.ErrorResponse
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3e2fc04

Please sign in to comment.