From ec49a694cd653c2b78bcec52b2acbb4c48b85661 Mon Sep 17 00:00:00 2001 From: Tom Nagle Date: Sun, 31 Mar 2024 06:21:21 +1000 Subject: [PATCH] test: register --- .../{login_test.go => postlogin_test.go} | 10 +-- internal/handlers/postregister.go | 9 ++- internal/handlers/postregister_test.go | 66 +++++++++++++++++++ internal/handlers/posttodo.go | 7 -- internal/templates/register.templ | 10 ++- internal/templates/register_templ.go | 26 +++++++- 6 files changed, 111 insertions(+), 17 deletions(-) rename internal/handlers/{login_test.go => postlogin_test.go} (82%) create mode 100644 internal/handlers/postregister_test.go delete mode 100644 internal/handlers/posttodo.go diff --git a/internal/handlers/login_test.go b/internal/handlers/postlogin_test.go similarity index 82% rename from internal/handlers/login_test.go rename to internal/handlers/postlogin_test.go index 4edde1f..319ab39 100644 --- a/internal/handlers/login_test.go +++ b/internal/handlers/postlogin_test.go @@ -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) diff --git a/internal/handlers/postregister.go b/internal/handlers/postregister.go index b42422a..d6ac304 100644 --- a/internal/handlers/postregister.go +++ b/internal/handlers/postregister.go @@ -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 } diff --git a/internal/handlers/postregister_test.go b/internal/handlers/postregister_test.go new file mode 100644 index 0000000..dd05cf6 --- /dev/null +++ b/internal/handlers/postregister_test.go @@ -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(`

Registration successful

Go to login

`), + }, + { + name: "fail - error creating user", + email: "test@example.com", + password: "password", + createUserError: gorm.ErrDuplicatedKey, + expectedStatusCode: http.StatusBadRequest, + expectedBody: []byte(`

Registration failed

`), + }, + } + + 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) + }) + } +} diff --git a/internal/handlers/posttodo.go b/internal/handlers/posttodo.go deleted file mode 100644 index 809b625..0000000 --- a/internal/handlers/posttodo.go +++ /dev/null @@ -1,7 +0,0 @@ -package handlers - -import () - -type PostTodoHandler struct { - -} \ No newline at end of file diff --git a/internal/templates/register.templ b/internal/templates/register.templ index 53031b9..c581bfc 100644 --- a/internal/templates/register.templ +++ b/internal/templates/register.templ @@ -30,7 +30,15 @@ templ RegisterPage() { } -templ RegisterSucces() { +templ RegisterSuccess() {

Registration successful

Go to login

} + + +templ RegisterError() { +
+

Registration failed

+

There was an error registering your account

+
+} \ No newline at end of file diff --git a/internal/templates/register_templ.go b/internal/templates/register_templ.go index aa7e9e2..3d968dc 100644 --- a/internal/templates/register_templ.go +++ b/internal/templates/register_templ.go @@ -34,7 +34,7 @@ func RegisterPage() templ.Component { }) } -func RegisterSucces() templ.Component { +func RegisterSuccess() templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) if !templ_7745c5c3_IsBuffer { @@ -57,3 +57,27 @@ func RegisterSucces() templ.Component { return templ_7745c5c3_Err }) } + +func RegisterError() templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var3 := templ.GetChildren(ctx) + if templ_7745c5c3_Var3 == nil { + templ_7745c5c3_Var3 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Registration failed

There was an error registering your account

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +}