|
3 | 3 | from typing import Any |
4 | 4 |
|
5 | 5 | import pytest |
| 6 | +from zulip import ZulipError |
6 | 7 |
|
7 | 8 | from zulipterminal.helper import initial_index, powerset |
8 | 9 | from zulipterminal.model import Model, ServerConnectionFailure |
@@ -67,6 +68,49 @@ def test_init(self, model, initial_data, user_profile): |
67 | 68 | self.classify_unread_counts.assert_called_once_with(model) |
68 | 69 | assert model.unread_counts == [] |
69 | 70 |
|
| 71 | + def test_init_InvalidAPIKey_response(self, mocker, initial_data): |
| 72 | + # Both network calls indicate the same response |
| 73 | + mocker.patch('zulipterminal.model.Model.get_messages', |
| 74 | + return_value='Invalid API key') |
| 75 | + mocker.patch('zulipterminal.model.Model._register_desired_events', |
| 76 | + return_value='Invalid API key') |
| 77 | + |
| 78 | + mocker.patch('zulipterminal.model.Model.get_all_users', |
| 79 | + return_value=[]) |
| 80 | + mocker.patch('zulipterminal.model.Model.' |
| 81 | + '_stream_info_from_subscriptions', |
| 82 | + return_value=({}, set(), [], [])) |
| 83 | + self.classify_unread_counts = mocker.patch( |
| 84 | + 'zulipterminal.model.classify_unread_counts', |
| 85 | + return_value=[]) |
| 86 | + |
| 87 | + with pytest.raises(ServerConnectionFailure) as e: |
| 88 | + model = Model(self.controller) |
| 89 | + |
| 90 | + assert str(e.value) == 'Invalid API key (get_messages, register)' |
| 91 | + |
| 92 | + def test_init_ZulipError_exception(self, mocker, initial_data, |
| 93 | + exception_text="X"): |
| 94 | + # Both network calls fail, resulting in exceptions |
| 95 | + mocker.patch('zulipterminal.model.Model.get_messages', |
| 96 | + side_effect=ZulipError(exception_text)) |
| 97 | + mocker.patch('zulipterminal.model.Model._register_desired_events', |
| 98 | + side_effect=ZulipError(exception_text)) |
| 99 | + |
| 100 | + mocker.patch('zulipterminal.model.Model.get_all_users', |
| 101 | + return_value=[]) |
| 102 | + mocker.patch('zulipterminal.model.Model.' |
| 103 | + '_stream_info_from_subscriptions', |
| 104 | + return_value=({}, set(), [], [])) |
| 105 | + self.classify_unread_counts = mocker.patch( |
| 106 | + 'zulipterminal.model.classify_unread_counts', |
| 107 | + return_value=[]) |
| 108 | + |
| 109 | + with pytest.raises(ServerConnectionFailure) as e: |
| 110 | + model = Model(self.controller) |
| 111 | + |
| 112 | + assert str(e.value) == exception_text + ' (get_messages, register)' |
| 113 | + |
70 | 114 | def test_register_initial_desired_events(self, mocker, initial_data): |
71 | 115 | mocker.patch('zulipterminal.model.Model.get_messages', |
72 | 116 | return_value='') |
|
0 commit comments