diff --git a/stream_chat/async_chat/channel.py b/stream_chat/async_chat/channel.py index 18342f0d..6c54c00b 100644 --- a/stream_chat/async_chat/channel.py +++ b/stream_chat/async_chat/channel.py @@ -84,8 +84,8 @@ async def update_partial( payload = {"set": to_set or {}, "unset": to_unset or []} return await self.client.patch(self.url, data=payload) - async def delete(self) -> StreamResponse: - return await self.client.delete(self.url) + async def delete(self, hard: bool = False) -> StreamResponse: + return await self.client.delete(self.url, {"hard_delete": hard}) async def truncate(self, **options: Any) -> StreamResponse: return await self.client.post(f"{self.url}/truncate", data=options) diff --git a/stream_chat/base/channel.py b/stream_chat/base/channel.py index a00d329b..8bde162e 100644 --- a/stream_chat/base/channel.py +++ b/stream_chat/base/channel.py @@ -165,7 +165,9 @@ def update_partial( pass @abc.abstractmethod - def delete(self) -> Union[StreamResponse, Awaitable[StreamResponse]]: + def delete( + self, hard: bool = False + ) -> Union[StreamResponse, Awaitable[StreamResponse]]: """ Delete the channel. Messages are permanently removed. diff --git a/stream_chat/channel.py b/stream_chat/channel.py index cacfdd69..a9a3df6b 100644 --- a/stream_chat/channel.py +++ b/stream_chat/channel.py @@ -82,8 +82,8 @@ def update_partial( payload = {"set": to_set or {}, "unset": to_unset or []} return self.client.patch(self.url, data=payload) - def delete(self) -> StreamResponse: - return self.client.delete(self.url) + def delete(self, hard: bool = False) -> StreamResponse: + return self.client.delete(self.url, params={"hard_delete": hard}) def truncate(self, **options: Any) -> StreamResponse: return self.client.post(f"{self.url}/truncate", data=options) diff --git a/stream_chat/tests/async_chat/conftest.py b/stream_chat/tests/async_chat/conftest.py index 06544e66..425cee01 100644 --- a/stream_chat/tests/async_chat/conftest.py +++ b/stream_chat/tests/async_chat/conftest.py @@ -86,7 +86,7 @@ async def channel(client: StreamChatAsync, random_user: Dict): yield channel try: - await channel.delete() + await client.delete_channels([channel.cid], hard_delete=True) except Exception: pass @@ -128,10 +128,10 @@ async def fellowship_of_the_ring(client: StreamChatAsync): await channel.create("gandalf") yield try: - await channel.delete() + await channel.delete(hard=True) + await hard_delete_users(client, [m["id"] for m in members]) except Exception: pass - await hard_delete_users(client, [m["id"] for m in members]) async def hard_delete_users(client: StreamChatAsync, user_ids: List[str]): diff --git a/stream_chat/tests/async_chat/test_channel.py b/stream_chat/tests/async_chat/test_channel.py index 62ca36d4..c65ce34d 100644 --- a/stream_chat/tests/async_chat/test_channel.py +++ b/stream_chat/tests/async_chat/test_channel.py @@ -8,6 +8,7 @@ from stream_chat.async_chat.channel import Channel from stream_chat.async_chat.client import StreamChatAsync from stream_chat.base.exceptions import StreamAPIException +from stream_chat.tests.async_chat.conftest import hard_delete_users @pytest.mark.incremental @@ -34,6 +35,7 @@ async def test_create_without_id( await channel.create(random_users[0]["id"]) assert channel.id is not None + await channel.delete(hard=True) async def test_create_with_options( self, client: StreamChatAsync, random_users: List[Dict] @@ -45,6 +47,7 @@ async def test_create_with_options( await channel.create(random_users[0]["id"], hide_for_creator=True) assert channel.id is not None + await channel.delete(hard=True) async def test_send_message_with_options(self, channel: Channel, random_user: Dict): response = await channel.send_message( @@ -54,26 +57,23 @@ async def test_send_message_with_options(self, channel: Channel, random_user: Di assert response["message"]["text"] == "hi" async def test_send_message_with_restricted_visibility( - self, client: StreamChatAsync, channel: Channel, random_user: Dict + self, channel: Channel, random_users: List[Dict] ): - # Create test users first - restricted_users = [ - {"id": "amy", "name": "Amy"}, - {"id": "paul", "name": "Paul"}, - ] - await client.upsert_users(restricted_users) + amy = random_users[0]["id"] + paul = random_users[1]["id"] + user = random_users[2]["id"] # Add users to channel - await channel.add_members([u["id"] for u in restricted_users]) + await channel.add_members([amy, paul]) # Send message with restricted visibility response = await channel.send_message( - {"text": "hi", "restricted_visibility": ["amy", "paul"]}, random_user["id"] + {"text": "hi", "restricted_visibility": [amy, paul]}, user ) assert "message" in response assert response["message"]["text"] == "hi" - assert response["message"]["restricted_visibility"] == ["amy", "paul"] + assert response["message"]["restricted_visibility"] == [amy, paul] async def test_send_event(self, channel: Channel, random_user: Dict): response = await channel.send_event({"type": "typing.start"}, random_user["id"]) @@ -303,39 +303,44 @@ async def test_channel_hide_show( ) assert len(response["channels"]) == 1 - async def test_invites(self, client: StreamChatAsync, channel: Channel): - members = ["john", "paul", "george", "pete", "ringo", "eric"] - await client.upsert_users([{"id": m} for m in members]) + async def test_invites(self, client: StreamChatAsync, random_users: List[Dict]): + john = random_users[0]["id"] + ringo = random_users[1]["id"] + eric = random_users[2]["id"] + channel = client.channel( "team", "beatles-" + str(uuid.uuid4()), - {"members": members, "invites": ["ringo", "eric"]}, + {"members": [john], "invites": [ringo, eric]}, ) - await channel.create("john") + await channel.create(john) # accept the invite when not a member with pytest.raises(StreamAPIException): - await channel.accept_invite("brian") + await channel.accept_invite("brian" + str(uuid.uuid4())) # accept the invite when a member - accept = await channel.accept_invite("ringo") + accept = await channel.accept_invite(ringo) for m in accept["members"]: - if m["user_id"] == "ringo": + if m["user_id"] == ringo: assert m["invited"] is True assert "invite_accepted_at" in m # can accept again, noop - await channel.accept_invite("ringo") + await channel.accept_invite(ringo) - reject = await channel.reject_invite("eric") + reject = await channel.reject_invite(eric) for m in reject["members"]: - if m["user_id"] == "eric": + if m["user_id"] == eric: assert m["invited"] is True assert "invite_rejected_at" in m # can reject again, noop - await channel.reject_invite("eric") + await channel.reject_invite(eric) + await channel.delete(hard=True) async def test_query_members(self, client: StreamChatAsync, channel: Channel): - members = ["paul", "george", "john", "jessica", "john2"] - await client.upsert_users([{"id": m, "name": m} for m in members]) - for member in members: + rand = str(uuid.uuid4()) + user_ids = ["paul", "george", "john", "jessica", "john2"] + user_ids = [f"{n}-{rand}" for n in user_ids] + await client.upsert_users([{"id": m, "name": m} for m in user_ids]) + for member in user_ids: await channel.add_members([member]) response = await channel.query_members( @@ -346,8 +351,10 @@ async def test_query_members(self, client: StreamChatAsync, channel: Channel): ) assert len(response) == 2 - assert response[0]["user"]["id"] == "jessica" - assert response[1]["user"]["id"] == "john2" + assert response[0]["user"]["id"] == f"jessica-{rand}" + assert response[1]["user"]["id"] == f"john2-{rand}" + + await hard_delete_users(client, user_ids) async def test_mute_unmute( self, client: StreamChatAsync, channel: Channel, random_users: List[Dict] diff --git a/stream_chat/tests/async_chat/test_client.py b/stream_chat/tests/async_chat/test_client.py index 271bbd3d..140eea46 100644 --- a/stream_chat/tests/async_chat/test_client.py +++ b/stream_chat/tests/async_chat/test_client.py @@ -15,6 +15,7 @@ from stream_chat.async_chat import StreamChatAsync from stream_chat.async_chat.channel import Channel from stream_chat.base.exceptions import StreamAPIException +from stream_chat.tests.async_chat.conftest import hard_delete_users from stream_chat.tests.utils import wait_for_async @@ -140,23 +141,26 @@ async def test_update_user(self, client: StreamChatAsync): assert "users" in response assert user["id"] in response["users"] + await hard_delete_users(client, [user["id"]]) + async def test_update_users(self, client: StreamChatAsync): user = {"id": str(uuid.uuid4())} response = await client.upsert_users([user]) assert "users" in response assert user["id"] in response["users"] - async def test_update_user_partial(self, client: StreamChatAsync): - user_id = str(uuid.uuid4()) - await client.upsert_user({"id": user_id, "field": "value"}) + await hard_delete_users(client, [user["id"]]) + async def test_update_user_partial( + self, client: StreamChatAsync, random_user: Dict + ): response = await client.update_user_partial( - {"id": user_id, "set": {"field": "updated"}} + {"id": random_user["id"], "set": {"field": "updated"}} ) assert "users" in response - assert user_id in response["users"] - assert response["users"][user_id]["field"] == "updated" + assert random_user["id"] in response["users"] + assert response["users"][random_user["id"]]["field"] == "updated" async def test_delete_user(self, client: StreamChatAsync, random_user: Dict): response = await client.delete_user(random_user["id"]) @@ -364,22 +368,22 @@ async def test_update_message_partial( assert response["message"]["awesome"] is True async def test_update_message_restricted_visibility( - self, client: StreamChatAsync, channel: Channel, random_user: Dict + self, + client: StreamChatAsync, + channel: Channel, + random_users: List[Dict], ): - # Create test users first - restricted_users = [ - {"id": "amy", "name": "Amy"}, - {"id": "paul", "name": "Paul"}, - ] - await client.upsert_users(restricted_users) + amy = random_users[0]["id"] + paul = random_users[1]["id"] + user = random_users[2]["id"] # Add users to channel - await channel.add_members([u["id"] for u in restricted_users]) + await channel.add_members([amy, paul]) # Send initial message msg_id = str(uuid.uuid4()) response = await channel.send_message( - {"id": msg_id, "text": "hello world"}, random_user["id"] + {"id": msg_id, "text": "hello world"}, user ) assert response["message"]["text"] == "hello world" @@ -388,38 +392,38 @@ async def test_update_message_restricted_visibility( { "id": msg_id, "text": "helloworld", - "restricted_visibility": ["amy", "paul"], + "restricted_visibility": [amy, paul], "user": {"id": response["message"]["user"]["id"]}, } ) assert response["message"]["text"] == "helloworld" - assert response["message"]["restricted_visibility"] == ["amy", "paul"] + assert response["message"]["restricted_visibility"] == [amy, paul] async def test_update_message_partial_restricted_visibility( - self, client: StreamChatAsync, channel: Channel, random_user: Dict + self, + client: StreamChatAsync, + channel: Channel, + random_users: List[Dict], ): - # Create test users first - restricted_users = [ - {"id": "amy", "name": "Amy"}, - {"id": "paul", "name": "Paul"}, - ] - client.upsert_users(restricted_users) + amy = random_users[0]["id"] + paul = random_users[1]["id"] + user = random_users[2]["id"] # Add users to channel - channel.add_members([u["id"] for u in restricted_users]) + await channel.add_members([amy, paul]) msg_id = str(uuid.uuid4()) response = await channel.send_message( - {"id": msg_id, "text": "hello world"}, random_user["id"] + {"id": msg_id, "text": "hello world"}, user ) assert response["message"]["text"] == "hello world" response = await client.update_message_partial( msg_id, - dict(set=dict(text="helloworld", restricted_visibility=["amy"])), - random_user["id"], + dict(set=dict(text="helloworld", restricted_visibility=[amy])), + user, ) - assert response["message"]["restricted_visibility"] == ["amy"] + assert response["message"]["restricted_visibility"] == [amy] async def test_delete_message( self, client: StreamChatAsync, channel: Channel, random_user: Dict @@ -615,6 +619,7 @@ async def test_search( await channel.send_message( {"text": "Does 'cious' count as one or two?"}, random_user["id"] ) + time.sleep(1) # wait for the message to be indexed in elasticsearch response = await client.search( {"type": "messaging"}, query, **{"limit": 2, "offset": 0} ) @@ -638,6 +643,7 @@ async def test_search_message_filters( await channel.send_message( {"text": "Does 'cious' count as one or two?"}, random_user["id"] ) + time.sleep(1) # wait for the message to be indexed in elasticsearch response = await client.search( {"type": "messaging"}, {"text": {"$q": query}}, @@ -724,14 +730,18 @@ async def test_check_sns(self, client: StreamChatAsync): assert response["status"] == "error" assert "publishing the message failed." in response["error"] - async def test_guest_user(self, client: StreamChatAsync, random_user: Dict): + async def test_guest_user(self, client: StreamChatAsync): try: - response = await client.set_guest_user({"user": {"id": str(uuid.uuid4())}}) + user_id = str(uuid.uuid4()) + response = await client.set_guest_user({"user": {"id": user_id}}) assert "access_token" in response except StreamAPIException: # Guest user isn't turned on for every test app pass + # clean up + await hard_delete_users(client, [user_id]) + async def test_run_message_actions( self, client: StreamChatAsync, channel: Channel, random_user: Dict ): @@ -893,14 +903,14 @@ async def test_imports_end2end(self, client: StreamChatAsync): assert len(list_resp["import_tasks"]) == 1 async def test_unread_counts( - self, client: StreamChatAsync, channel, random_user: Dict + self, client: StreamChatAsync, channel, random_users: List[Dict] ): - await channel.add_members([random_user["id"]]) + user1 = random_users[0]["id"] + user2 = random_users[1]["id"] + await channel.add_members([user1]) msg_id = str(uuid.uuid4()) - await channel.send_message( - {"id": msg_id, "text": "helloworld"}, str(uuid.uuid4()) - ) - response = await client.unread_counts(random_user["id"]) + await channel.send_message({"id": msg_id, "text": "helloworld"}, user2) + response = await client.unread_counts(user1) assert "total_unread_count" in response assert "channels" in response assert "channel_type" in response @@ -910,13 +920,9 @@ async def test_unread_counts( assert len(response["channel_type"]) == 1 # test threads unread counts - await channel.send_message( - {"parent_id": msg_id, "text": "helloworld"}, random_user["id"] - ) - await channel.send_message( - {"parent_id": msg_id, "text": "helloworld"}, str(uuid.uuid4()) - ) - response = await client.unread_counts(random_user["id"]) + await channel.send_message({"parent_id": msg_id, "text": "helloworld"}, user1) + await channel.send_message({"parent_id": msg_id, "text": "helloworld"}, user2) + response = await client.unread_counts(user1) assert "total_unread_threads_count" in response assert "threads" in response assert response["total_unread_threads_count"] == 1 @@ -924,16 +930,16 @@ async def test_unread_counts( assert response["threads"][0]["parent_message_id"] == msg_id async def test_unread_counts_batch( - self, client: StreamChatAsync, channel, random_users: Dict + self, client: StreamChatAsync, channel, random_users: List[Dict] ): - await channel.add_members([x["id"] for x in random_users]) + user1 = random_users[0]["id"] + members = [x["id"] for x in random_users[1:]] + await channel.add_members(members) msg_id = str(uuid.uuid4()) - await channel.send_message( - {"id": msg_id, "text": "helloworld"}, str(uuid.uuid4()) - ) - response = await client.unread_counts_batch([x["id"] for x in random_users]) + await channel.send_message({"id": msg_id, "text": "helloworld"}, user1) + response = await client.unread_counts_batch(members) assert "counts_by_user" in response - for user_id in [x["id"] for x in random_users]: + for user_id in members: assert user_id in response["counts_by_user"] assert response["counts_by_user"][user_id]["total_unread_count"] == 1 @@ -943,11 +949,9 @@ async def test_unread_counts_batch( ) # test threads unread counts - await channel.send_message( - {"parent_id": msg_id, "text": "helloworld"}, str(uuid.uuid4()) - ) - response = await client.unread_counts_batch([x["id"] for x in random_users]) - for user_id in [x["id"] for x in random_users]: + await channel.send_message({"parent_id": msg_id, "text": "helloworld"}, user1) + response = await client.unread_counts_batch(members) + for user_id in members: assert user_id in response["counts_by_user"] assert ( response["counts_by_user"][user_id]["total_unread_threads_count"] == 1 diff --git a/stream_chat/tests/conftest.py b/stream_chat/tests/conftest.py index 22c74dd0..3fe5e7a3 100644 --- a/stream_chat/tests/conftest.py +++ b/stream_chat/tests/conftest.py @@ -77,7 +77,7 @@ def channel(client: StreamChat, random_user: Dict): yield channel try: - channel.delete() + client.delete_channels([channel.cid], hard_delete=True) except Exception: pass @@ -120,10 +120,10 @@ def fellowship_of_the_ring(client: StreamChat): yield try: - channel.delete() + channel.delete(hard=True) + hard_delete_users(client, [m["id"] for m in members]) except Exception: pass - hard_delete_users(client, [m["id"] for m in members]) def hard_delete_users(client: StreamChat, user_ids: List[str]): diff --git a/stream_chat/tests/test_channel.py b/stream_chat/tests/test_channel.py index ddec1d51..bf6bb5b3 100644 --- a/stream_chat/tests/test_channel.py +++ b/stream_chat/tests/test_channel.py @@ -8,6 +8,7 @@ from stream_chat import StreamChat from stream_chat.base.exceptions import StreamAPIException from stream_chat.channel import Channel +from stream_chat.tests.conftest import hard_delete_users @pytest.mark.incremental @@ -31,6 +32,8 @@ def test_create_without_id(self, client: StreamChat, random_users: List[Dict]): channel.create(random_users[0]["id"]) assert channel.id is not None + client.delete_channels([channel.cid], hard_delete=True) + def test_create_with_options(self, client: StreamChat, random_users: List[Dict]): channel = client.channel( "messaging", data={"members": [u["id"] for u in random_users]} @@ -40,6 +43,8 @@ def test_create_with_options(self, client: StreamChat, random_users: List[Dict]) channel.create(random_users[0]["id"], hide_for_creator=True) assert channel.id is not None + client.delete_channels([channel.cid], hard_delete=True) + def test_send_message_with_options(self, channel: Channel, random_user: Dict): response = channel.send_message( {"text": "hi"}, random_user["id"], skip_push=True @@ -64,25 +69,24 @@ def test_send_pending_message( assert response["message"]["text"] == "hi" def test_send_message_with_restricted_visibility( - self, client: StreamChat, channel: Channel, random_user: Dict + self, + channel: Channel, + random_users: List[Dict], ): - # Create test users first - restricted_users = [ - {"id": "amy", "name": "Amy"}, - {"id": "paul", "name": "Paul"}, - ] - client.upsert_users(restricted_users) + amy = random_users[0]["id"] + paul = random_users[1]["id"] + user = random_users[2]["id"] # Add users to channel - channel.add_members([u["id"] for u in restricted_users]) + channel.add_members([amy, paul]) # Send message with restricted visibility response = channel.send_message( - {"text": "hi", "restricted_visibility": ["amy", "paul"]}, random_user["id"] + {"text": "hi", "restricted_visibility": [amy, paul]}, user ) assert "message" in response assert response["message"]["text"] == "hi" - assert response["message"]["restricted_visibility"] == ["amy", "paul"] + assert response["message"]["restricted_visibility"] == [amy, paul] def test_send_event(self, channel: Channel, random_user: Dict): response = channel.send_event({"type": "typing.start"}, random_user["id"]) @@ -301,39 +305,45 @@ def test_channel_hide_show( ) assert len(response["channels"]) == 1 - def test_invites(self, client: StreamChat, channel: Channel): - members = ["john", "paul", "george", "pete", "ringo", "eric"] - client.upsert_users([{"id": m} for m in members]) + def test_invites(self, client: StreamChat, random_users: List[Dict]): + john = random_users[0]["id"] + ringo = random_users[1]["id"] + eric = random_users[2]["id"] + + members = [u["id"] for u in random_users] channel = client.channel( "team", "beatles-" + str(uuid.uuid4()), - {"members": members, "invites": ["ringo", "eric"]}, + {"members": members, "invites": [ringo, eric]}, ) - channel.create("john") + channel.create(john) # accept the invite when not a member with pytest.raises(StreamAPIException): - channel.accept_invite("brian") + channel.accept_invite("brian-" + str(uuid.uuid4())) # accept the invite when a member - accept = channel.accept_invite("ringo") + accept = channel.accept_invite(ringo) for m in accept["members"]: - if m["user_id"] == "ringo": + if m["user_id"] == ringo: assert m["invited"] is True assert "invite_accepted_at" in m # can accept again, noop - channel.accept_invite("ringo") + channel.accept_invite(ringo) - reject = channel.reject_invite("eric") + reject = channel.reject_invite(eric) for m in reject["members"]: - if m["user_id"] == "eric": + if m["user_id"] == eric: assert m["invited"] is True assert "invite_rejected_at" in m # cannot reject again, noop - channel.reject_invite("eric") + channel.reject_invite(eric) + channel.delete(hard=True) def test_query_members(self, client: StreamChat, channel: Channel): - members = ["paul", "george", "john", "jessica", "john2"] - client.upsert_users([{"id": m, "name": m} for m in members]) - for member in members: + rand = str(uuid.uuid4()) + user_ids = ["paul", "george", "john", "jessica", "john2"] + user_ids = [f"{n}-{rand}" for n in user_ids] + client.upsert_users([{"id": m, "name": m} for m in user_ids]) + for member in user_ids: channel.add_members([member]) response = channel.query_members( @@ -344,8 +354,10 @@ def test_query_members(self, client: StreamChat, channel: Channel): ) assert len(response) == 2 - assert response[0]["user"]["id"] == "jessica" - assert response[1]["user"]["id"] == "john2" + assert response[0]["user"]["id"] == f"jessica-{rand}" + assert response[1]["user"]["id"] == f"john2-{rand}" + + hard_delete_users(client, user_ids) def test_mute_unmute( self, client: StreamChat, channel: Channel, random_users: List[Dict] diff --git a/stream_chat/tests/test_client.py b/stream_chat/tests/test_client.py index 2df7dfd7..f28d593c 100644 --- a/stream_chat/tests/test_client.py +++ b/stream_chat/tests/test_client.py @@ -15,6 +15,7 @@ from stream_chat import StreamChat from stream_chat.base.exceptions import StreamAPIException from stream_chat.channel import Channel +from stream_chat.tests.conftest import hard_delete_users from stream_chat.tests.utils import wait_for @@ -142,6 +143,8 @@ def test_update_user(self, client: StreamChat): response = client.upsert_user(user) assert "users" in response assert user["id"] in response["users"] + # clean up + hard_delete_users(client, [user["id"]]) def test_update_user_with_team(self, client: StreamChat): user = { @@ -154,28 +157,30 @@ def test_update_user_with_team(self, client: StreamChat): assert user["id"] in response["users"] assert response["users"][user["id"]]["team"] == "blue" assert response["users"][user["id"]]["teams_role"]["blue"] == "admin" + # clean up + hard_delete_users(client, [user["id"]]) def test_update_users(self, client: StreamChat): user = {"id": str(uuid.uuid4())} response = client.upsert_users([user]) assert "users" in response assert user["id"] in response["users"] + # clean up + hard_delete_users(client, [user["id"]]) - def test_update_user_partial(self, client: StreamChat): - user_id = str(uuid.uuid4()) - client.upsert_user({"id": user_id, "field": "value"}) - + def test_update_user_partial(self, client: StreamChat, random_user: Dict): response = client.update_user_partial( - {"id": user_id, "set": {"field": "updated"}} + {"id": random_user["id"], "set": {"field": "updated"}} ) assert "users" in response - assert user_id in response["users"] - assert response["users"][user_id]["field"] == "updated" + assert random_user["id"] in response["users"] + assert response["users"][random_user["id"]]["field"] == "updated" - def test_update_user_partial_with_team(self, client: StreamChat): - user_id = str(uuid.uuid4()) - client.upsert_user({"id": user_id, "name": "Test User"}) + def test_update_user_partial_with_team(self, client: StreamChat, random_user: Dict): + user_id = random_user["id"] + # add user to the team + client.update_user_partial({"id": user_id, "set": {"teams": ["blue"]}}) response = client.update_user_partial( {"id": user_id, "set": {"team": "blue", "teams_role": {"blue": "admin"}}} @@ -426,23 +431,20 @@ def test_update_message_partial( assert response["message"]["awesome"] is True def test_update_message_restricted_visibility( - self, client: StreamChat, channel: Channel, random_user: Dict + self, + client: StreamChat, + channel: Channel, + random_users: List[Dict], ): - # Create test users first - restricted_users = [ - {"id": "amy", "name": "Amy"}, - {"id": "paul", "name": "Paul"}, - ] - client.upsert_users(restricted_users) - + amy = random_users[0]["id"] + paul = random_users[1]["id"] + user = random_users[2]["id"] # Add users to channel - channel.add_members([u["id"] for u in restricted_users]) + channel.add_members([amy, paul]) # Send initial message msg_id = str(uuid.uuid4()) - response = channel.send_message( - {"id": msg_id, "text": "hello world"}, random_user["id"] - ) + response = channel.send_message({"id": msg_id, "text": "hello world"}, user) assert response["message"]["text"] == "hello world" # Update message with restricted visibility @@ -450,38 +452,35 @@ def test_update_message_restricted_visibility( { "id": msg_id, "text": "helloworld", - "restricted_visibility": ["amy", "paul"], + "restricted_visibility": [amy, paul], "user": {"id": response["message"]["user"]["id"]}, } ) assert response["message"]["text"] == "helloworld" - assert response["message"]["restricted_visibility"] == ["amy", "paul"] + assert response["message"]["restricted_visibility"] == [amy, paul] def test_update_message_partial_restricted_visibility( - self, client: StreamChat, channel: Channel, random_user: Dict + self, + client: StreamChat, + channel: Channel, + random_users: List[Dict], ): - # Create test users first - restricted_users = [ - {"id": "amy", "name": "Amy"}, - {"id": "paul", "name": "Paul"}, - ] - client.upsert_users(restricted_users) - + amy = random_users[0]["id"] + paul = random_users[1]["id"] + user = random_users[2]["id"] # Add users to channel - channel.add_members([u["id"] for u in restricted_users]) + channel.add_members([amy, paul]) msg_id = str(uuid.uuid4()) - response = channel.send_message( - {"id": msg_id, "text": "hello world"}, random_user["id"] - ) + response = channel.send_message({"id": msg_id, "text": "hello world"}, user) assert response["message"]["text"] == "hello world" response = client.update_message_partial( msg_id, - dict(set=dict(text="helloworld", restricted_visibility=["amy"])), - random_user["id"], + dict(set=dict(text="helloworld", restricted_visibility=[amy])), + user, ) - assert response["message"]["restricted_visibility"] == ["amy"] + assert response["message"]["restricted_visibility"] == [amy] def test_delete_message(self, client: StreamChat, channel, random_user: Dict): msg_id = str(uuid.uuid4()) @@ -605,6 +604,7 @@ def test_search(self, client: StreamChat, channel, random_user: Dict): channel.send_message( {"text": "Does 'cious' count as one or two?"}, random_user["id"] ) + time.sleep(1) # wait for the message to be indexed in elasticsearch response = client.search( {"type": "messaging"}, query, **{"limit": 2, "offset": 0} ) @@ -628,6 +628,7 @@ def test_search_message_filters( channel.send_message( {"text": "Does 'cious' count as one or two?"}, random_user["id"] ) + time.sleep(1) # wait for the message to be indexed in elasticsearch response = client.search( {"type": "messaging"}, {"text": {"$q": query}}, @@ -710,14 +711,18 @@ def test_check_sns(self, client: StreamChat): assert response["status"] == "error" assert "publishing the message failed." in response["error"] - def test_guest_user(self, client: StreamChat, random_user: Dict): + def test_guest_user(self, client: StreamChat): try: - response = client.set_guest_user({"user": {"id": str(uuid.uuid4())}}) + user_id = str(uuid.uuid4()) + response = client.set_guest_user({"user": {"id": user_id}}) assert "access_token" in response except StreamAPIException: # Guest user isn't turned on for every test app pass + # clean up + hard_delete_users(client, [user_id]) + def test_run_message_actions( self, client: StreamChat, channel: Channel, random_user: Dict ): @@ -891,11 +896,13 @@ def test_imports_end2end(self, client: StreamChat): list_resp = client.list_imports({"limit": 1}) assert len(list_resp["import_tasks"]) == 1 - def test_unread_counts(self, client: StreamChat, channel, random_user: Dict): - channel.add_members([random_user["id"]]) + def test_unread_counts(self, client: StreamChat, channel, random_users: List[Dict]): + user1 = random_users[0]["id"] + user2 = random_users[1]["id"] + channel.add_members([user1]) msg_id = str(uuid.uuid4()) - channel.send_message({"id": msg_id, "text": "helloworld"}, str(uuid.uuid4())) - response = client.unread_counts(random_user["id"]) + channel.send_message({"id": msg_id, "text": "helloworld"}, user2) + response = client.unread_counts(user1) assert "total_unread_count" in response assert "channels" in response assert "channel_type" in response @@ -905,26 +912,26 @@ def test_unread_counts(self, client: StreamChat, channel, random_user: Dict): assert len(response["channel_type"]) == 1 # test threads unread counts - channel.send_message( - {"parent_id": msg_id, "text": "helloworld"}, random_user["id"] - ) - channel.send_message( - {"parent_id": msg_id, "text": "helloworld"}, str(uuid.uuid4()) - ) - response = client.unread_counts(random_user["id"]) + channel.send_message({"parent_id": msg_id, "text": "helloworld"}, user1) + channel.send_message({"parent_id": msg_id, "text": "helloworld"}, user2) + response = client.unread_counts(user1) assert "total_unread_threads_count" in response assert "threads" in response assert response["total_unread_threads_count"] == 1 assert len(response["threads"]) == 1 assert response["threads"][0]["parent_message_id"] == msg_id - def test_unread_counts_batch(self, client: StreamChat, channel, random_users: Dict): - channel.add_members([x["id"] for x in random_users]) + def test_unread_counts_batch( + self, client: StreamChat, channel, random_users: List[Dict] + ): + user1 = random_users[0]["id"] + members = [x["id"] for x in random_users[1:]] + channel.add_members(members) msg_id = str(uuid.uuid4()) - channel.send_message({"id": msg_id, "text": "helloworld"}, str(uuid.uuid4())) - response = client.unread_counts_batch([x["id"] for x in random_users]) + channel.send_message({"id": msg_id, "text": "helloworld"}, user1) + response = client.unread_counts_batch(members) assert "counts_by_user" in response - for user_id in [x["id"] for x in random_users]: + for user_id in members: assert user_id in response["counts_by_user"] assert response["counts_by_user"][user_id]["total_unread_count"] == 1 @@ -932,11 +939,9 @@ def test_unread_counts_batch(self, client: StreamChat, channel, random_users: Di channel.send_message({"parent_id": msg_id, "text": "helloworld"}, user_id) # test threads unread counts - channel.send_message( - {"parent_id": msg_id, "text": "helloworld"}, str(uuid.uuid4()) - ) - response = client.unread_counts_batch([x["id"] for x in random_users]) - for user_id in [x["id"] for x in random_users]: + channel.send_message({"parent_id": msg_id, "text": "helloworld"}, user1) + response = client.unread_counts_batch(members) + for user_id in members: assert user_id in response["counts_by_user"] assert ( response["counts_by_user"][user_id]["total_unread_threads_count"] == 1