|
1 |
| -# # This Source Code Form is subject to the terms of the Mozilla Public |
2 |
| -# # License, v. 2.0. If a copy of the MPL was not distributed with this |
3 |
| -# # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 |
| -# |
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +""" |
| 5 | +Tests for the PhabricatorClient |
| 6 | +""" |
5 | 7 | import os
|
6 |
| -import json |
| 8 | +import pytest |
7 | 9 | import requests_mock
|
8 | 10 |
|
9 |
| -from tests.utils import versionfile, app |
10 |
| - |
11 | 11 | from landoapi.phabricator_client import PhabricatorClient, \
|
12 | 12 | PhabricatorAPIException
|
13 | 13 |
|
| 14 | +from tests.utils import versionfile, app |
| 15 | +from tests.canned_responses.phabricator.revisions import * |
| 16 | +from tests.canned_responses.phabricator.users import * |
| 17 | +from tests.canned_responses.phabricator.repos import * |
| 18 | +from tests.canned_responses.phabricator.errors import * |
14 | 19 |
|
15 |
| -def test_get_revision_returns_200(): |
| 20 | + |
| 21 | +def test_get_revision_with_200_response(): |
16 | 22 | phab = PhabricatorClient(api_key='api-key')
|
17 | 23 | api_url = '%s/api/differential.query' % os.getenv('PHABRICATOR_URL')
|
18 | 24 |
|
19 | 25 | with requests_mock.mock() as m:
|
20 |
| - # TODO finish testing response with actual data |
21 |
| - result = {'result': [{}], 'error_code': None, 'error_info': None} |
22 |
| - m.get(api_url, text=json.dumps(result)) |
23 |
| - response = phab.get_revision(id='D123') |
24 |
| - assert response == {} |
| 26 | + m.get(api_url, status_code=200, json=CANNED_REVISION_1) |
| 27 | + revision = phab.get_revision(id=CANNED_REVISION_1['result'][0]['id']) |
| 28 | + assert revision == CANNED_REVISION_1['result'][0] |
25 | 29 |
|
26 | 30 |
|
27 |
| -def test_get_current_user_returns_200(): |
28 |
| - pass |
| 31 | +def test_get_current_user_with_200_response(): |
| 32 | + phab = PhabricatorClient(api_key='api-key') |
| 33 | + api_url = '%s/api/user.whoami' % os.getenv('PHABRICATOR_URL') |
29 | 34 |
|
| 35 | + with requests_mock.mock() as m: |
| 36 | + m.get(api_url, status_code=200, json=CANNED_USER_WHOAMI_1) |
| 37 | + user = phab.get_current_user() |
| 38 | + assert user == CANNED_USER_WHOAMI_1['result'] |
30 | 39 |
|
31 |
| -def test_get_user_returns_200(): |
32 |
| - pass |
33 | 40 |
|
| 41 | +def test_get_user_returns_with_200_response(): |
| 42 | + phab = PhabricatorClient(api_key='api-key') |
| 43 | + api_url = '%s/api/user.query' % os.getenv('PHABRICATOR_URL') |
34 | 44 |
|
35 |
| -def test_get_repo_returns_200(): |
36 |
| - pass |
| 45 | + with requests_mock.mock() as m: |
| 46 | + m.get(api_url, status_code=200, json=CANNED_USER_1) |
| 47 | + user = phab.get_user(phid=CANNED_USER_1['result'][0]['phid']) |
| 48 | + assert user == CANNED_USER_1['result'][0] |
37 | 49 |
|
38 | 50 |
|
39 |
| -def test_phabricator_exception(): |
40 |
| - pass |
| 51 | +def test_get_repo_returns_with_200_response(): |
| 52 | + phab = PhabricatorClient(api_key='api-key') |
| 53 | + api_url = '%s/api/phid.query' % os.getenv('PHABRICATOR_URL') |
| 54 | + |
| 55 | + with requests_mock.mock() as m: |
| 56 | + m.get(api_url, status_code=200, json=CANNED_REPO_MOZCENTRAL) |
| 57 | + canned_response_repo = \ |
| 58 | + list(CANNED_REPO_MOZCENTRAL['result'].values())[0] |
| 59 | + repo = phab.get_repo(phid=canned_response_repo['phid']) |
| 60 | + assert repo == canned_response_repo |
| 61 | + |
41 | 62 |
|
| 63 | +def test_phabricator_exception(): |
| 64 | + """ Ensures that the PhabricatorClient converts JSON errors from Phabricator |
| 65 | + into proper exceptions with the error_code and error_message in tact. |
| 66 | + """ |
| 67 | + phab = PhabricatorClient(api_key='api-key') |
| 68 | + api_url = '%s/api/differential.query' % os.getenv('PHABRICATOR_URL') |
42 | 69 |
|
43 |
| -def test_request_exception(): |
44 |
| - pass |
| 70 | + with requests_mock.mock() as m: |
| 71 | + m.get(api_url, status_code=200, json=CANNED_ERROR_1) |
| 72 | + with pytest.raises(PhabricatorAPIException) as e_info: |
| 73 | + phab.get_revision(id=CANNED_REVISION_1['result'][0]['id']) |
| 74 | + assert e_info.value.error_code == CANNED_ERROR_1['error_code'] |
| 75 | + assert e_info.value.error_info == CANNED_ERROR_1['error_info'] |
0 commit comments