|
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 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,
|
@@ -812,6 +817,90 @@ def test_keypress_exit_popup(
|
812 | 817 | assert self.controller.exit_popup.called
|
813 | 818 |
|
814 | 819 |
|
| 820 | +class TestFileUploadView: |
| 821 | + @pytest.fixture(scope="class") |
| 822 | + def write_box(self) -> Any: |
| 823 | + return TestWriteBox().write_box |
| 824 | + |
| 825 | + @pytest.fixture(autouse=True) |
| 826 | + def mock_external_classes(self, mocker: MockerFixture, write_box: WriteBox) -> None: |
| 827 | + self.controller = mocker.Mock() |
| 828 | + mocker.patch.object( |
| 829 | + self.controller, "maximum_popup_dimensions", return_value=(64, 64) |
| 830 | + ) |
| 831 | + mocker.patch(LISTWALKER, return_value=[]) |
| 832 | + self.file_upload_view = FileUploadView( |
| 833 | + self.controller, write_box, "Upload File" |
| 834 | + ) |
| 835 | + |
| 836 | + def test_keypress_any_key( |
| 837 | + self, widget_size: Callable[[Widget], urwid_Size] |
| 838 | + ) -> None: |
| 839 | + key = "a" |
| 840 | + size = widget_size(self.file_upload_view) |
| 841 | + self.file_upload_view.keypress(size, key) |
| 842 | + assert not self.controller.exit_popup.called |
| 843 | + |
| 844 | + @pytest.mark.parametrize("key", {*keys_for_command("GO_BACK")}) |
| 845 | + def test_keypress_exit_popup( |
| 846 | + self, key: str, widget_size: Callable[[Widget], urwid_Size] |
| 847 | + ) -> None: |
| 848 | + size = widget_size(self.file_upload_view) |
| 849 | + self.file_upload_view.keypress(size, key) |
| 850 | + assert self.controller.exit_popup.called |
| 851 | + |
| 852 | + @pytest.mark.parametrize( |
| 853 | + "file_location, expected_uri, expected_error_message", |
| 854 | + [ |
| 855 | + case( |
| 856 | + "example.txt", |
| 857 | + "http://example.txt/uploaded_file", |
| 858 | + None, |
| 859 | + id="txt_file_with_successful_uri", |
| 860 | + ), |
| 861 | + case( |
| 862 | + "example.pdf", |
| 863 | + "http://example.pdf/uploaded_file", |
| 864 | + None, |
| 865 | + id="pdf_file_with_unsuccessful_uri", |
| 866 | + ), |
| 867 | + case( |
| 868 | + "invalid.txt", |
| 869 | + "", |
| 870 | + ["ERROR: Unable to get the URI"], |
| 871 | + id="invalid_txt_file_with_error", |
| 872 | + ), |
| 873 | + case( |
| 874 | + "invalid.pdf", |
| 875 | + "", |
| 876 | + ["ERROR: Unable to get the URI"], |
| 877 | + id="invalid_pdf_file_with_error", |
| 878 | + ), |
| 879 | + ], |
| 880 | + ) |
| 881 | + def test_handle_file_upload( |
| 882 | + self, |
| 883 | + file_location: str, |
| 884 | + expected_uri: str, |
| 885 | + expected_error_message: Optional[str], |
| 886 | + ) -> None: |
| 887 | + self.file_upload_view.write_box = MagicMock() |
| 888 | + self.controller.model.get_file_upload_uri.return_value = expected_uri |
| 889 | + |
| 890 | + self.file_upload_view._handle_file_upload(file_location) |
| 891 | + |
| 892 | + self.controller.model.get_file_upload_uri.assert_called_once_with(file_location) |
| 893 | + if expected_uri is not None: |
| 894 | + file_name = Path(file_location).name |
| 895 | + self.file_upload_view.write_box.append_uri_and_filename.assert_called_once_with( |
| 896 | + file_name, self.file_upload_view.uri |
| 897 | + ) |
| 898 | + else: |
| 899 | + self.controller.append_uri_and_filename.assert_not_called() |
| 900 | + self.controller.report_error.assert_called_with(expected_error_message) |
| 901 | + self.controller.exit_popup.assert_called() |
| 902 | + |
| 903 | + |
815 | 904 | class TestHelpView:
|
816 | 905 | @pytest.fixture(autouse=True)
|
817 | 906 | def mock_external_classes(self, mocker: MockerFixture) -> None:
|
|
0 commit comments