|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestReleasedPublicProjectRequiresArtifactWithoutFallingBackToEstate(t *testing.T) { |
| 13 | + root := releasedProjectFixture(t, "public") |
| 14 | + services, err := NewServices(DefaultClock) |
| 15 | + if err != nil { |
| 16 | + t.Fatal(err) |
| 17 | + } |
| 18 | + archive := filepath.Join(t.TempDir(), "missing-release.tar.gz") |
| 19 | + _, findings := services.projectionOperationContext(context.Background(), root, ProjectionSourceOptions{ |
| 20 | + BundleArchive: archive, ReleaseEnvelope: archive + ".json", |
| 21 | + }) |
| 22 | + if len(findings) != 1 || findings[0].Code != "GDS_LOCAL_OPERATION_NOT_PROVEN" || |
| 23 | + findings[0].Evidence["path"] != archive { |
| 24 | + t.Fatalf("public project did not require its exact released artifact: %#v", findings) |
| 25 | + } |
| 26 | + if _, err := os.Stat(filepath.Join(root, ".gds", "compiled-policy.json")); !os.IsNotExist(err) { |
| 27 | + t.Fatalf("artifact failure wrote a projection: %v", err) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestReleasedPrivateProjectCannotDetachFromEstatePolicy(t *testing.T) { |
| 32 | + root := releasedProjectFixture(t, "private") |
| 33 | + services, err := NewServices(DefaultClock) |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + _, findings := services.projectionOperationContext(context.Background(), root, ProjectionSourceOptions{ |
| 38 | + BundleArchive: "untrusted.tar.gz", ReleaseEnvelope: "untrusted.json", |
| 39 | + }) |
| 40 | + if len(findings) != 1 || findings[0].Code != "GDS_PROJECTION_RELEASE_TARGET_INVALID" { |
| 41 | + t.Fatalf("private project could bypass canonical estate policy: %#v", findings) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func releasedProjectFixture(t *testing.T, visibility string) string { |
| 46 | + t.Helper() |
| 47 | + root := t.TempDir() |
| 48 | + if err := os.Mkdir(filepath.Join(root, ".gds"), 0o755); err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + anchor := `schema_version: 1 |
| 52 | +repository: |
| 53 | + id: repo_01JEXAMPZ00000000000000001 |
| 54 | + display_name: example-project |
| 55 | + roles: [project] |
| 56 | + lifecycle: active |
| 57 | +provider: |
| 58 | + type: github |
| 59 | + installation: installation:example |
| 60 | + repository_id: 1234 |
| 61 | + owner: example-owner |
| 62 | + name: example-project |
| 63 | +classification: |
| 64 | + portfolios: [portfolio:example] |
| 65 | + visibility_contract: VISIBILITY |
| 66 | + data_classification: VISIBILITY |
| 67 | +policy: |
| 68 | + profiles: [repository-default] |
| 69 | + rollout_ring: standard |
| 70 | +git: |
| 71 | + default_branch: main |
| 72 | + integration: pull-request |
| 73 | + branch_model: task-branches |
| 74 | + handoff_pr: preferred |
| 75 | + cleanup: merged-only |
| 76 | +verification: |
| 77 | + commands: |
| 78 | + test: [git diff --check] |
| 79 | + required: [test] |
| 80 | +agent: |
| 81 | + context_profile: project-default |
| 82 | + generated_agents: false |
| 83 | + serena: |
| 84 | + enabled: false |
| 85 | + provenance_required: false |
| 86 | +release: |
| 87 | + mode: none |
| 88 | +` |
| 89 | + if err := os.WriteFile(filepath.Join(root, ".gds", "repository.yaml"), |
| 90 | + []byte(strings.ReplaceAll(anchor, "VISIBILITY", visibility)), 0o644); err != nil { |
| 91 | + t.Fatal(err) |
| 92 | + } |
| 93 | + for _, args := range [][]string{ |
| 94 | + {"init", "--quiet", "--initial-branch=main"}, |
| 95 | + {"add", ".gds/repository.yaml"}, |
| 96 | + {"-c", "user.name=Example", "-c", "user.email=example@example.test", "-c", "commit.gpgsign=false", |
| 97 | + "commit", "--quiet", "-m", "fixture"}, |
| 98 | + } { |
| 99 | + command := exec.Command("git", append([]string{"-C", root}, args...)...) |
| 100 | + if output, err := command.CombinedOutput(); err != nil { |
| 101 | + t.Fatalf("fixture Git %v: %v: %s", args, err, output) |
| 102 | + } |
| 103 | + } |
| 104 | + return root |
| 105 | +} |
0 commit comments