Ejemplo n.º 1
0
    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"]
        )
Ejemplo n.º 2
0
    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()))
Ejemplo n.º 3
0
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")
Ejemplo n.º 4
0
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"])
Ejemplo n.º 5
0
 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"])
Ejemplo n.º 6
0
 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"]
Ejemplo n.º 7
0
    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"
Ejemplo n.º 8
0
 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"])
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
    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")
Ejemplo n.º 11
0
    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"]
Ejemplo n.º 12
0
 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"]
Ejemplo n.º 13
0
    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
Ejemplo n.º 14
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}]},
         )
Ejemplo n.º 15
0
 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
Ejemplo n.º 16
0
 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
Ejemplo n.º 17
0
    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
Ejemplo n.º 18
0
 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']))
Ejemplo n.º 19
0
 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
Ejemplo n.º 20
0
    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"
Ejemplo n.º 21
0
 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)
Ejemplo n.º 22
0
 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']))
Ejemplo n.º 23
0
 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"},
         },
     )
Ejemplo n.º 24
0
    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
Ejemplo n.º 25
0
 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"]},
         }
     )
Ejemplo n.º 26
0
    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")
Ejemplo n.º 27
0
    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")
Ejemplo n.º 28
0
 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
Ejemplo n.º 29
0
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
Ejemplo n.º 30
0
 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