Skip to content

Commit 10c1678

Browse files
committed
Added create and get methods for website.Website, by: Filip, Piotr
1 parent 73056c7 commit 10c1678

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

pythonanywhere_core/website.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import getpass
2+
from urllib.parse import urljoin
3+
4+
from pythonanywhere_core.base import call_api, get_api_endpoint
5+
6+
7+
class Website:
8+
api_endpoint = get_api_endpoint(username=getpass.getuser(), flavor="websites")
9+
10+
def create(self, domain_name: str, command: str):
11+
response = call_api(
12+
self.api_endpoint,
13+
"post",
14+
data={
15+
"domain_name": domain_name,
16+
"enabled": True,
17+
"webapp": {"command": command}
18+
}
19+
)
20+
return response.json()
21+
22+
def get(self, domain_name: str):
23+
response = call_api(
24+
urljoin(self.api_endpoint, domain_name),
25+
"get",
26+
)
27+
return response.json()
28+
29+
def list(self):
30+
raise NotImplemented
31+
32+
def reload(self):
33+
raise NotImplemented
34+
35+
def delete(self):
36+
raise NotImplemented

tests/test_website.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
 (0)