|
6 | 6 | from unittest.mock import patch |
7 | 7 |
|
8 | 8 | from pythonanywhere_core.base import get_api_endpoint |
9 | | -from pythonanywhere_core.exceptions import PythonAnywhereApiException |
| 9 | +from pythonanywhere_core.exceptions import PythonAnywhereApiException, DomainAlreadyExistsException |
10 | 10 | from pythonanywhere_core.website import Website |
11 | | -from pythonanywhere_core.exceptions import SanityException, PythonAnywhereApiException |
12 | 11 |
|
13 | 12 |
|
14 | 13 | pytestmark = pytest.mark.usefixtures("api_token") |
@@ -77,6 +76,39 @@ def test_create_returns_json_with_created_website_info( |
77 | 76 | ) |
78 | 77 |
|
79 | 78 |
|
| 79 | +def test_create_raises_when_domain_name_already_exists( |
| 80 | + api_responses, |
| 81 | + websites_base_url, |
| 82 | + domain_name, |
| 83 | + command |
| 84 | +): |
| 85 | + with pytest.raises(DomainAlreadyExistsException): |
| 86 | + api_responses.add( |
| 87 | + responses.POST, |
| 88 | + url=websites_base_url, |
| 89 | + status=400, |
| 90 | + body=json.dumps({"domain_name":["domain with this domain name already exists."]}) |
| 91 | + ) |
| 92 | + Website().create(domain_name=domain_name, command=command) |
| 93 | + |
| 94 | +def test_raises_with_api_error_message_for_any_other_error( |
| 95 | + api_responses, |
| 96 | + websites_base_url, |
| 97 | + domain_name, |
| 98 | + command |
| 99 | +): |
| 100 | + with pytest.raises(PythonAnywhereApiException) as e: |
| 101 | + api_responses.add( |
| 102 | + responses.POST, |
| 103 | + url=websites_base_url, |
| 104 | + status=400, |
| 105 | + body=json.dumps({"message":["Something went wrong."]}) |
| 106 | + ) |
| 107 | + Website().create(domain_name=domain_name, command=command) |
| 108 | + |
| 109 | + assert "Something went wrong." in str(e) |
| 110 | + |
| 111 | + |
80 | 112 | def test_get_returns_json_with_info_for_given_domain( |
81 | 113 | api_responses, websites_base_url, website_info, domain_name |
82 | 114 | ): |
@@ -161,39 +193,3 @@ def test_raises_if_ssl_info_does_not_return_200(api_responses, domain_name, doma |
161 | 193 |
|
162 | 194 | assert "GET SSL details via API failed, got" in str(e.value) |
163 | 195 | assert "nope" in str(e.value) |
164 | | - |
165 | | - |
166 | | -def test_website_sanity_check_domain_already_exists(api_responses, websites_base_url, website_info, domain_name): |
167 | | - api_responses.add( |
168 | | - responses.GET, |
169 | | - url=f"{websites_base_url}{domain_name}/", |
170 | | - status=200, |
171 | | - body=json.dumps(website_info) |
172 | | - ) |
173 | | - |
174 | | - with pytest.raises(SanityException) as e: |
175 | | - Website().sanity_check(domain_name=domain_name, nuke=False) |
176 | | - |
177 | | - |
178 | | -def test_website_sanity_check_can_be_bypassed_with_nuke_option(api_responses, websites_base_url, website_info, domain_name): |
179 | | - # Test that the sanity check does not raise an exception |
180 | | - Website().sanity_check(domain_name=domain_name, nuke=True) |
181 | | - |
182 | | - |
183 | | -def test_website_sanity_check_tests_api_token_present(api_responses, websites_base_url, website_info, domain_name): |
184 | | - import os |
185 | | - del os.environ["API_TOKEN"] |
186 | | - |
187 | | - # Test that the sanity check does not raise an exception |
188 | | - with pytest.raises(SanityException) as e: |
189 | | - Website().sanity_check(domain_name=domain_name, nuke=False) |
190 | | - |
191 | | - assert "Could not find your API token" in str(e.value) |
192 | | - |
193 | | - |
194 | | -@patch('pythonanywhere_core.website.Website.sanity_check') |
195 | | -def test_create_website_calls_sanity_check(mock_sanity_check, api_responses, websites_base_url, website_info, domain_name, command): |
196 | | - mock_sanity_check.side_effect = SanityException('Boom!') |
197 | | - |
198 | | - with pytest.raises(SanityException): |
199 | | - Website().create(domain_name=domain_name, command=command, nuke=False) |
0 commit comments