forked from longtao.wu/droboticflow
86 lines
3.6 KiB
Go
86 lines
3.6 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestProductionDashboardScopeIsolationAndFullCorpus(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
ds := store.NewMemoryStore()
|
|
h := NewHandler(ds, nil)
|
|
root := bootstrapSuperAdminSession(t, h)
|
|
org := adminCreateOrganization(t, h, root, "Statistics", "stats")
|
|
admin := adminCreateUser(t, h, root, org.ID, "Admin", "stats-admin@example.com", domain.UserRoleAdmin, "test-password")
|
|
cookie := loginSession(t, h, admin.Phone, "test-password")
|
|
p, err := ds.CreateProject(t.Context(), domain.CreateProjectInput{OrganizationID: org.ID, Name: "Production"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
test, err := ds.CreateProject(t.Context(), domain.CreateProjectInput{OrganizationID: org.ID, Name: "Excluded"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for n := 0; n < 251; n++ {
|
|
if err = ds.UpsertCaptureRecord(t.Context(), domain.CaptureRecord{ID: fmt.Sprint(n), OrganizationID: org.ID, ProjectID: p.ID, Prefix: fmt.Sprint(n), CaptureID: fmt.Sprint(n), Status: domain.CaptureRecordIncomplete}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err = ds.UpsertCaptureRecord(t.Context(), domain.CaptureRecord{ID: "excluded", OrganizationID: org.ID, ProjectID: test.ID, Prefix: "excluded", CaptureID: "excluded"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := "/api/dashboard/production"
|
|
r := serveJSONWithCookie(h, "GET", path, nil, cookie)
|
|
if r.Code != 200 || !jsonContains(r.Body.String(), `"configured":false`) {
|
|
t.Fatalf("unconfigured: %d %s", r.Code, r.Body.String())
|
|
}
|
|
r = serveJSONWithCookie(h, "PUT", path+"/scope", []byte(fmt.Sprintf(`{"projectIds":[%q],"revision":0}`, p.ID)), cookie)
|
|
if r.Code != 200 {
|
|
t.Fatalf("scope: %d %s", r.Code, r.Body.String())
|
|
}
|
|
r = serveJSONWithCookie(h, "PUT", path+"/scope", []byte(`{"projectIds":[],"revision":0}`), cookie)
|
|
if r.Code != 409 {
|
|
t.Fatalf("concurrent scope overwrite: %d", r.Code)
|
|
}
|
|
r = serveJSONWithCookie(h, "GET", path+"?details=true&page=11", nil, cookie)
|
|
var got struct {
|
|
Total int `json:"total"`
|
|
Items []any `json:"items"`
|
|
Summary struct {
|
|
Totals map[string]int `json:"totals"`
|
|
} `json:"summary"`
|
|
}
|
|
if err = json.Unmarshal(r.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Code != 200 || got.Total != 251 || len(got.Items) != 1 || got.Summary.Totals["total"] != 251 || jsonContains(r.Body.String(), "Excluded") {
|
|
t.Fatalf("full scope and page: %d %s", r.Code, r.Body.String())
|
|
}
|
|
r = serveJSONWithCookie(h, "GET", path+"?projectId="+test.ID, nil, cookie)
|
|
if r.Code != 400 {
|
|
t.Fatalf("excluded project allowed: %d", r.Code)
|
|
}
|
|
other := adminCreateOrganization(t, h, root, "Other", "stats-other")
|
|
otherAdmin := adminCreateUser(t, h, root, other.ID, "Other", "stats-other@example.com", domain.UserRoleAdmin, "test-password")
|
|
otherCookie := loginSession(t, h, otherAdmin.Phone, "test-password")
|
|
r = serveJSONWithCookie(h, "PUT", path+"/scope", []byte(fmt.Sprintf(`{"projectIds":[%q],"revision":0}`, p.ID)), otherCookie)
|
|
if r.Code != 400 {
|
|
t.Fatalf("cross tenant scope allowed: %d", r.Code)
|
|
}
|
|
supplier := domain.SupplierRecord{ID: "stats-vendor", OrganizationID: org.ID, Name: "Vendor", Status: "active"}
|
|
if err = ds.UpsertSupplier(t.Context(), supplier); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manager := adminCreateUser(t, h, root, org.ID, "Vendor manager", "stats-vendor@example.com", domain.UserRoleSupplierManager, "test-password", supplier.ID)
|
|
mc := loginSession(t, h, manager.Phone, "test-password")
|
|
for _, url := range []string{path, path + "/scope"} {
|
|
r = serveJSONWithCookie(h, "GET", url, nil, mc)
|
|
if r.Code != 403 {
|
|
t.Fatalf("vendor accessed %s: %d", url, r.Code)
|
|
}
|
|
}
|
|
}
|