from datetime import datetime, timedelta from django.test import TestCase, Client from mail.models import Email from tickets.models import IssueThread, StateChange, Comment client = Client() class IssueApiTest(TestCase): def test_issues_empty(self): response = client.get('/api/2/tickets/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), []) def test_issues(self): now = datetime.now() issue = IssueThread.objects.create( name="test issue", ) 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), ) response = client.get('/api/2/tickets/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) self.assertEqual(response.json()[0]['id'], issue.id) self.assertEqual(response.json()[0]['name'], "test issue") self.assertEqual(response.json()[0]['state'], "new") self.assertEqual(response.json()[0]['assigned_to'], None) self.assertEqual(response.json()[0]['last_activity'], issue.last_activity.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) 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'))