|
1 | 1 | import pytest |
2 | 2 | import responses |
3 | 3 |
|
4 | | -from pythonanywhere_core.base import call_api, get_api_endpoint |
5 | | -from pythonanywhere_core.exceptions import AuthenticationError |
| 4 | +from pythonanywhere_core.base import ( |
| 5 | + call_api, |
| 6 | + get_api_endpoint, |
| 7 | + helpful_token_error_message, |
| 8 | +) |
| 9 | +from pythonanywhere_core.exceptions import AuthenticationError, NoTokenError |
| 10 | + |
6 | 11 |
|
7 | 12 | @pytest.fixture |
8 | 13 | def mock_requests(mocker): |
9 | 14 | return mocker.patch("pythonanywhere_core.base.requests") |
10 | 15 |
|
11 | 16 |
|
12 | | -class TestGetAPIEndpoint: |
| 17 | +def test_api_endpoint_defaults_to_pythonanywhere_dot_com_if_no_environment_variables(): |
| 18 | + assert get_api_endpoint() == "https://www.pythonanywhere.com/api/v0/user/{username}/{flavor}/" |
| 19 | + |
| 20 | + |
| 21 | +def test_gets_domain_from_pythonanywhere_site_and_ignores_pythonanywhere_domain_if_both_set(monkeypatch): |
| 22 | + monkeypatch.setenv("PYTHONANYWHERE_SITE", "www.foo.com") |
| 23 | + monkeypatch.setenv("PYTHONANYWHERE_DOMAIN", "wibble.com") |
| 24 | + |
| 25 | + assert get_api_endpoint() == "https://www.foo.com/api/v0/user/{username}/{flavor}/" |
| 26 | + |
| 27 | + |
| 28 | +def test_gets_domain_from_pythonanywhere_domain_and_adds_on_www_if_set_but_no_pythonanywhere_site(monkeypatch): |
| 29 | + monkeypatch.setenv("PYTHONANYWHERE_DOMAIN", "foo.com") |
| 30 | + |
| 31 | + assert get_api_endpoint() == "https://www.foo.com/api/v0/user/{username}/{flavor}/" |
| 32 | + |
| 33 | + |
| 34 | +def test_raises_on_401(api_token, api_responses): |
| 35 | + url = "https://foo.com/" |
| 36 | + api_responses.add(responses.POST, url, status=401, body="nope") |
| 37 | + with pytest.raises(AuthenticationError) as e: |
| 38 | + call_api(url, "post") |
| 39 | + assert str(e.value) == "Authentication error 401 calling API: nope" |
| 40 | + |
| 41 | + |
| 42 | +def test_passes_verify_from_environment(api_token, monkeypatch, mock_requests): |
| 43 | + monkeypatch.setenv("PYTHONANYWHERE_INSECURE_API", "true") |
| 44 | + |
| 45 | + call_api("url", "post", foo="bar") |
| 46 | + |
| 47 | + _, kwargs = mock_requests.request.call_args |
| 48 | + assert kwargs["verify"] is False |
| 49 | + |
| 50 | + |
| 51 | +def test_verify_is_true_if_env_not_set(api_token, mock_requests): |
| 52 | + call_api("url", "post", foo="bar") |
13 | 53 |
|
14 | | - def test_defaults_to_pythonanywhere_dot_com_if_no_environment_variables(self): |
15 | | - assert get_api_endpoint() == "https://www.pythonanywhere.com/api/v0/user/{username}/{flavor}/" |
| 54 | + _, kwargs = mock_requests.request.call_args |
| 55 | + assert kwargs["verify"] is True |
16 | 56 |
|
17 | | - def test_gets_domain_from_pythonanywhere_site_and_ignores_pythonanywhere_domain_if_both_set(self, monkeypatch): |
18 | | - monkeypatch.setenv("PYTHONANYWHERE_SITE", "www.foo.com") |
19 | | - monkeypatch.setenv("PYTHONANYWHERE_DOMAIN", "wibble.com") |
20 | | - assert get_api_endpoint() == "https://www.foo.com/api/v0/user/{username}/{flavor}/" |
21 | 57 |
|
22 | | - def test_gets_domain_from_pythonanywhere_domain_and_adds_on_www_if_set_but_no_pythonanywhere_site( |
23 | | - self, monkeypatch |
24 | | - ): |
25 | | - monkeypatch.setenv("PYTHONANYWHERE_DOMAIN", "foo.com") |
26 | | - assert get_api_endpoint() == "https://www.foo.com/api/v0/user/{username}/{flavor}/" |
| 58 | +def test_raises_with_helpful_message_if_no_token_present(mocker): |
| 59 | + mock_helpful_message = mocker.patch("pythonanywhere_core.base.helpful_token_error_message") |
| 60 | + mock_helpful_message.return_value = "I'm so helpful" |
27 | 61 |
|
| 62 | + with pytest.raises(NoTokenError) as exc: |
| 63 | + call_api("blah", "get") |
28 | 64 |
|
29 | | -class TestCallAPI: |
30 | | - def test_raises_on_401(self, api_token, api_responses): |
31 | | - url = "https://foo.com/" |
32 | | - api_responses.add(responses.POST, url, status=401, body="nope") |
33 | | - with pytest.raises(AuthenticationError) as e: |
34 | | - call_api(url, "post") |
35 | | - assert str(e.value) == "Authentication error 401 calling API: nope" |
| 65 | + assert str(exc.value) == "I'm so helpful" |
36 | 66 |
|
37 | | - def test_passes_verify_from_environment(self, api_token, monkeypatch, mock_requests): |
38 | | - monkeypatch.setenv("PYTHONANYWHERE_INSECURE_API", "true") |
39 | 67 |
|
40 | | - call_api("url", "post", foo="bar") |
| 68 | +def test_helpful_message_inside_pythonanywhere(monkeypatch): |
| 69 | + monkeypatch.setenv("PYTHONANYWHERE_SITE", "www.foo.com") |
41 | 70 |
|
42 | | - args, kwargs = mock_requests.request.call_args |
43 | | - assert kwargs["verify"] is False |
| 71 | + assert "Oops, you don't seem to have an API token." in helpful_token_error_message() |
44 | 72 |
|
45 | | - def test_verify_is_true_if_env_not_set(self, api_token, mock_requests): |
46 | | - call_api("url", "post", foo="bar") |
47 | 73 |
|
48 | | - args, kwargs = mock_requests.request.call_args |
49 | | - assert kwargs["verify"] is True |
| 74 | +def test_helpful_message_outside_pythonanywhere(): |
| 75 | + assert "Oops, you don't seem to have an API_TOKEN environment variable set." in helpful_token_error_message() |
0 commit comments