clone

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

main
tinyrelay / internal / gates / signer_test.go
signer_test.go 2.5K
1package gates23import (4 "context"5 "strings"6 "testing"78 "github.com/FelineStateMachine/tinyrelay/internal/event"9 "github.com/FelineStateMachine/tinyrelay/internal/policy"10 "github.com/FelineStateMachine/tinyrelay/internal/relay"11)1213func TestSignerRepliesReachUnauthenticatedRecipientSubscription(t *testing.T) {14 sender, recipient := strings.Repeat("a", 64), strings.Repeat("b", 64)15 message := event.Event{Kind: event.KIND_NOSTR_CONNECT, PubKey: sender, Tags: [][]string{{"p", recipient}}}16 for _, tc := range []struct {17 name string18 enabled bool19 filter *event.Filter20 session relay.Session21 want bool22 }{23 {"encrypted reply", true, &event.Filter{Kinds: []int{24133}, Tags: map[string][]string{"p": {recipient}}}, relay.Session{}, true},24 {"unrelated recipient", true, &event.Filter{Kinds: []int{24133}, Tags: map[string][]string{"p": {sender}}}, relay.Session{}, false},25 {"unscoped subscription", true, &event.Filter{Kinds: []int{24133}}, relay.Session{}, false},26 {"authenticated recipient", true, nil, relay.Session{PubKeys: []string{recipient}}, true},27 {"signer disabled", false, &event.Filter{Kinds: []int{24133}, Tags: map[string][]string{"p": {recipient}}}, relay.Session{PubKeys: []string{recipient}}, false},28 } {29 t.Run(tc.name, func(t *testing.T) {30 p := policy.Defaults(sender)31 p.Reads, p.Writes = "members", "owner"32 p.Features.Signer = tc.enabled33 g, err := New(Config{Policy: func() policy.Policy { return p }})34 if err != nil {35 t.Fatal(err)36 }37 if got := g.CanSee(context.Background(), message, tc.session, tc.filter); got != tc.want {38 t.Fatalf("can see signer reply: got %v, want %v", got, tc.want)39 }40 })41 }42}4344func TestArtifactVisibilityTracksNestedSourcesAndCycles(t *testing.T) {45 p := policy.Defaults(strings.Repeat("a", 64))46 nodes := map[string]event.Event{47 "a": {ID: "a", Kind: event.KIND_VIEW},48 "b": {ID: "b", Kind: event.KIND_VIEW},49 }50 refs := map[string][]string{"a": {"b"}, "b": {"hidden"}}51 var gate *Gate52 gate, _ = New(Config{Policy: func() policy.Policy { return p }, ArtifactVisible: func(ctx context.Context, e event.Event, s relay.Session) bool {53 for _, id := range refs[e.ID] {54 if id == "hidden" {55 return false56 }57 if !gate.CanSee(ctx, nodes[id], s, nil) {58 return false59 }60 }61 return true62 }})63 if gate.CanSee(context.Background(), nodes["a"], relay.Session{}, nil) {64 t.Fatal("nested artifact with hidden source became visible")65 }66 refs["b"] = []string{"a"}67 if gate.CanSee(context.Background(), nodes["a"], relay.Session{}, nil) {68 t.Fatal("cyclic artifact source became visible")69 }70}