c3lf-system-3/core/tickets/tests/v2/test_tickets.py

94 lines
4.3 KiB
Python
Raw Normal View History

2023-12-06 03:34:22 +00:00
from datetime import datetime, timedelta
2023-11-23 22:17:20 +00:00
from django.test import TestCase, Client
2023-12-11 21:18:33 +00:00
from authentication.models import ExtendedUser
2023-12-06 03:34:22 +00:00
from mail.models import Email
from tickets.models import IssueThread, StateChange, Comment
2023-12-11 21:18:33 +00:00
from django.contrib.auth.models import User
from knox.models import AuthToken
2023-11-23 22:17:20 +00:00
class IssueApiTest(TestCase):
2023-12-11 21:18:33 +00:00
def setUp(self):
super().setUp()
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
self.user.save()
self.token = AuthToken.objects.create(user=self.user)
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
2023-12-06 03:34:22 +00:00
def test_issues_empty(self):
2023-12-11 21:18:33 +00:00
response = self.client.get('/api/2/tickets/')
2023-11-23 22:17:20 +00:00
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), [])
2023-12-06 03:34:22 +00:00
def test_issues(self):
now = datetime.now()
2023-12-06 04:06:26 +00:00
issue = IssueThread.objects.create(
name="test issue",
)
2023-12-06 03:34:22 +00:00
mail1 = Email.objects.create(
subject='test',
body='test',
sender='test',
recipient='test',
issue_thread=issue,
timestamp=now,
)
state = StateChange.objects.create(
issue_thread=issue,
state="new",
timestamp=now + timedelta(seconds=1),
)
mail2 = Email.objects.create(
subject='test',
body='test',
sender='test',
recipient='test',
issue_thread=issue,
in_reply_to=mail1.reference,
timestamp=now + timedelta(seconds=2),
)
comment = Comment.objects.create(
issue_thread=issue,
comment="test",
timestamp=now + timedelta(seconds=3),
)
2023-11-23 22:17:20 +00:00
2023-12-11 21:18:33 +00:00
response = self.client.get('/api/2/tickets/')
2023-12-06 03:34:22 +00:00
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()), 1)
self.assertEqual(response.json()[0]['id'], issue.id)
2023-12-06 04:06:26 +00:00
self.assertEqual(response.json()[0]['name'], "test issue")
self.assertEqual(response.json()[0]['state'], "new")
self.assertEqual(response.json()[0]['assigned_to'], None)
2023-12-06 03:51:47 +00:00
self.assertEqual(response.json()[0]['last_activity'], issue.last_activity.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
2023-12-06 03:34:22 +00:00
self.assertEqual(len(response.json()[0]['timeline']), 4)
self.assertEqual(response.json()[0]['timeline'][0]['type'], 'mail')
self.assertEqual(response.json()[0]['timeline'][1]['type'], 'state')
self.assertEqual(response.json()[0]['timeline'][2]['type'], 'mail')
self.assertEqual(response.json()[0]['timeline'][3]['type'], 'comment')
self.assertEqual(response.json()[0]['timeline'][0]['id'], mail1.id)
self.assertEqual(response.json()[0]['timeline'][1]['id'], state.id)
self.assertEqual(response.json()[0]['timeline'][2]['id'], mail2.id)
self.assertEqual(response.json()[0]['timeline'][3]['id'], comment.id)
self.assertEqual(response.json()[0]['timeline'][0]['sender'], 'test')
self.assertEqual(response.json()[0]['timeline'][0]['recipient'], 'test')
self.assertEqual(response.json()[0]['timeline'][0]['subject'], 'test')
self.assertEqual(response.json()[0]['timeline'][0]['body'], 'test')
self.assertEqual(response.json()[0]['timeline'][0]['timestamp'],
mail1.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
self.assertEqual(response.json()[0]['timeline'][1]['state'], 'new')
self.assertEqual(response.json()[0]['timeline'][1]['timestamp'],
state.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
self.assertEqual(response.json()[0]['timeline'][2]['sender'], 'test')
self.assertEqual(response.json()[0]['timeline'][2]['recipient'], 'test')
self.assertEqual(response.json()[0]['timeline'][2]['subject'], 'test')
self.assertEqual(response.json()[0]['timeline'][2]['body'], 'test')
self.assertEqual(response.json()[0]['timeline'][2]['timestamp'],
mail2.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
self.assertEqual(response.json()[0]['timeline'][3]['comment'], 'test')
self.assertEqual(response.json()[0]['timeline'][3]['timestamp'],
comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))