Skip to content

Commit

Permalink
test: register
Browse files Browse the repository at this point in the history
  • Loading branch information
tomanagle committed Mar 30, 2024
1 parent 50efaa8 commit ec49a69
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ func TestLogin(t *testing.T) {

handler.ServeHTTP(rr, req)

assert.Equal(tc.expectedStatusCode, rr.Code)
assert.Equal(tc.expectedStatusCode, rr.Code, "handler returned wrong status code: got %v want %v", rr.Code, tc.expectedStatusCode)

cookies := rr.Result().Cookies()
if tc.expectedCookie != nil {

sessionCookie := cookies[0]

assert.Equal(tc.expectedCookie.Name, sessionCookie.Name)
assert.Equal(tc.expectedCookie.Value, sessionCookie.Value)
assert.Equal(tc.expectedCookie.HttpOnly, sessionCookie.HttpOnly)
assert.Equal(tc.expectedCookie.Name, sessionCookie.Name, "handler returned wrong cookie name: got %v want %v", sessionCookie.Name, tc.expectedCookie.Name)
assert.Equal(tc.expectedCookie.Value, sessionCookie.Value, "handler returned wrong cookie value: got %v want %v", sessionCookie.Value, tc.expectedCookie.Value)
assert.Equal(tc.expectedCookie.HttpOnly, sessionCookie.HttpOnly, "handler returned wrong cookie HttpOnly: got %v want %v", sessionCookie.HttpOnly, tc.expectedCookie.HttpOnly)
} else {
assert.Empty(cookies)
assert.Empty(cookies, "handler returned unexpected cookie: got %v want %v", cookies, tc.expectedCookie)
}

userStore.AssertExpectations(t)
Expand Down
9 changes: 6 additions & 3 deletions internal/handlers/postregister.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ func (h *PostRegisterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
err := h.userStore.CreateUser(email, password)

if err != nil {
http.Error(w, "Error creating user", http.StatusInternalServerError)

w.WriteHeader(http.StatusBadRequest)
c := templates.RegisterError()
c.Render(r.Context(), w)
return
}

c := templates.RegisterSucces()
c := templates.RegisterSuccess()
err = c.Render(r.Context(), w)

if err != nil {
http.Error(w, "Error rendering template", http.StatusInternalServerError)
http.Error(w, "error rendering template", http.StatusInternalServerError)
return
}

Expand Down
66 changes: 66 additions & 0 deletions internal/handlers/postregister_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package handlers

import (
"bytes"

storemock "goth/internal/store/mock"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)

func TestRegisterUserHandler(t *testing.T) {

testCases := []struct {
name string
email string
password string
createUserError error
expectedStatusCode int
expectedBody []byte
}{
{
name: "success",
email: "test@example.com",
password: "password",
expectedStatusCode: http.StatusOK,
expectedBody: []byte(`<h1>Registration successful</h1><p>Go to <a href="login">login</a></p>`),
},
{
name: "fail - error creating user",
email: "test@example.com",
password: "password",
createUserError: gorm.ErrDuplicatedKey,
expectedStatusCode: http.StatusBadRequest,
expectedBody: []byte(`<h1>Registration failed</h1>`),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert := assert.New(t)
userStore := &storemock.UserStoreMock{}

userStore.On("CreateUser", tc.email, tc.password).Return(tc.createUserError)

handler := NewPostRegisterHandler(PostRegisterHandlerParams{
UserStore: userStore,
})
body := bytes.NewBufferString("email=" + tc.email + "&password=" + tc.password)
req, _ := http.NewRequest("POST", "/", body)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
rr := httptest.NewRecorder()

handler.ServeHTTP(rr, req)

assert.Equal(tc.expectedStatusCode, rr.Code, "handler returned wrong status code: got %v want %v", rr.Code, tc.expectedStatusCode)

assert.True(bytes.Contains(rr.Body.Bytes(), tc.expectedBody), "handler returned unexpected body: got %v want %v", rr.Body.String(), tc.expectedBody)

userStore.AssertExpectations(t)
})
}
}
7 changes: 0 additions & 7 deletions internal/handlers/posttodo.go

This file was deleted.

10 changes: 9 additions & 1 deletion internal/templates/register.templ
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ templ RegisterPage() {
</div>
}

templ RegisterSucces() {
templ RegisterSuccess() {
<h1>Registration successful</h1>
<p>Go to <a href="login">login</a></p>
}


templ RegisterError() {
<div>
<h1>Registration failed</h1>
<p>There was an error registering your account</p>
</div>
}
26 changes: 25 additions & 1 deletion internal/templates/register_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ec49a69

Please sign in to comment.