test_email.py 5.21 KB
Newer Older
1
2
3
from __future__ import annotations
from typing import List
from django.test import TestCase
4
from django.core import mail
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from backend.email import StoredEmail
import tempfile
import contextlib
import mailbox
import email.message
import email


class TestStoredEmail(TestCase):
    @contextlib.contextmanager
    def mbox(self, messages: List[str]):
        with tempfile.NamedTemporaryFile(suffix=".mbox") as tf:
            mb = mailbox.mbox(tf.name)
            try:
                for msg in messages:
                    mb.add(email.message_from_string(msg.strip()))
            finally:
                mb.close()
            yield tf.name

    def test_plain(self):
        messages = [
            """
From: Enrico Zini <enrico@debian.org>
Date: Sat, 29 Feb 2020 16:27:47 +0100
Subject: test message

Test body
""", """
From: Enrico Zini <enrico@debian.org>
Date: Sat, 28 Feb 2020 16:27:47 +0100
Subject: earlier test message

Earlier test body
""",
        ]

        with self.mbox(messages) as fname:
            res = StoredEmail.get_mbox_jsonable(fname)

        self.assertEqual(res, [
            {
                "from": "Enrico Zini <enrico@debian.org>",
                "date_rfc": "Sat, 28 Feb 2020 16:27:47 +0100",
                "date_iso": "2020-02-28T16:27:47+01:00",
                "subject": "earlier test message",
                "body": "Earlier test body",
            }, {
                "from": "Enrico Zini <enrico@debian.org>",
                "date_rfc": "Sat, 29 Feb 2020 16:27:47 +0100",
                "date_iso": "2020-02-29T16:27:47+01:00",
                "subject": "test message",
                "body": "Test body",
            }
        ])

    def test_missing_date(self):
        messages = [
            """
From: Enrico Zini <enrico@debian.org>
Date: Sat, 29 Feb 2020 16:27:47 +0100
Subject: test message

Test body
""", """
From: Enrico Zini <enrico@debian.org>
Subject: earlier test message

Earlier test body
""",
        ]

        with self.mbox(messages) as fname:
            res = StoredEmail.get_mbox_jsonable(fname)

        self.assertEqual(res, [
            {
                "from": "Enrico Zini <enrico@debian.org>",
                "date_rfc": None,
                "date_iso": None,
                "subject": "earlier test message",
                "body": "Earlier test body",
            }, {
                "from": "Enrico Zini <enrico@debian.org>",
                "date_rfc": "Sat, 29 Feb 2020 16:27:47 +0100",
                "date_iso": "2020-02-29T16:27:47+01:00",
                "subject": "test message",
                "body": "Test body",
            }
        ])

    def test_missing_timezone(self):
        messages = [
            """
From: Enrico Zini <enrico@debian.org>
Date: Sat, 29 Feb 2020 16:27:47 +0100
Subject: test message

Test body
""", """
From: Enrico Zini <enrico@debian.org>
Date: Sat, 28 Feb 2020 16:27:47
Subject: earlier test message

Earlier test body
""",
        ]

        with self.mbox(messages) as fname:
            res = StoredEmail.get_mbox_jsonable(fname)

        self.assertEqual(res, [
            {
                "from": "Enrico Zini <enrico@debian.org>",
                "date_rfc": "Sat, 28 Feb 2020 16:27:47",
                "date_iso": "2020-02-28T16:27:47+00:00",
                "subject": "earlier test message",
                "body": "Earlier test body",
            }, {
                "from": "Enrico Zini <enrico@debian.org>",
                "date_rfc": "Sat, 29 Feb 2020 16:27:47 +0100",
                "date_iso": "2020-02-29T16:27:47+01:00",
                "subject": "test message",
                "body": "Test body",
            }
        ])
131
132
133
134
135
136
137
138
139
140
141


class TestNonce(TestCase):
    def test_send_challenge(self):
        from backend.email import send_nonce
        from backend.models import Person

        self.maxDiff = None
        person = Person.objects.create_user("test@example.org", fullname="Test Person", audit_skip=True)
        mail.outbox = []

142
        send_nonce("notification_mails/newperson.txt", person, encrypted_nonce="♥")
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
        self.assertEqual(len(mail.outbox), 1)
        m = mail.outbox[0]
        self.assertEqual(m.from_email, "nm@debian.org")
        self.assertEqual(m.to, ["Test Person <test@example.org>"])
        self.assertEqual(m.cc, [])
        self.assertEqual(m.bcc, [])
        self.assertEqual(m.extra_headers, {'Reply-To': 'nm@debian.org'})
        self.assertEqual(m.body, """Hello Test Person,

to confirm your new entry at https://nm.debian.org/person/test@example.org/
you need to decrypt the following text. The result will be a URL, which you
then visit to make your entry confirmed.



You should not need instructions to decrypt this. If you do not know how to do
it, you can consider it a challenge. In that case, you can start from here:
160
https://www.gnupg.org/howtos/en/GPGMiniHowto.html
161
162
163
164
165
166
167
168
169
170
171

For any problem, feel free to contact Front Desk at nm@debian.org.


Regards,

the nm.debian.org robotic minion for Front Desk""")
        # self.assertIn(reverse("process_emeritus_self") + "?t=", mail.outbox[0].body)
        # self.assertIn(reverse("process_cancel", args=[process.pk]), mail.outbox[0].body)
        # self.assertIn(process.get_absolute_url(), mail.outbox[0].body)
        # self.assertIn("test ping", mail.outbox[0].body)