122 lines
2.8 KiB
Go
122 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"git.32bit.cafe/32bitcafe/guestbook/internal/assert"
|
|
)
|
|
|
|
func TestUserSignup(t *testing.T) {
|
|
app := newTestApplication(t)
|
|
ts := newTestServer(t, app.routes())
|
|
defer ts.Close()
|
|
|
|
_, _, body := ts.get(t, "/users/register")
|
|
validCSRFToken := extractCSRFToken(t, body)
|
|
|
|
const (
|
|
validName = "John"
|
|
validPassword = "validPassword"
|
|
validEmail = "john@example.com"
|
|
formTag = `<form action="/users/register" method="post">`
|
|
)
|
|
|
|
tests := []struct {
|
|
name string
|
|
userName string
|
|
userEmail string
|
|
userPassword string
|
|
csrfToken string
|
|
wantCode int
|
|
wantFormTag string
|
|
}{
|
|
{
|
|
name: "Valid submission",
|
|
userName: validName,
|
|
userEmail: validEmail,
|
|
userPassword: validPassword,
|
|
csrfToken: validCSRFToken,
|
|
wantCode: http.StatusSeeOther,
|
|
},
|
|
{
|
|
name: "Missing token",
|
|
userName: validName,
|
|
userEmail: validEmail,
|
|
userPassword: validPassword,
|
|
wantCode: http.StatusBadRequest,
|
|
},
|
|
{
|
|
name: "Empty name",
|
|
userName: "",
|
|
userEmail: validEmail,
|
|
userPassword: validPassword,
|
|
csrfToken: validCSRFToken,
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
wantFormTag: formTag,
|
|
},
|
|
{
|
|
name: "Empty email",
|
|
userName: validName,
|
|
userEmail: "",
|
|
userPassword: validPassword,
|
|
csrfToken: validCSRFToken,
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
wantFormTag: formTag,
|
|
},
|
|
{
|
|
name: "Empty password",
|
|
userName: validName,
|
|
userEmail: validEmail,
|
|
userPassword: "",
|
|
csrfToken: validCSRFToken,
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
wantFormTag: formTag,
|
|
},
|
|
{
|
|
name: "Invalid email",
|
|
userName: validName,
|
|
userEmail: "asdfasdf",
|
|
userPassword: validPassword,
|
|
csrfToken: validCSRFToken,
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
wantFormTag: formTag,
|
|
},
|
|
{
|
|
name: "Invalid password",
|
|
userName: validName,
|
|
userEmail: validEmail,
|
|
userPassword: "asdfasd",
|
|
csrfToken: validCSRFToken,
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
wantFormTag: formTag,
|
|
},
|
|
{
|
|
name: "Duplicate email",
|
|
userName: validName,
|
|
userEmail: "dupe@example.com",
|
|
userPassword: validPassword,
|
|
csrfToken: validCSRFToken,
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
wantFormTag: formTag,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(*testing.T) {
|
|
form := url.Values{}
|
|
form.Add("username", tt.userName)
|
|
form.Add("email", tt.userEmail)
|
|
form.Add("password", tt.userPassword)
|
|
form.Add("csrf_token", tt.csrfToken)
|
|
code, _, body := ts.postForm(t, "/users/register", form)
|
|
assert.Equal(t, code, tt.wantCode)
|
|
if tt.wantFormTag != "" {
|
|
assert.StringContains(t, body, tt.wantFormTag)
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|