clone

a self-hosted multitenant nostr relay with sites, file storage, git hosting, and inbox and outbox delivery

feat/standalone-relay
tinyrelay / internal / policy / privacy_test.go
privacy_test.go 2.5K
1package policy23import (4 "context"5 "testing"67 "github.com/FelineStateMachine/tinyrelay/internal/event"8)910type privateRepositoryLookup struct {11 private bool12}1314func (l privateRepositoryLookup) IsPrivateRepository(context.Context, string, string) (bool, error) {15 return l.private, nil16}1718func 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}3435func 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}4748func 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}5354func TestPrivateServiceEnabledRequiresCompleteContract(t *testing.T) {55 p := Defaults("owner")56 p.Features.Grasp08 = true57 if p.PrivateServiceEnabled() {58 t.Fatal("GRASP-08 without base GRASP or member reads should be disabled")59 }60 p.Features.Grasp = true61 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}