Skip to content

Commit e0ddec1

Browse files
committed
fix: make sure we don't have left over users and channels
1 parent b73ce04 commit e0ddec1

File tree

9 files changed

+211
-184
lines changed

9 files changed

+211
-184
lines changed

stream_chat/async_chat/channel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ async def update_partial(
8484
payload = {"set": to_set or {}, "unset": to_unset or []}
8585
return await self.client.patch(self.url, data=payload)
8686

87-
async def delete(self) -> StreamResponse:
88-
return await self.client.delete(self.url)
87+
async def delete(self, hard: bool = False) -> StreamResponse:
88+
return await self.client.delete(self.url, {"hard_delete": hard})
8989

9090
async def truncate(self, **options: Any) -> StreamResponse:
9191
return await self.client.post(f"{self.url}/truncate", data=options)

stream_chat/base/channel.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ def update_partial(
165165
pass
166166

167167
@abc.abstractmethod
168-
def delete(self) -> Union[StreamResponse, Awaitable[StreamResponse]]:
168+
def delete(
169+
self, hard: bool = False
170+
) -> Union[StreamResponse, Awaitable[StreamResponse]]:
169171
"""
170172
Delete the channel. Messages are permanently removed.
171173

stream_chat/channel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def update_partial(
8282
payload = {"set": to_set or {}, "unset": to_unset or []}
8383
return self.client.patch(self.url, data=payload)
8484

85-
def delete(self) -> StreamResponse:
86-
return self.client.delete(self.url)
85+
def delete(self, hard: bool = False) -> StreamResponse:
86+
return self.client.delete(self.url, params={"hard_delete": hard})
8787

8888
def truncate(self, **options: Any) -> StreamResponse:
8989
return self.client.post(f"{self.url}/truncate", data=options)

stream_chat/tests/async_chat/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async def channel(client: StreamChatAsync, random_user: Dict):
8686
yield channel
8787

8888
try:
89-
await channel.delete()
89+
await client.delete_channels([channel.cid], hard_delete=True)
9090
except Exception:
9191
pass
9292

@@ -128,10 +128,10 @@ async def fellowship_of_the_ring(client: StreamChatAsync):
128128
await channel.create("gandalf")
129129
yield
130130
try:
131-
await channel.delete()
131+
await channel.delete(hard=True)
132+
await hard_delete_users(client, [m["id"] for m in members])
132133
except Exception:
133134
pass
134-
await hard_delete_users(client, [m["id"] for m in members])
135135

136136

137137
async def hard_delete_users(client: StreamChatAsync, user_ids: List[str]):

stream_chat/tests/async_chat/test_channel.py

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from stream_chat.async_chat.channel import Channel
99
from stream_chat.async_chat.client import StreamChatAsync
1010
from stream_chat.base.exceptions import StreamAPIException
11+
from stream_chat.tests.async_chat.conftest import hard_delete_users
1112

1213

1314
@pytest.mark.incremental
@@ -34,6 +35,7 @@ async def test_create_without_id(
3435

3536
await channel.create(random_users[0]["id"])
3637
assert channel.id is not None
38+
await channel.delete(hard=True)
3739

3840
async def test_create_with_options(
3941
self, client: StreamChatAsync, random_users: List[Dict]
@@ -45,6 +47,7 @@ async def test_create_with_options(
4547

4648
await channel.create(random_users[0]["id"], hide_for_creator=True)
4749
assert channel.id is not None
50+
await channel.delete(hard=True)
4851

4952
async def test_send_message_with_options(self, channel: Channel, random_user: Dict):
5053
response = await channel.send_message(
@@ -54,26 +57,23 @@ async def test_send_message_with_options(self, channel: Channel, random_user: Di
5457
assert response["message"]["text"] == "hi"
5558

5659
async def test_send_message_with_restricted_visibility(
57-
self, client: StreamChatAsync, channel: Channel, random_user: Dict
60+
self, channel: Channel, random_users: List[Dict]
5861
):
59-
# Create test users first
60-
restricted_users = [
61-
{"id": "amy", "name": "Amy"},
62-
{"id": "paul", "name": "Paul"},
63-
]
64-
await client.upsert_users(restricted_users)
62+
amy = random_users[0]["id"]
63+
paul = random_users[1]["id"]
64+
user = random_users[2]["id"]
6565

6666
# Add users to channel
67-
await channel.add_members([u["id"] for u in restricted_users])
67+
await channel.add_members([amy, paul])
6868

6969
# Send message with restricted visibility
7070
response = await channel.send_message(
71-
{"text": "hi", "restricted_visibility": ["amy", "paul"]}, random_user["id"]
71+
{"text": "hi", "restricted_visibility": [amy, paul]}, user
7272
)
7373

7474
assert "message" in response
7575
assert response["message"]["text"] == "hi"
76-
assert response["message"]["restricted_visibility"] == ["amy", "paul"]
76+
assert response["message"]["restricted_visibility"] == [amy, paul]
7777

7878
async def test_send_event(self, channel: Channel, random_user: Dict):
7979
response = await channel.send_event({"type": "typing.start"}, random_user["id"])
@@ -303,39 +303,44 @@ async def test_channel_hide_show(
303303
)
304304
assert len(response["channels"]) == 1
305305

306-
async def test_invites(self, client: StreamChatAsync, channel: Channel):
307-
members = ["john", "paul", "george", "pete", "ringo", "eric"]
308-
await client.upsert_users([{"id": m} for m in members])
306+
async def test_invites(self, client: StreamChatAsync, random_users: List[Dict]):
307+
john = random_users[0]["id"]
308+
ringo = random_users[1]["id"]
309+
eric = random_users[2]["id"]
310+
309311
channel = client.channel(
310312
"team",
311313
"beatles-" + str(uuid.uuid4()),
312-
{"members": members, "invites": ["ringo", "eric"]},
314+
{"members": [john], "invites": [ringo, eric]},
313315
)
314-
await channel.create("john")
316+
await channel.create(john)
315317
# accept the invite when not a member
316318
with pytest.raises(StreamAPIException):
317-
await channel.accept_invite("brian")
319+
await channel.accept_invite("brian" + str(uuid.uuid4()))
318320
# accept the invite when a member
319-
accept = await channel.accept_invite("ringo")
321+
accept = await channel.accept_invite(ringo)
320322
for m in accept["members"]:
321-
if m["user_id"] == "ringo":
323+
if m["user_id"] == ringo:
322324
assert m["invited"] is True
323325
assert "invite_accepted_at" in m
324326
# can accept again, noop
325-
await channel.accept_invite("ringo")
327+
await channel.accept_invite(ringo)
326328

327-
reject = await channel.reject_invite("eric")
329+
reject = await channel.reject_invite(eric)
328330
for m in reject["members"]:
329-
if m["user_id"] == "eric":
331+
if m["user_id"] == eric:
330332
assert m["invited"] is True
331333
assert "invite_rejected_at" in m
332334
# can reject again, noop
333-
await channel.reject_invite("eric")
335+
await channel.reject_invite(eric)
336+
await channel.delete(hard=True)
334337

335338
async def test_query_members(self, client: StreamChatAsync, channel: Channel):
336-
members = ["paul", "george", "john", "jessica", "john2"]
337-
await client.upsert_users([{"id": m, "name": m} for m in members])
338-
for member in members:
339+
rand = str(uuid.uuid4())
340+
user_ids = ["paul", "george", "john", "jessica", "john2"]
341+
user_ids = [f"{n}-{rand}" for n in user_ids]
342+
await client.upsert_users([{"id": m, "name": m} for m in user_ids])
343+
for member in user_ids:
339344
await channel.add_members([member])
340345

341346
response = await channel.query_members(
@@ -346,8 +351,10 @@ async def test_query_members(self, client: StreamChatAsync, channel: Channel):
346351
)
347352

348353
assert len(response) == 2
349-
assert response[0]["user"]["id"] == "jessica"
350-
assert response[1]["user"]["id"] == "john2"
354+
assert response[0]["user"]["id"] == f"jessica-{rand}"
355+
assert response[1]["user"]["id"] == f"john2-{rand}"
356+
357+
await hard_delete_users(client, user_ids)
351358

352359
async def test_mute_unmute(
353360
self, client: StreamChatAsync, channel: Channel, random_users: List[Dict]

0 commit comments

Comments
 (0)