Skip to content

Commit

Permalink
✨ Add updateReleaseNote mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
dankolbman committed Nov 18, 2019
1 parent a752671 commit 29f6d17
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
46 changes: 46 additions & 0 deletions coordinator/graphql/release_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,49 @@ def mutate(root, info, input=None):
return CreateReleaseNote(release_note=release_note)


class UpdateReleaseNoteInput(graphene.InputObjectType):
description = graphene.String(
required=True,
description="Description of changes made to a study within a release",
)


class UpdateReleaseNote(graphene.Mutation):
class Arguments:
release_note = graphene.ID(
required=True, description="The release note to update"
)
input = UpdateReleaseNoteInput(required=True)

release_note = graphene.Field(ReleaseNoteNode)

@staticmethod
def mutate(root, info, release_note, input):
"""
Update 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 update 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"
)

if "description" in input:
release_note.description = input["description"]

release_note.save()

return UpdateReleaseNote(release_note=release_note)


class Query:
event = graphene.relay.Node.Field(
ReleaseNoteNode, description="Retrieve a single release note"
Expand Down Expand Up @@ -126,3 +169,6 @@ class Mutation:
create_release_note = CreateReleaseNote.Field(
description="Create a new release note for a given study in release"
)
update_release_note = UpdateReleaseNote.Field(
description="Update an existing release note"
)
86 changes: 86 additions & 0 deletions tests/graphql/release_notes/test_edit_release_notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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


UPDATE_RELEASE_NOTE = """
mutation ($releaseNote: ID!, $input: UpdateReleaseNoteInput!) {
updateReleaseNote(releaseNote: $releaseNote, input: $input) {
releaseNote {
id
kfId
uuid
author
description
createdAt
study {
id
kfId
}
release {
id
kfId
}
}
}
}
"""


@pytest.mark.parametrize(
"user_type,expected",
[("admin", True), ("dev", True), ("user", False), ("anon", False)],
)
def test_update_release_note_permissions(db, test_client, user_type, expected):
"""
ADMIN - Can update new release notes
DEV - Can update new release notes
USER - May not update relase notes
anonomous - May not update release notes
"""
release_note = ReleaseNoteFactory()
release_note_id = to_global_id("ReleaseNoteNode", release_note.kf_id)

variables = {
"releaseNote": release_note_id,
"input": {"description": "Test "},
}

client = test_client(user_type)
resp = client.post(
"/graphql",
format="json",
data={"query": UPDATE_RELEASE_NOTE, "variables": variables},
)
print(resp.json())

if expected:
assert (
"kfId" in resp.json()["data"]["updateReleaseNote"]["releaseNote"]
)
else:
assert "errors" in resp.json()


def test_update_release_note(db, admin_client):
"""
Test that release notes are updated correctly.
"""
release_note = ReleaseNoteFactory()
release_note_id = to_global_id("ReleaseNoteNode", release_note.kf_id)

variables = {
"releaseNote": release_note_id,
"input": {"description": "Updated description"},
}
resp = admin_client.post(
"/graphql",
format="json",
data={"query": UPDATE_RELEASE_NOTE, "variables": variables},
)

release = resp.json()["data"]["updateReleaseNote"]["releaseNote"]
assert ReleaseNote.objects.count() == 1
assert release["kfId"] == ReleaseNote.objects.first().kf_id
assert release["description"] == variables["input"]["description"]

0 comments on commit 29f6d17

Please sign in to comment.