move tests to dedicated package

This commit is contained in:
2025-10-27 18:08:47 +01:00
parent cae46614ba
commit d407b41c9f
7 changed files with 7 additions and 7 deletions

View File

@@ -0,0 +1,9 @@
# Path to sqlite database file.
database_file_path = "file::memory:?cache=shared"
# The path to the sql file to load for demo data.
demo_data_path = "../../demodata.sql"
# The port to listen on for the server.
port = "8080"

View File

@@ -0,0 +1,66 @@
package apitest
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/stretchr/testify/assert"
)
type fetchedBook struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
Rating int `json:"rating"`
Read bool `json:"read"`
}
func TestGetBook_Ok(t *testing.T) {
book := testGetBook(t, "5", http.StatusOK)
assert.Equal(t,
fetchedBook{
Title: "Rigodon",
Author: "Louis-Ferdinand Céline",
Rating: 10,
Read: true,
}, book)
}
func TestGetBook_NoUserBook(t *testing.T) {
book := testGetBook(t, "18", http.StatusOK)
assert.Equal(t,
fetchedBook{
Title: "De sang-froid",
Author: "Truman Capote",
Read: false,
}, book)
}
func TestGetBook_IdNotFound(t *testing.T) {
testGetBook(t, "46544", http.StatusNotFound)
}
func TestGetBook_IdNotInt(t *testing.T) {
testGetBook(t, "wrong", http.StatusBadRequest)
}
func testGetBook(t *testing.T, id string, status int) fetchedBook {
router := testutils.TestSetup()
token := testutils.ConnectDemoUser(router)
req, _ := http.NewRequest("GET", "/book/"+id, nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
var book fetchedBook
err := json.Unmarshal(w.Body.Bytes(), &book)
if err != nil {
t.Error(err)
}
assert.Equal(t, status, w.Code)
return book
}

View File

@@ -0,0 +1,52 @@
package apitest
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
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 := testutils.TestSetup()
token := testutils.ConnectDemoUser(router)
books := testGetbooksHandler(t, router, token, 200)
assert.Equal(t, 26, len(books))
}
func TestGetBooksHandler_Demo2(t *testing.T) {
router := testutils.TestSetup()
token := testutils.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 {
t.Error(err)
}
assert.Equal(t, expectedCode, w.Code)
return parsedResponse
}

View File

@@ -0,0 +1,43 @@
package apitest
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/stretchr/testify/assert"
)
func TestPostBookReadHandler_Ok(t *testing.T) {
userBookJson :=
`{
"bookId": 6
}`
testPostBookReadHandler(t, userBookJson, http.StatusOK)
}
func TestPostBookReadHandler_IDDoesNotExist(t *testing.T) {
userBookJson :=
`{
"bookId": 46546
}`
testPostBookReadHandler(t, userBookJson, http.StatusNotFound)
}
func testPostBookReadHandler(t *testing.T, userBookJson string, expectedCode int) {
router := testutils.TestSetup()
w := httptest.NewRecorder()
token := testutils.ConnectDemo2User(router)
req, _ := http.NewRequest("POST", "/book/read",
strings.NewReader(string(userBookJson)))
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, req)
log.Printf("%s\n", w.Body.String())
assert.Equal(t, expectedCode, w.Code)
}

View File

@@ -0,0 +1,69 @@
package apitest
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/stretchr/testify/assert"
)
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 := testutils.TestSetup()
w := httptest.NewRecorder()
token := testutils.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)
}

View File

@@ -0,0 +1,68 @@
package apitest
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/stretchr/testify/assert"
)
func TestPostUserHandler_Working(t *testing.T) {
userJson :=
`{
"username": "artlef",
"password": "123456789"
}`
testPostUserHandler(t, userJson, 200)
}
func TestPostUserHandler_UsernameTooSmall(t *testing.T) {
userJson :=
`{
"username": "a",
"password": "123456789"
}`
testPostUserHandler(t, userJson, 400)
}
func TestPostUserHandler_UsernameTooBig(t *testing.T) {
userJson :=
`{
"username": "thisusernameistoolong",
"password": "123456789"
}`
testPostUserHandler(t, userJson, 400)
}
func TestPostUserHandler_PasswordTooSmall(t *testing.T) {
userJson :=
`{
"username": "thisusernameisok",
"password": "lol"
}`
testPostUserHandler(t, userJson, 400)
}
func TestPostUserHandler_PasswordTooBig(t *testing.T) {
userJson :=
`{
"username": "thisusernameisok",
"password": "According to all known laws of aviation, there is no way a bee should be able to fly. Its wings are too small to get its fat little body off the ground."
}`
testPostUserHandler(t, userJson, 400)
}
func testPostUserHandler(t *testing.T, userJson string, expectedCode int) {
router := testutils.TestSetup()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/auth/signup",
strings.NewReader(string(userJson)))
router.ServeHTTP(w, req)
assert.Equal(t, expectedCode, w.Code)
}

View File

@@ -0,0 +1,51 @@
package apitest
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/stretchr/testify/assert"
)
type bookSearchGet struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
Id uint `json:"id"`
}
func TestSearchBook_MultipleBooks(t *testing.T) {
books := testSearchBook(t, "san")
assert.Equal(t, 2, len(books))
}
func TestSearchBook_AllFields(t *testing.T) {
books := testSearchBook(t, "de san")
assert.Equal(t, 1, len(books))
assert.Equal(t,
[]bookSearchGet{{Title: "De sang-froid", Author: "Truman Capote", Id: 18}},
books)
}
func testSearchBook(t *testing.T, searchterm string) []bookSearchGet {
router := testutils.TestSetup()
token := testutils.ConnectDemoUser(router)
req, _ := http.NewRequest("GET", "/search/"+searchterm, nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
var books []bookSearchGet
err := json.Unmarshal(w.Body.Bytes(), &books)
if err != nil {
t.Error(err)
}
assert.Equal(t, 200, w.Code)
return books
}