Files
bibliomane/book_test.go
Arthur Lefebvre 1cc7881ff0 fixed filtering on connected user
added tests and updated demo data
2025-10-08 15:55:25 +02:00

152 lines
4.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"git.artlef.fr/PersonalLibraryManager/internal/config"
"github.com/stretchr/testify/assert"
)
func testSetup() *gin.Engine {
c := config.LoadConfig("config_test/test.toml")
return setup(&c)
}
type loginResponse struct {
Message string `json:"message"`
Token string `json:"token"`
}
func connectDemoUser(router *gin.Engine) string {
loginJson :=
`{
"username": "demo",
"password":"demopw"
}`
return connectUser(router, loginJson)
}
func connectDemo2User(router *gin.Engine) string {
loginJson :=
`{
"username": "demo2",
"password":"demopw"
}`
return connectUser(router, loginJson)
}
func connectUser(router *gin.Engine, loginJson string) string {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/auth/login", strings.NewReader(loginJson))
router.ServeHTTP(w, req)
var parsedResponse loginResponse
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
if err != nil {
log.Fatal(err)
}
return parsedResponse.Token
}
type bookUserGet struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
Rating int `json:"rating" binding:"min=0,max=10"`
}
func TestGetBooksHandler_Demo(t *testing.T) {
router := testSetup()
token := connectDemoUser(router)
books := testGetbooksHandler(t, router, token, 200)
assert.Equal(t, 26, len(books))
}
func TestGetBooksHandler_Demo2(t *testing.T) {
router := testSetup()
token := connectDemo2User(router)
books := testGetbooksHandler(t, router, token, 200)
assert.Equal(t, 2, len(books))
}
func testGetbooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int) []bookUserGet {
req, _ := http.NewRequest("GET", "/mybooks", nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", userToken))
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
var parsedResponse []bookUserGet
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
if err != nil {
log.Fatal(err)
}
assert.Equal(t, expectedCode, w.Code)
return parsedResponse
}
func TestPostBookHandler_Ok(t *testing.T) {
bookJson :=
`{
"title": "Le château",
"author": "Kafka"
}`
testPostBookHandler(t, bookJson, 200)
}
func TestPostBookHandler_OkOnlyTitle(t *testing.T) {
bookJson :=
`{
"title": "Le château"
}`
testPostBookHandler(t, bookJson, 200)
}
func TestPostBookHandler_noTitle(t *testing.T) {
bookJson :=
`{
"author": "Kafka"
}`
testPostBookHandler(t, bookJson, 400)
}
func TestPostBookHandler_TitleTooLong(t *testing.T) {
bookJson :=
`{
"title": "Noisy outlaws, unfriendly blobs, and some other things that aren't as scary, maybe, depending on how you feel about lost lands, stray cellphones, creatures from the sky, parents who disappear in Peru, a man named Lars Farf, and one other story we couldn't quite finish, so maybe you could help us out.",
"author": "Eli Horowitz"
}`
testPostBookHandler(t, bookJson, 400)
}
func TestPostBookHandler_AuthorTooLong(t *testing.T) {
bookJson :=
`{
"title": "something",
"author": "Wolfeschlegelsteinhausenbergerdorffwelchevoralternwarengewissenhaftschaferswessenschafewarenwohlgepflegeundsorgfaltigkeitbeschutzenvonangreifendurchihrraubgierigfeindewelchevoralternzwolftausendjahresvorandieerscheinenvanderersteerdemenschderraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchenachdiesternwelchegehabtbewohnbarplanetenkreisedrehensichundwohinderneurassevonverstandigmenschlichkeitkonntefortpflanzenundsicherfreuenanlebenslanglichfreudeundruhemitnichteinfurchtvorangreifenvonandererintelligentgeschopfsvonhinzwischensternartigraum"
}`
testPostBookHandler(t, bookJson, 400)
}
func testPostBookHandler(t *testing.T, bookJson string, expectedCode int) {
router := testSetup()
w := httptest.NewRecorder()
token := connectDemoUser(router)
req, _ := http.NewRequest("POST", "/book",
strings.NewReader(string(bookJson)))
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, req)
assert.Equal(t, expectedCode, w.Code)
}