1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2017-2021 by the Free Software Foundation, Inc.
4 # This file is part of Postorius.
6 # Postorius is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option)
11 # Postorius is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 # You should have received a copy of the GNU General Public License along with
17 # Postorius. If not, see <http://www.gnu.org/licenses/>.
21 from email
import message_from_bytes
, message_from_file
23 from django
.contrib
.auth
.models
import User
25 from allauth
.account
.models
import EmailAddress
27 from postorius
.tests
.utils
import ViewTestCase
, get_test_file
, reverse
28 from postorius
.views
.rest
import get_attachments
, parse
31 class TestRestViews(ViewTestCase
):
35 self
.user
= User
.objects
.create(
36 email
='bob@example.com', password
='testpass')
37 self
.su
= User
.objects
.create_superuser(
38 username
='alice', email
='alice@example.com', password
='testpass')
39 EmailAddress
.objects
.create(
40 user
=self
.user
, email
=self
.user
.email
, verified
=True)
41 EmailAddress
.objects
.create(
42 user
=self
.su
, email
=self
.su
.email
, verified
=True)
43 self
.domain
= self
.mm_client
.create_domain('example.com')
44 self
.foo_list
= self
.domain
.create_list('foo')
46 def test_get_attachments(self
):
47 with
open(get_test_file('test-email-attachment.txt')) as fp
:
48 attachments
= get_attachments(fp
.read())
49 self
.assertEqual(len(attachments
), 1)
51 self
.assertEqual(at1
[1], 'signature.asc')
52 self
.assertEqual(at1
[2], 'application/pgp-signature')
54 def test_get_attachments_bad_charset(self
):
55 # While this part is supposed to be handled by Django-Mailman3.
56 # This should nor fail or error out.
57 with
open(get_test_file('bad-email.txt')) as fp
:
58 attachments
= get_attachments(fp
.read())
59 self
.assertEqual(len(attachments
), 0)
61 def test_parsed_email(self
):
62 with
open(get_test_file('simple-email.txt')) as fp
:
63 retval
= parse(fp
.read())
64 self
.assertTrue('This is a test message.' in retval
['body'])
65 for header_str
in ['From: gil <puntogil@libero.it>',
66 'Content-Type: multipart/mixed;',
67 'Sender: devel-bounces@lists.fedoraproject.org']:
68 self
.assertTrue(header_str
, retval
['headers'])
71 class TestGetHeldMessage(ViewTestCase
):
75 self
.user
= User
.objects
.create(
76 email
='bob@example.com', password
='testpass')
77 self
.su
= User
.objects
.create_superuser(
78 username
='alice', email
='alice@example.com', password
='testpass')
79 EmailAddress
.objects
.create(
80 user
=self
.user
, email
=self
.user
.email
, verified
=True)
81 EmailAddress
.objects
.create(
82 user
=self
.su
, email
=self
.su
.email
, verified
=True)
83 self
.domain
= self
.mm_client
.create_domain('example.com')
84 self
.foo_list
= self
.domain
.create_list('foo')
85 self
.inq
= self
.mm_client
.queues
['in']
86 with
open(get_test_file('test-email-attachment.txt')) as fp
:
87 message
= message_from_file(fp
)
88 # Now, we inject a non-member post to this email list to simulate a
90 self
.inq
.inject('foo.example.com', message
.as_string())
91 # We sleep till this message is processed and held.
94 def test_get_held_message(self
):
95 self
.client
.login(username
='alice', password
='testpass')
96 response
= self
.client
.get(reverse('rest_held_message',
97 args
=['foo.example.com', '1']))
98 self
.assertEqual(response
.status_code
, 200)
99 content
= json
.loads(response
.content
.decode('utf-8'))
101 sorted(list(content
.keys())),
102 ['attachments', 'hold_date', 'msg', 'msgid', 'reason', 'sender', 'subject']) # noqa
103 self
.assertEqual(content
['msgid'], 1)
104 self
.assertEqual(len(content
['attachments']), 1)
105 attachment_uri
= content
['attachments'][0]
106 # Now we will get this attachment.
107 self
.client
.get(attachment_uri
)
109 def test_get_raw_held_message(self
):
110 self
.client
.login(username
='alice', password
='testpass')
111 response
= self
.client
.get(reverse('rest_held_message',
112 args
=['foo.example.com', '1']),
114 self
.assertEqual(response
.status_code
, 200)
115 email
= message_from_bytes(response
.content
)
116 self
.assertIsNotNone(email
)
118 def test_get_attachments_for_held_message(self
):
119 self
.client
.login(username
='alice', password
='testpass')
120 response
= self
.client
.get(reverse('rest_held_message',
121 args
=['foo.example.com', '1']))
122 self
.assertEqual(response
.status_code
, 200)
123 attachment_uri
= json
.loads(response
.content
.decode('utf-8'))['attachments'][0][0] # noqa: E501
124 # Now we will get this attachment.
125 response
= self
.client
.get(attachment_uri
)
126 self
.assertEqual(response
.status_code
, 200)
127 self
.assertTrue('BEGIN PGP SIGNATURE' in str(response
.content
))