def test_get_rate_limits(self, client: StreamChat): response = client.get_rate_limits() assert "server_side" in response assert "android" in response assert "ios" in response assert "web" in response response = client.get_rate_limits(server_side=True, android=True) assert "server_side" in response assert "android" in response assert "ios" not in response assert "web" not in response response = client.get_rate_limits( server_side=True, android=True, endpoints=["GetRateLimits", "SendMessage"] ) assert "server_side" in response assert "android" in response assert "ios" not in response assert "web" not in response assert len(response["android"]) == 2 assert len(response["server_side"]) == 2 assert ( response["android"]["GetRateLimits"]["limit"] == response["android"]["GetRateLimits"]["remaining"] ) assert ( response["server_side"]["GetRateLimits"]["limit"] > response["server_side"]["GetRateLimits"]["remaining"] )
def test_export_channel_status(self, client: StreamChat, channel: Channel): with pytest.raises(StreamAPIException, match=r".*Can't find task.*"): client.get_export_channel_status(str(uuid.uuid4())) with pytest.raises(StreamAPIException, match=r".*Can't find channel.*"): client.export_channel("messaging", str(uuid.uuid4()))
def fellowship_of_the_ring(client: StreamChat): members: List[Dict] = [ { "id": "frodo-baggins", "name": "Frodo Baggins", "race": "Hobbit", "age": 50 }, { "id": "sam-gamgee", "name": "Samwise Gamgee", "race": "Hobbit", "age": 38 }, { "id": "gandalf", "name": "Gandalf the Grey", "race": "Istari" }, { "id": "legolas", "name": "Legolas", "race": "Elf", "age": 500 }, { "id": "gimli", "name": "Gimli", "race": "Dwarf", "age": 139 }, { "id": "aragorn", "name": "Aragorn", "race": "Man", "age": 87 }, { "id": "boromir", "name": "Boromir", "race": "Man", "age": 40 }, { "id": "meriadoc-brandybuck", "name": "Meriadoc Brandybuck", "race": "Hobbit", "age": 36, }, { "id": "peregrin-took", "name": "Peregrin Took", "race": "Hobbit", "age": 28 }, ] client.update_users(members) channel = client.channel("team", "fellowship-of-the-ring", {"members": [m["id"] for m in members]}) channel.create("gandalf")
def command(client: StreamChat): response = client.create_command( dict(name=str(uuid.uuid4()), description="My command")) yield response["command"] client.delete_command(response["command"]["name"])
def test_mute_user(self, client: StreamChat, random_users: List[Dict]): response = client.mute_user(random_users[0]["id"], random_users[1]["id"]) assert "mute" in response assert "expires" not in response["mute"] assert response["mute"]["target"]["id"] == random_users[0]["id"] assert response["mute"]["user"]["id"] == random_users[1]["id"] client.unmute_user(random_users[0]["id"], random_users[1]["id"])
def test_search_with_sort(self, client: StreamChat, channel, random_user: Dict): text = str(uuid.uuid4()) ids = ["0" + text, "1" + text] channel.send_message( {"text": text, "id": ids[0]}, random_user["id"], ) channel.send_message( {"text": text, "id": ids[1]}, random_user["id"], ) response = client.search( {"type": "messaging"}, text, **{"limit": 1, "sort": [{"created_at": -1}]} ) # searches all channels so make sure at least one is found assert len(response["results"]) >= 1 assert response["next"] is not None assert ids[1] == response["results"][0]["message"]["id"] response = client.search( {"type": "messaging"}, text, **{"limit": 1, "next": response["next"]} ) assert len(response["results"]) >= 1 assert response["previous"] is not None assert response["next"] is None assert ids[0] == response["results"][0]["message"]["id"]
def test_review_flag_report( self, client: StreamChat, channel, random_user, server_user: Dict ): msg = {"id": str(uuid.uuid4()), "text": "hello world"} channel.send_message(msg, random_user["id"]) client.flag_message(msg["id"], user_id=server_user["id"]) try: wait_for( lambda: client._query_flag_reports(message_id=msg["id"]), timeout=10 ) except Exception: # The backend is sometimes unstable ¯\_(ツ)_/¯ return response = client._query_flag_reports(message_id=msg["id"]) response = client._review_flag_report( report_id=response["flag_reports"][0]["id"], review_result="reviewed", user_id=random_user["id"], custom="reason_a", ) report = response["flag_report"] assert report["id"] is not None assert report["message"]["id"] == msg["id"] assert report["message"]["text"] == msg["text"] assert report["review_result"] == "reviewed" assert report["review_details"]["custom"] == "reason_a"
def test_unflag_message( self, client: StreamChat, channel, random_user, server_user: Dict ): msg_id = str(uuid.uuid4()) channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"]) client.flag_message(msg_id, user_id=server_user["id"]) client.unflag_message(msg_id, user_id=server_user["id"])
def test_delete_message(self, client: StreamChat, channel, random_user: Dict): msg_id = str(uuid.uuid4()) channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"]) client.delete_message(msg_id) msg_id = str(uuid.uuid4()) channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"]) client.delete_message(msg_id, hard=True)
def test_invites(self, client: StreamChat, channel: Channel): members = ["john", "paul", "george", "pete", "ringo", "eric"] client.update_users([{"id": m} for m in members]) channel = client.channel( "team", "beatles-" + str(uuid.uuid4()), { "members": members, "invites": ["ringo", "eric"] }, ) channel.create("john") # accept the invite when not a member with pytest.raises(StreamAPIException): channel.accept_invite("brian") # accept the invite when a member accept = channel.accept_invite("ringo") for m in accept["members"]: if m["user_id"] == "ringo": assert m["invited"] is True assert "invite_accepted_at" in m # can accept again, noop channel.accept_invite("ringo") reject = channel.reject_invite("eric") for m in reject["members"]: if m["user_id"] == "eric": assert m["invited"] is True assert "invite_rejected_at" in m # cannot reject again, noop channel.reject_invite("eric")
def test_shadow_ban( self, client: StreamChat, random_user, server_user, channel: Channel ): msg_id = str(uuid.uuid4()) response = channel.send_message( {"id": msg_id, "text": "hello world"}, random_user["id"] ) response = client.get_message(msg_id) assert not response["message"]["shadowed"] response = client.shadow_ban(random_user["id"], user_id=server_user["id"]) msg_id = str(uuid.uuid4()) response = channel.send_message( {"id": msg_id, "text": "hello world"}, random_user["id"] ) response = client.get_message(msg_id) assert response["message"]["shadowed"] response = client.remove_shadow_ban( random_user["id"], user_id=server_user["id"] ) msg_id = str(uuid.uuid4()) response = channel.send_message( {"id": msg_id, "text": "hello world"}, random_user["id"] ) response = client.get_message(msg_id) assert not response["message"]["shadowed"]
def test_reactivate_user(self, client: StreamChat, random_user: Dict): response = client.deactivate_user(random_user["id"]) assert "user" in response assert random_user["id"] == response["user"]["id"] response = client.reactivate_user(random_user["id"]) assert "user" in response assert random_user["id"] == response["user"]["id"]
def test_mute_unmute(self, client: StreamChat, channel: Channel, random_users: List[Dict]): user_id = random_users[0]["id"] response = channel.mute(user_id, expiration=30000) assert "channel_mute" in response assert "expires" in response["channel_mute"] assert response["channel_mute"]["channel"]["cid"] == channel.cid assert response["channel_mute"]["user"]["id"] == user_id response = client.query_channels({ "muted": True, "cid": channel.cid }, user_id=user_id) assert len(response["channels"]) == 1 channel.unmute(user_id) response = client.query_channels( { "muted": True, "cid": channel.cid }, user_id=user_id, ) assert len(response["channels"]) == 0
def test_search_offset_with_sort(self, client: StreamChat): query = "supercalifragilisticexpialidocious" with pytest.raises(ValueError): client.search( {"type": "messaging"}, query, **{"limit": 2, "offset": 1, "sort": [{"created_at": -1}]}, )
def test_get_message(self, client: StreamChat, channel, random_user: Dict): msg_id = str(uuid.uuid4()) channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"]) client.delete_message(msg_id) msg_id = str(uuid.uuid4()) channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"]) message = client.get_message(msg_id) assert message["message"]["id"] == msg_id
def test_query_banned_user( self, client: StreamChat, random_user, server_user: Dict ): client.ban_user(random_user["id"], user_id=server_user["id"], reason="because") resp = client.query_banned_users( {"filter_conditions": {"reason": "because"}, "limit": 1} ) assert len(resp["bans"]) == 1
def get_stream_token(self, obj): """ docstring """ client = StreamChat(api_key=settings.STREAM_API_KEY, api_secret=settings.STREAM_API_SECRET) token = client.create_token(obj.user.id) return token
def handle(self, *args, **options): client = StreamChat(api_key=settings.STREAM_API_KEY, api_secret=settings.STREAM_API_SECRET) client.update_user({"id": "0", "name": "The System"}) channel = client.channel("messaging", settings.CHANNEL_NAME) channel.create(settings.CHANNEL_NAME) response = channel.send_message({"text": options['message']}, 'system') self.stdout.write( self.style.SUCCESS('Successfully posted a message with id "%s"' % response['message']['id']))
def test_query_message_flags( self, client: StreamChat, channel, random_user, server_user: Dict ): msg_id = str(uuid.uuid4()) channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"]) client.flag_message(msg_id, user_id=server_user["id"]) response = client.query_message_flags({"channel_cid": channel.cid}) assert len(response["flags"]) == 1 response = client.query_message_flags({"user_id": {"$in": [random_user["id"]]}}) assert len(response["flags"]) == 1
def test_update_user_partial(self, client: StreamChat): user_id = str(uuid.uuid4()) client.update_user({"id": user_id, "field": "value"}) response = client.update_user_partial( {"id": user_id, "set": {"field": "updated"}} ) assert "users" in response assert user_id in response["users"] assert response["users"][user_id]["field"] == "updated"
def __init__(self, api_key: Text, api_secret: Text, channel_id: Text) -> None: self.bot = { "id": "rubi", "name": "rubi", "image": "/images/aichatbot.svg", "role": "admin" } chat = StreamChat(api_key=api_key, api_secret=api_secret) chat.update_user(self.bot) self.channel = chat.channel("messaging", channel_id)
def handle(self, *args, **options): client = StreamChat(api_key=settings.STREAM_API_KEY, api_secret=settings.STREAM_API_SECRET) client.update_user({'id': 'system', 'name': 'The server'}) channel = client.channel('messaging', 'kung-fu') channel.create('system') response = channel.send_message({'text': 'AMA about kung-fu'}, 'system') self.stdout.write( self.style.SUCCESS('Successfully posted a message with id "%s"' % respnse['message']['id']))
def test_run_message_actions( self, client: StreamChat, channel: Channel, random_user: Dict ): msg = {"id": str(uuid.uuid4()), "text": "/giphy wave"} channel.send_message(msg, random_user["id"]) client.run_message_action( msg["id"], { "user": {"id": random_user["id"]}, "form_data": {"image_action": "shuffle"}, }, )
def test_pin_unpin_message(self, client: StreamChat, channel, random_user: Dict): msg_id = str(uuid.uuid4()) response = channel.send_message( {"id": msg_id, "text": "hello world"}, random_user["id"] ) assert response["message"]["text"] == "hello world" response = client.pin_message(msg_id, random_user["id"]) assert response["message"]["pinned_at"] is not None assert response["message"]["pinned_by"]["id"] == random_user["id"] response = client.unpin_message(msg_id, random_user["id"]) assert response["message"]["pinned_at"] is None assert response["message"]["pinned_by"] is None
def test_update_message(self, client: StreamChat, channel, random_user: Dict): msg_id = str(uuid.uuid4()) response = channel.send_message( {"id": msg_id, "text": "hello world"}, random_user["id"] ) assert response["message"]["text"] == "hello world" client.update_message( { "id": msg_id, "awesome": True, "text": "helloworld", "user": {"id": response["message"]["user"]["id"]}, } )
def test_delete_channels(self, client: StreamChat, channel: Channel): response = client.delete_channels([channel.cid]) assert "task_id" in response for _ in range(10): response = client.get_task(response["task_id"]) if response["status"] == "completed" and response["result"][ channel.cid ] == {"status": "ok"}: return time.sleep(1) pytest.fail("task did not succeed")
def test_delete_users(self, client: StreamChat, random_user: Dict): response = client.delete_users( [random_user["id"]], "hard", conversations="hard", messages="hard" ) assert "task_id" in response for _ in range(10): response = client.get_task(response["task_id"]) if response["status"] == "completed" and response["result"][ random_user["id"] ] == {"status": "ok"}: return time.sleep(1) pytest.fail("task did not succeed")
def test_query_channels_members_in( self, client: StreamChat, fellowship_of_the_ring ): response = client.query_channels({"members": {"$in": ["gimli"]}}, {"id": 1}) assert len(response["channels"]) == 1 assert response["channels"][0]["channel"]["id"] == "fellowship-of-the-ring" assert len(response["channels"][0]["members"]) == 9
def channel(client: StreamChat, random_user: Dict): channel = client.channel("messaging", str(uuid.uuid4()), { "test": True, "language": "python" }) channel.create(random_user["id"]) return channel
def test_guest_user(self, client: StreamChat, random_user: Dict): try: response = client.set_guest_user({"user": {"id": str(uuid.uuid4())}}) assert "access_token" in response except StreamAPIException: # Guest user isn't turned on for every test app pass