gunk-dev/the-valley

policy_test.go

sourceef0e6a83672emain's published tip history
3.6 KiB
1package main
2
3import (
4 "strings"
5 "testing"
6)
7
8// The patterns are documented as matching the full refname with `*`
9// crossing path separators, and the hook they replace matched them as
10// shell case patterns. A matcher is wrong exactly at the stars, so the
11// stars are what is pinned.
12func TestAPatternMatchesTheWholeRefnameAndItsStarsCrossSeparators(t *testing.T) {
13 for _, c := range []struct {
14 pattern, ref string
15 want bool
16 }{
17 {"refs/heads/main", "refs/heads/main", true},
18 {"refs/heads/main", "refs/heads/main2", false},
19 {"refs/heads/main", "refs/heads/mai", false},
20 {"refs/heads/release/*", "refs/heads/release/1.0", true},
21 {"refs/heads/release/*", "refs/heads/release/1/hotfix", true},
22 {"refs/heads/release/*", "refs/heads/release/", true},
23 {"refs/heads/release/*", "refs/heads/releases/1", false},
24 {"refs/tags/release/*/v*", "refs/tags/release/cosmo/v1", true},
25 {"refs/tags/release/*/v*", "refs/tags/release/cosmo/nightly", false},
26 {"refs/*/main", "refs/heads/x/main", true},
27 {"refs/*/main", "refs/heads/x/main/y", false},
28 {"refs/heads/*a*b", "refs/heads/aab", true},
29 {"refs/heads/*a*b", "refs/heads/aaba", false},
30 } {
31 if got := match(c.pattern, c.ref); got != c.want {
32 t.Errorf("match(%q, %q) = %v, want %v", c.pattern, c.ref, got, c.want)
33 }
34 }
35}
36
37// The rules are tried in order, and the order is what keeps a declaration
38// from opening more than it says. Each case here is one where a later rule
39// would answer differently from the one that decides.
40func TestTheFirstRuleThatMatchesDecides(t *testing.T) {
41 const zero = "0000000000000000000000000000000000000000"
42 const some = "1111111111111111111111111111111111111111"
43 p := policy{
44 project: "everything",
45 protection: protection{
46 // Every ref the project has is protected, so only the fixed
47 // rules ahead of protection can say anything different.
48 Refs: []string{"refs/*"},
49 Writers: []string{"writer"},
50 Allow: []opening{
51 {Refs: []string{"refs/*"}, Writers: []string{"opened"}},
52 },
53 },
54 grants: grants{"requester": {"request": true}},
55 }
56 for _, c := range []struct {
57 who, ref, old string
58 allowed bool
59 says string
60 }{
61 // No declaration reaches a replacement ref, not even a writer of
62 // a pattern that covers it.
63 {"writer", "refs/replace/" + some, zero, false, "refs/replace/ is closed to every push"},
64 // Attestations are create-only for everyone, ahead of protection.
65 {"anyone", "refs/the-valley/attestations/d/k", zero, true, ""},
66 {"writer", "refs/the-valley/attestations/d/k", some, false, "create-only"},
67 // A protected ref is its writers' alone: neither an allow entry
68 // nor the request grant opens it.
69 {"writer", "refs/tags/v1", zero, true, ""},
70 {"opened", "refs/tags/v1", zero, false, "writers: writer"},
71 {"requester", "refs/the-valley/integration-requests/main/c", zero, false, "a protected ref"},
72 } {
73 got := p.decide(c.who, update{old: c.old, new: some, ref: c.ref})
74 if (got == "") != c.allowed || !strings.Contains(got, c.says) {
75 t.Errorf("%s writing %s: got %q, want allowed=%v saying %q", c.who, c.ref, got, c.allowed, c.says)
76 }
77 }
78
79 // With nothing protected, the request namespace answers before an
80 // allow entry can: opening it by declaration would be a request grant
81 // nobody holds in the registry.
82 p.protection.Refs = nil
83 for _, c := range []struct {
84 who string
85 allowed bool
86 }{
87 {"requester", true},
88 {"opened", false},
89 {"", false},
90 } {
91 got := p.decide(c.who, update{old: zero, new: some, ref: "refs/the-valley/integration-requests/main/c"})
92 if (got == "") != c.allowed {
93 t.Errorf("%q filing a request: got %q, want allowed=%v", c.who, got, c.allowed)
94 }
95 }
96}