From fbccaa0e1ed19ce1cbe96ba422df84a7c3c196ec Mon Sep 17 00:00:00 2001 From: David Code Howard Date: Thu, 9 Nov 2023 16:53:57 -0500 Subject: [PATCH] fix: Allow updates with null privacy (#963) The GraphQL type allows this, but we were trying to access the privacy regardless of whether it was null or not. --- .../project_management/graphql/projects.py | 6 +++-- .../tests/graphql/mutations/test_projects.py | 24 +++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/terraso_backend/apps/project_management/graphql/projects.py b/terraso_backend/apps/project_management/graphql/projects.py index d5c5b6616..5a3206c13 100644 --- a/terraso_backend/apps/project_management/graphql/projects.py +++ b/terraso_backend/apps/project_management/graphql/projects.py @@ -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, diff --git a/terraso_backend/tests/graphql/mutations/test_projects.py b/terraso_backend/tests/graphql/mutations/test_projects.py index c4dfb029a..b2f65aaf1 100644 --- a/terraso_backend/tests/graphql/mutations/test_projects.py +++ b/terraso_backend/tests/graphql/mutations/test_projects.py @@ -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) @@ -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