| 1 | package docs_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/carlosframework/docs/internal/docs" |
| 10 | ) |
| 11 | |
| 12 | // The Mac and iPhone apps' Delete… (spec 2026-11-22 §1, "Delete…"): the |
| 13 | // owner's POST with Accept: application/json answers {"ok":true} once the |
| 14 | // delete has committed, with no re-read and no redirect, and the document |
| 15 | // is gone from its URL and the list. The browser's 303 is unchanged |
| 16 | // (delete_test.go). The client reads only this JSON as "deleted"; anything |
| 17 | // else it checks with a GET of /d/{sqid}. |
| 18 | func TestNativeOwnerDeleteAnswersOK(t *testing.T) { |
| 19 | app, _ := docs.NewForTest(t) |
| 20 | alice := signInCookies(t, app, "alice@example.com") |
| 21 | sqid := createDoc(t, app, alice, "Old+minutes") |
| 22 | |
| 23 | rec := nativeRequest(app, alice, "POST", "/d/"+sqid+"/delete", "") |
| 24 | if rec.Code != http.StatusOK { |
| 25 | t.Fatalf("native delete: %d %q, want 200", rec.Code, rec.Body.String()) |
| 26 | } |
| 27 | if got := rec.Header().Get("Content-Type"); got != "application/json" { |
| 28 | t.Errorf("native delete Content-Type %q, want application/json", got) |
| 29 | } |
| 30 | if rec.Header().Get("Location") != "" { |
| 31 | t.Errorf("native delete redirected to %q", rec.Header().Get("Location")) |
| 32 | } |
| 33 | var answer map[string]any |
| 34 | if err := json.Unmarshal(rec.Body.Bytes(), &answer); err != nil || len(answer) != 1 || answer["ok"] != true { |
| 35 | t.Fatalf("native delete answered %q, want exactly {\"ok\":true}", rec.Body.String()) |
| 36 | } |
| 37 | if got := nativeRequest(app, alice, "GET", "/d/"+sqid, ""); got.Code != http.StatusNotFound { |
| 38 | t.Errorf("after a native delete the document answers %d, want 404", got.Code) |
| 39 | } |
| 40 | if list := nativeRequest(app, alice, "GET", "/", ""); strings.Contains(list.Body.String(), sqid) { |
| 41 | t.Error("a deleted document is still in the native list") |
| 42 | } |
| 43 | // Already deleted: 404, the answer a retry after a lost {"ok":true} |
| 44 | // gets. The client's re-GET (also 404) then says it is deleted. |
| 45 | if again := nativeRequest(app, alice, "POST", "/d/"+sqid+"/delete", ""); again.Code != http.StatusNotFound { |
| 46 | t.Errorf("deleting it again: %d, want 404", again.Code) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Everybody but the owner gets 404 from a native delete, exactly as a |
| 51 | // browser does, and nothing is deleted: a writer grantee, a reader |
| 52 | // grantee, a guest and a team member who can read the document by its |
| 53 | // team rung. A 404 is ambiguous to the client for exactly this reason, |
| 54 | // so it re-GETs before it drops anything. |
| 55 | func TestNativeDeleteByAnyoneButTheOwnerIs404AndDeletesNothing(t *testing.T) { |
| 56 | app := members(t, "alice@example.com,bob@example.com,dora@example.com") |
| 57 | alice := signInCookies(t, app, "alice@example.com") |
| 58 | private := createPrivateDoc(t, app, alice, "Private+plan") |
| 59 | team := createDoc(t, app, alice, "Team+plan") |
| 60 | for _, grant := range []string{"address=bob@example.com&role=writer", "address=carol@example.com&role=reader"} { |
| 61 | if rec := post(t, app, alice, "/d/"+private+"/share", grant); rec.Code != http.StatusSeeOther { |
| 62 | t.Fatalf("share %s: %d", grant, rec.Code) |
| 63 | } |
| 64 | } |
| 65 | if rec := post(t, app, alice, "/d/"+private+"/share", "address=dora@example.com&role=reader"); rec.Code != http.StatusSeeOther { |
| 66 | t.Fatalf("share with dora: %d", rec.Code) |
| 67 | } |
| 68 | bob := signInCookies(t, app, "bob@example.com") |
| 69 | // Carol is on no allowlist: her reader grant admits her as a guest. |
| 70 | carol := signInCookies(t, app, "carol@example.com") |
| 71 | dora := signInCookies(t, app, "dora@example.com") |
| 72 | |
| 73 | for _, c := range []struct { |
| 74 | who string |
| 75 | cookies []*http.Cookie |
| 76 | sqid string |
| 77 | }{ |
| 78 | {"a writer grantee", bob, private}, |
| 79 | {"a guest (reader grantee)", carol, private}, |
| 80 | {"a member reader grantee", dora, private}, |
| 81 | {"a team member, not the owner", bob, team}, |
| 82 | } { |
| 83 | if got := nativeRequest(app, c.cookies, "GET", "/d/"+c.sqid, ""); got.Code != http.StatusOK { |
| 84 | t.Fatalf("precondition: %s cannot read the document (%d)", c.who, got.Code) |
| 85 | } |
| 86 | rec := nativeRequest(app, c.cookies, "POST", "/d/"+c.sqid+"/delete", "") |
| 87 | if rec.Code != http.StatusNotFound { |
| 88 | t.Errorf("%s: native delete %d, want 404", c.who, rec.Code) |
| 89 | } |
| 90 | if strings.Contains(rec.Body.String(), `"ok"`) { |
| 91 | t.Errorf("%s: a refused delete answered ok: %q", c.who, rec.Body.String()) |
| 92 | } |
| 93 | if got := nativeRequest(app, alice, "GET", "/d/"+c.sqid, ""); got.Code != http.StatusOK { |
| 94 | t.Errorf("after %s's refused delete the owner gets %d", c.who, got.Code) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |