| 1 | package docs_test |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/docs/internal/docs" |
| 9 | ) |
| 10 | |
| 11 | // File → Delete… is the owner's: the menu offers it, its page confirms, |
| 12 | // the POST removes the document from the list and from its own URL, and |
| 13 | // a second POST finds nothing to delete. |
| 14 | func TestOwnerDeletesADocument(t *testing.T) { |
| 15 | app, _ := docs.NewForTest(t) |
| 16 | alice := signInCookies(t, app, "alice@example.com") |
| 17 | sqid := createDoc(t, app, alice, "Old minutes") |
| 18 | |
| 19 | if body := get(t, app, alice, "/d/"+sqid).Body.String(); !strings.Contains(body, `href="/d/`+sqid+`/delete"`) { |
| 20 | t.Error("the owner's File menu has no Delete…") |
| 21 | } |
| 22 | page := get(t, app, alice, "/d/"+sqid+"/delete") |
| 23 | if page.Code != http.StatusOK || !strings.Contains(page.Body.String(), `action="/d/`+sqid+`/delete"`) { |
| 24 | t.Fatalf("confirmation page: %d, no delete form", page.Code) |
| 25 | } |
| 26 | if rec := post(t, app, alice, "/d/"+sqid+"/delete", ""); rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/" { |
| 27 | t.Fatalf("delete: %d to %q, want 303 to /", rec.Code, rec.Header().Get("Location")) |
| 28 | } |
| 29 | if body := get(t, app, alice, "/").Body.String(); strings.Contains(body, "/d/"+sqid) { |
| 30 | t.Error("a deleted document is still listed") |
| 31 | } |
| 32 | if rec := get(t, app, alice, "/d/"+sqid); rec.Code != http.StatusNotFound { |
| 33 | t.Errorf("a deleted document answers %d, want 404", rec.Code) |
| 34 | } |
| 35 | if rec := post(t, app, alice, "/d/"+sqid+"/delete", ""); rec.Code != http.StatusNotFound { |
| 36 | t.Errorf("deleting it again answers %d, want 404", rec.Code) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // A writer the document is shared with may edit it and may not delete |
| 41 | // it: no menu item, and both routes answer 404, as share does. |
| 42 | func TestAWriterCannotDelete(t *testing.T) { |
| 43 | app, _ := docs.NewForTest(t) |
| 44 | alice := signInCookies(t, app, "alice@example.com") |
| 45 | bob := signInCookies(t, app, "bob@example.com") |
| 46 | sqid := createDoc(t, app, alice, "Shared plan") |
| 47 | if rec := post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=writer"); rec.Code != http.StatusSeeOther { |
| 48 | t.Fatalf("share: %d", rec.Code) |
| 49 | } |
| 50 | |
| 51 | if body := get(t, app, bob, "/d/"+sqid).Body.String(); strings.Contains(body, "/delete") { |
| 52 | t.Error("a writer's File menu offers Delete…") |
| 53 | } |
| 54 | if rec := get(t, app, bob, "/d/"+sqid+"/delete"); rec.Code != http.StatusNotFound { |
| 55 | t.Errorf("a writer's confirmation page answers %d, want 404", rec.Code) |
| 56 | } |
| 57 | if rec := post(t, app, bob, "/d/"+sqid+"/delete", ""); rec.Code != http.StatusNotFound { |
| 58 | t.Errorf("a writer's delete answers %d, want 404", rec.Code) |
| 59 | } |
| 60 | if rec := get(t, app, alice, "/d/"+sqid); rec.Code != http.StatusOK { |
| 61 | t.Errorf("after a writer's attempt the document answers %d to its owner", rec.Code) |
| 62 | } |
| 63 | } |
| 64 | |