Skip to content

Commit

Permalink
Improved test coverage for post endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlDu2407 committed May 31, 2024
1 parent b8644c2 commit a617e76
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 13 deletions.
2 changes: 1 addition & 1 deletion admin-app/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
257 changes: 251 additions & 6 deletions tests/admin_app_tests/endpoint_tests/posts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package endpoint_tests
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -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{
Expand All @@ -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")
}
12 changes: 6 additions & 6 deletions tests/mocks/mocks.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -20,13 +20,13 @@ 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 {
return nil
}

func (db DatabaseMock) DeletePost(id int) (int, error) {
return 0, fmt.Errorf("not implemented")
return db.DeletePostHandler(id)
}

0 comments on commit a617e76

Please sign in to comment.