From f795680cc4f40e5c213907233faaa72a3ede58db Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Wed, 13 Nov 2024 09:03:14 -0300 Subject: [PATCH 1/7] adding MonthlyContributorsError to pyApi --- mergin/common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mergin/common.py b/mergin/common.py index 297ec3e1..05596cf2 100644 --- a/mergin/common.py +++ b/mergin/common.py @@ -14,6 +14,7 @@ class ErrorCode(Enum): ProjectsLimitHit = "ProjectsLimitHit" StorageLimitHit = "StorageLimitHit" + MonthlyContributorsError = "MonthlyContributorsError" class ClientError(Exception): From ac3f6841736df3f8763a61e96f314b22d6c66131 Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Thu, 21 Nov 2024 09:17:53 -0300 Subject: [PATCH 2/7] support for MonthlyContributorsLimitHit --- mergin/common.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mergin/common.py b/mergin/common.py index 05596cf2..1e4ba8fc 100644 --- a/mergin/common.py +++ b/mergin/common.py @@ -14,8 +14,7 @@ class ErrorCode(Enum): ProjectsLimitHit = "ProjectsLimitHit" StorageLimitHit = "StorageLimitHit" - MonthlyContributorsError = "MonthlyContributorsError" - + MonthlyContributorsLimitHit = "MonthlyContributorsLimitHit" class ClientError(Exception): def __init__(self, detail, url=None, server_code=None, server_response=None, http_error=None, http_method=None): From 3f41a6197e55e7fd14a807cf1222c67e21a91c5f Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Thu, 21 Nov 2024 09:23:34 -0300 Subject: [PATCH 3/7] code formatting --- mergin/common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mergin/common.py b/mergin/common.py index 1e4ba8fc..8ac5fbb8 100644 --- a/mergin/common.py +++ b/mergin/common.py @@ -16,6 +16,7 @@ class ErrorCode(Enum): StorageLimitHit = "StorageLimitHit" MonthlyContributorsLimitHit = "MonthlyContributorsLimitHit" + class ClientError(Exception): def __init__(self, detail, url=None, server_code=None, server_response=None, http_error=None, http_method=None): self.detail = detail From b34dae3630866ee96bdab0fbe5f64cc8dd1fc551 Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Tue, 26 Nov 2024 11:50:30 -0300 Subject: [PATCH 4/7] commenting monthlyContributorsErrorTest and adding TODO plus code style fix --- mergin/test/test_client.py | 41 +++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/mergin/test/test_client.py b/mergin/test/test_client.py index 458eef63..3c66f2cb 100644 --- a/mergin/test/test_client.py +++ b/mergin/test/test_client.py @@ -83,7 +83,14 @@ def teardown(): # back to original values... (1 project, api allowed ...) client.patch( f"/v1/tests/workspaces/{client_workspace_id}", - {"limits_override": {"storage": client_workspace_storage, "projects": 1, "api_allowed": True}}, + { + "limits_override": { + "storage": client_workspace_storage, + "projects": 1, + "monthly_contributors": 1000, + "api_allowed": True, + } + }, {"Content-Type": "application/json"}, ) @@ -2711,3 +2718,35 @@ def test_error_projects_limit_hit(mcStorage: MerginClient): assert e.value.http_error == 422 assert e.value.http_method == "POST" assert e.value.url == f"{mcStorage.url}v1/project/testpluginstorage" + + +# TODO: refactor tests to create workspaces on each run and apply test_error_monthly_contributors_limit_hit +# def test_error_monthly_contributors_limit_hit(mcStorage: MerginClient): +# test_project = "test_monthly_contributors_limit_hit" +# test_project_fullname = STORAGE_WORKSPACE + "/" + test_project + +# client_workspace = None +# for workspace in mcStorage.workspaces_list(): +# if workspace["name"] == STORAGE_WORKSPACE: +# client_workspace = workspace +# break + +# client_workspace_id = client_workspace["id"] +# mcStorage.patch( +# f"/v1/tests/workspaces/{client_workspace_id}", +# {"limits_override": {"monthly_contributors": 0, "api_allowed": True}}, +# {"Content-Type": "application/json"}, +# ) + +# with pytest.raises(ClientError) as e: +# mcStorage.create_project_and_push(test_project_fullname, project_dir) + +# assert e.value.server_code == ErrorCode.MonthlyContributorsLimitHit.value +# assert e.value.detail == ( +# "Maximum number of workspace contributors is reached. " +# "Please upgrade your subscription to push changes or create projects." +# ) +# assert e.value.http_error == 422 +# assert e.value.http_method == "POST" +# assert e.value.url == f"{mc.url}v1/project/testpluginstorage" +# assert e.value.contributors_quota == 0 From b979251f20a74d586ac9c515be2c7188505faadf Mon Sep 17 00:00:00 2001 From: VitorVieiraZ Date: Tue, 26 Nov 2024 12:09:33 -0300 Subject: [PATCH 5/7] reverting teardown --- mergin/test/test_client.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/mergin/test/test_client.py b/mergin/test/test_client.py index 3c66f2cb..505ea300 100644 --- a/mergin/test/test_client.py +++ b/mergin/test/test_client.py @@ -83,14 +83,7 @@ def teardown(): # back to original values... (1 project, api allowed ...) client.patch( f"/v1/tests/workspaces/{client_workspace_id}", - { - "limits_override": { - "storage": client_workspace_storage, - "projects": 1, - "monthly_contributors": 1000, - "api_allowed": True, - } - }, + {"limits_override": {"storage": client_workspace_storage, "projects": 1, "api_allowed": True}}, {"Content-Type": "application/json"}, ) From 07abda886f0815e3c4778e9a4364eb48bee90ffb Mon Sep 17 00:00:00 2001 From: "marcel.kocisek" Date: Thu, 28 Nov 2024 11:24:57 +0100 Subject: [PATCH 6/7] Fix tests for monthly contributors: - add cleanup of project directory for better handling of errors in local machine - introduce default limit overrides in method get_limit_overries - make contributor limit hit test working Improvement: - make internal error code as printable in ClientError --- mergin/common.py | 2 + mergin/test/test_client.py | 79 +++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/mergin/common.py b/mergin/common.py index 8ac5fbb8..f97ddb37 100644 --- a/mergin/common.py +++ b/mergin/common.py @@ -37,6 +37,8 @@ def __str__(self): string_res += f"URL: {self.url}\n" if self.http_method: string_res += f"Method: {self.http_method}\n" + if self.server_code: + string_res += f"Error code: {self.server_code}\n" if self.extra: string_res += f"{self.extra}\n" return string_res diff --git a/mergin/test/test_client.py b/mergin/test/test_client.py index 505ea300..aecbf589 100644 --- a/mergin/test/test_client.py +++ b/mergin/test/test_client.py @@ -52,6 +52,10 @@ STORAGE_WORKSPACE = os.environ.get("TEST_STORAGE_WORKSPACE", "testpluginstorage") +def get_limit_overrides(storage: int): + return {"storage": storage, "projects": 2, "api_allowed": True, "monthly_contributors": -1} + + @pytest.fixture(scope="function") def mc(): client = create_client(API_USER, USER_PWD) @@ -70,7 +74,6 @@ def mc2(): def mcStorage(request): client = create_client(API_USER, USER_PWD) workspace_name = create_workspace_for_client(client, STORAGE_WORKSPACE) - print(workspace_name) client_workspace = None for workspace in client.workspaces_list(): if workspace["name"] == workspace_name: @@ -83,7 +86,7 @@ def teardown(): # back to original values... (1 project, api allowed ...) client.patch( f"/v1/tests/workspaces/{client_workspace_id}", - {"limits_override": {"storage": client_workspace_storage, "projects": 1, "api_allowed": True}}, + {"limits_override": get_limit_overrides(client_workspace_storage)}, {"Content-Type": "application/json"}, ) @@ -2683,8 +2686,10 @@ def test_error_push_already_named_project(mc: MerginClient): def test_error_projects_limit_hit(mcStorage: MerginClient): - test_project = "test_another_project_above_projects_limit" + test_project = "project_above_projects_limit" test_project_fullname = STORAGE_WORKSPACE + "/" + test_project + project_dir = os.path.join(TMP_DIR, test_project, API_USER) + cleanup(mcStorage, test_project, [project_dir]) client_workspace = None for workspace in mcStorage.workspaces_list(): @@ -2695,12 +2700,10 @@ def test_error_projects_limit_hit(mcStorage: MerginClient): client_workspace_storage = client_workspace["storage"] mcStorage.patch( f"/v1/tests/workspaces/{client_workspace_id}", - {"limits_override": {"storage": client_workspace_storage, "projects": 0, "api_allowed": True}}, + {"limits_override": {**get_limit_overrides(client_workspace_storage), "projects": 0}}, {"Content-Type": "application/json"}, ) - project_dir = os.path.join(TMP_DIR, test_project, API_USER) - with pytest.raises(ClientError) as e: mcStorage.create_project_and_push(test_project_fullname, project_dir) assert e.value.server_code == ErrorCode.ProjectsLimitHit.value @@ -2714,32 +2717,38 @@ def test_error_projects_limit_hit(mcStorage: MerginClient): # TODO: refactor tests to create workspaces on each run and apply test_error_monthly_contributors_limit_hit -# def test_error_monthly_contributors_limit_hit(mcStorage: MerginClient): -# test_project = "test_monthly_contributors_limit_hit" -# test_project_fullname = STORAGE_WORKSPACE + "/" + test_project - -# client_workspace = None -# for workspace in mcStorage.workspaces_list(): -# if workspace["name"] == STORAGE_WORKSPACE: -# client_workspace = workspace -# break - -# client_workspace_id = client_workspace["id"] -# mcStorage.patch( -# f"/v1/tests/workspaces/{client_workspace_id}", -# {"limits_override": {"monthly_contributors": 0, "api_allowed": True}}, -# {"Content-Type": "application/json"}, -# ) - -# with pytest.raises(ClientError) as e: -# mcStorage.create_project_and_push(test_project_fullname, project_dir) - -# assert e.value.server_code == ErrorCode.MonthlyContributorsLimitHit.value -# assert e.value.detail == ( -# "Maximum number of workspace contributors is reached. " -# "Please upgrade your subscription to push changes or create projects." -# ) -# assert e.value.http_error == 422 -# assert e.value.http_method == "POST" -# assert e.value.url == f"{mc.url}v1/project/testpluginstorage" -# assert e.value.contributors_quota == 0 +def test_error_monthly_contributors_limit_hit(mcStorage: MerginClient): + test_project = "test_monthly_contributors_limit_hit" + project_dir = os.path.join(TMP_DIR, test_project) + test_project_fullname = STORAGE_WORKSPACE + "/" + test_project + cleanup(mcStorage, test_project_fullname, [project_dir]) + + client_workspace = None + for workspace in mcStorage.workspaces_list(): + if workspace["name"] == STORAGE_WORKSPACE: + client_workspace = workspace + break + + client_workspace_id = client_workspace["id"] + client_workspace_storage = client_workspace["storage"] + mcStorage.patch( + f"/v1/tests/workspaces/{client_workspace_id}", + {"limits_override": {**get_limit_overrides(client_workspace_storage), "monthly_contributors": 0}}, + {"Content-Type": "application/json"}, + ) + + mcStorage.create_project_and_push(test_project_fullname, project_dir) + shutil.copy(os.path.join(TEST_DATA_DIR, "test.txt"), project_dir) + with pytest.raises(ClientError) as e: + mcStorage.push_project(project_dir) + + print(e.value.server_response) + assert e.value.server_code == ErrorCode.MonthlyContributorsLimitHit.value + assert e.value.detail == ( + "Maximum number of workspace contributors is reached. " + "Please upgrade your subscription to push changes or create projects. (MonthlyContributorsLimitHit)" + ) + assert e.value.http_error == 422 + assert e.value.http_method == "POST" + assert e.value.url == f"{mcStorage.url}v1/project/push/testpluginstorage/{test_project}" + assert e.value.server_response.get("contributors_quota") == 0 From b931d5cb3bc0a93811ff4bde00b25126f21a467d Mon Sep 17 00:00:00 2001 From: "marcel.kocisek" Date: Thu, 28 Nov 2024 11:39:21 +0100 Subject: [PATCH 7/7] Clenup print --- mergin/test/test_client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mergin/test/test_client.py b/mergin/test/test_client.py index aecbf589..6e35c6e5 100644 --- a/mergin/test/test_client.py +++ b/mergin/test/test_client.py @@ -2742,7 +2742,6 @@ def test_error_monthly_contributors_limit_hit(mcStorage: MerginClient): with pytest.raises(ClientError) as e: mcStorage.push_project(project_dir) - print(e.value.server_response) assert e.value.server_code == ErrorCode.MonthlyContributorsLimitHit.value assert e.value.detail == ( "Maximum number of workspace contributors is reached. "