Files
bibliomane/internal/apitest/post_user_test.go
2026-02-11 01:12:53 +01:00

69 lines
1.5 KiB
Go

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", "/ws/auth/signup",
strings.NewReader(string(userJson)))
router.ServeHTTP(w, req)
assert.Equal(t, expectedCode, w.Code)
}