|
| 1 | +package cloudstore |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// TestScopedWildcardPassesAllProjects verifies that a wildcard allowlist does not |
| 10 | +// filter the dashboard read model — all projects must survive scoped(). |
| 11 | +func TestScopedWildcardPassesAllProjects(t *testing.T) { |
| 12 | + t1 := time.Date(2026, 4, 23, 10, 0, 0, 0, time.UTC) |
| 13 | + chunks := []dashboardChunkRow{ |
| 14 | + {chunkID: "c1", project: "team-alpha", createdBy: "alice", createdAt: t1, |
| 15 | + parsed: parseMustChunk(t, []byte(`{"sessions":[{"id":"s1","project":"team-alpha","started_at":"2026-04-23T08:00:00Z"}],"observations":[],"prompts":[]}`))}, |
| 16 | + {chunkID: "c2", project: "team-beta", createdBy: "bob", createdAt: t1, |
| 17 | + parsed: parseMustChunk(t, []byte(`{"sessions":[{"id":"s2","project":"team-beta","started_at":"2026-04-23T09:00:00Z"}],"observations":[],"prompts":[]}`))}, |
| 18 | + {chunkID: "c3", project: "other-project", createdBy: "charlie", createdAt: t1, |
| 19 | + parsed: parseMustChunk(t, []byte(`{"sessions":[{"id":"s3","project":"other-project","started_at":"2026-04-23T10:00:00Z"}],"observations":[],"prompts":[]}`))}, |
| 20 | + } |
| 21 | + |
| 22 | + model, err := buildDashboardReadModel(chunks) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("buildDashboardReadModel: %v", err) |
| 25 | + } |
| 26 | + |
| 27 | + // Wildcard "*" map — represents the wildcard sentinel. |
| 28 | + wildcard := map[string]struct{}{"*": {}} |
| 29 | + scoped := model.scoped(wildcard) |
| 30 | + if len(scoped.projects) != 3 { |
| 31 | + t.Fatalf("wildcard allowlist must pass all 3 projects through scoped(), got %d: %v", len(scoped.projects), scoped.projects) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// TestScopedWithExactAllowlist ensures that scoped() with an explicit list |
| 36 | +// (no wildcard) filters the dashboard correctly — backward compatibility guard. |
| 37 | +func TestScopedWithExactAllowlist(t *testing.T) { |
| 38 | + t1 := time.Date(2026, 4, 23, 10, 0, 0, 0, time.UTC) |
| 39 | + chunks := []dashboardChunkRow{ |
| 40 | + {chunkID: "c1", project: "team-alpha", createdBy: "alice", createdAt: t1, |
| 41 | + parsed: parseMustChunk(t, []byte(`{"sessions":[{"id":"s1","project":"team-alpha","started_at":"2026-04-23T08:00:00Z"}],"observations":[],"prompts":[]}`))}, |
| 42 | + {chunkID: "c2", project: "team-beta", createdBy: "bob", createdAt: t1, |
| 43 | + parsed: parseMustChunk(t, []byte(`{"sessions":[{"id":"s2","project":"team-beta","started_at":"2026-04-23T09:00:00Z"}],"observations":[],"prompts":[]}`))}, |
| 44 | + } |
| 45 | + |
| 46 | + model, err := buildDashboardReadModel(chunks) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("buildDashboardReadModel: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + // Explicit list: only "team-alpha" must survive scoped(). |
| 52 | + scoped := model.scoped(map[string]struct{}{"team-alpha": {}}) |
| 53 | + if len(scoped.projects) != 1 || scoped.projects[0].Project != "team-alpha" { |
| 54 | + t.Fatalf("exact allowlist must keep only team-alpha, got %v", scoped.projects) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// TestNormalizeDashboardProjectWildcardAllowsAnyProject verifies that with wildcard |
| 59 | +// set, any project passes normalizeDashboardProject (no ErrDashboardProjectForbidden). |
| 60 | +func TestNormalizeDashboardProjectWildcardAllowsAnyProject(t *testing.T) { |
| 61 | + cs := &CloudStore{} |
| 62 | + cs.SetDashboardAllowedProjects([]string{"*"}) |
| 63 | + |
| 64 | + _, err := cs.normalizeDashboardProject("any-project") |
| 65 | + if err != nil && !errors.Is(err, ErrDashboardProjectNotFound) { |
| 66 | + // ErrDashboardProjectNotFound is fine (no DB) — ErrDashboardProjectForbidden is not. |
| 67 | + if errors.Is(err, ErrDashboardProjectForbidden) { |
| 68 | + t.Fatalf("wildcard allowlist must not forbid any project, got %v", err) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments