From 52ed44a2d8334b1e6d1e5c27f4512b3d48c133be Mon Sep 17 00:00:00 2001 From: Dan Kolbman Date: Mon, 18 Nov 2019 14:32:54 -0500 Subject: [PATCH] :sparkles: Add removeReleaseNote mutation --- coordinator/graphql/release_notes.py | 36 ++++++++++ .../test_remove_release_notes.py | 65 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 tests/graphql/release_notes/test_remove_release_notes.py diff --git a/coordinator/graphql/release_notes.py b/coordinator/graphql/release_notes.py index 60e601f..5d1e1f1 100644 --- a/coordinator/graphql/release_notes.py +++ b/coordinator/graphql/release_notes.py @@ -133,6 +133,39 @@ def mutate(root, info, release_note, input): return UpdateReleaseNote(release_note=release_note) +class RemoveReleaseNote(graphene.Mutation): + class Arguments: + release_note = graphene.ID( + required=True, description="The release note to remove" + ) + + success = graphene.Boolean() + + @staticmethod + def mutate(root, info, release_note): + """ + Delete an existing release note + """ + user = info.context.user + if not hasattr(user, "auth_roles") or ( + "ADMIN" not in user.auth_roles and "DEV" not in user.auth_roles + ): + raise GraphQLError("Not authenticated to delete a release note.") + + try: + _, release_note_id = from_global_id(release_note) + release_note = ReleaseNote.objects.get(kf_id=release_note_id) + except ReleaseNote.DoesNotExist as err: + raise GraphQLError( + f"Release note {release_note_id} does not exist" + ) + + release_note.delete() + release_note.save() + + return RemoveReleaseNote(success=True) + + class Query: event = graphene.relay.Node.Field( ReleaseNoteNode, description="Retrieve a single release note" @@ -172,3 +205,6 @@ class Mutation: update_release_note = UpdateReleaseNote.Field( description="Update an existing release note" ) + remove_release_note = RemoveReleaseNote.Field( + description="Remove an existing release note" + ) diff --git a/tests/graphql/release_notes/test_remove_release_notes.py b/tests/graphql/release_notes/test_remove_release_notes.py new file mode 100644 index 0000000..e8dd40d --- /dev/null +++ b/tests/graphql/release_notes/test_remove_release_notes.py @@ -0,0 +1,65 @@ +import pytest +from graphql_relay.node.node import to_global_id +from coordinator.api.models import ReleaseNote +from coordinator.api.factories.release_note import ReleaseNoteFactory + + +REMOVE_RELEASE_NOTE = """ +mutation ($releaseNote: ID!) { + removeReleaseNote(releaseNote: $releaseNote) { + success + } +} +""" + + +@pytest.mark.parametrize( + "user_type,expected", + [("admin", True), ("dev", True), ("user", False), ("anon", False)], +) +def test_remove_release_note_permissions(db, test_client, user_type, expected): + """ + ADMIN - Can remove new release notes + DEV - Can remove new release notes + USER - May not remove relase notes + anonomous - May not remove release notes + """ + release_note = ReleaseNoteFactory() + release_note_id = to_global_id("ReleaseNoteNode", release_note.kf_id) + + variables = {"releaseNote": release_note_id} + + client = test_client(user_type) + resp = client.post( + "/graphql", + format="json", + data={"query": REMOVE_RELEASE_NOTE, "variables": variables}, + ) + print(resp.json()) + + if expected: + assert resp.json()["data"]["removeReleaseNote"]["success"] + with pytest.raises(ReleaseNote.DoesNotExist): + ReleaseNote.objects.get(kf_id=release_note.kf_id) + else: + assert "errors" in resp.json() + + +def test_remove_release_note_not_found(db, admin_client): + """ + Test that we may not remove release notes that do not exist + """ + release_note = ReleaseNoteFactory() + release_note_id = to_global_id("ReleaseNoteNode", release_note.kf_id) + release_note.delete() + + variables = {"releaseNote": release_note_id} + resp = admin_client.post( + "/graphql", + format="json", + data={"query": REMOVE_RELEASE_NOTE, "variables": variables}, + ) + + assert "errors" in resp.json() + errors = resp.json()["errors"] + assert "does not exist" in errors[0]["message"]