summaryrefslogtreecommitdiffstats
path: root/tests/functional/test_wallace/test_001_user_add.py
blob: a6945c936c4589b5ec8f8dd98e6ab093172a3f42 (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
from email import message_from_string

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 TestUserAdd(unittest.TestCase):

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

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

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

        from tests.functional.user_add import user_add
        user_add("John", "Doe")
        user_add("Jane", "Doe")
        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 test_001_inbox_created(self):
        imap = IMAP()
        imap.connect()

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

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

    def test_002_send_forwarded_email(self):
        import smtplib
        from email.MIMEMultipart import MIMEMultipart
        from email.MIMEBase import MIMEBase
        from email.MIMEText import MIMEText
        from email.Utils import COMMASPACE, formatdate
        from email import Encoders

        smtp = smtplib.SMTP('localhost', 10026)
        subject = "%s" % (time.time())
        body = "This is a test message"
        msg = MIMEMultipart()
        msg['From'] = '"Doe, Jane" <jane.doe@example.org>'
        msg['To'] = '"Doe, John" <john.doe@example.org>'
        msg['Subject'] = subject
        msg['Date'] = formatdate(localtime=True)
        msg.attach(MIMEText(body))

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

        smtp.sendmail(send_from, send_to, msg.as_string())

        imap = IMAP()
        imap.connect()
        imap.set_acl("user/jane.doe@example.org", "cyrus-admin", "lrs")
        imap.imap.m.select("user/jane.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)

        if not found:
            raise Exception