diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 9534a56..a72a3b4 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -17,3 +17,7 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) { func (app *application) notImplemented(w http.ResponseWriter, r *http.Request) { views.ComingSoon("Coming Soon", app.newCommonData(r)).Render(r.Context(), w) } + +func ping(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("OK")) +} diff --git a/cmd/web/handlers_guestbook_test.go b/cmd/web/handlers_guestbook_test.go new file mode 100644 index 0000000..3faaa03 --- /dev/null +++ b/cmd/web/handlers_guestbook_test.go @@ -0,0 +1,60 @@ +package main + +import ( + "fmt" + "net/http" + "testing" + + "git.32bit.cafe/32bitcafe/guestbook/internal/assert" +) + +func TestPing(t *testing.T) { + app := newTestApplication(t) + ts := newTestServer(t, app.routes()) + defer ts.Close() + + code, _, body := ts.get(t, "/ping") + + assert.Equal(t, code, http.StatusOK) + assert.Equal(t, body, "OK") +} + +func TestGetGuestbookView(t *testing.T) { + app := newTestApplication(t) + ts := newTestServer(t, app.routes()) + defer ts.Close() + + tests := []struct { + name string + urlPath string + wantCode int + wantBody string + }{ + { + name: "Valid id", + urlPath: fmt.Sprintf("/websites/%s/guestbook", shortIdToSlug(1)), + wantCode: http.StatusOK, + wantBody: "Guestbook for Example", + }, + { + name: "Non-existent ID", + urlPath: fmt.Sprintf("/websites/%s/guestbook", shortIdToSlug(2)), + wantCode: http.StatusNotFound, + }, + { + name: "String ID", + urlPath: "/websites/abcd/guestbook", + wantCode: http.StatusNotFound, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + code, _, body := ts.get(t, tt.urlPath) + assert.Equal(t, code, tt.wantCode) + if tt.wantBody != "" { + assert.StringContains(t, body, tt.wantBody) + } + }) + } +} diff --git a/cmd/web/handlers_user_test.go b/cmd/web/handlers_user_test.go new file mode 100644 index 0000000..8a97224 --- /dev/null +++ b/cmd/web/handlers_user_test.go @@ -0,0 +1,121 @@ +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 = `