|
18 | 18 | base_url = "http://localhost:8080/save-restore" |
19 | 19 |
|
20 | 20 |
|
21 | | -def test_version(): |
| 21 | +def test_version_01(): |
| 22 | + """ |
| 23 | + Test that the versioning works correctly. |
| 24 | + """ |
22 | 25 | assert importlib.metadata.version("save_and_restore_api") == save_and_restore_api.__version__ |
23 | 26 |
|
24 | 27 |
|
| 28 | +# # fmt: off |
| 29 | +# @pytest.mark.parametrize("library", ["THREADS", "ASYNC"]) |
| 30 | +# # fmt: on |
| 31 | +# def test_api_call_01(library): |
| 32 | +# """ |
| 33 | +# Test generic API call |
| 34 | +# """ |
| 35 | +# username, password = user_username, user_password |
| 36 | + |
| 37 | +# if not _is_async(library): |
| 38 | +# SR = SaveRestoreAPI_Threads(base_url=base_url, timeout=2) |
| 39 | +# SR.set_auth(username=user_username, password=user_password) |
| 40 | +# SR.open() |
| 41 | +# response = SR.login(username=username, password=password) |
| 42 | +# assert response["userName"] == username |
| 43 | +# SR.close() |
| 44 | +# SR.open() |
| 45 | +# response = SR.login(username=username, password=password) |
| 46 | +# assert response["userName"] == username |
| 47 | +# SR.close() |
| 48 | +# else: |
| 49 | +# async def testing(): |
| 50 | +# SR = SaveRestoreAPI_Async(base_url=base_url, timeout=2) |
| 51 | +# SR.set_auth(username=user_username, password=user_password) |
| 52 | +# SR.open() |
| 53 | +# response = await SR.login(username=username, password=password) |
| 54 | +# assert response["userName"] == username |
| 55 | +# await SR.close() |
| 56 | +# SR.open() |
| 57 | +# response = await SR.login(username=username, password=password) |
| 58 | +# assert response["userName"] == username |
| 59 | +# await SR.close() |
| 60 | + |
| 61 | +# asyncio.run(testing()) |
| 62 | + |
| 63 | + |
25 | 64 | # fmt: off |
26 | 65 | @pytest.mark.parametrize("library", ["THREADS", "ASYNC"]) |
27 | 66 | @pytest.mark.parametrize("username, password, roles, code", [ |
28 | 67 | (admin_username, admin_password, ["ROLE_SAR-ADMIN"], 200), |
29 | 68 | (user_username, user_password, ["ROLE_SAR-USER"], 200), |
30 | 69 | (read_username, read_password, [], 200), |
31 | | - (user_username, read_password, [], 401), |
| 70 | + (user_username, read_password, [], 401), # Incorrect password |
| 71 | + (user_username + "_a", user_password, [], 401), # Incorrect login |
32 | 72 | ]) |
33 | 73 | # fmt: on |
34 | 74 | def test_login_01(username, password, roles, library, code): |
| 75 | + """ |
| 76 | + Tests for the 'login' API. |
| 77 | + """ |
35 | 78 | if not _is_async(library): |
36 | | - SR = SaveRestoreAPI_Threads(base_url=base_url, timeout=2) |
37 | | - SR.set_auth(username=user_username, password=user_password) |
38 | | - SR.open() |
39 | | - if code == 200: |
40 | | - response = SR.login(username=username, password=password) |
41 | | - assert response["userName"] == username |
42 | | - assert response["roles"] == roles |
43 | | - else: |
44 | | - with pytest.raises(SR.HTTPClientError, match=f"{code}"): |
45 | | - SR.login(username=username, password=password) |
46 | | - SR.close() |
47 | | - else: |
48 | | - async def testing(): |
49 | | - SR = SaveRestoreAPI_Async(base_url=base_url, timeout=2) |
50 | | - SR.set_auth(username=user_username, password=user_password) |
| 79 | + with SaveRestoreAPI_Threads(base_url=base_url, timeout=2) as SR: |
51 | 80 | SR.open() |
52 | 81 | if code == 200: |
53 | | - response = await SR.login(username=username, password=password) |
| 82 | + response = SR.login(username=username, password=password) |
54 | 83 | assert response["userName"] == username |
55 | 84 | assert response["roles"] == roles |
56 | 85 | else: |
57 | 86 | with pytest.raises(SR.HTTPClientError, match=f"{code}"): |
58 | | - await SR.login(username=username, password=password) |
59 | | - await SR.close() |
| 87 | + SR.login(username=username, password=password) |
| 88 | + else: |
| 89 | + async def testing(): |
| 90 | + async with SaveRestoreAPI_Async(base_url=base_url, timeout=2) as SR: |
| 91 | + SR.open() |
| 92 | + if code == 200: |
| 93 | + response = await SR.login(username=username, password=password) |
| 94 | + assert response["userName"] == username |
| 95 | + assert response["roles"] == roles |
| 96 | + else: |
| 97 | + with pytest.raises(SR.HTTPClientError, match=f"{code}"): |
| 98 | + await SR.login(username=username, password=password) |
60 | 99 |
|
61 | 100 | asyncio.run(testing()) |
62 | 101 |
|
63 | 102 |
|
| 103 | +# fmt: off |
| 104 | +@pytest.mark.parametrize("library", ["THREADS", "ASYNC"]) |
| 105 | +@pytest.mark.parametrize("node_uid, code", [ |
| 106 | + (SaveRestoreAPI_Threads.ROOT_NODE_UID, 200), |
| 107 | + ("abc", 404), |
| 108 | +]) |
| 109 | +# fmt: on |
| 110 | +def test_get_node_01(node_uid, library, code): |
| 111 | + """ |
| 112 | + Tests for the 'login' API. |
| 113 | + """ |
| 114 | + if not _is_async(library): |
| 115 | + with SaveRestoreAPI_Threads(base_url=base_url, timeout=2) as SR: |
| 116 | + SR.open() |
| 117 | + if code == 200: |
| 118 | + response = SR.get_node(node_uid) |
| 119 | + assert response["uniqueId"] == node_uid |
| 120 | + else: |
| 121 | + with pytest.raises(SR.HTTPClientError, match=f"{code}"): |
| 122 | + SR.get_node(node_uid) |
| 123 | + else: |
| 124 | + async def testing(): |
| 125 | + async with SaveRestoreAPI_Async(base_url=base_url, timeout=2) as SR: |
| 126 | + SR.open() |
| 127 | + if code == 200: |
| 128 | + response = await SR.get_node(node_uid) |
| 129 | + assert response["uniqueId"] == node_uid |
| 130 | + else: |
| 131 | + with pytest.raises(SR.HTTPClientError, match=f"{code}"): |
| 132 | + await SR.get_node(node_uid) |
| 133 | + |
| 134 | + asyncio.run(testing()) |
| 135 | + |
| 136 | + |
| 137 | + |
64 | 138 | def test_comm(): |
65 | 139 | SR = SaveRestoreAPI_Threads(base_url=base_url, timeout=2) |
66 | 140 | SR.set_auth(username=user_username, password=user_password) |
67 | 141 | SR.open() |
68 | 142 | SR.login(username="user", password="userPass") |
69 | 143 | SR.get_node(SR.ROOT_NODE_UID) |
70 | 144 | SR.close() |
71 | | - |
72 | | - |
73 | | -@pytest.mark.asyncio |
74 | | -async def test_comm_async(): |
75 | | - SR = SaveRestoreAPI_Async(base_url=base_url, timeout=2) |
76 | | - SR.set_auth(username="user", password="userPass") |
77 | | - SR.open() |
78 | | - await SR.login(username="user", password="userPass") |
79 | | - await SR.get_node(SR.ROOT_NODE_UID) |
80 | | - await SR.close() |
|
0 commit comments