-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_spicedb_test.go
More file actions
246 lines (206 loc) · 6.36 KB
/
Copy pathrag_spicedb_test.go
File metadata and controls
246 lines (206 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package rag_test
import (
"bytes"
"context"
"fmt"
"io"
"testing"
"time"
spicedbcontainer "github.com/Mariscal6/testcontainers-spicedb-go"
apiv1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
authzed "github.com/authzed/authzed-go/v1"
"github.com/authzed/grpcutil"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/sohanmaheshwar/rag-spicedb-testcontainers"
)
// Shared test constants
const (
testImage = "authzed/spicedb:v1.47.1" // or any recent SpiceDB image
testPreshared = "somepresharedkey"
spiceDBTypeDoc = "document"
spiceDBPermRead = "read"
)
// TestRAGWithSpiceDBPermissions demonstrates how the RAG results
// change depending on the calling user, while using a SpiceDB
// Testcontainer to back permission checks.
func TestRAGWithSpiceDBPermissions(t *testing.T) {
t.Parallel()
ctx := context.Background()
// 1. Start SpiceDB via the community Testcontainers module
container, err := spicedbcontainer.Run(ctx, testImage)
require.NoError(t, err, "failed to start SpiceDB container")
{
logs, err := container.Logs(ctx)
require.NoError(t, err)
buf := new(bytes.Buffer)
_, _ = io.Copy(buf, logs)
t.Logf("=== SpiceDB Container Logs ===\n%s\n===============================\n", buf.String())
}
defer func() { _ = container.Terminate(ctx) }()
// Discover host:port for gRPC (50051 inside container)
host, err := container.Host(ctx)
require.NoError(t, err)
mappedPort, err := container.MappedPort(ctx, "50051/tcp")
require.NoError(t, err)
endpoint := fmt.Sprintf("%s:%s", host, mappedPort.Port())
// 2. Connect to this SpiceDB using the insecure local pattern
client, err := authzed.NewClient(
endpoint,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpcutil.WithInsecureBearerToken(testPreshared),
)
require.NoError(t, err, "failed to create authzed client")
// Give SpiceDB a moment if needed (depending on config)
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
// 3. Write a minimal schema + relationships:
//
// definition user {}
//
// definition document {
// relation owner: user
// relation viewer: user | owner
// permission read = owner + viewer
// }
//
// Emilia owns doc1, Beatrice can view doc2, everyone can view doc3.
writeTestSchema(t, ctx, client)
writeTestTuples(t, ctx, client)
// 4. Prepare 3 documents for the RAG index.
//
// Important: metadata.spicedb_object matches the SpiceDB object IDs we wrote.
docs := []rag.Document{
{
ID: "doc1",
Text: "Internal roadmap for 2025. Highly confidential.",
Metadata: map[string]string{
"spicedb_object": "document:doc1",
},
},
{
ID: "doc2",
Text: "Customer success playbook and escalation procedures.",
Metadata: map[string]string{
"spicedb_object": "document:doc2",
},
},
{
ID: "doc3",
Text: "Public FAQ for all users.",
Metadata: map[string]string{
"spicedb_object": "document:doc3",
},
},
}
pipeline := rag.NewRAGPipeline(client, spiceDBTypeDoc, spiceDBPermRead, docs)
// 5. Run some queries as different users and assert which docs appear.
// Emilia should see doc1 + doc3, but not doc2.
{
results, err := pipeline.Query(ctx, "emilia", "roadmap")
require.NoError(t, err)
requireEqualDocIDs(t, []string{"doc1"}, results)
results, err = pipeline.Query(ctx, "emilia", "public")
require.NoError(t, err)
requireEqualDocIDs(t, []string{"doc3"}, results)
}
// Beatrice should see doc2 + doc3, but not doc1.
{
results, err := pipeline.Query(ctx, "beatrice", "playbook")
require.NoError(t, err)
requireEqualDocIDs(t, []string{"doc2"}, results)
results, err = pipeline.Query(ctx, "beatrice", "public")
require.NoError(t, err)
requireEqualDocIDs(t, []string{"doc3"}, results)
}
// A random user 'charlie' should only see the public doc3.
{
results, err := pipeline.Query(ctx, "charlie", "public")
require.NoError(t, err)
requireEqualDocIDs(t, []string{"doc3"}, results)
}
}
// writeTestSchema configures a tiny SpiceDB schema for documents/users.
func writeTestSchema(t *testing.T, ctx context.Context, client *authzed.Client) {
t.Helper()
schema := `
definition user {}
definition document {
relation owner: user
relation viewer: user
permission read = owner + viewer
}
`
_, err := client.WriteSchema(ctx, &apiv1.WriteSchemaRequest{
Schema: schema,
})
require.NoError(t, err, "failed to write schema")
}
// writeTestTuples seeds a few relationships in SpiceDB:
// - Emilia owns doc1
// - Beatrice can view doc2
// - Everyone can view doc3 (via viewer).
func writeTestTuples(t *testing.T, ctx context.Context, client *authzed.Client) {
t.Helper()
var updates []*apiv1.RelationshipUpdate
// Emilia owns doc1
updates = append(updates, relUpdate(
spiceDBTypeDoc, "doc1",
"owner",
"user", "emilia",
))
// Beatrice views doc2
updates = append(updates, relUpdate(
spiceDBTypeDoc, "doc2",
"viewer",
"user", "beatrice",
))
// Everyone can view doc3 (viewer for emilia + beatrice + charlie)
for _, userID := range []string{"emilia", "beatrice", "charlie"} {
updates = append(updates, relUpdate(
spiceDBTypeDoc, "doc3",
"viewer",
"user", userID,
))
}
_, err := client.WriteRelationships(ctx, &apiv1.WriteRelationshipsRequest{
Updates: updates,
})
require.NoError(t, err, "failed to write relationships")
}
func relUpdate(resType, resID, relation, subjType, subjID string) *apiv1.RelationshipUpdate {
return &apiv1.RelationshipUpdate{
Operation: apiv1.RelationshipUpdate_OPERATION_CREATE,
Relationship: &apiv1.Relationship{
Resource: &apiv1.ObjectReference{
ObjectType: resType,
ObjectId: resID,
},
Relation: relation,
Subject: &apiv1.SubjectReference{
Object: &apiv1.ObjectReference{
ObjectType: subjType,
ObjectId: subjID,
},
},
},
}
}
// requireEqualDocIDs is a tiny helper that asserts the returned documents
// have exactly these IDs (order-insensitive).
func requireEqualDocIDs(t *testing.T, expected []string, docs []rag.Document) {
t.Helper()
got := make(map[string]struct{}, len(docs))
for _, d := range docs {
got[d.ID] = struct{}{}
}
if len(got) != len(expected) {
t.Fatalf("expected %d docs, got %d (got: %+v)", len(expected), len(got), got)
}
for _, id := range expected {
if _, ok := got[id]; !ok {
t.Fatalf("expected doc %q in results, but it was missing; got: %+v", id, got)
}
}
}