1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2017-2023 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
):
34 self
.user
= User
.objects
.create(
35 email
='bob@example.com', password
='testpass'
37 self
.su
= User
.objects
.create_superuser(
38 username
='alice', email
='alice@example.com', password
='testpass'
40 EmailAddress
.objects
.create(
41 user
=self
.user
, email
=self
.user
.email
, verified
=True
43 EmailAddress
.objects
.create(
44 user
=self
.su
, email
=self
.su
.email
, verified
=True
46 self
.domain
= self
.mm_client
.create_domain('example.com')
47 self
.foo_list
= self
.domain
.create_list('foo')
49 def test_get_attachments(self
):
50 with
open(get_test_file('test-email-attachment.txt')) as fp
:
51 attachments
= get_attachments(fp
.read())
52 self
.assertEqual(len(attachments
), 1)
54 self
.assertEqual(at1
[1], 'signature.asc')
55 self
.assertEqual(at1
[2], 'application/pgp-signature')
57 def test_get_attachments_bad_charset(self
):
58 # While this part is supposed to be handled by Django-Mailman3.
59 # This should nor fail or error out.
60 with
open(get_test_file('bad-email.txt')) as fp
:
61 attachments
= get_attachments(fp
.read())
62 self
.assertEqual(len(attachments
), 0)
64 def test_parsed_email(self
):
65 with
open(get_test_file('simple-email.txt')) as fp
:
66 retval
= parse(fp
.read())
67 self
.assertTrue('This is a test message.' in retval
['body'])
69 'From: gil <puntogil@libero.it>',
70 'Content-Type: multipart/mixed;',
71 'Sender: devel-bounces@lists.fedoraproject.org',
73 self
.assertTrue(header_str
, retval
['headers'])
76 class TestGetHeldMessage(ViewTestCase
):
79 self
.user
= User
.objects
.create(
80 email
='bob@example.com', password
='testpass'
82 self
.su
= User
.objects
.create_superuser(
83 username
='alice', email
='alice@example.com', password
='testpass'
85 EmailAddress
.objects
.create(
86 user
=self
.user
, email
=self
.user
.email
, verified
=True
88 EmailAddress
.objects
.create(
89 user
=self
.su
, email
=self
.su
.email
, verified
=True
91 self
.domain
= self
.mm_client
.create_domain('example.com')
92 self
.foo_list
= self
.domain
.create_list('foo')
93 self
.inq
= self
.mm_client
.queues
['in']
94 with
open(get_test_file('test-email-attachment.txt')) as fp
:
95 message
= message_from_file(fp
)
96 # Now, we inject a non-member post to this email list to simulate a
98 self
.inq
.inject('foo.example.com', message
.as_string())
99 # We sleep till this message is processed and held.
102 def test_get_held_message(self
):
103 self
.client
.login(username
='alice', password
='testpass')
104 response
= self
.client
.get(
105 reverse('rest_held_message', args
=['foo.example.com', '1'])
107 self
.assertEqual(response
.status_code
, 200)
108 content
= json
.loads(response
.content
.decode('utf-8'))
110 sorted(list(content
.keys())),
121 self
.assertEqual(content
['msgid'], 1)
122 self
.assertEqual(len(content
['attachments']), 1)
123 attachment_uri
= content
['attachments'][0]
124 # Now we will get this attachment.
125 self
.client
.get(attachment_uri
)
127 def test_get_raw_held_message(self
):
128 self
.client
.login(username
='alice', password
='testpass')
129 response
= self
.client
.get(
130 reverse('rest_held_message', args
=['foo.example.com', '1']),
133 self
.assertEqual(response
.status_code
, 200)
134 email
= message_from_bytes(response
.content
)
135 self
.assertIsNotNone(email
)
137 def test_get_attachments_for_held_message(self
):
138 self
.client
.login(username
='alice', password
='testpass')
139 response
= self
.client
.get(
140 reverse('rest_held_message', args
=['foo.example.com', '1'])
142 self
.assertEqual(response
.status_code
, 200)
143 attachment_uri
= json
.loads(response
.content
.decode('utf-8'))[
148 # Now we will get this attachment.
149 response
= self
.client
.get(attachment_uri
)
150 self
.assertEqual(response
.status_code
, 200)
151 self
.assertTrue('BEGIN PGP SIGNATURE' in str(response
.content
))