added user signup feature

This commit is contained in:
2025-09-26 23:57:36 +02:00
parent 2f0a9b5127
commit 57355fe9ac
15 changed files with 242 additions and 14 deletions

67
user_test.go Normal file
View File

@@ -0,0 +1,67 @@
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"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 := testSetup()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/auth/signup",
strings.NewReader(string(userJson)))
router.ServeHTTP(w, req)
assert.Equal(t, expectedCode, w.Code)
}