Esempio n. 1
0
    async def test_on_interaction_when_json_encode_fails(
            self, mock_interaction_server, mock_entity_factory):
        mock_interaction_server._verify = mock.Mock(return_value=True)
        mock_exception = TypeError("OK")
        mock_interaction_server._dumps = mock.Mock(side_effect=mock_exception)
        mock_entity_factory.deserialize_interaction.return_value = base_interactions.PartialInteraction(
            app=None,
            id=123,
            application_id=541324,
            type=2,
            token="ok",
            version=1)
        mock_builder = mock.Mock(build=mock.Mock())
        mock_interaction_server.set_listener(
            base_interactions.PartialInteraction,
            mock.AsyncMock(return_value=mock_builder))

        with mock.patch.object(asyncio,
                               "get_running_loop") as get_running_loop:
            result = await mock_interaction_server.on_interaction(
                b'{"type": 2}', b"signature", b"timestamp")

            get_running_loop.return_value.call_exception_handler.assert_called_once_with(
                {
                    "message":
                    "Exception occurred during interaction dispatch",
                    "exception": mock_exception
                })

        assert result.headers == {"Content-Type": "text/plain; charset=UTF-8"}
        assert result.payload == b"Exception occurred during interaction dispatch"
        assert result.status_code == 500
    async def test_on_interaction_when_response_builder_error(
        self,
        mock_interaction_server: interaction_server_impl.InteractionServer,
        mock_entity_factory: entity_factory_impl.EntityFactoryImpl,
    ):
        mock_interaction_server._public_key = mock.Mock()
        mock_exception = TypeError("OK")
        mock_entity_factory.deserialize_interaction.return_value = base_interactions.PartialInteraction(
            app=None, id=123, application_id=541324, type=2, token="ok", version=1
        )
        mock_builder = mock.Mock(build=mock.Mock(side_effect=mock_exception))
        mock_interaction_server.set_listener(
            base_interactions.PartialInteraction, mock.AsyncMock(return_value=mock_builder)
        )

        with mock.patch.object(asyncio, "get_running_loop") as get_running_loop:
            result = await mock_interaction_server.on_interaction(b'{"type": 2}', b"signature", b"timestamp")

            get_running_loop.return_value.call_exception_handler.assert_called_once_with(
                {"message": "Exception occurred during interaction dispatch", "exception": mock_exception}
            )

        assert result.content_type == "text/plain; charset=UTF-8"
        assert result.files == ()
        assert result.headers is None
        assert result.payload == b"Exception occurred during interaction dispatch"
        assert result.status_code == 500
Esempio n. 3
0
    async def test_on_interaction(self, mock_interaction_server,
                                  mock_entity_factory):
        mock_verifier = mock.Mock(return_value=True)
        mock_interaction_server._verify = mock_verifier
        mock_entity_factory.deserialize_interaction.return_value = base_interactions.PartialInteraction(
            app=None,
            id=123,
            application_id=541324,
            type=2,
            token="ok",
            version=1)
        mock_builder = mock.Mock(build=mock.Mock(
            return_value={"ok": "No boomer"}))
        mock_listener = mock.AsyncMock(return_value=mock_builder)
        mock_interaction_server.set_listener(
            base_interactions.PartialInteraction, mock_listener)

        result = await mock_interaction_server.on_interaction(
            b'{"type": 2}', b"signature", b"timestamp")

        mock_verifier.assert_called_once_with(b'{"type": 2}', b"signature",
                                              b"timestamp")
        mock_listener.assert_awaited_once_with(
            mock_entity_factory.deserialize_interaction.return_value)
        mock_builder.build.assert_called_once_with(mock_entity_factory)
        assert result.headers == {
            "Content-Type": "application/json; charset=UTF-8"
        }
        assert result.payload == b'{"ok": "No boomer"}'
        assert result.status_code == 200
    async def test_on_interaction(
        self,
        mock_interaction_server: interaction_server_impl.InteractionServer,
        mock_entity_factory: entity_factory_impl.EntityFactoryImpl,
        public_key: bytes,
        valid_edd25519: bytes,
        valid_payload: bytes,
    ):
        mock_interaction_server._public_key = nacl.signing.VerifyKey(public_key)
        mock_file_1 = mock.Mock()
        mock_file_2 = mock.Mock()
        mock_entity_factory.deserialize_interaction.return_value = base_interactions.PartialInteraction(
            app=None, id=123, application_id=541324, type=2, token="ok", version=1
        )
        mock_builder = mock.Mock(build=mock.Mock(return_value=({"ok": "No boomer"}, [mock_file_1, mock_file_2])))
        mock_listener = mock.AsyncMock(return_value=mock_builder)
        mock_interaction_server.set_listener(base_interactions.PartialInteraction, mock_listener)

        result = await mock_interaction_server.on_interaction(*valid_edd25519)

        mock_listener.assert_awaited_once_with(mock_entity_factory.deserialize_interaction.return_value)
        mock_builder.build.assert_called_once_with(mock_entity_factory)
        mock_entity_factory.deserialize_interaction.assert_called_once_with(valid_payload)
        assert result.content_type == "application/json; charset=UTF-8"
        assert result.files == [mock_file_1, mock_file_2]
        assert result.headers is None
        assert result.payload == b'{"ok": "No boomer"}'
        assert result.status_code == 200
Esempio n. 5
0
 def mock_partial_interaction(self, mock_app):
     return base_interactions.PartialInteraction(
         app=mock_app,
         id=34123,
         application_id=651231,
         type=base_interactions.InteractionType.APPLICATION_COMMAND,
         token="399393939doodsodso",
         version=3122312,
     )