Skip to content

Commit

Permalink
fix: Allow updates with null privacy (#963)
Browse files Browse the repository at this point in the history
The GraphQL type allows this, but we were trying to access the privacy
regardless of whether it was null or not.
  • Loading branch information
David Code Howard authored Nov 9, 2023
1 parent 2162f0e commit fbccaa0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 4 additions & 2 deletions terraso_backend/apps/project_management/graphql/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,15 @@ def mutate_and_get_payload(cls, root, info, **kwargs):
cls.remove_null_fields(kwargs, ["privacy", "measurement_units"])
if not user.has_perm(Project.get_perm("change"), project):
cls.not_allowed()
kwargs["privacy"] = kwargs["privacy"].value

metadata = {
"name": kwargs["name"],
"privacy": kwargs["privacy"],
"description": kwargs["description"] if "description" in kwargs else None,
}

if privacy := kwargs.get("privacy"):
metadata["privacy"] = privacy.value

logger.log(
user=user,
action=log_api.CHANGE,
Expand Down
24 changes: 19 additions & 5 deletions terraso_backend/tests/graphql/mutations/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,25 @@ def test_update_project_user_is_manager(project, client, project_manager):
assert content["data"]["updateProject"]["project"]["privacy"] == "PRIVATE"


def test_update_project_audit_log(project, client, project_manager):
@pytest.mark.parametrize(
"metadata",
[
{
"name": "test_name",
"privacy": "PRIVATE",
"description": "A test project",
},
{
"name": "test_name",
"description": "A test project",
},
],
)
def test_update_project_audit_log(metadata, project, client, project_manager):
input = {
"id": str(project.id),
"name": "test_name",
"privacy": "PRIVATE",
"description": "A test project",
}
input.update(metadata)
client.force_login(project_manager)

response = graphql_query(UPDATE_PROJECT_GRAPHQL, input_data=input, client=client)
Expand All @@ -204,7 +216,9 @@ def test_update_project_audit_log(project, client, project_manager):
assert log_result.event == CHANGE.value
assert log_result.user_human_readable == project_manager.full_name()
assert log_result.resource_object == project
expected_metadata = {"name": "test_name", "privacy": "private", "description": "A test project"}
expected_metadata = metadata
if "privacy" in expected_metadata:
expected_metadata["privacy"] = expected_metadata["privacy"].lower()
assert log_result.metadata == expected_metadata


Expand Down

0 comments on commit fbccaa0

Please sign in to comment.