110 lines
5.2 KiB
Python
110 lines
5.2 KiB
Python
from datetime import datetime, timedelta
|
|
|
|
from django.test import TestCase, Client
|
|
|
|
from authentication.models import ExtendedUser
|
|
from mail.models import Email
|
|
from tickets.models import IssueThread, StateChange, Comment
|
|
from django.contrib.auth.models import Permission
|
|
from knox.models import AuthToken
|
|
|
|
|
|
class IssueApiTest(TestCase):
|
|
|
|
def setUp(self):
|
|
super().setUp()
|
|
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
|
self.user.user_permissions.add(*Permission.objects.all())
|
|
self.user.save()
|
|
self.token = AuthToken.objects.create(user=self.user)
|
|
self.client = Client(headers={'Authorization': 'Token ' + self.token[1]})
|
|
|
|
def test_issues_empty(self):
|
|
response = self.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 = self.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'))
|
|
|
|
def test_manual_creation(self):
|
|
response = self.client.post('/api/2/tickets/manual/', {'name': 'test issue', 'sender': 'test',
|
|
'recipient': 'test', 'body': 'test'})
|
|
self.assertEqual(response.status_code, 201)
|
|
self.assertEqual(response.json()['state'], 'new')
|
|
self.assertEqual(response.json()['name'], 'test issue')
|
|
self.assertEqual(response.json()['assigned_to'], None)
|
|
timeline = response.json()['timeline']
|
|
self.assertEqual(len(timeline), 1)
|
|
self.assertEqual(timeline[0]['type'], 'mail')
|
|
self.assertEqual(timeline[0]['sender'], 'test')
|
|
self.assertEqual(timeline[0]['recipient'], 'test')
|
|
self.assertEqual(timeline[0]['subject'], 'test issue')
|
|
self.assertEqual(timeline[0]['body'], 'test')
|
|
|