Skip to content

Commit c5574e8

Browse files
committed
Refactor mc fixtures and use secrets for secure password generation
1 parent c6a118d commit c5574e8

File tree

1 file changed

+38
-45
lines changed

1 file changed

+38
-45
lines changed

mergin/test/test_client.py

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pytz
1212
import sqlite3
1313
import glob
14+
import secrets
1415
from unittest.mock import patch, Mock
1516

1617
from unittest.mock import patch, Mock
@@ -55,25 +56,29 @@
5556
USER_PWD = os.environ.get("TEST_API_PASSWORD")
5657
API_USER2 = os.environ.get("TEST_API_USERNAME2")
5758
USER_PWD2 = os.environ.get("TEST_API_PASSWORD2")
59+
PASSWORD_DEFAULT = PASSWORD_DEFAULT = secrets.token_urlsafe(10)
60+
DEFAULT_OVERRIDES = {"projects": 100, "api_allowed": True}
5861
TMP_DIR = tempfile.gettempdir()
5962
TEST_DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_data")
6063
CHANGED_SCHEMA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "modified_schema")
6164

6265
json_headers = {"Content-Type": "application/json"}
6366

6467

65-
def get_default_overrides():
66-
return {"projects": 100, "api_allowed": True}
67-
68-
6968
def get_limit_overrides(storage: int):
7069
return {"storage": storage, "projects": 2, "api_allowed": True}
7170

7271

73-
def teardown(mc: MerginClient, client_workspace_id):
72+
def reset_workspace_limits(mc: MerginClient, client_workspace_id):
73+
"""
74+
Resets workspace storage and project limits to default values after testing.
75+
76+
This reset applies specifically to the pre-created test user's workspace.
77+
It is not intended for use with dynamically generated test users.
78+
"""
7479
mc.patch(
7580
f"/v1/tests/workspaces/{client_workspace_id}",
76-
{"limits_override": get_default_overrides()},
81+
{"limits_override": DEFAULT_OVERRIDES},
7782
{"Content-Type": "application/json"},
7883
)
7984

@@ -82,21 +87,18 @@ def create_project_path(name, mc):
8287
return mc.username() + "/" + name
8388

8489

85-
@pytest.fixture(scope="function")
86-
def mc():
87-
assert SERVER_URL and SERVER_URL.rstrip("/") != "https://app.merginmaps.com"
88-
89-
if API_USER != "" and USER_PWD != "": # if API_USER and USER_PWD is provided we log in user and use him for tests
90-
user = API_USER
91-
password = USER_PWD
90+
def create_mc_client_and_workspace(api_user, user_pwd, test_tag=""):
91+
if api_user != "" and user_pwd != "": # if API_USER and USER_PWD is provided we log in user and use him for tests
92+
user = api_user
93+
password = user_pwd
9294

9395
client = create_client(user, password)
94-
yield client
96+
return client
9597

9698
else: # if user is not provided we create one
9799
# user emial is generated with random
98-
user = f"apitest_{create_random_suffix()}@example.com"
99-
password = "testpass123"
100+
user = f"apitest{test_tag}_{create_random_suffix()}@example.com"
101+
password = PASSWORD_DEFAULT
100102

