|
4 | 4 | """
|
5 | 5 | Tests for the PhabricatorClient
|
6 | 6 | """
|
| 7 | + |
7 | 8 | import pytest
|
| 9 | +import requests |
8 | 10 | import requests_mock
|
9 | 11 |
|
10 | 12 | from landoapi.phabricator_client import PhabricatorClient, \
|
@@ -122,6 +124,57 @@ def test_get_latest_patch_for_revision(phabfactory):
|
122 | 124 | assert returned_patch == patch
|
123 | 125 |
|
124 | 126 |
|
| 127 | +def test_check_connection_success(): |
| 128 | + phab = PhabricatorClient(api_key='api-key') |
| 129 | + success_json = CANNED_EMPTY_RESULT.copy() |
| 130 | + with requests_mock.mock() as m: |
| 131 | + m.get(phab_url('conduit.ping'), status_code=200, json=success_json) |
| 132 | + phab.check_connection() |
| 133 | + assert m.called |
| 134 | + |
| 135 | + |
| 136 | +def test_raise_exception_if_ping_encounters_connection_error(): |
| 137 | + phab = PhabricatorClient(api_key='api-key') |
| 138 | + with requests_mock.mock() as m: |
| 139 | + # Test with the generic ConnectionError, which is a superclass for |
| 140 | + # other connection error types. |
| 141 | + m.get(phab_url('conduit.ping'), exc=requests.ConnectionError) |
| 142 | + |
| 143 | + with pytest.raises(PhabricatorAPIException): |
| 144 | + phab.check_connection() |
| 145 | + assert m.called |
| 146 | + |
| 147 | + |
| 148 | +def test_raise_exception_if_api_ping_times_out(): |
| 149 | + phab = PhabricatorClient(api_key='api-key') |
| 150 | + with requests_mock.mock() as m: |
| 151 | + # Test with the generic Timeout exception, which all other timeout |
| 152 | + # exceptions derive from. |
| 153 | + m.get(phab_url('conduit.ping'), exc=requests.Timeout) |
| 154 | + |
| 155 | + with pytest.raises(PhabricatorAPIException): |
| 156 | + phab.check_connection() |
| 157 | + assert m.called |
| 158 | + |
| 159 | + |
| 160 | +def test_raise_exception_if_api_returns_error_json_response(): |
| 161 | + phab = PhabricatorClient(api_key='api-key') |
| 162 | + error_json = { |
| 163 | + "result": None, |
| 164 | + "error_code": "ERR-CONDUIT-CORE", |
| 165 | + "error_info": "BOOM" |
| 166 | + } |
| 167 | + |
| 168 | + with requests_mock.mock() as m: |
| 169 | + # Test with the generic Timeout exception, which all other timeout |
| 170 | + # exceptions derive from. |
| 171 | + m.get(phab_url('conduit.ping'), status_code=500, json=error_json) |
| 172 | + |
| 173 | + with pytest.raises(PhabricatorAPIException): |
| 174 | + phab.check_connection() |
| 175 | + assert m.called |
| 176 | + |
| 177 | + |
125 | 178 | def test_phabricator_exception():
|
126 | 179 | """ Ensures that the PhabricatorClient converts JSON errors from Phabricator
|
127 | 180 | into proper exceptions with the error_code and error_message in tact.
|
|
0 commit comments