show mail attachments in frontend

This commit is contained in:
j3d1 2024-01-14 16:01:32 +01:00
parent d1626d1777
commit 04f774404a
11 changed files with 252 additions and 23 deletions

View file

@ -3,7 +3,7 @@ from datetime import datetime, timedelta
from django.test import TestCase, Client
from authentication.models import ExtendedUser
from mail.models import Email
from mail.models import Email, EmailAttachment
from tickets.models import IssueThread, StateChange, Comment
from django.contrib.auth.models import Permission
from knox.models import AuthToken
@ -147,6 +147,88 @@ class IssueApiTest(TestCase):
self.assertEqual(comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
response.json()[2]['timeline'][1]['timestamp'])
def test_issues_with_files(self):
from django.core.files.base import ContentFile
from hashlib import sha256
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,
)
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),
)
file1 = EmailAttachment.objects.create(
name='file1', mime_type='text/plain', file=ContentFile(b"foo1", "f1"),
hash=sha256(b"foo1").hexdigest(), email=mail1
)
file2 = EmailAttachment.objects.create(
name='file2', mime_type='text/plain', file=ContentFile(b"foo2", "f2"),
hash=sha256(b"foo2").hexdigest(), email=mail1
)
response = self.client.get('/api/2/tickets/')
self.assertEqual(200, response.status_code)
self.assertEqual(1, len(response.json()))
self.assertEqual(issue.id, response.json()[0]['id'])
self.assertEqual('pending_new', response.json()[0]['state'])
self.assertEqual('test issue', response.json()[0]['name'])
self.assertEqual(None, response.json()[0]['assigned_to'])
self.assertEqual(36, len(response.json()[0]['uuid']))
self.assertEqual(comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
response.json()[0]['last_activity'])
self.assertEqual(4, len(response.json()[0]['timeline']))
self.assertEqual('state', response.json()[0]['timeline'][0]['type'])
self.assertEqual('mail', response.json()[0]['timeline'][1]['type'])
self.assertEqual('mail', response.json()[0]['timeline'][2]['type'])
self.assertEqual('comment', response.json()[0]['timeline'][3]['type'])
self.assertEqual(mail1.id, response.json()[0]['timeline'][1]['id'])
self.assertEqual(mail2.id, response.json()[0]['timeline'][2]['id'])
self.assertEqual(comment.id, response.json()[0]['timeline'][3]['id'])
self.assertEqual('pending_new', response.json()[0]['timeline'][0]['state'])
self.assertEqual('test', response.json()[0]['timeline'][1]['sender'])
self.assertEqual('test', response.json()[0]['timeline'][1]['recipient'])
self.assertEqual('test', response.json()[0]['timeline'][1]['subject'])
self.assertEqual('test', response.json()[0]['timeline'][1]['body'])
self.assertEqual(mail1.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
response.json()[0]['timeline'][1]['timestamp'])
self.assertEqual('test', response.json()[0]['timeline'][2]['sender'])
self.assertEqual('test', response.json()[0]['timeline'][2]['recipient'])
self.assertEqual('test', response.json()[0]['timeline'][2]['subject'])
self.assertEqual('test', response.json()[0]['timeline'][2]['body'])
self.assertEqual(mail2.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
response.json()[0]['timeline'][2]['timestamp'])
self.assertEqual('test', response.json()[0]['timeline'][3]['comment'])
self.assertEqual(comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
response.json()[0]['timeline'][3]['timestamp'])
self.assertEqual(2, len(response.json()[0]['timeline'][1]['attachments']))
self.assertEqual(0, len(response.json()[0]['timeline'][2]['attachments']))
self.assertEqual('file1', response.json()[0]['timeline'][1]['attachments'][0]['name'])
self.assertEqual('file2', response.json()[0]['timeline'][1]['attachments'][1]['name'])
self.assertEqual('text/plain', response.json()[0]['timeline'][1]['attachments'][0]['mime_type'])
self.assertEqual('text/plain', response.json()[0]['timeline'][1]['attachments'][1]['mime_type'])
self.assertEqual(file1.hash, response.json()[0]['timeline'][1]['attachments'][0]['hash'])
self.assertEqual(file2.hash, response.json()[0]['timeline'][1]['attachments'][1]['hash'])
def test_manual_creation(self):
response = self.client.post('/api/2/tickets/manual/',
{'name': 'test issue', 'sender': 'test', 'recipient': 'test', 'body': 'test'},