Skip to content

Commit

Permalink
OP-2220: added tests for gql comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sniedzielski committed Dec 12, 2024
1 parent e1144b0 commit ac2e42d
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 3 deletions.
40 changes: 40 additions & 0 deletions grievance_social_protection/tests/gql_payloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,43 @@
}
}
"""


gql_mutation_create_comment = """
mutation createComment {
createComment(input: {
comment: "%s",
ticketId: "%s",
commenterId: "%s",
commenterType: "%s",
clientMutationId: "%s"
}) {
clientMutationId
}
}
"""


gql_mutation_create_comment_anonymous_user = """
mutation createComment {
createComment(input: {
comment: "%s",
ticketId: "%s",
clientMutationId: "%s"
}) {
clientMutationId
}
}
"""


gql_mutation_resolve_ticket_by_comment = """
mutation resolveGrievanceByComment {
resolveGrievanceByComment(input: {
id: "%s",
clientMutationId: "%s"
}) {
clientMutationId
}
}
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from django.test import TestCase
from core.models import MutationLog
from graphene import Schema
from graphene.test import Client
from core.test_helpers import create_test_interactive_user
from individual.models import Individual
from individual.tests.data import (
service_add_individual_payload,
service_group_individual_payload
)
from grievance_social_protection.models import Comment
from grievance_social_protection.schema import Query, Mutation
from grievance_social_protection.tests.gql_payloads import (
gql_mutation_create_comment,
gql_mutation_create_comment_anonymous_user
)
from grievance_social_protection.tests.test_helpers import create_ticket


class GQLTicketCommentCreateTestCase(TestCase):
class GQLContext:
def __init__(self, user):
self.user = user

user = None

comment = None
individual = None
type = None
existing_ticket = None

@classmethod
def setUpClass(cls):
super(GQLTicketCommentCreateTestCase, cls).setUpClass()
cls.user = create_test_interactive_user(username='user_authorized', roles=[7])
cls.existing_ticket = create_ticket(cls.user.username)

gql_schema = Schema(
query=Query,
mutation=Mutation
)

cls.comment = "This is an awesome test comment!"
cls.individual = cls.__create_individual()
cls.type = "individual"

cls.gql_client = Client(gql_schema)
cls.gql_context = cls.GQLContext(cls.user)

def test_create_comment_individual_success(self):
mutation_id = "99g453h5g92h22xc33"
payload = gql_mutation_create_comment % (
self.comment,
self.existing_ticket.id,
self.individual.id,
self.type,
mutation_id
)

_ = self.gql_client.execute(payload, context=self.gql_context)
mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id)
self.assertFalse(mutation_log.error)
comment = Comment.objects.get(id=self.existing_ticket.id)
self.assertEquals(comment.ticket.id, self.existing_ticket.id)
self.assertEquals(comment.comment, self.comment)
self.assertEquals(comment.commenter_id, self.individual.id)
self.assertEquals(comment.is_resolution, False)
self.assertEquals(str(comment.commenter_type), self.type)

def test_create_comment_anonymous_user_success(self):
mutation_id = "99g453h5g92h04ww98"
payload = gql_mutation_create_comment_anonymous_user % (
self.comment,
self.existing_ticket.id,
mutation_id
)

_ = self.gql_client.execute(payload, context=self.gql_context)
mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id)
self.assertFalse(mutation_log.error)
comment = Comment.objects.get(id=self.existing_ticket.id)
self.assertEquals(comment.ticket.id, self.existing_ticket.id)
self.assertEquals(comment.comment, self.comment)
self.assertEquals(comment.commenter_id, None)
self.assertEquals(comment.is_resolution, False)
self.assertEquals(str(comment.commenter_type), None)

@classmethod
def __create_individual(cls):
object_data = {
**service_add_individual_payload
}

individual = Individual(**object_data)
individual.save(username=cls.user.username)

return individual
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def __init__(self, user):
self.user = user

user = None
eu = None

category = None
title = None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from django.test import TestCase
from core.models import MutationLog
from graphene import Schema
from graphene.test import Client
from core.test_helpers import create_test_interactive_user
from grievance_social_protection.schema import Query, Mutation
from grievance_social_protection.tests.gql_payloads import gql_mutation_resolve_ticket_by_comment
from grievance_social_protection.tests.test_helpers import (
create_ticket,
create_comment_for_existing_ticket
)


class GQLTicketResolveByCommentTestCase(TestCase):
class GQLContext:
def __init__(self, user):
self.user = user

user = None

existing_ticket = None
existing_comment = None
status = "CLOSED"

@classmethod
def setUpClass(cls):
super(GQLTicketResolveByCommentTestCase, cls).setUpClass()
cls.user = create_test_interactive_user(username='user_authorized', roles=[7])
cls.existing_ticket = create_ticket(cls.user.username)
cls.existing_comment = create_comment_for_existing_ticket(cls.user, cls.existing_ticket)

gql_schema = Schema(
query=Query,
mutation=Mutation
)

cls.gql_client = Client(gql_schema)
cls.gql_context = cls.GQLContext(cls.user)

def test_resolve_ticket_by_comment_success(self):
mutation_id = "99g154h5b92h11sd33"
payload = gql_mutation_resolve_ticket_by_comment % (
self.comment.id,
mutation_id
)

_ = self.gql_client.execute(payload, context=self.gql_context)
mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id)
self.assertFalse(mutation_log.error)
self.assertEquals(self.existing_comment.is_resolution, True)
self.assertEquals(self.existing_ticket.status, self.status)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def __init__(self, user):
self.user = user

user = None
eu = None

category = None
title = None
Expand All @@ -25,6 +24,7 @@ def __init__(self, user):
channel = None
flags = None
status = None
existing_ticket = None

@classmethod
def setUpClass(cls):
Expand Down
15 changes: 14 additions & 1 deletion grievance_social_protection/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
from grievance_social_protection.models import Ticket
from grievance_social_protection.models import (
Comment,
Ticket
)
from grievance_social_protection.tests.data import service_add_ticket_payload


def create_ticket(username):
ticket = Ticket(**service_add_ticket_payload)
ticket.save(username=username)
return ticket


def create_comment_for_existing_ticket(user, ticket):
ticket.save(user=user)
comment = Comment({
"ticket_id": ticket.id,
"comment": "awesome comment"
})
comment.save(user=user)
return comment

0 comments on commit ac2e42d

Please sign in to comment.