This commit is contained in:
68
post_user_test.go
Normal file
68
post_user_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user