Beispiel #1
0
class TestMsgInfoView:
    @pytest.fixture(autouse=True)
    def mock_external_classes(self, mocker, monkeypatch, message_fixture):
        self.controller = mocker.Mock()
        mocker.patch.object(self.controller,
                            "maximum_popup_dimensions",
                            return_value=(64, 64))
        mocker.patch(VIEWS + ".urwid.SimpleFocusListWalker", return_value=[])
        # The subsequent patches (index and initial_data) set
        # show_edit_history_label to False for this autoused fixture.
        self.controller.model.index = {"edited_messages": set()}
        self.controller.model.initial_data = {
            "realm_allow_edit_history": False,
        }
        self.controller.model.formatted_local_time.side_effect = [
            "Tue Mar 13 10:55:22",
            "Tue Mar 13 10:55:37",
        ]
        self.msg_info_view = MsgInfoView(
            self.controller,
            message_fixture,
            "Message Information",
            OrderedDict(),
            OrderedDict(),
            list(),
        )

    def test_init(self, message_fixture):
        assert self.msg_info_view.msg == message_fixture
        assert self.msg_info_view.topic_links == OrderedDict()
        assert self.msg_info_view.message_links == OrderedDict()
        assert self.msg_info_view.time_mentions == list()

    def test_keypress_any_key(self, widget_size):
        key = "a"
        size = widget_size(self.msg_info_view)
        self.msg_info_view.keypress(size, key)
        assert not self.controller.exit_popup.called

    @pytest.mark.parametrize("key", keys_for_command("EDIT_HISTORY"))
    @pytest.mark.parametrize("realm_allow_edit_history", [True, False])
    @pytest.mark.parametrize(
        "edited_message_id",
        [
            537286,
            537287,
            537288,
        ],
        ids=[
            "stream_message_id",
            "pm_message_id",
            "group_pm_message_id",
        ],
    )
    def test_keypress_edit_history(
        self,
        message_fixture,
        key,
        widget_size,
        realm_allow_edit_history,
        edited_message_id,
    ):
        self.controller.model.index = {
            "edited_messages": set([edited_message_id]),
        }
        self.controller.model.initial_data = {
            "realm_allow_edit_history": realm_allow_edit_history,
        }
        msg_info_view = MsgInfoView(
            self.controller,
            message_fixture,
            title="Message Information",
            topic_links=OrderedDict(),
            message_links=OrderedDict(),
            time_mentions=list(),
        )
        size = widget_size(msg_info_view)

        msg_info_view.keypress(size, key)

        if msg_info_view.show_edit_history_label:
            self.controller.show_edit_history.assert_called_once_with(
                message=message_fixture,
                topic_links=OrderedDict(),
                message_links=OrderedDict(),
                time_mentions=list(),
            )
        else:
            self.controller.show_edit_history.assert_not_called()

    @pytest.mark.parametrize(
        "key", {*keys_for_command("GO_BACK"), *keys_for_command("MSG_INFO")})
    def test_keypress_exit_popup(self, key, widget_size):
        size = widget_size(self.msg_info_view)
        self.msg_info_view.keypress(size, key)
        assert self.controller.exit_popup.called

    def test_height_noreactions(self):
        expected_height = 3
        assert self.msg_info_view.height == expected_height

    # FIXME This is the same parametrize as MessageBox:test_reactions_view
    @pytest.mark.parametrize(
        "to_vary_in_each_message",
        [{
            "reactions": [
                {
                    "emoji_name": "thumbs_up",
                    "emoji_code": "1f44d",
                    "user": {
                        "email": "*****@*****.**",
                        "full_name": "Iago",
                        "id": 5,
                    },
                    "reaction_type": "unicode_emoji",
                },
                {
                    "emoji_name": "zulip",
                    "emoji_code": "zulip",
                    "user": {
                        "email": "*****@*****.**",
                        "full_name": "Iago",
                        "id": 5,
                    },
                    "reaction_type": "zulip_extra_emoji",
                },
                {
                    "emoji_name": "zulip",
                    "emoji_code": "zulip",
                    "user": {
                        "email": "*****@*****.**",
                        "full_name": "aaron",
                        "id": 1,
                    },
                    "reaction_type": "zulip_extra_emoji",
                },
                {
                    "emoji_name": "heart",
                    "emoji_code": "2764",
                    "user": {
                        "email": "*****@*****.**",
                        "full_name": "Iago",
                        "id": 5,
                    },
                    "reaction_type": "unicode_emoji",
                },
            ]
        }],
    )
    def test_height_reactions(self, message_fixture, to_vary_in_each_message):
        varied_message = dict(message_fixture, **to_vary_in_each_message)
        self.msg_info_view = MsgInfoView(
            self.controller,
            varied_message,
            "Message Information",
            OrderedDict(),
            OrderedDict(),
            list(),
        )
        # 9 = 3 labels + 1 blank line + 1 'Reactions' (category) + 4 reactions.
        expected_height = 9
        assert self.msg_info_view.height == expected_height

    @pytest.mark.parametrize(
        [
            "initial_link",
            "expected_text",
            "expected_attr_map",
            "expected_focus_map",
            "expected_link_width",
        ],
        [
            (
                OrderedDict([("https://bar.com", ("Foo", 1, True))]),
                "1: Foo\nhttps://bar.com",
                {
                    None: "popup_contrast"
                },
                {
                    None: "selected"
                },
                15,
            ),
            (
                OrderedDict([("https://foo.com", ("", 1, True))]),
                "1: https://foo.com",
                {
                    None: "popup_contrast"
                },
                {
                    None: "selected"
                },
                18,
            ),
        ],
        ids=[
            "link_with_link_text",
            "link_without_link_text",
        ],
    )
    def test_create_link_buttons(
        self,
        initial_link,
        expected_text,
        expected_attr_map,
        expected_focus_map,
        expected_link_width,
    ):
        [link_w], link_width = self.msg_info_view.create_link_buttons(
            self.controller, initial_link)

        assert [link_w.link] == list(initial_link)
        assert link_w._wrapped_widget.original_widget.text == expected_text
        assert link_w._wrapped_widget.focus_map == expected_focus_map
        assert link_w._wrapped_widget.attr_map == expected_attr_map
        assert link_width == expected_link_width

    def test_keypress_navigation(self, mocker, widget_size,
                                 navigation_key_expected_key_pair):
        key, expected_key = navigation_key_expected_key_pair
        size = widget_size(self.msg_info_view)
        super_keypress = mocker.patch(VIEWS + ".urwid.ListBox.keypress")
        self.msg_info_view.keypress(size, key)
        super_keypress.assert_called_once_with(size, expected_key)