101103
anon_client = MerginClient(SERVER_URL)
102104
anon_client.post(
@@ -111,39 +113,30 @@ def mc():
111113
create_workspace_for_client(client)
112114

113115
# we yield client
114-
yield client
115-
116-
# afterwards we disable client and workspace
117-
delete_test_user_and_workspace(client)
116+
return client
118117

119118

120119
@pytest.fixture(scope="function")
121-
def mc2():
120+
def mc():
122121
assert SERVER_URL and SERVER_URL.rstrip("/") != "https://app.merginmaps.com"
123122

124-
if API_USER2 != "" and USER_PWD2 != "":
125-
user = API_USER2
126-
password = USER_PWD2
123+
client = create_mc_client_and_workspace(API_USER, USER_PWD)
127124

128-
client = create_client(user, password)
129-
yield client
130-
else:
131-
user = f"apitest2_{create_random_suffix()}@example.com"
132-
password = "testpass123"
125+
yield client
133126

134-
anon_client = MerginClient(SERVER_URL)
135-
anon_client.post(
136-
"/v1/tests/users",
137-
{"email": user, "password": password},
138-
json_headers,
139-
validate_auth=False,
140-
)
127+
if API_USER == "" or USER_PWD == "":
128+
delete_test_user_and_workspace(client)
141129

142-
client = create_client(user, password)
143-
create_workspace_for_client(client)
144130

145-
yield client
131+
@pytest.fixture(scope="function")
132+
def mc2():
133+
assert SERVER_URL and SERVER_URL.rstrip("/") != "https://app.merginmaps.com"
134+
135+
client = create_mc_client_and_workspace(API_USER2, USER_PWD2, test_tag="2")
136+
137+
yield client
146138

139+
if API_USER2 == "" or USER_PWD2 == "":
147140
delete_test_user_and_workspace(client)
148141

149142

@@ -161,12 +154,12 @@ def create_user_in_workspace(workspace_id: int, mc: MerginClient, role: Workspac
161154

162155
client = mc.create_user(
163156
email=f"apitest_userInWorkspace{create_random_suffix()}@example.com",
164-
password="Testpass123",
157+
password=PASSWORD_DEFAULT,
165158
workspace_id=workspace_id,
166159
workspace_role=role,
167160
)
168161

169-
return MerginClient(url=SERVER_URL, login=client["email"], password="Testpass123")
162+
return MerginClient(url=SERVER_URL, login=client["email"], password=PASSWORD_DEFAULT)
170163

171164

172165
def create_random_suffix():
@@ -987,7 +980,7 @@ def test_available_workspace_storage(mc: MerginClient):
987980
# remove dummy big file from a disk
988981
remove_folders([project_dir])
989982

990-
teardown(mc, client_workspace_id)
983+
reset_workspace_limits(mc, client_workspace_id)
991984

992985

993986
def test_available_storage_validation2(mc, mc2):
@@ -1575,7 +1568,7 @@ def test_push_gpkg_schema_change(mc):
15751568
# at this point we still have an open sqlite connection to the GPKG, so checkpointing will not work correctly)
15761569
mc.push_project(project_dir)
15771570

1578-
subprocess.run(["sleep", "15"])
1571+
subprocess.run(["sleep", "5"])
15791572

15801573
# WITH TWO SQLITE copies: fails here (sqlite3.OperationalError: disk I/O error) + in geodiff log: SQLITE3: (283)recovered N frames from WAL file
15811574
_check_test_table(test_gpkg)
@@ -2885,7 +2878,7 @@ def test_error_projects_limit_hit(mc: MerginClient):
28852878
assert e.value.http_method == "POST"
28862879
assert e.value.url == f"{mc.url}v1/project/{mc.username()}"
28872880

2888-
teardown(mc, client_workspace_id)
2881+
reset_workspace_limits(mc, client_workspace_id)
28892882

28902883

28912884
def test_workspace_requests(mc: MerginClient):
@@ -3131,7 +3124,7 @@ def test_validate_auth(mc: MerginClient):
31313124
# ----- Client with token and username/password -----
31323125
# create a client with valid auth token based on other MerginClient instance with username/password that allows relogin if the token is expired
31333126
mc_auth_token_login = MerginClient(
3134-
SERVER_URL, auth_token=mc._auth_session["token"], login=mc.username(), password="testpass123"
3127+
SERVER_URL, auth_token=mc._auth_session["token"], login=mc.username(), password=PASSWORD_DEFAULT
31353128
)
31363129

31373130
# this should pass and not raise an error

0 commit comments

Comments
 (0)