Skip to content

Commit 3cc76f8

Browse files
authored
Merge pull request #219 from MerginMaps/first-implementation-limit-overrides
Implementing testing api for storage change to limits tests
2 parents d734ee4 + 007cf90 commit 3cc76f8

File tree

2 files changed

+74
-21
lines changed

2 files changed

+74
-21
lines changed

mergin/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ def post(self, path, data=None, headers={}):
237237
request = urllib.request.Request(url, data, headers, method="POST")
238238
return self._do_request(request)
239239

240+
def patch(self, path, data=None, headers={}):
241+
url = urllib.parse.urljoin(self.url, urllib.parse.quote(path))
242+
if headers.get("Content-Type", None) == "application/json":
243+
data = json.dumps(data, cls=DateTimeEncoder).encode("utf-8")
244+
request = urllib.request.Request(url, data, headers, method="PATCH")
245+
return self._do_request(request)
246+
240247
def is_server_compatible(self):
241248
"""
242249
Test whether version of the server meets the required set of endpoints.

mergin/test/test_client.py

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from ..utils import (
3333
generate_checksum,
3434
get_versions_with_file_changes,
35-
is_version_acceptable,
3635
unique_path_name,
3736
conflicted_copy_file_name,
3837
edit_conflict_file_name,
@@ -68,9 +67,27 @@ def mc2():
6867

6968

7069
@pytest.fixture(scope="function")
71-
def mcStorage():
70+
def mcStorage(request):
7271
client = create_client(API_USER, USER_PWD)
73-
create_workspace_for_client(client, STORAGE_WORKSPACE)
72+
workspace_name = create_workspace_for_client(client, STORAGE_WORKSPACE)
73+
print(workspace_name)
74+
client_workspace = None
75+
for workspace in client.workspaces_list():
76+
if workspace["name"] == workspace_name:
77+
client_workspace = workspace
78+
break
79+
client_workspace_id = client_workspace["id"]
80+
client_workspace_storage = client_workspace["storage"]
81+
82+
def teardown():
83+
# back to original values... (1 project, api allowed ...)
84+
client.patch(
85+
f"/v1/tests/workspaces/{client_workspace_id}",
86+
{"limits_override": {"storage": client_workspace_storage, "projects": 1, "api_allowed": True}},
87+
{"Content-Type": "application/json"},
88+
)
89+
90+
request.addfinalizer(teardown)
7491
return client
7592

7693

@@ -79,11 +96,13 @@ def create_client(user, pwd):
7996
return MerginClient(SERVER_URL, login=user, password=pwd)
8097

8198

82-
def create_workspace_for_client(mc: MerginClient, workspace_name=None):
99+
def create_workspace_for_client(mc: MerginClient, workspace_name=None) -> str:
100+
workspace_name = workspace_name or mc.username()
83101
try:
84-
mc.create_workspace(workspace_name or mc.username())
102+
mc.create_workspace(workspace_name)
85103
except ClientError:
86-
return
104+
pass
105+
return workspace_name
87106

88107

89108
def cleanup(mc, project, dirs):
@@ -745,12 +764,12 @@ def test_set_editor_access(mc):
745764
assert API_USER2 not in access["writersnames"]
746765

747766

748-
def test_available_storage_validation(mcStorage):
767+
def test_available_workspace_storage(mcStorage):
749768
"""
750769
Testing of storage limit - applies to user pushing changes into own project (namespace matching username).
751770
This test also tests giving read and write access to another user. Additionally tests also uploading of big file.
752771
"""
753-
test_project = "test_available_storage_validation"
772+
test_project = "test_available_workspace_storage"
754773
test_project_fullname = STORAGE_WORKSPACE + "/" + test_project
755774

756775
# cleanups
@@ -766,15 +785,28 @@ def test_available_storage_validation(mcStorage):
766785

767786
# get info about storage capacity
768787
storage_remaining = 0
788+
client_workspace = None
789+
for workspace in mcStorage.workspaces_list():
790+
if workspace["name"] == STORAGE_WORKSPACE:
791+
client_workspace = workspace
792+
break
793+
assert client_workspace is not None
794+
current_storage = client_workspace["storage"]
795+
client_workspace_id = client_workspace["id"]
796+
# 5 MB
797+
testing_storage = 5242880
798+
# add storage limit, to prevent creating too big files
799+
mcStorage.patch(
800+
f"/v1/tests/workspaces/{client_workspace_id}",
801+
{"limits_override": {"storage": testing_storage, "projects": 1, "api_allowed": True}},
802+
{"Content-Type": "application/json"},
803+
)
769804

770805
if mcStorage.server_type() == ServerType.OLD:
771806
user_info = mcStorage.user_info()
772-
storage_remaining = user_info["storage"] - user_info["disk_usage"]
807+
storage_remaining = testing_storage - user_info["disk_usage"]
773808
else:
774-
for workspace in mcStorage.workspaces_list():
775-
if workspace["name"] == STORAGE_WORKSPACE:
776-
storage_remaining = workspace["storage"] - workspace["disk_usage"]
777-
break
809+
storage_remaining = testing_storage - client_workspace["disk_usage"]
778810

779811
# generate dummy data (remaining storage + extra 1024b)
780812
dummy_data_path = project_dir + "/data"
@@ -789,15 +821,16 @@ def test_available_storage_validation(mcStorage):
789821
# Expecting "You have reached a data limit" 400 server error msg.
790822
assert "You have reached a data limit" in str(e)
791823
got_right_err = True
792-
assert got_right_err
824+
finally:
825+
assert got_right_err
793826

794-
# Expecting empty project
795-
project_info = get_project_info(mcStorage, STORAGE_WORKSPACE, test_project)
796-
assert project_info["version"] == "v0"
797-
assert project_info["disk_usage"] == 0
827+
# Expecting empty project
828+
project_info = get_project_info(mcStorage, STORAGE_WORKSPACE, test_project)
829+
assert project_info["version"] == "v0"
830+
assert project_info["disk_usage"] == 0
798831

799-
# remove dummy big file from a disk
800-
remove_folders([project_dir])
832+
# remove dummy big file from a disk
833+
remove_folders([project_dir])
801834

802835

803836
def test_available_storage_validation2(mc, mc2):
@@ -866,7 +899,7 @@ def get_project_info(mc, namespace, project_name):
866899
:param project_name: project's name
867900
:return: dict with project info
868901
"""
869-
projects = mc.projects_list(flag="created")
902+
projects = mc.projects_list(flag="created", namespace=namespace)
870903
test_project_list = [p for p in projects if p["name"] == project_name and p["namespace"] == namespace]
871904
assert len(test_project_list) == 1
872905
return test_project_list[0]
@@ -2653,6 +2686,19 @@ def test_error_projects_limit_hit(mcStorage: MerginClient):
26532686
test_project = "test_another_project_above_projects_limit"
26542687
test_project_fullname = STORAGE_WORKSPACE + "/" + test_project
26552688

2689+
client_workspace = None
2690+
for workspace in mcStorage.workspaces_list():
2691+
if workspace["name"] == STORAGE_WORKSPACE:
2692+
client_workspace = workspace
2693+
break
2694+
client_workspace_id = client_workspace["id"]
2695+
client_workspace_storage = client_workspace["storage"]
2696+
mcStorage.patch(
2697+
f"/v1/tests/workspaces/{client_workspace_id}",
2698+
{"limits_override": {"storage": client_workspace_storage, "projects": 0, "api_allowed": True}},
2699+
{"Content-Type": "application/json"},
2700+
)
2701+
26562702
project_dir = os.path.join(TMP_DIR, test_project, API_USER)
26572703

26582704
with pytest.raises(ClientError) as e:

0 commit comments

Comments
 (0)