Skip to content

Commit 5ff4842

Browse files
tests: add integration tests
1 parent f62e550 commit 5ff4842

File tree

7 files changed

+267
-0
lines changed

7 files changed

+267
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# python
22
venv/
3+
.venv/
34
__pycache__/
45
.pytest_cache/
56
.hypothesis/

requirements.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This file was autogenerated by uv via the following command:
2+
# uv pip compile - -o requirements.txt
3+
certifi==2024.2.2
4+
# via requests
5+
charset-normalizer==3.3.2
6+
# via requests
7+
idna==3.7
8+
# via requests
9+
iniconfig==2.0.0
10+
# via pytest
11+
packaging==24.0
12+
# via pytest
13+
pip==23.2.1
14+
pluggy==1.4.0
15+
# via pytest
16+
pytest==8.1.1
17+
requests==2.31.0
18+
urllib3==2.2.1
19+
# via requests

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import pytest
2+
from .utils import BaseUrlSession
3+
from uuid import uuid4
4+
5+
6+
@pytest.fixture(scope="session")
7+
def zod():
8+
"""Request session for ~zod which is authenticated"""
9+
baseUrl = "http://localhost:8080"
10+
data = {"password": "lidlut-tabwed-pillex-ridrup"}
11+
with BaseUrlSession(baseUrl) as session:
12+
session.post("/~/login", data=data) # perform the login
13+
yield session
14+
15+
16+
@pytest.fixture(scope="session")
17+
def nus():
18+
"""Request session for ~nus which is authenticated"""
19+
baseUrl = "http://localhost:8081"
20+
data = {"password": "bortem-pinwyl-macnyx-topdeg"}
21+
with BaseUrlSession(baseUrl) as session:
22+
session.post("/~/login", data=data) # perform the login
23+
yield session
24+
25+
26+
@pytest.fixture(scope="session")
27+
def lus():
28+
"""Request session for ~nus which is authenticated"""
29+
baseUrl = "http://localhost:8082"
30+
data = {"password": "macsyr-davfed-tanrux-linnec"}
31+
with BaseUrlSession(baseUrl) as session:
32+
session.post("/~/login", data=data) # perform the login
33+
yield session
34+
35+
36+
@pytest.fixture
37+
def uuid():
38+
return str(uuid4())
39+
40+
41+
@pytest.fixture
42+
def eid():
43+
return str(uuid4())
44+
45+
46+
@pytest.fixture
47+
def gid():
48+
return str(uuid4())
49+
50+
51+
@pytest.fixture
52+
def list_(zod, gid) -> dict:
53+
list_ = {
54+
"gid": gid,
55+
"title": "assembly",
56+
"host": "~zod",
57+
"public": False,
58+
}
59+
url = "/apps/tahuti/api/lists"
60+
zod.put(url, json=list_)
61+
return list_

tests/integration/__init__.py

Whitespace-only changes.

