package web import ( "net/http/httptest" "strings" "testing" "gitea.mynodes.xyz/ilker/llmgw/internal/store" ) func TestEachStaffRoleMayDoWhatItSays(t *testing.T) { cases := []struct { role, method, path string may bool }{ {store.RoleViewer, "GET", "/admin/users", true}, {store.RoleViewer, "POST", "/admin/users/u1/suspend", false}, {store.RoleViewer, "POST", "/admin/mirror/fetch", false}, {store.RoleOperator, "GET", "/admin", true}, {store.RoleOperator, "POST", "/admin/mirror/fetch", true}, {store.RoleOperator, "POST", "/admin/nodes/n1/engines", true}, {store.RoleOperator, "POST", "/admin/quotas", false}, {store.RoleOperator, "POST", "/admin/tenancy/org", false}, {store.RoleBilling, "GET", "/admin/usage", true}, {store.RoleBilling, "GET", "/admin/quotas", true}, {store.RoleBilling, "GET", "/admin/users", false}, {store.RoleBilling, "POST", "/admin/quotas", false}, {"", "GET", "/admin", false}, } for _, c := range cases { u := store.User{Role: c.role} if got := staffMay(u, httptest.NewRequest(c.method, c.path, nil)); got != c.may { t.Errorf("%s %s %s: %v", c.role, c.method, c.path, got) } } if !staffMay(store.User{IsAdmin: true}, httptest.NewRequest("POST", "/admin/users/u1/delete", nil)) { t.Error("an administrator may do anything") } } func TestABillingRoleIsShownUsageAndQuotasOnly(t *testing.T) { rec := httptest.NewRecorder() render(rec, nil, "admin_guard", guardPageData{Nav: Nav{Admin: true, Role: store.RoleBilling}}) body := rec.Body.String() if !strings.Contains(body, `href="/admin/usage"`) || strings.Contains(body, `href="/admin/users"`) { t.Errorf("menu: %s", body) } }