79 lines
2.4 KiB
Go
79 lines
2.4 KiB
Go
package apitest
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestPostBookHandler_OkAuthorExist(t *testing.T) {
|
|
bookJson :=
|
|
`{
|
|
"title": "Le Procès",
|
|
"author": "Franz Kafka"
|
|
}`
|
|
testPostBookHandler(t, bookJson, 200)
|
|
}
|
|
|
|
func TestPostBookHandler_OkNewAuthor(t *testing.T) {
|
|
bookJson :=
|
|
`{
|
|
"title": "Le joueur d'échecs",
|
|
"author": "Stefan Zweig"
|
|
}`
|
|
testPostBookHandler(t, bookJson, 200)
|
|
}
|
|
|
|
func TestPostBookHandler_OkOnlyTitle(t *testing.T) {
|
|
bookJson :=
|
|
`{
|
|
"title": "Le Procès"
|
|
}`
|
|
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 := testutils.TestSetup()
|
|
w := httptest.NewRecorder()
|
|
|
|
token := testutils.ConnectDemoUser(router)
|
|
req, _ := http.NewRequest("POST", "/ws/book",
|
|
strings.NewReader(string(bookJson)))
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, expectedCode, w.Code)
|
|
|
|
}
|