|
| 1 | +import getpass |
| 2 | +import json |
| 3 | +from urllib.parse import urljoin |
| 4 | + |
| 5 | +import pytest |
| 6 | +import responses |
| 7 | + |
| 8 | +from pythonanywhere_core.base import get_api_endpoint |
| 9 | +from pythonanywhere_core.website import Website |
| 10 | + |
| 11 | + |
| 12 | +pytestmark = pytest.mark.usefixtures("api_token") |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def webapps_base_url(): |
| 17 | + return get_api_endpoint(username=getpass.getuser(), flavor="websites") |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def domain_name(): |
| 22 | + return "foo.bar.com" |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def command(): |
| 27 | + return "/usr/local/bin/uvicorn --uds $DOMAIN_SOCKET main:app" |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def website_info(domain_name, command): |
| 32 | + return { |
| 33 | + "domain_name": domain_name, |
| 34 | + "enabled": True, |
| 35 | + "id": 42, |
| 36 | + "user": getpass.getuser(), |
| 37 | + "webapp": { |
| 38 | + "command": command, |
| 39 | + "domains": [ |
| 40 | + {"domain_name": domain_name, |
| 41 | + "enabled": True} |
| 42 | + ], |
| 43 | + "id": 42 |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +def test_create_returns_json_with_created_website_info( |
| 49 | + api_responses, webapps_base_url, website_info, domain_name, command |
| 50 | +): |
| 51 | + api_responses.add( |
| 52 | + responses.POST, url=webapps_base_url, status=201, body=json.dumps(website_info) |
| 53 | + ) |
| 54 | + |
| 55 | + assert Website().create(domain_name=domain_name, command=command) == website_info |
| 56 | + |
| 57 | + |
| 58 | +def test_get_returns_json_with_info_for_given_domain( |
| 59 | + api_responses, webapps_base_url, website_info, domain_name |
| 60 | +): |
| 61 | + api_responses.add( |
| 62 | + responses.GET, |
| 63 | + url=urljoin(webapps_base_url, domain_name), |
| 64 | + status=200, |
| 65 | + body=json.dumps(website_info) |
| 66 | + ) |
| 67 | + |
| 68 | + assert Website().get(domain_name=domain_name) == website_info |
0 commit comments