| 1 | package policy |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/FelineStateMachine/tinyrelay/internal/event" |
| 8 | ) |
| 9 | |
| 10 | type privateRepositoryLookup struct { |
| 11 | private bool |
| 12 | } |
| 13 | |
| 14 | func (l privateRepositoryLookup) IsPrivateRepository(context.Context, string, string) (bool, error) { |
| 15 | return l.private, nil |
| 16 | } |
| 17 | |
| 18 | func TestPrivateRepositoryStateInheritsAnnouncement(t *testing.T) { |
| 19 | announcement := event.Event{Kind: event.KIND_REPO, Tags: [][]string{{"private" , "true" }}} |
| 20 | private, err := PrivateRepository(context.Background(), nil, announcement) |
| 21 | if err != nil || !private { |
| 22 | t.Fatalf("announcement privacy = %v, %v; want true, nil" , private, err) |
| 23 | } |
| 24 | state := event.Event{Kind: event.KIND_REPO_STATE, PubKey: "owner" , Tags: [][]string{{"d" , "repo" }}} |
| 25 | private, err = PrivateRepository(context.Background(), privateRepositoryLookup{private: true}, state) |
| 26 | if err != nil || !private { |
| 27 | t.Fatalf("stored state privacy = %v, %v; want true, nil" , private, err) |
| 28 | } |
| 29 | private, err = PrivateRepository(context.Background(), nil, state) |
| 30 | if err != nil || private { |
| 31 | t.Fatalf("state without lookup privacy = %v, %v; want false, nil" , private, err) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestPrivatePeerBaseMatchesPathAndNormalizesScheme(t *testing.T) { |
| 36 | peers := []string{"wss://peer.example/relay/" } |
| 37 | if got := PrivatePeerBase("https://peer.example/relay/repo.git" , peers); got != "https://peer.example/relay" { |
| 38 | t.Fatalf("matching peer base = %q; want https://peer.example/relay" , got) |
| 39 | } |
| 40 | if got := PrivatePeerBase("wss://peer.example/relay/repo.git" , []string{"https://peer.example/relay" }); got != "https://peer.example/relay" { |
| 41 | t.Fatalf("reverse websocket peer base = %q; want https://peer.example/relay" , got) |
| 42 | } |
| 43 | if got := PrivatePeerBase("https://peer.example/other" , peers); got != "" { |
| 44 | t.Fatalf("non-matching peer base = %q; want empty" , got) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestPrivatePeerBaseMatchesRootPeer(t *testing.T) { |
| 49 | if got := PrivatePeerBase("https://peer.example/repo.git" , []string{"https://peer.example" }); got != "https://peer.example" { |
| 50 | t.Fatalf("root peer base = %q; want https://peer.example" , got) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestPrivateServiceEnabledRequiresCompleteContract(t *testing.T) { |
| 55 | p := Defaults("owner" ) |
| 56 | p.Features.Grasp08 = true |
| 57 | if p.PrivateServiceEnabled() { |
| 58 | t.Fatal("GRASP-08 without base GRASP or member reads should be disabled" ) |
| 59 | } |
| 60 | p.Features.Grasp = true |
| 61 | if p.PrivateServiceEnabled() { |
| 62 | t.Fatal("GRASP-08 with open reads should be disabled" ) |
| 63 | } |
| 64 | p.Reads = "members" |
| 65 | if !p.PrivateServiceEnabled() { |
| 66 | t.Fatal("complete private service contract should be enabled" ) |
| 67 | } |
| 68 | } |