Skip to content

Commit 74222e4

Browse files
authored
Merge pull request #221 from MerginMaps/monthlyContributorsError
Support for MonthlyContributorsLimitHit error
2 parents 3cc76f8 + b931d5c commit 74222e4

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

mergin/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
class ErrorCode(Enum):
1515
ProjectsLimitHit = "ProjectsLimitHit"
1616
StorageLimitHit = "StorageLimitHit"
17+
MonthlyContributorsLimitHit = "MonthlyContributorsLimitHit"
1718

1819

1920
class ClientError(Exception):
@@ -36,6 +37,8 @@ def __str__(self):
3637
string_res += f"URL: {self.url}\n"
3738
if self.http_method:
3839
string_res += f"Method: {self.http_method}\n"
40+
if self.server_code:
41+
string_res += f"Error code: {self.server_code}\n"
3942
if self.extra:
4043
string_res += f"{self.extra}\n"
4144
return string_res

mergin/test/test_client.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
STORAGE_WORKSPACE = os.environ.get("TEST_STORAGE_WORKSPACE", "testpluginstorage")
5353

5454

55+
def get_limit_overrides(storage: int):
56+
return {"storage": storage, "projects": 2, "api_allowed": True, "monthly_contributors": -1}
57+
58+
5559
@pytest.fixture(scope="function")
5660
def mc():
5761
client = create_client(API_USER, USER_PWD)
@@ -70,7 +74,6 @@ def mc2():
7074
def mcStorage(request):
7175
client = create_client(API_USER, USER_PWD)
7276
workspace_name = create_workspace_for_client(client, STORAGE_WORKSPACE)
73-
print(workspace_name)
7477
client_workspace = None
7578
for workspace in client.workspaces_list():
7679
if workspace["name"] == workspace_name:
@@ -83,7 +86,7 @@ def teardown():
8386
# back to original values... (1 project, api allowed ...)
8487
client.patch(
8588
f"/v1/tests/workspaces/{client_workspace_id}",
86-
{"limits_override": {"storage": client_workspace_storage, "projects": 1, "api_allowed": True}},
89+
{"limits_override": get_limit_overrides(client_workspace_storage)},
8790
{"Content-Type": "application/json"},
8891
)
8992

@@ -2683,8 +2686,10 @@ def test_error_push_already_named_project(mc: MerginClient):
26832686

26842687

26852688
def test_error_projects_limit_hit(mcStorage: MerginClient):
2686-
test_project = "test_another_project_above_projects_limit"
2689+
test_project = "project_above_projects_limit"
26872690
test_project_fullname = STORAGE_WORKSPACE + "/" + test_project
2691+
project_dir = os.path.join(TMP_DIR, test_project, API_USER)
2692+
cleanup(mcStorage, test_project, [project_dir])
26882693

26892694
client_workspace = None
26902695
for workspace in mcStorage.workspaces_list():
@@ -2695,12 +2700,10 @@ def test_error_projects_limit_hit(mcStorage: MerginClient):
26952700
client_workspace_storage = client_workspace["storage"]
26962701
mcStorage.patch(
26972702
f"/v1/tests/workspaces/{client_workspace_id}",
2698-
{"limits_override": {"storage": client_workspace_storage, "projects": 0, "api_allowed": True}},
2703+
{"limits_override": {**get_limit_overrides(client_workspace_storage), "projects": 0}},
26992704
{"Content-Type": "application/json"},
27002705
)
27012706

2702-
project_dir = os.path.join(TMP_DIR, test_project, API_USER)
2703-
27042707
with pytest.raises(ClientError) as e:
27052708
mcStorage.create_project_and_push(test_project_fullname, project_dir)
27062709
assert e.value.server_code == ErrorCode.ProjectsLimitHit.value
@@ -2711,3 +2714,40 @@ def test_error_projects_limit_hit(mcStorage: MerginClient):
27112714
assert e.value.http_error == 422
27122715
assert e.value.http_method == "POST"
27132716
assert e.value.url == f"{mcStorage.url}v1/project/testpluginstorage"
2717+
2718+
2719+
# TODO: refactor tests to create workspaces on each run and apply test_error_monthly_contributors_limit_hit
2720+
def test_error_monthly_contributors_limit_hit(mcStorage: MerginClient):
2721+
test_project = "test_monthly_contributors_limit_hit"
2722+
project_dir = os.path.join(TMP_DIR, test_project)
2723+
test_project_fullname = STORAGE_WORKSPACE + "/" + test_project
2724+
cleanup(mcStorage, test_project_fullname, [project_dir])
2725+
2726+
client_workspace = None
2727+
for workspace in mcStorage.workspaces_list():
2728+
if workspace["name"] == STORAGE_WORKSPACE:
2729+
client_workspace = workspace
2730+
break
2731+
2732+
client_workspace_id = client_workspace["id"]
2733+
client_workspace_storage = client_workspace["storage"]
2734+
mcStorage.patch(
2735+
f"/v1/tests/workspaces/{client_workspace_id}",
2736+
{"limits_override": {**get_limit_overrides(client_workspace_storage), "monthly_contributors": 0}},
2737+
{"Content-Type": "application/json"},
2738+
)
2739+
2740+
mcStorage.create_project_and_push(test_project_fullname, project_dir)
2741+
shutil.copy(os.path.join(TEST_DATA_DIR, "test.txt"), project_dir)
2742+
with pytest.raises(ClientError) as e:
2743+
mcStorage.push_project(project_dir)
2744+
2745+
assert e.value.server_code == ErrorCode.MonthlyContributorsLimitHit.value
2746+
assert e.value.detail == (
2747+
"Maximum number of workspace contributors is reached. "
2748+
"Please upgrade your subscription to push changes or create projects. (MonthlyContributorsLimitHit)"
2749+
)
2750+
assert e.value.http_error == 422
2751+
assert e.value.http_method == "POST"
2752+
assert e.value.url == f"{mcStorage.url}v1/project/push/testpluginstorage/{test_project}"
2753+
assert e.value.server_response.get("contributors_quota") == 0

0 commit comments

Comments
 (0)