tests/integration/test_lists.py

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
from time import sleep
2+
from uuid import uuid4
3+
4+
import pytest
5+
import requests
6+
from schema import Schema
7+
8+
9+
@pytest.fixture
10+
def list_schema():
11+
"""Response schema"""
12+
return Schema(
13+
{
14+
"lid": str,
15+
"title": str,
16+
"host": str,
17+
"public": bool,
18+
},
19+
)
20+
21+
22+
@pytest.fixture
23+
def lists_schema(list_schema):
24+
"""Response schema"""
25+
return Schema([list_schema])
26+
27+
28+
@pytest.mark.parametrize("payload", (None, "", " ", {}, []))
29+
def test_lists_put_empty_payload(payload, zod):
30+
url = "/apps/maat/api/lists"
31+
response = zod.put(url, json=payload)
32+
assert response.status_code in (418, 500)
33+
34+
35+
@pytest.mark.parametrize("title", (None, "", " "))
36+
def test_lists_put_invalid_title(title, zod):
37+
list_ = {
38+
"lid": str(uuid4()),
39+
"title": title,
40+
"public": False,
41+
}
42+
url = "/apps/maat/api/lists"
43+
44+
# PUT /lists
45+
response = zod.put(url, json=list_)
46+
assert response.status_code in (422, 500)
47+
48+
# GET /lists
49+
response = zod.get(url)
50+
assert response.status_code == 200
51+
result = response.json()
52+
assert list_ not in result
53+
54+
55+
def test_lists_get_all(zod, list_, lists_schema):
56+
# GET /lists
57+
url = "/apps/maat/api/lists"
58+
response = zod.get(url)
59+
assert response.status_code == 200
60+
result = response.json()
61+
assert lists_schema.is_valid(result)
62+
assert list_ in result
63+
64+
65+
@pytest.mark.usefixtures("list_")
66+
def test_lists_get_all_unauthorized():
67+
# GET /lists
68+
url = "http://localhost:8080/apps/maat/api/lists"
69+
response = requests.get(url)
70+
assert response.status_code == 401
71+
72+
73+
def test_lists_get_single(zod, gid, list_, list_schema):
74+
# GET /lists/{uuid}
75+
url = f"/apps/maat/api/lists/{gid}"
76+
response = zod.get(url)
77+
assert response.status_code == 200
78+
result = response.json()
79+
assert list_schema.is_valid(result)
80+
assert result == list_
81+
82+
83+
def test_lists_get_single_not_found(zod):
84+
# GET /lists/{uuid}
85+
url = f"/apps/maat/api/lists/{str(uuid4())}"
86+
response = zod.get(url)
87+
# TODO: should be 4xx
88+
assert response.status_code == 500
89+
90+
91+
def test_lists_delete(zod, gid, list_):
92+
# PUT
93+
url = f"/apps/maat/api/lists/{gid}"
94+
response = zod.delete(url)
95+
assert response.status_code == 200
96+
97+
# GET /lists
98+
url = "/apps/maat/api/lists"
99+
response = zod.get(url)
100+
assert response.status_code == 200
101+
result = response.json()
102+
assert list_ not in result
103+
104+
105+
# @pytest.mark.usefixtures("member")
106+
# def test_lists_delete_unauthorized(zod, nus, gid, list):
107+
# # wait for join
108+
# sleep(0.5)
109+
110+
# # PUT
111+
# url = f"/apps/maat/api/lists/{gid}"
112+
# response = nus.delete(url)
113+
# assert response.status_code == 403
114+
115+
# # GET /lists
116+
# url = "/apps/maat/api/lists"
117+
# response = zod.get(url)
118+
# assert response.status_code == 200
119+
# result = response.json()
120+
# assert list in result
121+
122+
123+
# @pytest.mark.usefixtures("list")
124+
# def test_lists_get_private(gid):
125+
# # GET /lists/{uuid}
126+
# url = f"http://localhost:8080/apps/maat/api/lists/{gid}"
127+
# response = requests.get(url)
128+
# assert response.status_code == 401
129+
130+
131+
# def test_lists_get_public(gid, list_public, list_schema):
132+
# # GET /lists/{uuid}
133+
# url = f"http://localhost:8080/apps/maat/api/lists/{gid}"
134+
# response = requests.get(url)
135+
# assert response.status_code == 200
136+
# result = response.json()
137+
# assert list_schema.is_valid(result)
138+
# assert result == list_public
139+
140+
141+
# @pytest.mark.usefixtures("list")
142+
# def test_lists_delete_private(gid):
143+
# # PUT
144+
# url = f"http://localhost:8080/apps/maat/api/lists/{gid}"
145+
# response = requests.delete(url)
146+
# assert response.status_code == 401
147+
148+
149+
# @pytest.mark.usefixtures("list_public")
150+
# def test_lists_delete_public(gid):
151+
# # PUT
152+
# url = f"http://localhost:8080/apps/maat/api/lists/{gid}"
153+
# response = requests.delete(url)
154+
# assert response.status_code == 401

tests/utils.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Requests Session with Base URL support
2+
3+
https://github.com/requests/toolbelt/blob/master/requests_toolbelt/sessions.py
4+
"""
5+
6+
7+
from urllib.parse import urljoin
8+
9+
import requests
10+
11+
12+
class BaseUrlSession(requests.Session):
13+
base_url = None
14+
15+
def __init__(self, base_url=None):
16+
if base_url:
17+
self.base_url = base_url
18+
super(BaseUrlSession, self).__init__()
19+
20+
def request(self, method, url, *args, **kwargs):
21+
"""Send the request after generating the complete URL."""
22+
url = self.create_url(url)
23+
return super(BaseUrlSession, self).request(method, url, *args, **kwargs)
24+
25+
def prepare_request(self, request, *args, **kwargs):
26+
"""Prepare the request after generating the complete URL."""
27+
request.url = self.create_url(request.url)
28+
return super(BaseUrlSession, self).prepare_request(request, *args, **kwargs)
29+
30+
def create_url(self, url):
31+
"""Create the URL based off this partial path."""
32+
return urljoin(self.base_url, url)

0 commit comments

Comments
 (0)