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,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)
}