summaryrefslogtreecommitdiffstats
path: root/tests/functional/test_wallace/test_002_footer.py
blob: ac3e1ba1d89282b6b45da457f9fc97d924c0cfab (plain)
1
2
3
4
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from email import message_from_string
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEImage import MIMEImage
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
import smtplib
import time
import unittest

import pykolab
from pykolab import wap_client
from pykolab.auth import Auth
from pykolab.imap import IMAP

conf = pykolab.getConf()

class TestWallaceFooter(unittest.TestCase):

    @classmethod
    def setup_class(self, *args, **kw):
        from tests.functional.purge_users import purge_users
        purge_users()

        self.user = {
                'local': 'john.doe',
                'domain': 'example.org'
            }

        self.footer = {}

        footer_html_file = conf.get('wallace', 'footer_html')
        footer_text_file = conf.get('wallace', 'footer_text')

        if os.path.isfile(footer_text_file):
            self.footer['plain'] = open(footer_text_file, 'r').read()

        if not os.path.isfile(footer_html_file):
            self.footer['html'] = '<p>' + self.footer['plain'] + '</p>'
        else:
            self.footer['html'] = open(footer_html_file, 'r').read()

        self.send_to = 'john.doe@example.org'
        self.send_from = 'john.doe@example.org'

        self.message_to = '"Doe, John" <%s>' % (self.send_to)
        self.message_from = '"Doe, John" <%s>' % (self.send_from)

        from tests.functional.user_add import user_add
        user_add("John", "Doe")
        time.sleep(2)
        from tests.functional.synchronize import synchronize_once
        synchronize_once()

    @classmethod
    def teardown_class(self, *args, **kw):
        from tests.functional.purge_users import purge_users
        purge_users()

    def check_message_delivered(self, subject):
        imap = IMAP()
        imap.connect()
        imap.set_acl("user/john.doe@example.org", "cyrus-admin", "lrs")
        imap.imap.m.select("user/john.doe@example.org")

        found = False
        max_tries = 20

        while not found and max_tries > 0:
            max_tries -= 1

            typ, data = imap.imap.m.search(None, 'ALL')
            for num in data[0].split():
                typ, msg = imap.imap.m.fetch(num, '(RFC822)')
                _msg = message_from_string(msg[0][1])
                if _msg['Subject'] == subject:
                    found = True

            time.sleep(1)

        return found

    def html_attachment(self):
        html_body = "<html><body><p>This is an HTML attachment</p></body></html>"
        html_part = MIMEBase("text", "html")
        html_part.add_header("Content-Disposition", "attachment", filename="html_attachment.html")
        html_part.set_payload(html_body)
        return html_part

    def image_attachment(self):
        image_file = '/usr/share/kolab-webadmin/public_html/skins/default/images/logo_kolab.png'
        image_part = MIMEImage(open(image_file, 'r').read())
        image_part.add_header("Content-Disposition", "attachment", filename=os.path.basename(image_file))
        return image_part

    def message_standard_params(self, subject, msg):
        msg['From'] = self.message_from
        msg['To'] = self.message_to

        msg['Subject'] = subject
        msg['Date'] = formatdate(localtime=True)

        return msg

    def send_message(self, msg, _to=None, _from=None):
        smtp = smtplib.SMTP('localhost', 10026)

        if _to == None:
            _to = self.send_to

        if _from == None:
            _from = self.send_from

        smtp.sendmail(_from, _to, msg.as_string())

    def test_001_inbox_created(self):
        imap = IMAP()
        imap.connect()

        folders = imap.lm('user/%(local)s@%(domain)s' % (self.user))
        self.assertEqual(len(folders), 1)

    def test_002_send_plaintext(self):
        subject = "test_002_send_plaintext"
        body = "This is a test message"
        msg = MIMEBase("text", "plain")
        msg = self.message_standard_params(subject, msg)

        msg.set_payload(body)

        self.send_message(msg)

        if not self.check_message_delivered(subject):
            raise Exception

    def test_003_send_plaintext_with_attachment(self):
        subject = "test_003_send_plaintext_with_attachment"
        body = "This is a test message"
        msg = MIMEMultipart()
        msg = self.message_standard_params(subject, msg)

        msg.attach(MIMEText(body))
        msg.attach(self.image_attachment())

        self.send_message(msg)

        if not self.check_message_delivered(subject):
            raise Exception

    def test_004_send_html(self):
        subject = "test_004_send_html"
        body = "<html><body><p>This is a test message</p></body></html>"
        msg = MIMEBase("text", "html")
        msg = self.message_standard_params(subject, msg)
        msg.set_payload(body)

        self.send_message(msg)

        if not self.check_message_delivered(subject):
            raise Exception

    def test_005_send_html_with_plaintext_alternative(self):
        subject = "test_005_send_html_with_plaintext_alternative"
        html_body = "<html><body><p>This is the HTML part</p></body></html>"
        plain_body = "This is the plaintext part"

        msg = MIMEMultipart("alternative")
        msg = self.message_standard_params(subject, msg)

        html_part = MIMEBase("text", "html")
        html_part.set_payload(html_body)
        msg.attach(html_part)

        plain_part = MIMEText(plain_body)
        msg.attach(plain_part)

        self.send_message(msg)

        if not self.check_message_delivered(subject):
            raise Exception

    def test_006_send_html_with_attachment(self):
        subject = "test_006_send_html_with_attachment"
        html_body = "<html><body><p>This is the HTML part</p></body></html>"
        plain_body = "This is the plaintext part"

        msg = MIMEMultipart()
        msg = self.message_standard_params(subject, msg)

        html_part = MIMEBase("text", "html")
        html_part.set_payload(html_body)
        msg.attach(html_part)

        msg.attach(self.image_attachment())

        self.send_message(msg)

        if not self.check_message_delivered(subject):
            raise Exception

    def test_007_send_html_with_plaintext_alternative_and_attachment(self):
        subject = "test_007_send_html_with_plaintext_alternative_and_attachment"
        html_body = "<html><body><p>This is the HTML part</p></body></html>"
        plain_body = "This is the plaintext part"

        msg = MIMEMultipart("mixed")
        msg = self.message_standard_params(subject, msg)

        message_part = MIMEMultipart("alternative")

        html_part = MIMEBase("text", "html")
        html_part.set_payload(html_body)
        message_part.attach(html_part)

        plain_part = MIMEText(plain_body)
        message_part.attach(plain_part)

        msg.attach(message_part)

        msg.attach(self.image_attachment())

        self.send_message(msg)

        if not self.check_message_delivered(subject):
            raise Exception

    def test_008_send_plaintext_with_html_attachment(self):
        subject = "test_008_send_plaintext_with_html_attachment"
        body = "This is a plaintext message"
        msg = MIMEMultipart()
        msg = self.message_standard_params(subject, msg)

        msg.attach(MIMEText(body))

        msg.attach(self.html_attachment())

        self.send_message(msg)

        if not self.check_message_delivered(subject):
            raise Exception

    def test_009_send_plaintext_forwarded(self):
        subject = "test_009_send_plaintext_forwarded"
        body = "This is a plaintext message"

        from tests.functional.user_add import user_add
        user_add("Jane", "Doe")

        from tests.functional.synchronize import synchronize_once
        synchronize_once()

        admin_login = conf.get('cyrus-imap', 'admin_login')
        admin_password = conf.get('cyrus-imap', 'admin_password')

        import sievelib.factory
        script = sievelib.factory.FiltersSet("test_wallace_test_009_forward")
        script.require("copy")
        script.addfilter("forward", ["true"], [("redirect", ":copy", "john.doe@example.org")])

        import sievelib.managesieve
        sieveclient = sievelib.managesieve.Client('localhost', 4190, True)
        sieveclient.connect(None, None, True)
        sieveclient._plain_authentication(admin_login, admin_password, 'jane.doe@example.org')
        sieveclient.authenticated = True

        script_str = script.__str__()

        print script_str

        sieveclient.putscript("test_wallace_test_009_forward", script_str)

        sieveclient.setactive("test_wallace_test_009_forward")

        msg = MIMEText(body)
        msg['From'] = self.message_from
        msg['To'] = '"Doe, Jane" <jane.doe@example.org>'

        msg['Subject'] = subject
        msg['Date'] = formatdate(localtime=True)

        self.send_message(msg, _to='jane.doe@example.org', _from='john.doe@example.org')

        raise Exception