Skip to content

Commit 0bb0bad

Browse files
committed
ENH: testing for 'login' API
1 parent df952c3 commit 0bb0bad

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/save_and_restore_api/_api_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async def send_request(
3434

3535
async def login(self, *, username=None, password=None):
3636
method, url, params = self._prepare_login(username=username, password=password)
37-
await self.send_request(method, url, params=params)
37+
return await self.send_request(method, url, params=params)
3838

3939
async def get_node(self, node_uid):
4040
method, url = self._prepare_get_node(node_uid=node_uid)

src/save_and_restore_api/_api_threads.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def send_request(
3434

3535
def login(self, *, username=None, password=None):
3636
method, url, params = self._prepare_login(username=username, password=password)
37-
self.send_request(method, url, params=params)
37+
return self.send_request(method, url, params=params)
3838

3939
def get_node(self, node_uid):
4040
method, url = self._prepare_get_node(node_uid=node_uid)

tests/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def _is_async(library):
2+
if library == "ASYNC":
3+
return True
4+
elif library == "THREADS":
5+
return False
6+
else:
7+
raise ValueError(f"Unknown library: {library!r}")

tests/test_package.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import importlib.metadata
44

5+
import asyncio
56
import pytest
7+
from .common import _is_async
68

79
import save_and_restore_api
810
from save_and_restore_api import SaveRestoreAPI as SaveRestoreAPI_Threads
@@ -19,6 +21,36 @@ def test_version():
1921
assert importlib.metadata.version("save_and_restore_api") == save_and_restore_api.__version__
2022

2123

24+
# fmt: off
25+
@pytest.mark.parametrize("library", ["THREADS", "ASYNC"])
26+
@pytest.mark.parametrize("username, password, roles", [
27+
(admin_username, admin_password, ["ROLE_SAR-ADMIN"]),
28+
(user_username, user_password, ["ROLE_SAR-USER"]),
29+
(read_username, read_password, []),
30+
])
31+
# fmt: on
32+
def test_login_01(username, password, roles, library):
33+
if not _is_async(library):
34+
SR = SaveRestoreAPI_Threads(base_url=base_url, timeout=2)
35+
SR.set_auth(username=user_username, password=user_password)
36+
SR.open()
37+
response = SR.login(username=username, password=password)
38+
SR.close()
39+
assert response["userName"] == username
40+
assert response["roles"] == roles
41+
else:
42+
async def testing():
43+
SR = SaveRestoreAPI_Threads(base_url=base_url, timeout=2)
44+
SR.set_auth(username=user_username, password=user_password)
45+
SR.open()
46+
response = SR.login(username=username, password=password)
47+
SR.close()
48+
assert response["userName"] == username
49+
assert response["roles"] == roles
50+
51+
asyncio.run(testing())
52+
53+
2254
def test_comm():
2355
SR = SaveRestoreAPI_Threads(base_url=base_url, timeout=2)
2456
SR.set_auth(username=user_username, password=user_password)

0 commit comments

Comments
 (0)