|
| 1 | +# Licensed under a 3-clause BSD style license - see LICENSE.rst |
| 2 | +from ..core import AlmaAuth |
| 3 | +from ...exceptions import LoginError |
| 4 | + |
| 5 | +import json |
| 6 | +import pytest |
| 7 | +from unittest.mock import patch, Mock |
| 8 | + |
| 9 | + |
| 10 | +def test_host(): |
| 11 | + def _requests_mock_ok(method, url, **kwargs): |
| 12 | + response = Mock() |
| 13 | + response.status_code = 200 |
| 14 | + return response |
| 15 | + |
| 16 | + test_subject = AlmaAuth() |
| 17 | + test_subject.set_auth_hosts(['almaexample.com']) |
| 18 | + test_subject._request = Mock(side_effect=_requests_mock_ok) |
| 19 | + assert test_subject.host == 'almaexample.com' |
| 20 | + |
| 21 | +def test_host_default(): |
| 22 | + def _requests_mock_ok(method, url, **kwargs): |
| 23 | + response = Mock() |
| 24 | + response.status_code = 200 |
| 25 | + return response |
| 26 | + |
| 27 | + test_subject = AlmaAuth() |
| 28 | + test_subject._request = Mock(side_effect=_requests_mock_ok) |
| 29 | + assert test_subject.host == 'asa.alma.cl' |
| 30 | + |
| 31 | +def test_host_err(): |
| 32 | + def _requests_mock_err(method, url, **kwargs): |
| 33 | + response = Mock() |
| 34 | + response.status_code = 404 |
| 35 | + return response |
| 36 | + |
| 37 | + test_subject = AlmaAuth() |
| 38 | + test_subject.set_auth_hosts(['almaexample.com']) |
| 39 | + test_subject._request = Mock(side_effect=_requests_mock_err) |
| 40 | + with pytest.raises(LoginError): |
| 41 | + test_subject.host |
| 42 | + |
| 43 | +def test_login_bad_error(): |
| 44 | + def _response_json(): |
| 45 | + obj = { |
| 46 | + 'error': 'Badness', |
| 47 | + 'error_description': 'Something very bad' |
| 48 | + } |
| 49 | + return obj |
| 50 | + # return json.dumps(obj, indent = 4) |
| 51 | + |
| 52 | + def _requests_mock_err(method, url, **kwargs): |
| 53 | + response = Mock() |
| 54 | + if test_subject._VERIFY_WELL_KNOWN_ENDPOINT in url: |
| 55 | + response.status_code = 200 |
| 56 | + elif test_subject._LOGIN_ENDPOINT in url: |
| 57 | + response.json = _response_json |
| 58 | + return response |
| 59 | + |
| 60 | + test_subject = AlmaAuth() |
| 61 | + test_subject.set_auth_hosts(['almaexample.com']) |
| 62 | + test_subject._request = Mock(side_effect=_requests_mock_err) |
| 63 | + with pytest.raises(LoginError) as e: |
| 64 | + test_subject.login('TESTUSER', 'TESTPASS') |
| 65 | + assert 'Could not log in to ALMA authorization portal' in e.value.args[0] |
| 66 | + |
| 67 | +def test_login_missing_token(): |
| 68 | + def _response_json(): |
| 69 | + obj = { |
| 70 | + 'irrlevant': 'Weird', |
| 71 | + } |
| 72 | + return json.dumps(obj, indent = 4) |
| 73 | + |
| 74 | + def _requests_mock_err(method, url, **kwargs): |
| 75 | + response = Mock() |
| 76 | + if test_subject._VERIFY_WELL_KNOWN_ENDPOINT in url: |
| 77 | + response.status_code = 200 |
| 78 | + elif test_subject._LOGIN_ENDPOINT in url: |
| 79 | + response.json = _response_json |
| 80 | + return response |
| 81 | + |
| 82 | + test_subject = AlmaAuth() |
| 83 | + test_subject.set_auth_hosts(['almaexample.com']) |
| 84 | + test_subject._request = Mock(side_effect=_requests_mock_err) |
| 85 | + with pytest.raises(LoginError) as e: |
| 86 | + test_subject.login('TESTUSER', 'TESTPASS') |
| 87 | + |
| 88 | + assert 'No error from server, but missing access token from host' in e.value.args[0] |
| 89 | + |
| 90 | +def test_login_success(): |
| 91 | + def _response_json(): |
| 92 | + obj = { |
| 93 | + 'access_token': 'MYTOKEN' |
| 94 | + } |
| 95 | + return json.dumps(obj, indent = 4) |
| 96 | + |
| 97 | + def _requests_mock_good(method, url, **kwargs): |
| 98 | + response = Mock() |
| 99 | + print(f'URL is {url}') |
| 100 | + if test_subject._VERIFY_WELL_KNOWN_ENDPOINT in url: |
| 101 | + response.status_code = 200 |
| 102 | + elif test_subject._LOGIN_ENDPOINT in url: |
| 103 | + response.json = _response_json |
| 104 | + return response |
| 105 | + |
| 106 | + test_subject = AlmaAuth() |
| 107 | + test_subject.set_auth_hosts(['almaexample.com']) |
| 108 | + test_subject._request = Mock(side_effect=_requests_mock_good) |
| 109 | + test_subject.login('TESTUSER', 'TESTPASS') |
0 commit comments