diff --git a/grievance_social_protection/tests/gql_payloads.py b/grievance_social_protection/tests/gql_payloads.py new file mode 100644 index 0000000..1d45e21 --- /dev/null +++ b/grievance_social_protection/tests/gql_payloads.py @@ -0,0 +1,36 @@ +gql_mutation_create_ticket = """ +mutation createTicket { + createTicket(input: { + category: "%s", + title: "%s", + resolution: "%s", + priority: "%s", + dateOfIncident: "%s", + channel: "%s", + flags: "%s", + clientMutationId: "%s" + }) { + clientMutationId + } +} +""" + + +gql_mutation_update_ticket = """ +mutation updateTicket { + updateTicket(input: { + id: "%s", + category: "%s", + title: "%s", + resolution: "%s", + priority: "%s", + dateOfIncident: "%s", + channel: "%s", + flags: "%s", + status: %s, + clientMutationId: "%s" + }) { + clientMutationId + } +} +""" diff --git a/grievance_social_protection/tests/test_gql_ticket_create.py b/grievance_social_protection/tests/test_gql_ticket_create.py new file mode 100644 index 0000000..bb13ef0 --- /dev/null +++ b/grievance_social_protection/tests/test_gql_ticket_create.py @@ -0,0 +1,129 @@ +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.models import Ticket +from grievance_social_protection.schema import Query, Mutation +from grievance_social_protection.tests.gql_payloads import gql_mutation_create_ticket + + +class GQLTicketCreateTestCase(TestCase): + class GQLContext: + def __init__(self, user): + self.user = user + + user = None + eu = None + + category = None + title = None + resolution = None + priority = None + dateOfIncident = None + channel = None + flags = None + + @classmethod + def setUpClass(cls): + super(GQLTicketCreateTestCase, cls).setUpClass() + cls.user = create_test_interactive_user(username='user_authorized', roles=[7]) + + gql_schema = Schema( + query=Query, + mutation=Mutation + ) + + cls.category = "Default" + cls.title = "TestMutationCreate" + cls.resolution = "2,5" + cls.priority = "Medium" + cls.date_of_incident = "2024-11-20" + cls.channel = "Channel A" + cls.flags = "Default" + + cls.gql_client = Client(gql_schema) + cls.gql_context = cls.GQLContext(cls.user) + + def test_create_ticket_success(self): + mutation_id = "99g453h5g92h04gh88" + payload = gql_mutation_create_ticket % ( + self.category, + self.title, + self.resolution, + self.gender_id, + self.date_of_incident, + self.channel, + self.flags, + 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) + tickets = Ticket.objects.filter(title=self.title) + self.assertEquals(tickets.count(), 1) + ticket = tickets.first() + self.assertEquals(ticket.title, self.title) + self.assertEquals(ticket.category, self.category) + self.assertEquals(ticket.resolution, self.resolution) + self.assertEquals(ticket.priority, self.priority) + self.assertEquals(ticket.date_of_incident, self.date_of_incident) + self.assertEquals(ticket.flags, self.flags) + + def test_create_ticket_false_invalid_resolution_format(self): + mutation_id = "65g453h4g92h04gh98" + payload = gql_mutation_create_ticket % ( + self.category, + self.title, + "kjdslkdjslk", + self.gender_id, + self.date_of_incident, + self.channel, + self.flags, + mutation_id + ) + + _ = self.gql_client.execute(payload, context=self.gql_context) + mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id) + self.assertTrue(mutation_log.error) + tickets = Ticket.objects.filter(title=self.title) + self.assertEquals(tickets.count(), 0) + + def test_create_ticket_false_invalid_resolution_day_format(self): + mutation_id = "62g453h4g92h04gh90" + payload = gql_mutation_create_ticket % ( + self.category, + self.title, + "99,3", + self.gender_id, + self.date_of_incident, + self.channel, + self.flags, + mutation_id + ) + + _ = self.gql_client.execute(payload, context=self.gql_context) + mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id) + self.assertTrue(mutation_log.error) + tickets = Ticket.objects.filter(title=self.title) + self.assertEquals(tickets.count(), 0) + + def test_create_ticket_false_invalid_resolution_hour_format(self): + mutation_id = "15g453h4g92h04gh92" + payload = gql_mutation_create_ticket % ( + self.category, + self.title, + "4,66", + self.gender_id, + self.date_of_incident, + self.channel, + self.flags, + mutation_id + ) + + _ = self.gql_client.execute(payload, context=self.gql_context) + mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id) + self.assertTrue(mutation_log.error) + tickets = Ticket.objects.filter(title=self.title) + self.assertEquals(tickets.count(), 0) diff --git a/grievance_social_protection/tests/test_gql_ticket_update.py b/grievance_social_protection/tests/test_gql_ticket_update.py new file mode 100644 index 0000000..8e0064b --- /dev/null +++ b/grievance_social_protection/tests/test_gql_ticket_update.py @@ -0,0 +1,140 @@ +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.models import Ticket +from grievance_social_protection.schema import Query, Mutation +from grievance_social_protection.tests.gql_payloads import gql_mutation_update_ticket +from grievance_social_protection.tests.test_helpers import create_ticket + + +class GQLTicketUpdateTestCase(TestCase): + class GQLContext: + def __init__(self, user): + self.user = user + + user = None + eu = None + + category = None + title = None + resolution = None + priority = None + dateOfIncident = None + channel = None + flags = None + status = None + + @classmethod + def setUpClass(cls): + super(GQLTicketUpdateTestCase, 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.category = "Default" + cls.title = "TestMutationUpdate" + cls.resolution = "2,5" + cls.priority = "Medium" + cls.date_of_incident = "2024-11-20" + cls.channel = "Channel A" + cls.flags = "Default" + cls.status = "OPEN" + + cls.gql_client = Client(gql_schema) + cls.gql_context = cls.GQLContext(cls.user) + + def test_update_ticket_success(self): + mutation_id = "99g453h5g92h04xc66" + payload = gql_mutation_update_ticket % ( + self.existing_ticket.id, + self.category, + self.title, + self.resolution, + self.gender_id, + self.date_of_incident, + self.channel, + self.flags, + self.status, + 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) + ticket = Ticket.objects.get(id=self.existing_ticket.id) + self.assertEquals(ticket.title, self.title) + self.assertEquals(ticket.category, self.category) + self.assertEquals(ticket.resolution, self.resolution) + self.assertEquals(ticket.priority, self.priority) + self.assertEquals(ticket.date_of_incident, self.date_of_incident) + self.assertEquals(ticket.flags, self.flags) + self.assertEquals(ticket.status, self.status) + + def test_update_ticket_false_invalid_resolution_format(self): + mutation_id = "65g453h4g92h04yf43" + payload = gql_mutation_update_ticket % ( + self.existing_ticket.id, + self.category, + self.title, + "kjdslkdjslk", + self.gender_id, + self.date_of_incident, + self.channel, + self.flags, + self.status, + mutation_id + ) + + _ = self.gql_client.execute(payload, context=self.gql_context) + mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id) + self.assertTrue(mutation_log.error) + ticket = Ticket.objects.get(id=self.existing_ticket.id) + self.assertNotEquals(ticket.title, self.title) + + def test_update_ticket_false_invalid_resolution_day_format(self): + mutation_id = "65g453h4g92h0zx54" + payload = gql_mutation_update_ticket % ( + self.existing_ticket.id, + self.category, + self.title, + "99,3", + self.gender_id, + self.date_of_incident, + self.channel, + self.flags, + self.status, + mutation_id + ) + + _ = self.gql_client.execute(payload, context=self.gql_context) + mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id) + self.assertTrue(mutation_log.error) + ticket = Ticket.objects.get(id=self.existing_ticket.id) + self.assertNotEquals(ticket.title, self.title) + + def test_update_ticket_false_invalid_resolution_hour_format(self): + mutation_id = "65g453h4g92h04wl32" + payload = gql_mutation_update_ticket % ( + self.existing_ticket.id, + self.category, + self.title, + "4,66", + self.gender_id, + self.date_of_incident, + self.channel, + self.flags, + self.status, + mutation_id + ) + + _ = self.gql_client.execute(payload, context=self.gql_context) + mutation_log = MutationLog.objects.get(client_mutation_id=mutation_id) + self.assertTrue(mutation_log.error) + ticket = Ticket.objects.get(id=self.existing_ticket.id) + self.assertNotEquals(ticket.title, self.title) diff --git a/grievance_social_protection/validations.py b/grievance_social_protection/validations.py index 2c50a02..53a3c6c 100644 --- a/grievance_social_protection/validations.py +++ b/grievance_social_protection/validations.py @@ -77,7 +77,7 @@ def validate_resolution(data): pattern = r"^(?P[0-9]{1,2}),(?P[0-9]{1,2})$" match = re.match(pattern, resolution) if not match: - return {"message": _("validations.CommentValidation.validate_resolution.invalid_format")} + return {"message": _("validations.TicketValidation.validate_resolution.invalid_format")} else: days = int(match.group("days")) hours = int(match.group("hours"))