diff --git a/grievance_social_protection/tests/gql_payloads.py b/grievance_social_protection/tests/gql_payloads.py index 1d45e21..28e9164 100644 --- a/grievance_social_protection/tests/gql_payloads.py +++ b/grievance_social_protection/tests/gql_payloads.py @@ -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 + } +} +""" diff --git a/grievance_social_protection/tests/test_gql_ticket_comment_create.py b/grievance_social_protection/tests/test_gql_ticket_comment_create.py new file mode 100644 index 0000000..8bc598f --- /dev/null +++ b/grievance_social_protection/tests/test_gql_ticket_comment_create.py @@ -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 diff --git a/grievance_social_protection/tests/test_gql_ticket_create.py b/grievance_social_protection/tests/test_gql_ticket_create.py index e0e40ac..1285134 100644 --- a/grievance_social_protection/tests/test_gql_ticket_create.py +++ b/grievance_social_protection/tests/test_gql_ticket_create.py @@ -14,7 +14,6 @@ def __init__(self, user): self.user = user user = None - eu = None category = None title = None diff --git a/grievance_social_protection/tests/test_gql_ticket_resolve_by_comment.py b/grievance_social_protection/tests/test_gql_ticket_resolve_by_comment.py new file mode 100644 index 0000000..729cd4d --- /dev/null +++ b/grievance_social_protection/tests/test_gql_ticket_resolve_by_comment.py @@ -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) diff --git a/grievance_social_protection/tests/test_gql_ticket_update.py b/grievance_social_protection/tests/test_gql_ticket_update.py index 7b6d0bc..b60a54b 100644 --- a/grievance_social_protection/tests/test_gql_ticket_update.py +++ b/grievance_social_protection/tests/test_gql_ticket_update.py @@ -15,7 +15,6 @@ def __init__(self, user): self.user = user user = None - eu = None category = None title = None @@ -25,6 +24,7 @@ def __init__(self, user): channel = None flags = None status = None + existing_ticket = None @classmethod def setUpClass(cls): diff --git a/grievance_social_protection/tests/test_helpers.py b/grievance_social_protection/tests/test_helpers.py index 1749547..d2bb46e 100644 --- a/grievance_social_protection/tests/test_helpers.py +++ b/grievance_social_protection/tests/test_helpers.py @@ -1,4 +1,7 @@ -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 @@ -6,3 +9,13 @@ 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