Migration
This is my new public git source viewer. Source coming soon. Please contact with concerns/bugs.
Contact me
internal/access/access_test.go
main
· 2.6 KB · 107 lines
raw
| 1 | package access |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | func TestCanView(t *testing.T) { |
| 9 | revoked := time.Unix(1_700_000_000, 0) |
| 10 | granted := revoked.Add(time.Minute) |
| 11 | |
| 12 | cases := []struct { |
| 13 | name string |
| 14 | p Policy |
| 15 | id Identity |
| 16 | want bool |
| 17 | }{ |
| 18 | { |
| 19 | name: "public repo", |
| 20 | p: Policy{Slug: "demo"}, |
| 21 | want: true, |
| 22 | }, |
| 23 | { |
| 24 | name: "hidden anonymous", |
| 25 | p: Policy{Slug: "demo", Hidden: true}, |
| 26 | want: false, |
| 27 | }, |
| 28 | { |
| 29 | name: "hidden owner", |
| 30 | p: Policy{Slug: "demo", Hidden: true, VisibleTo: []string{"Alice"}}, |
| 31 | id: Identity{User: "alice"}, |
| 32 | want: true, |
| 33 | }, |
| 34 | { |
| 35 | name: "hidden non-owner", |
| 36 | p: Policy{Slug: "demo", Hidden: true, VisibleTo: []string{"alice"}}, |
| 37 | id: Identity{User: "bob"}, |
| 38 | want: false, |
| 39 | }, |
| 40 | { |
| 41 | name: "hidden share grant", |
| 42 | p: Policy{Slug: "demo", Hidden: true}, |
| 43 | id: Identity{SharedSlugs: []string{"demo"}, SharedIssuedAt: granted}, |
| 44 | want: true, |
| 45 | }, |
| 46 | { |
| 47 | name: "share grant for another repo", |
| 48 | p: Policy{Slug: "demo", Hidden: true}, |
| 49 | id: Identity{SharedSlugs: []string{"other"}, SharedIssuedAt: granted}, |
| 50 | want: false, |
| 51 | }, |
| 52 | { |
| 53 | name: "share grant before revocation", |
| 54 | p: Policy{Slug: "demo", Hidden: true, ShareRevokedAfter: revoked}, |
| 55 | id: Identity{SharedSlugs: []string{"demo"}, SharedIssuedAt: revoked}, |
| 56 | want: false, |
| 57 | }, |
| 58 | { |
| 59 | name: "share grant after revocation", |
| 60 | p: Policy{Slug: "demo", Hidden: true, ShareRevokedAfter: revoked}, |
| 61 | id: Identity{SharedSlugs: []string{"demo"}, SharedIssuedAt: granted}, |
| 62 | want: true, |
| 63 | }, |
| 64 | } |
| 65 | |
| 66 | for _, tc := range cases { |
| 67 | t.Run(tc.name, func(t *testing.T) { |
| 68 | if got := tc.p.CanView(tc.id); got != tc.want { |
| 69 | t.Fatalf("CanView() = %v, want %v", got, tc.want) |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestCanShare(t *testing.T) { |
| 76 | p := Policy{Slug: "demo", Hidden: true, VisibleTo: []string{"alice"}} |
| 77 | |
| 78 | if !p.CanShare("alice") { |
| 79 | t.Fatal("owner should be able to share a hidden repo") |
| 80 | } |
| 81 | if p.CanShare("bob") { |
| 82 | t.Fatal("non-owner should not be able to share") |
| 83 | } |
| 84 | if p.CanShare("") { |
| 85 | t.Fatal("anonymous should not be able to share") |
| 86 | } |
| 87 | if (Policy{Slug: "public", VisibleTo: []string{"alice"}}).CanShare("alice") { |
| 88 | t.Fatal("public repo should not be shareable") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestShareLive(t *testing.T) { |
| 93 | revoked := time.Unix(1_700_000_000, 0) |
| 94 | |
| 95 | if !ShareLive(time.Time{}, revoked) { |
| 96 | t.Fatal("zero revokedAfter should not revoke") |
| 97 | } |
| 98 | if ShareLive(revoked, revoked) { |
| 99 | t.Fatal("grant issued at the revocation time should be revoked") |
| 100 | } |
| 101 | if ShareLive(revoked, revoked.Add(-time.Second)) { |
| 102 | t.Fatal("grant issued before revocation should be revoked") |
| 103 | } |
| 104 | if !ShareLive(revoked, revoked.Add(time.Second)) { |
| 105 | t.Fatal("grant issued after revocation should survive") |
| 106 | } |
| 107 | } |