Skip to content

Commit a1953d9

Browse files
authored
Add user permissions for conversations (#1723)
1 parent 805114c commit a1953d9

File tree

5 files changed

+375
-290
lines changed

5 files changed

+375
-290
lines changed
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
import { r2rClient } from "../src/index";
2+
import { describe, test, beforeAll, expect } from "@jest/globals";
3+
4+
const baseUrl = "http://localhost:7272";
5+
6+
describe("r2rClient V3 Collections Integration Tests", () => {
7+
let client: r2rClient;
8+
let user1Client: r2rClient;
9+
let user2Client: r2rClient;
10+
let user1Id: string;
11+
let user2Id: string;
12+
let conversationId: string;
13+
let user1ConversationId: string;
14+
let user2ConversationId: string;
15+
16+
beforeAll(async () => {
17+
client = new r2rClient(baseUrl);
18+
user1Client = new r2rClient(baseUrl);
19+
user2Client = new r2rClient(baseUrl);
20+
21+
await client.users.login({
22+
23+
password: "change_me_immediately",
24+
});
25+
});
26+
27+
test("Register user 1", async () => {
28+
const response = await client.users.create({
29+
30+
password: "change_me_immediately",
31+
});
32+
33+
user1Id = response.results.id;
34+
expect(response.results).toBeDefined();
35+
expect(response.results.is_superuser).toBe(false);
36+
expect(response.results.name).toBe(null);
37+
});
38+
39+
test("Login as a user 1", async () => {
40+
const response = await user1Client.users.login({
41+
42+
password: "change_me_immediately",
43+
});
44+
expect(response.results).toBeDefined();
45+
});
46+
47+
test("Register user 2", async () => {
48+
const response = await client.users.create({
49+
50+
password: "change_me_immediately",
51+
});
52+
53+
user2Id = response.results.id;
54+
expect(response.results).toBeDefined();
55+
expect(response.results.is_superuser).toBe(false);
56+
expect(response.results.name).toBe(null);
57+
});
58+
59+
test("Login as a user 2", async () => {
60+
const response = await user2Client.users.login({
61+
62+
password: "change_me_immediately",
63+
});
64+
expect(response.results).toBeDefined();
65+
});
66+
67+
test("Get the health of the system", async () => {
68+
const response = await client.system.health();
69+
expect(response.results).toBeDefined();
70+
});
71+
72+
test("Get the health of the system as user 1", async () => {
73+
const response = await user1Client.system.health();
74+
expect(response.results).toBeDefined();
75+
});
76+
77+
test("Get the health of the system as user 2", async () => {
78+
const response = await user2Client.system.health();
79+
expect(response.results).toBeDefined();
80+
});
81+
82+
test("List all conversations", async () => {
83+
const response = await client.conversations.list();
84+
85+
expect(response.results).toBeDefined();
86+
expect(response.results).toEqual([]);
87+
expect(response.total_entries).toBe(0);
88+
});
89+
90+
test("List all conversations as user 1", async () => {
91+
const response = await user1Client.conversations.list();
92+
93+
expect(response.results).toBeDefined();
94+
expect(response.results).toEqual([]);
95+
expect(response.total_entries).toBe(0);
96+
});
97+
98+
test("List all conversations as user 2", async () => {
99+
const response = await user2Client.conversations.list();
100+
101+
expect(response.results).toBeDefined();
102+
expect(response.results).toEqual([]);
103+
expect(response.total_entries).toBe(0);
104+
});
105+
106+
test("Create a conversation with a name", async () => {
107+
const response = await client.conversations.create({
108+
name: "Test Conversation",
109+
});
110+
conversationId = response.results.id;
111+
expect(response.results).toBeDefined();
112+
expect(response.results.name).toBe("Test Conversation");
113+
});
114+
115+
test("Create a conversation with a name as user 1", async () => {
116+
const response = await user1Client.conversations.create({
117+
name: "User 1 Conversation",
118+
});
119+
user1ConversationId = response.results.id;
120+
expect(response.results).toBeDefined();
121+
expect(response.results.name).toBe("User 1 Conversation");
122+
});
123+
124+
test("Create a conversation with a name as user 2", async () => {
125+
const response = await user2Client.conversations.create({
126+
name: "User 2 Conversation",
127+
});
128+
user2ConversationId = response.results.id;
129+
expect(response.results).toBeDefined();
130+
expect(response.results.name).toBe("User 2 Conversation");
131+
});
132+
133+
test("Update a conversation name", async () => {
134+
const response = await client.conversations.update({
135+
id: conversationId,
136+
name: "Updated Name",
137+
});
138+
expect(response.results).toBeDefined();
139+
expect(response.results.name).toBe("Updated Name");
140+
});
141+
142+
test("Update a conversation name as user 1", async () => {
143+
const response = await user1Client.conversations.update({
144+
id: user1ConversationId,
145+
name: "User 1 Updated Name",
146+
});
147+
expect(response.results).toBeDefined();
148+
expect(response.results.name).toBe("User 1 Updated Name");
149+
});
150+
151+
test("Update a conversation name as user 2", async () => {
152+
const response = await user2Client.conversations.update({
153+
id: user2ConversationId,
154+
name: "User 2 Updated Name",
155+
});
156+
expect(response.results).toBeDefined();
157+
expect(response.results.name).toBe("User 2 Updated Name");
158+
});
159+
160+
test("Add a message to a conversation", async () => {
161+
const response = await client.conversations.addMessage({
162+
id: conversationId,
163+
content: "Hello, world!",
164+
role: "user",
165+
});
166+
expect(response.results).toBeDefined();
167+
});
168+
169+
test("Add a message to a conversation as user 1", async () => {
170+
const response = await user1Client.conversations.addMessage({
171+
id: user1ConversationId,
172+
content: "Hello, world!",
173+
role: "user",
174+
});
175+
expect(response.results).toBeDefined();
176+
});
177+
178+
test("Add a message to a conversation as user 2", async () => {
179+
const response = await user2Client.conversations.addMessage({
180+
id: user2ConversationId,
181+
content: "Hello, world!",
182+
role: "user",
183+
});
184+
expect(response.results).toBeDefined();
185+
});
186+
187+
test("User 1 should not be able to see user 2's conversation", async () => {
188+
await expect(
189+
user1Client.conversations.retrieve({ id: user2ConversationId }),
190+
).rejects.toThrow(/Status 404/);
191+
});
192+
193+
test("User 2 should not be able to see user 1's conversation", async () => {
194+
await expect(
195+
user2Client.conversations.retrieve({ id: user1ConversationId }),
196+
).rejects.toThrow(/Status 404/);
197+
});
198+
199+
test("User 1 should not see user 2's conversation when listing all conversations", async () => {
200+
const response = await user1Client.conversations.list();
201+
expect(response.results).toHaveLength(1);
202+
});
203+
204+
test("User 2 should not see user 1's conversation when listing all conversations", async () => {
205+
const response = await user2Client.conversations.list();
206+
expect(response.results).toHaveLength(1);
207+
});
208+
209+
test("The super user should see all conversations when listing all conversations", async () => {
210+
const response = await client.conversations.list();
211+
expect(response.results).toHaveLength(3);
212+
});
213+
214+
test("Delete a conversation", async () => {
215+
const response = await client.conversations.delete({ id: conversationId });
216+
expect(response.results).toBeDefined();
217+
});
218+
219+
test("User 1 should not be able to delete user 2's conversation", async () => {
220+
await expect(
221+
user1Client.conversations.delete({ id: user2ConversationId }),
222+
).rejects.toThrow(/Status 404/);
223+
});
224+
225+
test("User 2 should not be able to delete user 1's conversation", async () => {
226+
await expect(
227+
user2Client.conversations.delete({ id: user1ConversationId }),
228+
).rejects.toThrow(/Status 404/);
229+
});
230+
231+
test("Delete a conversation as user 1", async () => {
232+
const response = await user1Client.conversations.delete({
233+
id: user1ConversationId,
234+
});
235+
expect(response.results).toBeDefined();
236+
});
237+
238+
test("Super user should be able to delete any conversation", async () => {
239+
const response = await client.conversations.delete({
240+
id: user2ConversationId,
241+
});
242+
expect(response.results).toBeDefined();
243+
});
244+
245+
test("Delete user 1", async () => {
246+
const response = await client.users.delete({
247+
id: user1Id,
248+
password: "change_me_immediately",
249+
});
250+
expect(response.results).toBeDefined();
251+
});
252+
253+
test("Delete user 2", async () => {
254+
const response = await client.users.delete({
255+
id: user2Id,
256+
password: "change_me_immediately",
257+
});
258+
expect(response.results).toBeDefined();
259+
});
260+
});

0 commit comments

Comments
 (0)