diff --git a/admin-app/post.go b/admin-app/post.go index cbe63f2..1ff3ff1 100644 --- a/admin-app/post.go +++ b/admin-app/post.go @@ -115,7 +115,7 @@ func deletePostHandler(database database.Database) func(*gin.Context) { return } - if rows_affected != 1 { + if rows_affected == 0 { log.Error().Msgf("no post found with id `%d`", delete_post_binding.Id) c.JSON(http.StatusNotFound, common.MsgErrorRes("no post found")) return diff --git a/tests/admin_app_tests/endpoint_tests/posts_test.go b/tests/admin_app_tests/endpoint_tests/posts_test.go index 3eb316b..b42e191 100644 --- a/tests/admin_app_tests/endpoint_tests/posts_test.go +++ b/tests/admin_app_tests/endpoint_tests/posts_test.go @@ -3,6 +3,7 @@ package endpoint_tests import ( "bytes" "encoding/json" + "fmt" "net/http" "net/http/httptest" "testing" @@ -34,9 +35,13 @@ var app_settings = common.AppSettings{ } func TestIndexPing(t *testing.T) { + databaseMock := mocks.DatabaseMock{ + AddPostHandler: func(string, string, string) (int, error) { + return 0, nil + }, + } - database_mock := mocks.DatabaseMock{} - r := admin_app.SetupRoutes(app_settings, database_mock) + r := admin_app.SetupRoutes(app_settings, databaseMock) w := httptest.NewRecorder() request := postRequest{ @@ -60,10 +65,250 @@ func TestIndexPing(t *testing.T) { assert.Equal(t, response.Id, 0) } -// TODO : Test request without excerpt +func TestPostPostSuccess(t *testing.T) { + databaseMock := mocks.DatabaseMock{ + AddPostHandler: func(string, string, string) (int, error) { + return 0, nil + }, + } + + postData := admin_app.AddPostRequest{ + Title: "Title", + Excerpt: "Excerpt", + Content: "Content", + } + + router := admin_app.SetupRoutes(app_settings, databaseMock) + responseRecorder := httptest.NewRecorder() + + body, _ := json.Marshal(postData) + request, _ := http.NewRequest(http.MethodPost, "/posts", bytes.NewBuffer(body)) + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, 200, responseRecorder.Code) + var response admin_app.PostIdResponse + err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) + assert.Nil(t, err) + + assert.NotNil(t, response.Id) +} + +func TestPostPostWithoutTitle(t *testing.T) { + databaseMock := mocks.DatabaseMock{ + AddPostHandler: func(string, string, string) (int, error) { + return 0, nil + }, + } + + postData := admin_app.AddPostRequest{ + Excerpt: "Excerpt", + Content: "Content", + } + + router := admin_app.SetupRoutes(app_settings, databaseMock) + responseRecorder := httptest.NewRecorder() + + body, _ := json.Marshal(postData) + request, _ := http.NewRequest(http.MethodPost, "/posts", bytes.NewBuffer(body)) + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, 200, responseRecorder.Code) + var response admin_app.PostIdResponse + err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) + assert.Nil(t, err) + + assert.NotNil(t, response.Id) +} + +func TestPostPostWithoutExcerpt(t *testing.T) { + databaseMock := mocks.DatabaseMock{ + AddPostHandler: func(string, string, string) (int, error) { + return 0, nil + }, + } + + postData := admin_app.AddPostRequest{ + Title: "Title", + Content: "Content", + } + + router := admin_app.SetupRoutes(app_settings, databaseMock) + responseRecorder := httptest.NewRecorder() + + body, _ := json.Marshal(postData) + request, _ := http.NewRequest(http.MethodPost, "/posts", bytes.NewBuffer(body)) + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, 200, responseRecorder.Code) + var response admin_app.PostIdResponse + err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) + assert.Nil(t, err) + + assert.NotNil(t, response.Id) +} + +func TestPostPostWithoutContent(t *testing.T) { + databaseMock := mocks.DatabaseMock{ + AddPostHandler: func(string, string, string) (int, error) { + return 0, nil + }, + } + + postData := admin_app.AddPostRequest{ + Title: "Title", + Excerpt: "Excerpt", + } + + router := admin_app.SetupRoutes(app_settings, databaseMock) + responseRecorder := httptest.NewRecorder() + + body, _ := json.Marshal(postData) + request, _ := http.NewRequest(http.MethodPost, "/posts", bytes.NewBuffer(body)) + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, 200, responseRecorder.Code) + var response admin_app.PostIdResponse + err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) + assert.Nil(t, err) + + assert.NotNil(t, response.Id) +} + +func TestPostPostNoBody(t *testing.T) { + databaseMock := mocks.DatabaseMock{} -// TODO : Test request without content + router := admin_app.SetupRoutes(app_settings, databaseMock) + responseRecorder := httptest.NewRecorder() -// TODO : Test request without title + request, _ := http.NewRequest(http.MethodPost, "/posts", nil) + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, 400, responseRecorder.Code) + var response common.ErrorResponse + err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) + assert.Nil(t, err) -// TODO : Test request that fails to be added to database + assert.Equal(t, response.Msg, "no request body provided") +} + +func TestPostPostInvalidPostRequest(t *testing.T) { + databaseMock := mocks.DatabaseMock{} + + router := admin_app.SetupRoutes(app_settings, databaseMock) + responseRecorder := httptest.NewRecorder() + + body, _ := json.Marshal("{\"test\": \"Something\"}") + request, _ := http.NewRequest(http.MethodPost, "/posts", bytes.NewBuffer(body)) + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, 400, responseRecorder.Code) + var response common.ErrorResponse + err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) + assert.Nil(t, err) + + assert.Equal(t, response.Msg, "invalid request body") + assert.NotNil(t, response.Err) +} + +func TestPostPostFailedSave(t *testing.T) { + databaseMock := mocks.DatabaseMock{ + AddPostHandler: func(string, string, string) (int, error) { + return 0, fmt.Errorf("saving post failed") + }, + } + + postData := admin_app.AddPostRequest{ + Title: "Title", + Excerpt: "Excerpt", + Content: "Content", + } + + router := admin_app.SetupRoutes(app_settings, databaseMock) + responseRecorder := httptest.NewRecorder() + + body, _ := json.Marshal(postData) + request, _ := http.NewRequest(http.MethodPost, "/posts", bytes.NewBuffer(body)) + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, 400, responseRecorder.Code) + var response common.ErrorResponse + err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) + assert.Nil(t, err) + + assert.Equal(t, response.Msg, "could not add post") + assert.NotNil(t, response.Err) +} + +func TestDeletePostSuccess(t *testing.T) { + databaseMock := mocks.DatabaseMock{ + DeletePostHandler: func(int) (int, error) { + return 1, nil + }, + } + + router := admin_app.SetupRoutes(app_settings, databaseMock) + responseRecorder := httptest.NewRecorder() + + request, _ := http.NewRequest(http.MethodDelete, "/posts/1", nil) + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, 200, responseRecorder.Code) + var response admin_app.PostIdResponse + err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) + assert.Nil(t, err) + + assert.Equal(t, response.Id, 1) +} + +func TestDeletePostFailedDelete(t *testing.T) { + databaseMock := mocks.DatabaseMock{ + DeletePostHandler: func(int) (int, error) { + return 0, fmt.Errorf("delete post failed") + }, + } + + router := admin_app.SetupRoutes(app_settings, databaseMock) + responseRecorder := httptest.NewRecorder() + + request, _ := http.NewRequest(http.MethodDelete, "/posts/1", nil) + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, 400, responseRecorder.Code) + var response common.ErrorResponse + err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) + assert.Nil(t, err) + + assert.Equal(t, response.Msg, "could not delete post") + assert.NotNil(t, response.Err) +} + +func TestDeletePostNotFound(t *testing.T) { + databaseMock := mocks.DatabaseMock{ + DeletePostHandler: func(int) (int, error) { + return 0, nil + }, + } + + router := admin_app.SetupRoutes(app_settings, databaseMock) + responseRecorder := httptest.NewRecorder() + + request, _ := http.NewRequest(http.MethodDelete, "/posts/1", nil) + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, 404, responseRecorder.Code) + var response common.ErrorResponse + err := json.Unmarshal(responseRecorder.Body.Bytes(), &response) + assert.Nil(t, err) + + assert.Equal(t, response.Msg, "no post found") +} diff --git a/tests/mocks/mocks.go b/tests/mocks/mocks.go index c56022e..5e9f277 100644 --- a/tests/mocks/mocks.go +++ b/tests/mocks/mocks.go @@ -1,14 +1,14 @@ package mocks import ( - "fmt" - "github.com/matheusgomes28/urchin/common" ) type DatabaseMock struct { - GetPostHandler func(int) (common.Post, error) - GetPostsHandler func(int, int) ([]common.Post, error) + GetPostHandler func(int) (common.Post, error) + GetPostsHandler func(int, int) ([]common.Post, error) + AddPostHandler func(string, string, string) (int, error) + DeletePostHandler func(int) (int, error) } func (db DatabaseMock) GetPosts(limit int, offset int) ([]common.Post, error) { @@ -20,7 +20,7 @@ func (db DatabaseMock) GetPost(post_id int) (common.Post, error) { } func (db DatabaseMock) AddPost(title string, excerpt string, content string) (int, error) { - return 0, nil + return db.AddPostHandler(title, excerpt, content) } func (db DatabaseMock) ChangePost(id int, title string, excerpt string, content string) error { @@ -28,5 +28,5 @@ func (db DatabaseMock) ChangePost(id int, title string, excerpt string, content } func (db DatabaseMock) DeletePost(id int) (int, error) { - return 0, fmt.Errorf("not implemented") + return db.DeletePostHandler(id) }