ch17
This commit is contained in:
@@ -192,3 +192,37 @@ func TestUserSignup(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnippetCreate(t *testing.T) {
|
||||
app := newTestApplication(t)
|
||||
ts := newTestServer(t, app.routes())
|
||||
defer ts.Close()
|
||||
|
||||
const (
|
||||
validPassword = "validPa$$word"
|
||||
validEmail = "bob@example.com"
|
||||
)
|
||||
|
||||
t.Run("Unauthenticated", func(t *testing.T) {
|
||||
code, head, _ := ts.get(t, "/snippet/create")
|
||||
assert.Equal(t, code, http.StatusSeeOther)
|
||||
assert.Equal(t, head.Get("Location"), "/user/login")
|
||||
})
|
||||
|
||||
t.Run("Authenticated", func(t *testing.T) {
|
||||
_, _, body := ts.get(t, "/user/login")
|
||||
validCsrfToken := extractCSRFToken(t, body)
|
||||
t.Logf("CsrfToken: %s", validCsrfToken)
|
||||
|
||||
form := url.Values{}
|
||||
form.Add("email", validEmail)
|
||||
form.Add("password", validPassword)
|
||||
form.Add("csrf_token", validCsrfToken)
|
||||
code, _, _ := ts.postForm(t, "/user/login", form)
|
||||
t.Logf("Auth result: %d", code)
|
||||
|
||||
code, _, body = ts.get(t, "/snippet/create")
|
||||
assert.Equal(t, code, http.StatusOK)
|
||||
assert.StringContains(t, body, "<form action='/snippet/create' method='POST'>")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user