Skip to content

Commit c6a118d

Browse files
committed
Add option to run tests using predefined users
1 parent 23bca59 commit c6a118d

File tree

3 files changed

+87
-46
lines changed

3 files changed

+87
-46
lines changed

.github/workflows/autotests.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ name: Auto Tests
22
on: [push]
33
env:
44
TEST_MERGIN_URL: https://app.dev.merginmaps.com/
5-
TEST_API_USERNAME: test_plugin
6-
TEST_API_PASSWORD: ${{ secrets.MERGINTEST_API_PASSWORD }}
7-
TEST_API_USERNAME2: test_plugin2
8-
TEST_API_PASSWORD2: ${{ secrets.MERGINTEST_API_PASSWORD2 }}
95

106
concurrency:
117
group: ci-${{github.ref}}-autotests

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ For running test do:
188188
export TEST_API_PASSWORD=<pwd>
189189
export TEST_API_USERNAME2=<username2>
190190
export TEST_API_PASSWORD2=<pwd2>
191-
# workspace name with controlled available storage space (e.g. 20MB), default value: testpluginstorage
192-
export TEST_STORAGE_WORKSPACE=<workspacename>
193191
pip install pytest pytest-cov coveralls
194192
pytest --cov-report html --cov=mergin mergin/test/
195193
```

mergin/test/test_client.py

Lines changed: 87 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,26 @@
5858
TMP_DIR = tempfile.gettempdir()
5959
TEST_DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_data")
6060
CHANGED_SCHEMA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "modified_schema")
61-
STORAGE_WORKSPACE = os.environ.get("TEST_STORAGE_WORKSPACE", "testpluginstorage")
6261

6362
json_headers = {"Content-Type": "application/json"}
6463

6564

65+
def get_default_overrides():
66+
return {"projects": 100, "api_allowed": True}
67+
68+
6669
def get_limit_overrides(storage: int):
6770
return {"storage": storage, "projects": 2, "api_allowed": True}
6871

6972

73+
def teardown(mc: MerginClient, client_workspace_id):
74+
mc.patch(
75+
f"/v1/tests/workspaces/{client_workspace_id}",
76+
{"limits_override": get_default_overrides()},
77+
{"Content-Type": "application/json"},
78+
)
79+
80+
7081
def create_project_path(name, mc):
7182
return mc.username() + "/" + name
7283

@@ -75,54 +86,81 @@ def create_project_path(name, mc):
7586
def mc():
7687
assert SERVER_URL and SERVER_URL.rstrip("/") != "https://app.merginmaps.com"
7788

78-
user = f"apitest_{create_random_suffix()}@example.com"
79-
password = "testpass123"
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
8092

81-
anon_client = MerginClient(SERVER_URL)
82-
anon_client.post(
83-
"/v1/tests/users",
84-
{"email": user, "password": password},
85-
json_headers,
86-
validate_auth=False,
87-
)
93+
client = create_client(user, password)
94+
yield client
8895

89-
client = create_client(user, password)
90-
info = client.user_info()
91-
user_id = info["id"]
92-
create_workspace_for_client(client)
96+
else: # if user is not provided we create one
97+
# user emial is generated with random
98+
user = f"apitest_{create_random_suffix()}@example.com"
99+
password = "testpass123"
93100

94-
yield client
101+
anon_client = MerginClient(SERVER_URL)
102+
anon_client.post(
103+
"/v1/tests/users",
104+
{"email": user, "password": password},
105+
json_headers,
106+
validate_auth=False,
107+
)
95108

96-
anon_client.delete(f"/v1/tests/users/{user_id}", validate_auth=False)
109+
# create MerginClient object and workspace for him
110+
client = create_client(user, password)
111+
create_workspace_for_client(client)
112+
113+
# we yield client
114+
yield client
115+
116+
# afterwards we disable client and workspace
117+
delete_test_user_and_workspace(client)
97118

98119

99120
@pytest.fixture(scope="function")
100121
def mc2():
101122
assert SERVER_URL and SERVER_URL.rstrip("/") != "https://app.merginmaps.com"
102123

103-
user = f"apitest2_{create_random_suffix()}@example.com"
104-
password = "testpass123"
124+
if API_USER2 != "" and USER_PWD2 != "":
125+
user = API_USER2
126+
password = USER_PWD2
105127

106-
anon_client = MerginClient(SERVER_URL)
107-
anon_client.post(
108-
"/v1/tests/users",
109-
{"email": user, "password": password},
110-
json_headers,
111-
validate_auth=False,
112-
)
128+
client = create_client(user, password)
129+
yield client
130+
else:
131+
user = f"apitest2_{create_random_suffix()}@example.com"
132+
password = "testpass123"
133+
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+
)
141+
142+
client = create_client(user, password)
143+
create_workspace_for_client(client)
144+
145+
yield client
113146

114-
client = create_client(user, password)
147+
delete_test_user_and_workspace(client)
148+
149+
150+
def delete_test_user_and_workspace(client: MerginClient):
151+
"""Takes MC object and call delete /v1/tests/users/{user_id} which sets inactive_sice and set active to false."""
115152
info = client.user_info()
116153
user_id = info["id"]
117-
create_workspace_for_client(client)
118-
yield client
119154

155+
anon_client = MerginClient(SERVER_URL)
120156
anon_client.delete(f"/v1/tests/users/{user_id}", validate_auth=False)
121157

122158

123159
def create_user_in_workspace(workspace_id: int, mc: MerginClient, role: WorkspaceRole):
160+
"""Creates user inside of workspace"""
161+
124162
client = mc.create_user(
125-
email=f"apitest_userInWorkspace_{create_random_suffix()}@example.com",
163+
email=f"apitest_userInWorkspace{create_random_suffix()}@example.com",
126164
password="Testpass123",
127165
workspace_id=workspace_id,
128166
workspace_role=role,
@@ -906,7 +944,7 @@ def test_available_workspace_storage(mc: MerginClient):
906944

907945
# get info about storage capacity
908946
storage_remaining = 0
909-
client_workspace = mc.workspaces_list()[0] # ask for alternative
947+
client_workspace = mc.workspaces_list()[0]
910948

911949
current_storage = client_workspace["storage"]
912950
client_workspace_id = client_workspace["id"]
@@ -949,6 +987,8 @@ def test_available_workspace_storage(mc: MerginClient):
949987
# remove dummy big file from a disk
950988
remove_folders([project_dir])
951989

990+
teardown(mc, client_workspace_id)
991+
952992

953993
def test_available_storage_validation2(mc, mc2):
954994
"""
@@ -2715,7 +2755,7 @@ def test_editor(mc: MerginClient):
27152755
def test_editor_push(mc: MerginClient):
27162756
"""Test push with editor"""
27172757

2718-
test_project_name = "test_editor_push"
2758+
test_project_name = f"test_editor_push{create_random_suffix()}"
27192759
test_project_fullname = create_project_path(test_project_name, mc)
27202760

27212761
mc.create_project(test_project_fullname)
@@ -2724,9 +2764,6 @@ def test_editor_push(mc: MerginClient):
27242764

27252765
mc2 = create_user_in_workspace(mc_workspace_id, mc, WorkspaceRole.READER)
27262766

2727-
if not mc.has_editor_support():
2728-
return
2729-
27302767
project_dir = os.path.join(TMP_DIR, test_project_name)
27312768
project_dir2 = os.path.join(TMP_DIR, test_project_name + "_2")
27322769
cleanup(mc, test_project_fullname, [project_dir, project_dir2])
@@ -2801,10 +2838,11 @@ def test_editor_push(mc: MerginClient):
28012838
conflicted_file = project_file
28022839
# There is no conflicted qgs file
28032840
assert conflicted_file is None
2841+
delete_test_user_and_workspace(mc2)
28042842

28052843

28062844
def test_error_push_already_named_project(mc: MerginClient):
2807-
test_project = "test_push_already_existing"
2845+
test_project = f"test_push_already_existing{create_random_suffix()}"
28082846
project_dir = os.path.join(TMP_DIR, test_project)
28092847
project_name = create_project_path(test_project, mc)
28102848

@@ -2826,7 +2864,7 @@ def test_error_projects_limit_hit(mc: MerginClient):
28262864
project_dir = os.path.join(TMP_DIR, test_project, mc.username())
28272865
cleanup(mc, test_project, [project_dir])
28282866

2829-
client_workspace = mc.workspaces_list()[0] # ask for alternative
2867+
client_workspace = mc.workspaces_list()[0]
28302868

28312869
client_workspace_id = client_workspace["id"]
28322870
client_workspace_storage = client_workspace["storage"]
@@ -2847,24 +2885,26 @@ def test_error_projects_limit_hit(mc: MerginClient):
28472885
assert e.value.http_method == "POST"
28482886
assert e.value.url == f"{mc.url}v1/project/{mc.username()}"
28492887

2888+
teardown(mc, client_workspace_id)
2889+
28502890

28512891
def test_workspace_requests(mc: MerginClient):
2852-
test_project = "test_permissions"
2892+
test_project = f"test_permissions{create_random_suffix()}"
28532893
test_project_fullname = create_project_path(test_project, mc)
28542894
mc.create_project(test_project_fullname)
28552895
project_info = mc.project_info(test_project_fullname)
28562896
ws_id = project_info.get("workspace_id")
28572897

2858-
mc2 = create_user_in_workspace(ws_id, mc, WorkspaceRole.OWNER)
2898+
user_in_workspace: MerginClient = create_user_in_workspace(ws_id, mc, WorkspaceRole.OWNER)
28592899

2860-
usage = mc2.workspace_usage(ws_id)
2900+
usage = user_in_workspace.workspace_usage(ws_id)
28612901
# Check type and common value
28622902
assert type(usage) == dict
28632903
assert usage["api"]["allowed"] == True
28642904
assert usage["history"]["quota"] > 0
28652905
assert usage["history"]["usage"] == 0
28662906

2867-
service = mc2.workspace_service(ws_id)
2907+
service = user_in_workspace.workspace_service(ws_id)
28682908
# Check type and common value
28692909
assert type(service) == dict
28702910
assert service["action_required"] == False
@@ -2874,6 +2914,13 @@ def test_workspace_requests(mc: MerginClient):
28742914
assert service["plan"]["type"] == "trial"
28752915
assert service["subscription"] == None
28762916

2917+
info = user_in_workspace.user_info()
2918+
user_id = info["id"]
2919+
2920+
# need to change role != OWNER because endpoint deletes workspaces where user is OWNER causing mc workspace to be disabled
2921+
user_in_workspace.update_workspace_member(ws_id, user_id, workspace_role=WorkspaceRole.READER)
2922+
delete_test_user_and_workspace(user_in_workspace)
2923+
28772924

28782925
def test_access_management(mc: MerginClient, mc2: MerginClient):
28792926
# create a user in the workspace -

0 commit comments

Comments
 (0)