Skip to content

Commit

Permalink
✨ Add removeReleaseNote mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
dankolbman committed Nov 18, 2019
1 parent ee780aa commit 52ed44a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
36 changes: 36 additions & 0 deletions coordinator/graphql/release_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
)
65 changes: 65 additions & 0 deletions tests/graphql/release_notes/test_remove_release_notes.py
Original file line number Diff line number Diff line change
@@ -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"]

0 comments on commit 52ed44a

Please sign in to comment.