package access import ( "testing" "time" ) func TestCanView(t *testing.T) { revoked := time.Unix(1_700_000_000, 0) granted := revoked.Add(time.Minute) cases := []struct { name string p Policy id Identity want bool }{ { name: "public repo", p: Policy{Slug: "demo"}, want: true, }, { name: "hidden anonymous", p: Policy{Slug: "demo", Hidden: true}, want: false, }, { name: "hidden owner", p: Policy{Slug: "demo", Hidden: true, VisibleTo: []string{"Alice"}}, id: Identity{User: "alice"}, want: true, }, { name: "hidden non-owner", p: Policy{Slug: "demo", Hidden: true, VisibleTo: []string{"alice"}}, id: Identity{User: "bob"}, want: false, }, { name: "hidden share grant", p: Policy{Slug: "demo", Hidden: true}, id: Identity{SharedSlugs: []string{"demo"}, SharedIssuedAt: granted}, want: true, }, { name: "share grant for another repo", p: Policy{Slug: "demo", Hidden: true}, id: Identity{SharedSlugs: []string{"other"}, SharedIssuedAt: granted}, want: false, }, { name: "share grant before revocation", p: Policy{Slug: "demo", Hidden: true, ShareRevokedAfter: revoked}, id: Identity{SharedSlugs: []string{"demo"}, SharedIssuedAt: revoked}, want: false, }, { name: "share grant after revocation", p: Policy{Slug: "demo", Hidden: true, ShareRevokedAfter: revoked}, id: Identity{SharedSlugs: []string{"demo"}, SharedIssuedAt: granted}, want: true, }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { if got := tc.p.CanView(tc.id); got != tc.want { t.Fatalf("CanView() = %v, want %v", got, tc.want) } }) } } func TestCanShare(t *testing.T) { p := Policy{Slug: "demo", Hidden: true, VisibleTo: []string{"alice"}} if !p.CanShare("alice") { t.Fatal("owner should be able to share a hidden repo") } if p.CanShare("bob") { t.Fatal("non-owner should not be able to share") } if p.CanShare("") { t.Fatal("anonymous should not be able to share") } if (Policy{Slug: "public", VisibleTo: []string{"alice"}}).CanShare("alice") { t.Fatal("public repo should not be shareable") } } func TestShareLive(t *testing.T) { revoked := time.Unix(1_700_000_000, 0) if !ShareLive(time.Time{}, revoked) { t.Fatal("zero revokedAfter should not revoke") } if ShareLive(revoked, revoked) { t.Fatal("grant issued at the revocation time should be revoked") } if ShareLive(revoked, revoked.Add(-time.Second)) { t.Fatal("grant issued before revocation should be revoked") } if !ShareLive(revoked, revoked.Add(time.Second)) { t.Fatal("grant issued after revocation should survive") } }