diff --git a/cmd/web/handlers_guestbook_test.go b/cmd/web/handlers_guestbook_test.go index 5ee8aeb..3faaa03 100644 --- a/cmd/web/handlers_guestbook_test.go +++ b/cmd/web/handlers_guestbook_test.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "net/http" "testing" @@ -17,3 +18,43 @@ func TestPing(t *testing.T) { 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 = `