test(16.2-07): full coverage of the site cache purge
- TestSiteCachePurgeConfig: every refused configuration names its key and never the token; https anywhere and http to loopback are accepted - TestSiteCachePurgeRequest: the contract request, Info and Warn lines, the 2 s timeout, a cancelled parent context, no token in any log line - Postgres: category create, update and delete and post create, untouched update (D-03 bytes kept), failing and unreachable purges and delete each purge once after the commit; failed saves send none
This commit is contained in:
@@ -91,6 +91,12 @@ func (r *purgeReceiver) take() []purgeRequest {
|
||||
return got
|
||||
}
|
||||
|
||||
func (r *purgeReceiver) setStatus(status int) {
|
||||
r.mu.Lock()
|
||||
r.status = status
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
func (r *purgeReceiver) setProbe(probe func() string) {
|
||||
r.mu.Lock()
|
||||
r.probe = probe
|
||||
@@ -121,14 +127,30 @@ func (r *purgeReceiver) expectOnePurge(t *testing.T, stage, kind, slug string) p
|
||||
return req
|
||||
}
|
||||
|
||||
// TestCategoriesAdminPurgesSiteCache proves an admin update of a category
|
||||
// sends one authorized purge request to the host site after the save
|
||||
// transaction commits (G-16.2-2, D-19).
|
||||
// TestCategoriesAdminPurgesSiteCache proves every admin write of a category
|
||||
// sends one authorized purge request with kind category after the save
|
||||
// transaction commits, and a failed save sends none (G-16.2-2, D-19).
|
||||
func TestCategoriesAdminPurgesSiteCache(t *testing.T) {
|
||||
recv := newPurgeReceiver(t)
|
||||
env := newPostsEnv(t, recv.env()...)
|
||||
token := env.admin(t, controllers.PermissionAccessCategories)
|
||||
|
||||
// The probe reads the committed row through the pool while the purge
|
||||
// request is in flight: "<content>" or "missing".
|
||||
recv.setProbe(func() string {
|
||||
var cat models.Category
|
||||
if err := env.gdb.Where("slug = ?", "purge-news").Limit(1).Find(&cat).Error; err != nil {
|
||||
return "error: " + err.Error()
|
||||
}
|
||||
if cat.ID == 0 {
|
||||
return "missing"
|
||||
}
|
||||
if cat.Content == nil {
|
||||
return ""
|
||||
}
|
||||
return *cat.Content
|
||||
})
|
||||
|
||||
rec := env.expect(t, http.StatusCreated, http.MethodPost, adminAPI(adminCategoriesPath), token, map[string]any{
|
||||
"name": map[string]any{"en": "Purge news", "pl": "Wiadomości"},
|
||||
"slug": map[string]any{"en": "purge-news", "pl": "wiadomosci"},
|
||||
@@ -136,43 +158,71 @@ func TestCategoriesAdminPurgesSiteCache(t *testing.T) {
|
||||
})
|
||||
id := adminCreatedID(t, rec)
|
||||
itemPath := adminAPI(fmt.Sprintf("%s/%d", adminCategoriesPath, id))
|
||||
recv.take()
|
||||
if req := recv.expectOnePurge(t, "create", "category", "purge-news"); req.Seen != "<p>Before</p>\n" {
|
||||
t.Fatalf("create: the purge saw %q, want the committed row", req.Seen)
|
||||
}
|
||||
|
||||
content := func() string {
|
||||
var cat models.Category
|
||||
if err := env.gdb.First(&cat, id).Error; err != nil {
|
||||
return "error: " + err.Error()
|
||||
served := func(t *testing.T) map[string]json.RawMessage {
|
||||
t.Helper()
|
||||
rec := env.expect(t, http.StatusOK, http.MethodGet, itemPath, token, nil)
|
||||
var data map[string]json.RawMessage
|
||||
if err := json.Unmarshal(adminDecode(t, rec).Data, &data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cat.Content == nil {
|
||||
return ""
|
||||
return data
|
||||
}
|
||||
|
||||
t.Run("update", func(t *testing.T) {
|
||||
const edited = "<p>After the edit</p>\n"
|
||||
body := servedBody(served(t), "name", "slug")
|
||||
body["content"] = edited
|
||||
env.expect(t, http.StatusOK, http.MethodPut, itemPath, token, body)
|
||||
if req := recv.expectOnePurge(t, "update", "category", "purge-news"); req.Seen != edited {
|
||||
t.Fatalf("the purge saw content %q, want the committed %q (purge before commit)", req.Seen, edited)
|
||||
}
|
||||
return *cat.Content
|
||||
}
|
||||
recv.setProbe(content)
|
||||
})
|
||||
|
||||
rec = env.expect(t, http.StatusOK, http.MethodGet, itemPath, token, nil)
|
||||
var served map[string]json.RawMessage
|
||||
if err := json.Unmarshal(adminDecode(t, rec).Data, &served); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const edited = "<p>After the edit</p>\n"
|
||||
body := servedBody(served, "name", "slug")
|
||||
body["content"] = edited
|
||||
env.expect(t, http.StatusOK, http.MethodPut, itemPath, token, body)
|
||||
t.Run("a failed update sends none", func(t *testing.T) {
|
||||
body := servedBody(served(t), "slug")
|
||||
body["name"] = map[string]any{"en": "", "pl": ""}
|
||||
body["content"] = "<p>Refused</p>\n"
|
||||
if rec := env.call(t, http.MethodPut, itemPath, token, body); rec.Code != http.StatusUnprocessableEntity {
|
||||
t.Fatalf("update without a name = %d, want 422: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if got := recv.take(); len(got) != 0 {
|
||||
t.Fatalf("a failed update sent %d purge requests: %+v", len(got), got)
|
||||
}
|
||||
})
|
||||
|
||||
req := recv.expectOnePurge(t, "update", "category", "purge-news")
|
||||
if req.Seen != edited {
|
||||
t.Fatalf("the purge saw content %q, want the committed %q (purge before commit)", req.Seen, edited)
|
||||
}
|
||||
t.Run("delete", func(t *testing.T) {
|
||||
env.expect(t, http.StatusOK, http.MethodDelete, itemPath, token, nil)
|
||||
if req := recv.expectOnePurge(t, "delete", "category", "purge-news"); req.Seen != "missing" {
|
||||
t.Fatalf("the purge saw %q, want the row already deleted", req.Seen)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestPostsAdminPurgesSiteCache proves an admin update of a post sends one
|
||||
// authorized purge request with kind post after the save commits.
|
||||
// TestPostsAdminPurgesSiteCache proves every admin write of a post sends one
|
||||
// authorized purge request with kind post after the commit, that an
|
||||
// untouched save still purges and keeps both content columns byte for byte
|
||||
// (D-03), that a refused, failing or unreachable purge never changes the
|
||||
// save response, and that a failed save sends none.
|
||||
func TestPostsAdminPurgesSiteCache(t *testing.T) {
|
||||
recv := newPurgeReceiver(t)
|
||||
env := newPostsEnv(t, recv.env()...)
|
||||
token := env.admin(t, controllers.PermissionAccessPosts)
|
||||
|
||||
recv.setProbe(func() string {
|
||||
var post models.Post
|
||||
if err := env.gdb.Where("slug = ?", "purge-post").Limit(1).Find(&post).Error; err != nil {
|
||||
return "error: " + err.Error()
|
||||
}
|
||||
if post.ID == 0 {
|
||||
return "missing"
|
||||
}
|
||||
return post.ContentHTML
|
||||
})
|
||||
|
||||
rec := env.expect(t, http.StatusCreated, http.MethodPost, adminAPI(adminPostsPath), token, map[string]any{
|
||||
"title": map[string]any{"en": "Purge post", "pl": "Wpis"},
|
||||
"slug": map[string]any{"en": "purge-post", "pl": "wpis"},
|
||||
@@ -180,23 +230,102 @@ func TestPostsAdminPurgesSiteCache(t *testing.T) {
|
||||
"excerpt": map[string]any{"en": "Excerpt", "pl": "Wyjątek"},
|
||||
})
|
||||
id := adminCreatedID(t, rec)
|
||||
recv.take()
|
||||
itemPath := adminAPI(fmt.Sprintf("%s/%d", adminPostsPath, id))
|
||||
if req := recv.expectOnePurge(t, "create", "post", "purge-post"); req.Seen != "<p>Before</p>" {
|
||||
t.Fatalf("create: the purge saw %q, want the committed row", req.Seen)
|
||||
}
|
||||
|
||||
recv.setProbe(func() string {
|
||||
put := func(t *testing.T, content string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
body := servedBody(servedForm(t, env, token, id), "title", "slug", "excerpt")
|
||||
body["content"] = map[string]any{"en": content}
|
||||
return env.call(t, http.MethodPut, itemPath, token, body)
|
||||
}
|
||||
stored := func(t *testing.T) models.Post {
|
||||
t.Helper()
|
||||
var post models.Post
|
||||
if err := env.gdb.First(&post, id).Error; err != nil {
|
||||
return "error: " + err.Error()
|
||||
t.Fatal(err)
|
||||
}
|
||||
return post.ContentHTML
|
||||
})
|
||||
served := servedForm(t, env, token, id)
|
||||
const edited = "<p>After the edit</p>"
|
||||
body := servedBody(served, "title", "slug", "excerpt")
|
||||
body["content"] = map[string]any{"en": edited}
|
||||
env.expect(t, http.StatusOK, http.MethodPut, adminAPI(fmt.Sprintf("%s/%d", adminPostsPath, id)), token, body)
|
||||
|
||||
req := recv.expectOnePurge(t, "update", "post", "purge-post")
|
||||
if req.Seen != edited {
|
||||
t.Fatalf("the purge saw content_html %q, want the committed %q (purge before commit)", req.Seen, edited)
|
||||
return post
|
||||
}
|
||||
|
||||
t.Run("update", func(t *testing.T) {
|
||||
const edited = "<p>After the edit</p>"
|
||||
if rec := put(t, edited); rec.Code != http.StatusOK {
|
||||
t.Fatalf("update = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if req := recv.expectOnePurge(t, "update", "post", "purge-post"); req.Seen != edited {
|
||||
t.Fatalf("the purge saw content_html %q, want the committed %q (purge before commit)", req.Seen, edited)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("an untouched update purges and keeps both columns", func(t *testing.T) {
|
||||
const imported = "\n<!-- imported -->\n<p onclick=\"x()\">Kept as stored</p>\n"
|
||||
const importedHTML = "<p>Rendered elsewhere</p>\n"
|
||||
if err := env.gdb.Model(&models.Post{}).Where("id = ?", id).Updates(map[string]any{"content": imported, "content_html": importedHTML}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
served := servedForm(t, env, token, id)
|
||||
env.expect(t, http.StatusOK, http.MethodPut, itemPath, token, servedBody(served, "title", "slug", "excerpt", "content"))
|
||||
recv.expectOnePurge(t, "untouched update", "post", "purge-post")
|
||||
assertHostContent(t, env, id, "untouched update", imported, importedHTML)
|
||||
})
|
||||
|
||||
t.Run("a failed save sends none", func(t *testing.T) {
|
||||
// The admin lacks access_publish: FormBeforeUpdate refuses inside
|
||||
// the save transaction.
|
||||
body := servedBody(servedForm(t, env, token, id), "title", "slug", "excerpt")
|
||||
body["published"] = true
|
||||
if rec := env.call(t, http.MethodPut, itemPath, token, body); rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("publish without access_publish = %d, want 403: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if got := recv.take(); len(got) != 0 {
|
||||
t.Fatalf("a failed save sent %d purge requests: %+v", len(got), got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("a failing purge never changes the save", func(t *testing.T) {
|
||||
recv.setStatus(http.StatusInternalServerError)
|
||||
defer recv.setStatus(http.StatusOK)
|
||||
const edited = "<p>Saved while the site answers 500</p>"
|
||||
if rec := put(t, edited); rec.Code != http.StatusOK {
|
||||
t.Fatalf("update with a failing purge = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
recv.expectOnePurge(t, "failing purge", "post", "purge-post")
|
||||
if got := stored(t); got.Content != edited || got.ContentHTML != edited {
|
||||
t.Fatalf("stored = %q / %q, want %q", got.Content, got.ContentHTML, edited)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("delete", func(t *testing.T) {
|
||||
spare := env.expect(t, http.StatusCreated, http.MethodPost, adminAPI(adminPostsPath), token, map[string]any{
|
||||
"title": map[string]any{"en": "Spare"},
|
||||
"slug": map[string]any{"en": "spare-post"},
|
||||
"content": map[string]any{"en": "<p>Spare</p>"},
|
||||
})
|
||||
spareID := adminCreatedID(t, spare)
|
||||
recv.expectOnePurge(t, "spare create", "post", "spare-post")
|
||||
env.expect(t, http.StatusOK, http.MethodDelete, adminAPI(fmt.Sprintf("%s/%d", adminPostsPath, spareID)), token, nil)
|
||||
recv.expectOnePurge(t, "delete", "post", "spare-post")
|
||||
var n int64
|
||||
if err := env.gdb.Model(&models.Post{}).Where("id = ?", spareID).Count(&n).Error; err != nil || n != 0 {
|
||||
t.Fatalf("spare post rows = %d (%v), want 0", n, err)
|
||||
}
|
||||
})
|
||||
|
||||
// Last: closing the receiver ends its use for this env.
|
||||
t.Run("an unreachable site never changes the save", func(t *testing.T) {
|
||||
recv.srv.Close()
|
||||
const edited = "<p>Saved while nothing listens</p>"
|
||||
if rec := put(t, edited); rec.Code != http.StatusOK {
|
||||
t.Fatalf("update with no receiver = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if got := stored(t); got.Content != edited || got.ContentHTML != edited {
|
||||
t.Fatalf("stored = %q / %q, want %q", got.Content, got.ContentHTML, edited)
|
||||
}
|
||||
if got := recv.take(); len(got) != 0 {
|
||||
t.Fatalf("a closed receiver recorded %d requests", len(got))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user