107 lines
2.8 KiB
Go
107 lines
2.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
DB "log101-blog-services/db"
|
|
"log101-blog-services/models"
|
|
"log101-blog-services/utils"
|
|
)
|
|
|
|
func TestGetComments(t *testing.T) {
|
|
db := utils.SetupTestDB()
|
|
DB.SetDB(db)
|
|
|
|
router := gin.Default()
|
|
router.LoadHTMLGlob("../templates/*")
|
|
|
|
// Populate the test database with data
|
|
// Create a comment with a username
|
|
db.Create(&models.Comment{PostId: "1", Body: "sample body 1", Username: "username1"})
|
|
db.Create(&models.Comment{PostId: "1", Body: "sample body 2", Username: "username2"})
|
|
|
|
router.GET("/comments", GetComments)
|
|
|
|
req, _ := http.NewRequest("GET", "/comments?postId=1", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
assert.Contains(t, w.Body.String(), "sample body 1")
|
|
assert.Contains(t, w.Body.String(), "sample body 2")
|
|
}
|
|
|
|
func TestPostComment(t *testing.T) {
|
|
db := utils.SetupTestDB()
|
|
DB.SetDB(db) // Set the mock DB
|
|
|
|
router := gin.Default()
|
|
router.LoadHTMLGlob("../templates/*")
|
|
|
|
router.POST("/comments", PostComment)
|
|
|
|
form := url.Values{}
|
|
form.Add("postId", "1")
|
|
form.Add("commentBody", "sample comment 1")
|
|
form.Add("username", "username1")
|
|
|
|
req, _ := http.NewRequest("POST", "/comments", strings.NewReader(form.Encode()))
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
assert.Contains(t, w.Body.String(), "sample comment 1")
|
|
}
|
|
|
|
func TestPostAnonymousComment(t *testing.T) {
|
|
db := utils.SetupTestDB()
|
|
DB.SetDB(db) // Set the mock DB
|
|
|
|
router := gin.Default()
|
|
router.LoadHTMLGlob("../templates/*")
|
|
|
|
router.POST("/comments", PostComment)
|
|
|
|
form := url.Values{}
|
|
form.Add("postId", "1")
|
|
form.Add("commentBody", "sample comment 1")
|
|
|
|
req, _ := http.NewRequest("POST", "/comments", strings.NewReader(form.Encode()))
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
assert.Contains(t, w.Body.String(), "sample comment 1")
|
|
assert.Contains(t, w.Body.String(), "anonim")
|
|
}
|
|
|
|
func TestPostCommentMissingParams(t *testing.T) {
|
|
db := utils.SetupTestDB()
|
|
DB.SetDB(db) // Set the mock DB
|
|
|
|
router := gin.Default()
|
|
router.LoadHTMLGlob("../templates/*") // Ensure you have your templates in the right path
|
|
|
|
router.POST("/comments", PostComment)
|
|
|
|
form := url.Values{}
|
|
form.Add("postId", "1")
|
|
|
|
req, _ := http.NewRequest("POST", "/comments", strings.NewReader(form.Encode()))
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|