|
1 | 1 | from collections import OrderedDict
|
| 2 | +from pathlib import Path |
2 | 3 | from typing import Any, Callable, Dict, List, Optional, Tuple
|
| 4 | +from unittest.mock import MagicMock |
3 | 5 |
|
4 | 6 | import pytest
|
5 | 7 | from pytest import param as case
|
6 | 8 | from pytest_mock import MockerFixture
|
7 | 9 | from urwid import Columns, Pile, Text, Widget
|
8 | 10 |
|
| 11 | +from tests.ui_tools.test_boxes import TestWriteBox |
9 | 12 | from zulipterminal.api_types import Message
|
10 | 13 | from zulipterminal.config.keys import is_command_key, keys_for_command
|
11 | 14 | from zulipterminal.config.ui_mappings import EDIT_MODE_CAPTIONS
|
12 | 15 | from zulipterminal.helper import CustomProfileData, TidiedUserInfo
|
| 16 | +from zulipterminal.ui_tools.boxes import WriteBox |
13 | 17 | from zulipterminal.ui_tools.messages import MessageBox
|
14 | 18 | from zulipterminal.ui_tools.views import (
|
15 | 19 | AboutView,
|
16 | 20 | EditHistoryTag,
|
17 | 21 | EditHistoryView,
|
18 | 22 | EditModeView,
|
19 | 23 | EmojiPickerView,
|
| 24 | + FileUploadView, |
20 | 25 | FullRawMsgView,
|
21 | 26 | FullRenderedMsgView,
|
22 | 27 | HelpView,
|
@@ -872,6 +877,90 @@ def test_keypress_exit_popup(
|
872 | 877 | assert self.controller.exit_popup.called
|
873 | 878 |
|
874 | 879 |
|
| 880 | +class TestFileUploadView: |
| 881 | + @pytest.fixture(scope="class") |
| 882 | + def write_box(self) -> Any: |
| 883 | + return TestWriteBox().write_box |
| 884 | + |
| 885 | + @pytest.fixture(autouse=True) |
| 886 | + def mock_external_classes(self, mocker: MockerFixture, write_box: WriteBox) -> None: |
| 887 | + self.controller = mocker.Mock() |
| 888 | + mocker.patch.object( |
| 889 | + self.controller, "maximum_popup_dimensions", return_value=(64, 64) |
| 890 | + ) |
| 891 | + mocker.patch(LISTWALKER, return_value=[]) |
| 892 | + self.file_upload_view = FileUploadView( |
| 893 | + self.controller, write_box, "Upload File" |
| 894 | + ) |
| 895 | + |
| 896 | + def test_keypress_any_key( |
| 897 | + self, widget_size: Callable[[Widget], urwid_Size] |
| 898 | + ) -> None: |
| 899 | + key = "a" |
| 900 | + size = widget_size(self.file_upload_view) |
| 901 | + self.file_upload_view.keypress(size, key) |
| 902 | + assert not self.controller.exit_popup.called |
| 903 | + |
| 904 | + @pytest.mark.parametrize("key", {*keys_for_command("GO_BACK")}) |
| 905 | + def test_keypress_exit_popup( |
| 906 | + self, key: str, widget_size: Callable[[Widget], urwid_Size] |
| 907 | + ) -> None: |
| 908 | + size = widget_size(self.file_upload_view) |
| 909 | + self.file_upload_view.keypress(size, key) |
| 910 | + assert self.controller.exit_popup.called |
| 911 | + |
| 912 | + @pytest.mark.parametrize( |
| 913 | + "file_location, expected_uri, expected_error_message", |
| 914 | + [ |
| 915 | + case( |
| 916 | + "example.txt", |
| 917 | + "http://example.txt/uploaded_file", |
| 918 | + None, |
| 919 | + id="txt_file_with_successful_uri", |
| 920 | + ), |
| 921 | + case( |
| 922 | + "example.pdf", |
| 923 | + "http://example.pdf/uploaded_file", |
| 924 | + None, |
| 925 | + id="pdf_file_with_successful_uri", |
| 926 | + ), |
| 927 | + case( |
| 928 | + "invalid.txt", |
| 929 | + "", |
| 930 | + ["ERROR: Unable to get the URI"], |
| 931 | + id="invalid_txt_file_with_error", |
| 932 | + ), |
| 933 | + case( |
| 934 | + "invalid.pdf", |
| 935 | + "", |
| 936 | + ["ERROR: Unable to get the URI"], |
| 937 | + id="invalid_pdf_file_with_error", |
| 938 | + ), |
| 939 | + ], |
| 940 | + ) |
| 941 | + def test_handle_file_upload( |
| 942 | + self, |
| 943 | + file_location: str, |
| 944 | + expected_uri: str, |
| 945 | + expected_error_message: Optional[str], |
| 946 | + ) -> None: |
| 947 | + self.file_upload_view.write_box = MagicMock() |
| 948 | + self.controller.model.get_file_upload_uri.return_value = expected_uri |
| 949 | + |
| 950 | + self.file_upload_view._handle_file_upload(file_location) |
| 951 | + |
| 952 | + self.controller.model.get_file_upload_uri.assert_called_once_with(file_location) |
| 953 | + if not expected_error_message: |
| 954 | + file_name = Path(file_location).name |
| 955 | + self.file_upload_view.write_box.append_uri_and_filename.assert_called_once_with( |
| 956 | + file_name, self.file_upload_view.uri |
| 957 | + ) |
| 958 | + else: |
| 959 | + self.controller.append_uri_and_filename.assert_not_called() |
| 960 | + self.controller.report_error.assert_called_with(expected_error_message) |
| 961 | + self.controller.exit_popup.assert_called() |
| 962 | + |
| 963 | + |
875 | 964 | class TestHelpView:
|
876 | 965 | @pytest.fixture(autouse=True)
|
877 | 966 | def mock_external_classes(self, mocker: MockerFixture) -> None:
|
|
0 commit comments