Beispiel #2
0
class TestMsgInfoView:
    @pytest.fixture(autouse=True)
    def mock_external_classes(self, mocker, monkeypatch, message_fixture):
        self.controller = mocker.Mock()
        mocker.patch.object(self.controller,
                            'maximum_popup_dimensions',
                            return_value=(64, 64))
        mocker.patch(VIEWS + ".urwid.SimpleFocusListWalker", return_value=[])
        # The subsequent patches (index and initial_data) set
        # show_edit_history_label to False for this autoused fixture.
        self.controller.model.index = {'edited_messages': set()}
        self.controller.model.initial_data = {
            'realm_allow_edit_history': False,
        }
        self.controller.model.formatted_local_time.side_effect = [
            "Tue Mar 13 10:55:22",
            "Tue Mar 13 10:55:37",
        ]
        self.msg_info_view = MsgInfoView(self.controller, message_fixture,
                                         'Message Information', OrderedDict(),
                                         OrderedDict(), list())

    def test_init(self, message_fixture):
        assert self.msg_info_view.msg == message_fixture
        assert self.msg_info_view.topic_links == OrderedDict()
        assert self.msg_info_view.message_links == OrderedDict()
        assert self.msg_info_view.time_mentions == list()

    def test_keypress_any_key(self, widget_size):
        key = "a"
        size = widget_size(self.msg_info_view)
        self.msg_info_view.keypress(size, key)
        assert not self.controller.exit_popup.called

    @pytest.mark.parametrize('key', keys_for_command('EDIT_HISTORY'))
    @pytest.mark.parametrize('realm_allow_edit_history', [True, False])
    @pytest.mark.parametrize('edited_message_id', [
        537286,
        537287,
        537288,
    ],
                             ids=[
                                 'stream_message_id',
                                 'pm_message_id',
                                 'group_pm_message_id',
                             ])
    def test_keypress_edit_history(self, message_fixture, key, widget_size,
                                   realm_allow_edit_history,
                                   edited_message_id):
        self.controller.model.index = {
            'edited_messages': set([edited_message_id]),
        }
        self.controller.model.initial_data = {
            'realm_allow_edit_history': realm_allow_edit_history,
        }
        msg_info_view = MsgInfoView(self.controller,
                                    message_fixture,
                                    title='Message Information',
                                    topic_links=OrderedDict(),
                                    message_links=OrderedDict(),
                                    time_mentions=list())
        size = widget_size(msg_info_view)

        msg_info_view.keypress(size, key)

        if msg_info_view.show_edit_history_label:
            self.controller.show_edit_history.assert_called_once_with(
                message=message_fixture,
                topic_links=OrderedDict(),
                message_links=OrderedDict(),
                time_mentions=list(),
            )
        else:
            self.controller.show_edit_history.assert_not_called()

    @pytest.mark.parametrize(
        'key', {*keys_for_command('GO_BACK'), *keys_for_command('MSG_INFO')})
    def test_keypress_exit_popup(self, key, widget_size):
        size = widget_size(self.msg_info_view)
        self.msg_info_view.keypress(size, key)
        assert self.controller.exit_popup.called

    def test_height_noreactions(self):
        expected_height = 3
        assert self.msg_info_view.height == expected_height

    # FIXME This is the same parametrize as MessageBox:test_reactions_view
    @pytest.mark.parametrize('to_vary_in_each_message', [{
        'reactions': [{
            'emoji_name': 'thumbs_up',
            'emoji_code': '1f44d',
            'user': {
                'email': '*****@*****.**',
                'full_name': 'Iago',
                'id': 5,
            },
            'reaction_type': 'unicode_emoji'
        }, {
            'emoji_name': 'zulip',
            'emoji_code': 'zulip',
            'user': {
                'email': '*****@*****.**',
                'full_name': 'Iago',
                'id': 5,
            },
            'reaction_type': 'zulip_extra_emoji'
        }, {
            'emoji_name': 'zulip',
            'emoji_code': 'zulip',
            'user': {
                'email': '*****@*****.**',
                'full_name': 'aaron',
                'id': 1,
            },
            'reaction_type': 'zulip_extra_emoji'
        }, {
            'emoji_name': 'heart',
            'emoji_code': '2764',
            'user': {
                'email': '*****@*****.**',
                'full_name': 'Iago',
                'id': 5,
            },
            'reaction_type': 'unicode_emoji'
        }]
    }])
    def test_height_reactions(self, message_fixture, to_vary_in_each_message):
        varied_message = dict(message_fixture, **to_vary_in_each_message)
        self.msg_info_view = MsgInfoView(self.controller, varied_message,
                                         'Message Information', OrderedDict(),
                                         OrderedDict(), list())
        # 9 = 3 labels + 1 blank line + 1 'Reactions' (category) + 4 reactions.
        expected_height = 9
        assert self.msg_info_view.height == expected_height

    @pytest.mark.parametrize([
        'initial_link', 'expected_text', 'expected_attr_map',
        'expected_focus_map', 'expected_link_width'
    ], [(
        OrderedDict([('https://bar.com', ('Foo', 1, True))]),
        '1: Foo\nhttps://bar.com',
        {
            None: 'popup_contrast'
        },
        {
            None: 'selected'
        },
        15,
    ),
        (
            OrderedDict([('https://foo.com', ('', 1, True))]),
            '1: https://foo.com',
            {
                None: 'popup_contrast'
            },
            {
                None: 'selected'
            },
            18,
        )],
                             ids=[
                                 'link_with_link_text',
                                 'link_without_link_text',
                             ])
    def test_create_link_buttons(self, initial_link, expected_text,
                                 expected_attr_map, expected_focus_map,
                                 expected_link_width):
        [link_w], link_width = self.msg_info_view.create_link_buttons(
            self.controller,
            initial_link,
        )

        assert [link_w.link] == list(initial_link)
        assert (link_w._wrapped_widget.original_widget.text == expected_text)
        assert (link_w._wrapped_widget.focus_map == expected_focus_map)
        assert (link_w._wrapped_widget.attr_map == expected_attr_map)
        assert link_width == expected_link_width

    def test_keypress_navigation(self, mocker, widget_size,
                                 navigation_key_expected_key_pair):
        key, expected_key = navigation_key_expected_key_pair
        size = widget_size(self.msg_info_view)
        super_keypress = mocker.patch(VIEWS + '.urwid.ListBox.keypress')
        self.msg_info_view.keypress(size, key)
        super_keypress.assert_called_once_with(size, expected_key)