| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/carlosframework/platform/internal/objstore" |
| 13 | "github.com/carlosframework/platform/internal/usage" |
| 14 | ) |
| 15 | |
| 16 | func TestStoreAccountPanelAndPrivateAudit(t *testing.T) { |
| 17 | fx := newConsoleFixture(t) |
| 18 | c := newBrowser(t) |
| 19 | signUp(t, fx, c, "store-panel") |
| 20 | putEntitlement(t, fx, fx.sqid, &objstore.Entitlement{Applied: &objstore.Applied{By: "paul@keymail.dev", At: time.Now(), Note: "PRIVATE-NOTE"}, Decision: &objstore.Decision{By: "PRIVATE-OPERATOR", Reason: "PRIVATE-REASON"}}) |
| 21 | for _, e := range []struct{ action, detail string }{{"store-apply", "PRIVATE-NOTE"}, {"store-decline", "PRIVATE-REASON"}, {"store-approve", "approved"}} { |
| 22 | if err := fx.st.RecordEvent(OpsAccountID, "", "PRIVATE-OPERATOR", e.action, e.detail, time.Now()); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | } |
| 26 | status, _, body := get(t, c, fx.u("/settings")) |
| 27 | if status != 200 || !strings.Contains(body, "Object storage") || !strings.Contains(body, "Declined") || !strings.Contains(body, "Applied for object storage") || !strings.Contains(body, "Object storage declined") || !strings.Contains(body, "Object storage approved") { |
| 28 | t.Fatalf("panel = %d %s", status, body) |
| 29 | } |
| 30 | if strings.Contains(body, "PRIVATE-") { |
| 31 | t.Fatal("tenant settings leaked private entitlement/audit fields") |
| 32 | } |
| 33 | if !strings.Contains(body, `/store/apply`) { |
| 34 | t.Fatal("owner has no apply control") |
| 35 | } |
| 36 | if err := fx.st.AddMember(OpsAccountID, "member@keymail.dev", RoleMember, time.Now()); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | fx.setUser("member@keymail.dev") |
| 40 | member := newBrowser(t) |
| 41 | signUp(t, fx, member, "store-member") |
| 42 | _, _, body = get(t, member, fx.u("/settings")) |
| 43 | if strings.Contains(body, `/store/apply`) || strings.Contains(body, "PRIVATE-") || !strings.Contains(body, "Declined") { |
| 44 | t.Fatal("member panel is not private and read-only") |
| 45 | } |
| 46 | status, _, _ = postForm(t, member, fx.u("/store/apply"), url.Values{"csrf": {sessionToken(t, fx, member)}}) |
| 47 | if status != http.StatusNotFound { |
| 48 | t.Fatalf("member apply=%d", status) |
| 49 | } |
| 50 | status, _, _ = postForm(t, c, fx.u("/store/apply"), url.Values{"csrf": {"wrong"}}) |
| 51 | if status != http.StatusForbidden { |
| 52 | t.Fatalf("bad csrf=%d", status) |
| 53 | } |
| 54 | status, _, body = postForm(t, c, fx.u("/store/apply"), url.Values{"csrf": {sessionToken(t, fx, c)}, "note": {"replacement"}}) |
| 55 | if status != http.StatusSeeOther { |
| 56 | t.Fatalf("apply=%d %s", status, body) |
| 57 | } |
| 58 | if e := storedEntitlement(t, fx, fx.sqid); !e.Pending() || e.Applied.Note != "replacement" { |
| 59 | t.Fatalf("apply=%+v", e) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestStoreQueueGateAndNonmemberDecision(t *testing.T) { |
| 64 | fx := newConsoleFixture(t) |
| 65 | putEntitlement(t, fx, fx.sqid, &objstore.Entitlement{Applied: &objstore.Applied{By: "paul@keymail.dev", At: time.Now(), Note: "operator-only-note"}}) |
| 66 | c := newBrowser(t) |
| 67 | fx.setUser("reviewer@keymail.dev") |
| 68 | signUp(t, fx, c, "store-reviewer") |
| 69 | for _, auth := range []struct{ fleet, listed bool }{{false, false}, {false, true}, {true, false}, {true, true}} { |
| 70 | if err := fx.st.SetOperator("reviewer@keymail.dev", auth.fleet); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | fx.srv.cfg.StoreOperators = nil |
| 74 | if auth.listed { |
| 75 | fx.srv.cfg.StoreOperators = []string{" REVIEWER@KEYMAIL.DEV "} |
| 76 | } |
| 77 | status, _, body := get(t, c, fx.ts.URL+"/fleet/store") |
| 78 | if !auth.fleet || !auth.listed { |
| 79 | if status != 404 || body != "404 page not found\n" { |
| 80 | t.Fatalf("gate=%d %q", status, body) |
| 81 | } |
| 82 | continue |
| 83 | } |
| 84 | if status != 200 || !strings.Contains(body, "operator-only-note") { |
| 85 | t.Fatalf("queue=%d %s", status, body) |
| 86 | } |
| 87 | _, _, body = get(t, c, fx.ts.URL+"/fleet") |
| 88 | if !strings.Contains(body, "1 pending") { |
| 89 | t.Fatal("fleet has no pending count") |
| 90 | } |
| 91 | } |
| 92 | target := fx.ts.URL + "/fleet/store/" + fx.sqid + "/approve" |
| 93 | status, _, _ := postForm(t, c, target, url.Values{"csrf": {"wrong"}}) |
| 94 | if status != 403 { |
| 95 | t.Fatalf("decision csrf=%d", status) |
| 96 | } |
| 97 | status, loc, _ := postForm(t, c, target, url.Values{"csrf": {sessionToken(t, fx, c)}}) |
| 98 | if status != 303 || !strings.Contains(loc, "/oauth/") || storedEntitlement(t, fx, fx.sqid).Approved() { |
| 99 | t.Fatalf("approval did not wait for fresh auth: %d %s", status, loc) |
| 100 | } |
| 101 | status, _, body := completeStepUp(t, fx, c, loc) |
| 102 | if status != 303 { |
| 103 | t.Fatalf("decision=%d %s", status, body) |
| 104 | } |
| 105 | if e := storedEntitlement(t, fx, fx.sqid); !e.Approved() || e.Decision.By != "reviewer@keymail.dev" { |
| 106 | t.Fatalf("decision=%+v", e) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestStoreBucketCountIsDatedAndNeverInventsZero(t *testing.T) { |
| 111 | for _, tc := range []struct { |
| 112 | name, body string |
| 113 | count int |
| 114 | known bool |
| 115 | }{ |
| 116 | {"empty", `{"v":1,"date":"2026-09-08","apps":{}}`, 0, true}, |
| 117 | {"provisioned including empty and unswept", `{"v":1,"date":"2026-09-08","apps":{"bab/a":{"store_provisioned":true},"bab/b":{"store_provisioned":true,"store_unswept":true},"bab/c":{"store_provisioned":false}}}`, 2, true}, |
| 118 | {"legacy", `{"v":1,"date":"2026-09-08","apps":{"bab/a":{"store_bytes":20}}}`, 0, false}, |
| 119 | {"record unreadable", `{"v":1,"date":"2026-09-08","apps":{"bab/a":{"store_provisioned":false,"store_unswept":true}}}`, 0, false}, |
| 120 | {"malformed", `{`, 0, false}, |
| 121 | } { |
| 122 | t.Run(tc.name, func(t *testing.T) { |
| 123 | fx := newConsoleFixture(t) |
| 124 | if _, err := fx.rel.Put(context.Background(), "usage/storage/2026-09-08.json", []byte(tc.body), ""); err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | n, date, known, _ := fx.srv.storeBucketCount(context.Background()) |
| 128 | if n != tc.count || known != tc.known || date != "2026-09-08" { |
| 129 | t.Fatalf("count=%d %s %v", n, date, known) |
| 130 | } |
| 131 | }) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestStoreAccountPanelStatesAndIsolation(t *testing.T) { |
| 136 | for _, state := range []string{"Not applied", "Pending", "Approved"} { |
| 137 | t.Run(state, func(t *testing.T) { |
| 138 | fx := newConsoleFixture(t) |
| 139 | c := newBrowser(t) |
| 140 | signUp(t, fx, c, "store-states") |
| 141 | if state != "Not applied" { |
| 142 | e := &objstore.Entitlement{Applied: &objstore.Applied{By: "private-owner", Note: "private-note", At: time.Now()}} |
| 143 | if state == "Approved" { |
| 144 | e.Decision = &objstore.Decision{Approved: true, By: "private-operator"} |
| 145 | } |
| 146 | putEntitlement(t, fx, fx.sqid, e) |
| 147 | } |
| 148 | if err := fx.st.RecordEvent(OpsAccountID+999, "", "other-account", "store-apply", "other-account-note", time.Now()); err != nil { |
| 149 | t.Fatal(err) |
| 150 | } |
| 151 | status, _, body := get(t, c, fx.u("/settings")) |
| 152 | if status != 200 || !strings.Contains(body, state) || strings.Contains(body, "private-") || strings.Contains(body, "other-account") { |
| 153 | t.Fatalf("state=%d %s", status, state) |
| 154 | } |
| 155 | }) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func TestStoreApprovalReplayRechecksOperatorAuthority(t *testing.T) { |
| 160 | for _, revoke := range []string{"fleet", "allowlist"} { |
| 161 | t.Run(revoke, func(t *testing.T) { |
| 162 | fx := newConsoleFixture(t) |
| 163 | c := newBrowser(t) |
| 164 | fx.setUser("reviewer@keymail.dev") |
| 165 | signUp(t, fx, c, "replay") |
| 166 | if err := fx.st.SetOperator("reviewer@keymail.dev", true); err != nil { |
| 167 | t.Fatal(err) |
| 168 | } |
| 169 | fx.srv.cfg.StoreOperators = []string{"reviewer@keymail.dev"} |
| 170 | putEntitlement(t, fx, fx.sqid, &objstore.Entitlement{Applied: &objstore.Applied{By: "owner@keymail.dev", At: time.Now()}}) |
| 171 | status, loc, _ := postForm(t, c, fx.ts.URL+"/fleet/store/"+fx.sqid+"/approve", url.Values{"csrf": {sessionToken(t, fx, c)}}) |
| 172 | if status != 303 { |
| 173 | t.Fatalf("gate=%d", status) |
| 174 | } |
| 175 | if revoke == "fleet" { |
| 176 | if err := fx.st.SetOperator("reviewer@keymail.dev", false); err != nil { |
| 177 | t.Fatal(err) |
| 178 | } |
| 179 | } else { |
| 180 | fx.srv.cfg.StoreOperators = nil |
| 181 | } |
| 182 | status, _, body := completeStepUp(t, fx, c, loc) |
| 183 | if status != 404 || body != "404 page not found\n" || storedEntitlement(t, fx, fx.sqid).Approved() { |
| 184 | t.Fatalf("revoked approval=%d %q", status, body) |
| 185 | } |
| 186 | }) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func TestStoreQueueRendersLatestCountWithoutCappingApproval(t *testing.T) { |
| 191 | fx := newConsoleFixture(t) |
| 192 | c := newBrowser(t) |
| 193 | signUp(t, fx, c, "queue-count") |
| 194 | if err := fx.st.SetOperator("paul@keymail.dev", true); err != nil { |
| 195 | t.Fatal(err) |
| 196 | } |
| 197 | fx.srv.cfg.StoreOperators = []string{"paul@keymail.dev"} |
| 198 | day := usage.StorageDay{V: 1, Date: "2026-09-08", Apps: map[string]usage.AppStorage{}} |
| 199 | for i := 0; i < 101; i++ { |
| 200 | day.Apps[fmt.Sprintf("bab/app%d", i)] = usage.AppStorage{StoreProvisioned: true} |
| 201 | } |
| 202 | putStoreJSON(t, fx.rel, usage.StorageDayKey(day.Date), day) |
| 203 | putStoreJSON(t, fx.rel, usage.StorageDayKey("2026-09-07"), usage.StorageDay{V: 1, Date: "2026-09-07", Apps: map[string]usage.AppStorage{}}) |
| 204 | putEntitlement(t, fx, fx.sqid, &objstore.Entitlement{Applied: &objstore.Applied{By: "paul@keymail.dev", At: time.Now()}}) |
| 205 | status, _, body := get(t, c, fx.ts.URL+"/fleet/store") |
| 206 | for _, want := range []string{"Provisioned stores: 101", "2026-09-08", "incomplete provisions", ">Approve</button>"} { |
| 207 | if status != 200 || !strings.Contains(body, want) { |
| 208 | t.Fatalf("queue missing %q (status %d)", want, status) |
| 209 | } |
| 210 | } |
| 211 | status, _, body = postGated(t, fx, c, fx.ts.URL+"/fleet/store/"+fx.sqid+"/approve", url.Values{"csrf": {sessionToken(t, fx, c)}}) |
| 212 | if status != 303 || !storedEntitlement(t, fx, fx.sqid).Approved() { |
| 213 | t.Fatalf("count capped approval: %d %s", status, body) |
| 214 | } |
| 215 | } |
| 216 | |