Welcome to mirror list, hosted at ThFree Co, Russian Federation.

account.py « tests « model « jmc « src - github.com/dax/jmc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5df4d5e63110b314afe081da0eb5882fa0e0dc83 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# -*- coding: utf-8 -*-
##
## test_account.py
## Login : <dax@happycoders.org>
## Started on  Wed Feb 14 08:23:17 2007 David Rousselie
## $Id$
##
## Copyright (C) 2007 David Rousselie
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##

import unittest
import thread

from jcl.tests import JCLTestCase
import jcl.model as model
from jcl.model.account import Account, PresenceAccount, User
from jmc.model.account import MailAccount, POP3Account, IMAPAccount, SMTPAccount

from jcl.model.tests.account import Account_TestCase, \
    PresenceAccount_TestCase, InheritableAccount_TestCase, \
    ExampleAccount
from jmc.model.tests import email_generator, server

class MailAccount_TestCase(PresenceAccount_TestCase):
    def setUp(self):
        PresenceAccount_TestCase.setUp(self, tables=[MailAccount])
        self.account = MailAccount(user=User(jid="user1@test.com"),
                                   name="account1",
                                   jid="account1@jmc.test.com")
        self.account_class = MailAccount

    def make_test(email_type, tested_func, expected_res):
        def inner(self):
            encoded, multipart, header = email_type
            email = email_generator.generate(encoded,
                                             multipart,
                                             header)
            part = tested_func(self, email)
            self.assertEquals(part, expected_res)
        return inner

    test_get_decoded_part_not_encoded = \
        make_test((False, False, False), \
                  lambda self, email: self.account.get_decoded_part(email, None), \
                  u"Not encoded single part")

    test_get_decoded_part_encoded = \
        make_test((True, False, False), \
                  lambda self, email: self.account.get_decoded_part(email, None), \
                  u"Encoded single part with 'iso-8859-15' charset (éàê)")

    test_format_message_summary_not_encoded = \
        make_test((False, False, True), \
                  lambda self, email: self.account.format_message_summary(email), \
                  (u"From : not encoded from\nSubject : not encoded subject\n\n", \
                   u"not encoded from"))

    test_format_message_summary_encoded = \
        make_test((True, False, True), \
                  lambda self, email: self.account.format_message_summary(email), \
                  (u"From : encoded from (éàê)\nSubject : encoded subject " + \
                   u"(éàê)\n\n", \
                   u"encoded from (éàê)"))

    test_format_message_summary_partial_encoded = \
        make_test((True, False, True), \
                  lambda self, email: \
                  email.replace_header("Subject", \
                                       "\" " + str(email["Subject"]) \
                                       + " \" not encoded part") or \
                  email.replace_header("From", \
                                       "\" " + str(email["From"]) \
                                       + " \" not encoded part") or \
                  self.account.format_message_summary(email), \
                  (u"From : \"encoded from (éàê)\" not encoded part\nSubject " + \
                   u": \"encoded subject (éàê)\" not encoded part\n\n", \
                   u"\"encoded from (éàê)\" not encoded part"))

    test_format_message_single_not_encoded = \
        make_test((False, False, True), \
                  lambda self, email: self.account.format_message(email), \
                  (u"From : not encoded from\nSubject : not encoded subject" + \
                   u"\n\nNot encoded single part\n", \
                   u"not encoded from"))

    test_format_message_single_encoded = \
        make_test((True, False, True), \
                  lambda self, email: self.account.format_message(email), \
                  (u"From : encoded from (éàê)\nSubject : encoded subject " + \
                   u"(éàê)\n\nEncoded single part with 'iso-8859-15' charset" + \
                   u" (éàê)\n", \
                   u"encoded from (éàê)"))

    test_format_message_multi_not_encoded = \
        make_test((False, True, True), \
                  lambda self, email: self.account.format_message(email), \
                  (u"From : not encoded from\nSubject : not encoded subject" + \
                   u"\n\nNot encoded multipart1\nNot encoded multipart2\n", \
                   u"not encoded from"))

    test_format_message_multi_encoded = \
        make_test((True, True, True), \
                  lambda self, email: self.account.format_message(email), \
                  (u"From : encoded from (éàê)\nSubject : encoded subject (éà" + \
                   u"ê)\n\nutf-8 multipart1 with no charset (éàê)" + \
                   u"\nEncoded multipart2 with 'iso-8859-15' charset (éàê)\n" + \
                   u"Encoded multipart3 with no charset (éàê)\n", \
                   u"encoded from (éàê)"))

class POP3Account_TestCase(InheritableAccount_TestCase):
    def setUp(self):
        JCLTestCase.setUp(self, tables=[Account, PresenceAccount, User,
                                        MailAccount, POP3Account])
        self.pop3_account = POP3Account(user=User(jid="user1@test.com"),
                                        name="account1",
                                        jid="account1@jmc.test.com",
                                        login="login")
        self.pop3_account.password = "pass"
        self.pop3_account.host = "localhost"
        self.pop3_account.port = 1110
        self.pop3_account.ssl = False
        model.db_disconnect()
        self.account_class = POP3Account

    def make_test(responses=None, queries=None, core=None):
        def inner(self):
            self.server = server.DummyServer("localhost", 1110)
            thread.start_new_thread(self.server.serve, ())
            self.server.responses = ["+OK connected\r\n",
                                     "+OK name is a valid mailbox\r\n",
                                     "+OK pass\r\n"]
            if responses:
                self.server.responses += responses
            self.server.queries = ["USER login\r\n",
                                   "PASS pass\r\n"]
            if queries:
                self.server.queries += queries
            self.server.queries += ["QUIT\r\n"]
            self.pop3_account.connect()
            self.failUnless(self.pop3_account.connection,
                            "Cannot establish connection")
            if core:
                model.db_connect()
                core(self)
                model.db_disconnect()
            self.pop3_account.disconnect()
            self.failUnless(self.server.verify_queries(),
                            "Sended queries does not match expected queries.")
        return inner

    test_connection = make_test()

    test_get_mail_list = \
        make_test(["+OK 2 20\r\n"],
                  ["STAT\r\n"],
                  lambda self: \
                      self.assertEquals(self.pop3_account.get_mail_list(),
                                        ["1", "2"]))

    test_get_mail_summary = \
        make_test(["+OK 10 octets\r\n" +
                   "From: user@test.com\r\n" +
                   "Subject: subject test\r\n\r\n" +
                   "mymessage\r\n.\r\n",
                   "+OK\r\n"],
                  ["RETR 1\r\n",
                   "RSET\r\n"],
                  lambda self: \
                      self.assertEquals(self.pop3_account.get_mail_summary(1),
                                        (u"From : user@test.com\n" +
                                         u"Subject : subject test\n\n",
                                         u"user@test.com")))

    test_get_mail = \
        make_test(["+OK 10 octets\r\n" +
                   "From: user@test.com\r\n" +
                   "Subject: subject test\r\n\r\n" +
                   "mymessage\r\n.\r\n",
                   "+OK\r\n"],
                  ["RETR 1\r\n",
                   "RSET\r\n"], \
                  lambda self: \
                  self.assertEquals(self.pop3_account.get_mail(1), \
                                    (u"From : user@test.com\n" + \
                                     u"Subject : subject test\n\n" + \
                                     u"mymessage\n", \
                                     u"user@test.com")))

    test_unsupported_reset_command_get_mail_summary = \
        make_test(["+OK 10 octets\r\n" + \
                   "From: user@test.com\r\n" + \
                   "Subject: subject test\r\n\r\n" + \
                   "mymessage\r\n.\r\n",
                   "-ERR unknown command\r\n"], \
                  ["RETR 1\r\n",
                   "RSET\r\n"], \
                  lambda self: \
                  self.assertEquals(self.pop3_account.get_mail_summary(1), \
                                    (u"From : user@test.com\n" + \
                                     u"Subject : subject test\n\n", \
                                     u"user@test.com")))

    test_unsupported_reset_command_get_mail = \
        make_test(["+OK 10 octets\r\n" + \
                   "From: user@test.com\r\n" + \
                   "Subject: subject test\r\n\r\n" + \
                   "mymessage\r\n.\r\n",
                   "-ERR unknown command\r\n"], \
                  ["RETR 1\r\n",
                   "RSET\r\n"], \
                  lambda self: \
                  self.assertEquals(self.pop3_account.get_mail(1), \
                                    (u"From : user@test.com\n" + \
                                     u"Subject : subject test\n\n" + \
                                     u"mymessage\n", \
                                     u"user@test.com")))

class IMAPAccount_TestCase(InheritableAccount_TestCase):
    def setUp(self):
        JCLTestCase.setUp(self, tables=[Account, PresenceAccount, User,
                                        MailAccount, IMAPAccount])
        self.imap_account = IMAPAccount(user=User(jid="user1@test.com"),
                                        name="account1",
                                        jid="account1@jmc.test.com",
                                        login="login")
        self.imap_account.password = "pass"
        self.imap_account.host = "localhost"
        self.imap_account.port = 1143
        self.imap_account.ssl = False
        self.account_class = IMAPAccount

    def make_test(self, responses=None, queries=None, core=None):
        def inner():
            self.server = server.DummyServer("localhost", 1143)
            thread.start_new_thread(self.server.serve, ())
            self.server.responses = ["* OK [CAPABILITY IMAP4 LOGIN-REFERRALS " + \
                                     "AUTH=PLAIN]\n", \
                                     lambda data: "* CAPABILITY IMAP4 " + \
                                     "LOGIN-REFERRALS AUTH=PLAIN\n" + \
                                     data.split()[0] + \
                                     " OK CAPABILITY completed\n", \
                                     lambda data: data.split()[0] + \
                                     " OK LOGIN completed\n"]
            if responses:
                self.server.responses += responses
            self.server.queries = ["^[^ ]* CAPABILITY", \
                                   "^[^ ]* LOGIN login \"pass\""]
            if queries:
                self.server.queries += queries
            self.server.queries += ["^[^ ]* LOGOUT"]
            if not self.imap_account.connected:
                self.imap_account.connect()
            self.failUnless(self.imap_account.connection, \
                            "Cannot establish connection")
            if core:
                model.db_connect()
                core(self)
                model.db_disconnect()
            if self.imap_account.connected:
                self.imap_account.disconnect()
            self.failUnless(self.server.verify_queries())
        return inner

    def test_connection(self):
        test_func = self.make_test()
        test_func()

    def test_get_mail_list(self):
        test_func = self.make_test([lambda data: "* 42 EXISTS\n* 1 RECENT\n* OK" +\
                                    " [UNSEEN 9]\n* FLAGS (\Deleted \Seen\*)\n*" +\
                                    " OK [PERMANENTFLAGS (\Deleted \Seen\*)\n" + \
                                    data.split()[0] + \
                                    " OK [READ-WRITE] SELECT completed\n", \
                                    lambda data: "* SEARCH 9 10 \n" + \
                                    data.split()[0] + " OK SEARCH completed\n"], \
                                   ["^[^ ]* SELECT INBOX", \
                                    "^[^ ]* SEARCH RECENT"], \
                                   lambda self: \
                                   self.assertEquals(self.imap_account.get_mail_list(), ['9', '10']))
        test_func()

    def test_get_mail_list_delimiter1(self):
        self.imap_account.mailbox = "INBOX/dir1/subdir2"
        self.imap_account.delimiter = "."
        test_func = self.make_test( \
            [lambda data: "* 42 EXISTS\n* 1 RECENT\n* OK" + \
                 " [UNSEEN 9]\n* FLAGS (\Deleted \Seen\*)\n*" + \
                 " OK [PERMANENTFLAGS (\Deleted \Seen\*)\n" + \
                 data.split()[0] + \
                 " OK [READ-WRITE] SELECT completed\n", \
                 lambda data: "* SEARCH 9 10 \n" + \
                 data.split()[0] + " OK SEARCH completed\n"], \
                ["^[^ ]* SELECT \"?INBOX\.dir1\.subdir2\"?",
                 "^[^ ]* SEARCH RECENT"], \
                lambda self: \
                self.assertEquals(self.imap_account.get_mail_list(), ['9', '10']))
        test_func()

    def test_get_mail_list_delimiter2(self):
        self.imap_account.mailbox = "INBOX/dir1/subdir2"
        self.imap_account.delimiter = "/"
        test_func = self.make_test( \
            [lambda data: "* 42 EXISTS\n* 1 RECENT\n* OK" + \
                 " [UNSEEN 9]\n* FLAGS (\Deleted \Seen\*)\n*" + \
                 " OK [PERMANENTFLAGS (\Deleted \Seen\*)\n" + \
                 data.split()[0] + \
                 " OK [READ-WRITE] SELECT completed\n", \
                 lambda data: "* SEARCH 9 10 \n" + \
                 data.split()[0] + " OK SEARCH completed\n"], \
                ["^[^ ]* SELECT \"?INBOX/dir1/subdir2\"?",
                 "^[^ ]* SEARCH RECENT"], \
                lambda self: \
                self.assertEquals(self.imap_account.get_mail_list(), ['9', '10']))
        test_func()

    def test_get_mail_summary(self):
        test_func = self.make_test([lambda data: "* 42 EXISTS\r\n* 1 RECENT\r\n* OK" +\
                                        " [UNSEEN 9]\r\n* FLAGS (\Deleted \Seen\*)\r\n*" +\
                                        " OK [PERMANENTFLAGS (\Deleted \Seen\*)\r\n" + \
                                        data.split()[0] + \
                                        " OK [READ-WRITE] SELECT completed\r\n", \
                                        lambda data: "* 1 FETCH ((RFC822) {12}\r\nbody" + \
                                        " text\r\n)\r\n" + \
                                        data.split()[0] + " OK FETCH completed\r\n"],
                                   ["^[^ ]* EXAMINE INBOX",
                                    "^[^ ]* FETCH 1 \(RFC822\)"],
                                   lambda self: self.assertEquals(self.imap_account.get_mail_summary(1),
                                                                  (u"From : None\nSubject : None\n\n",
                                                                   u"None")))
        test_func()

    def test_get_mail(self):
        test_func = self.make_test([lambda data: "* 42 EXISTS\r\n* 1 RECENT\r\n* OK" +\
                               " [UNSEEN 9]\r\n* FLAGS (\Deleted \Seen\*)\r\n*" +\
                               " OK [PERMANENTFLAGS (\Deleted \Seen\*)\r\n" + \
                               data.split()[0] + \
                               " OK [READ-WRITE] SELECT completed\r\n", \
                               lambda data: "* 1 FETCH ((RFC822) {11}\r\nbody" + \
                               " text\r\n)\r\n" + \
                               data.split()[0] + " OK FETCH completed\r\n"], \
                              ["^[^ ]* EXAMINE INBOX", \
                               "^[^ ]* FETCH 1 \(RFC822\)",], \
                              lambda self: self.assertEquals(self.imap_account.get_mail(1), \
                                                             (u"From : None\nSubject : None\n\nbody text\r\n\n", \
                                                              u"None")))
        test_func()

    def test_build_folder_cache(self):
        test_func = self.make_test(\
        [lambda data: '* LIST () "." "INBOX"\r\n' + \
         '* LIST () "." "INBOX.dir1"\r\n' + \
         '* LIST () "." "INBOX.dir1.subdir1"\r\n' + \
         '* LIST () "." "INBOX.dir1.subdir2"\r\n' + \
         '* LIST () "." "INBOX.dir2"\r\n' + \
         data.split()[0] + ' OK LIST completed\r\n'],
        ["^[^ ]* LIST \"\" \*"],
        lambda self: self.assertEquals(self.imap_account._build_folder_cache(),
                                       {"INBOX":
                                        {"dir1":
                                         {"subdir1": {},
                                          "subdir2": {}},
                                         "dir2": {}}}))
        test_func()

    def test_ls_dir_base(self):
        self.test_build_folder_cache()
        self.assertEquals(self.imap_account.ls_dir(""),
                          ["INBOX"])

    def test_ls_dir_subdir(self):
        self.test_build_folder_cache()
        result = self.imap_account.ls_dir("INBOX")
        result.sort()
        self.assertEquals(result,
                          ["dir1", "dir2"])

    def test_ls_dir_subsubdir_delim1(self):
        self.test_build_folder_cache()
        self.imap_account.default_delimiter = "."
        result = self.imap_account.ls_dir("INBOX/dir1")
        result.sort()
        self.assertEquals(result,
                          ["subdir1", "subdir2"])

    def test_ls_dir_subsubdir_delim2(self):
        self.test_build_folder_cache()
        result = self.imap_account.ls_dir("INBOX/dir1")
        result.sort()
        self.assertEquals(result,
                          ["subdir1", "subdir2"])

    def test_populate_handler(self):
        self.assertEquals(".", self.imap_account.delimiter)
        self.imap_account.mailbox = "INBOX.dir1.subdir2"
        def call_func(self):
            self.imap_account.populate_handler()
            self.assertEquals("INBOX/dir1/subdir2", self.imap_account.mailbox)
        test_func = self.make_test(\
            [lambda data: '* LIST () "." "INBOX.dir1.subdir2"\r\n' + \
                 data.split()[0] + ' OK LIST completed\r\n'],
            ["^[^ ]* LIST \"?INBOX.dir1.subdir2\"? \*"],
            call_func)
        test_func()

    def test_populate_handler_wrong_mailbox(self):
        self.assertEquals(".", self.imap_account.delimiter)
        self.imap_account.mailbox = "INBOX.dir1.subdir2"
        def call_func(self):
            try:
                self.imap_account.populate_handler()
            except Exception, e:
                return
            self.fail("Exception should have been raised")
        test_func = self.make_test(\
            [lambda data: data.split()[0] + ' ERR LIST completed\r\n'],
            ["^[^ ]* LIST \"?INBOX.dir1.subdir2\"? \*"],
            call_func)
        test_func()

class SMTPAccount_TestCase(Account_TestCase):
    def setUp(self):
        JCLTestCase.setUp(self, tables=[Account, ExampleAccount, User,
                                        SMTPAccount])
        self.account_class = SMTPAccount

    def test_default_account_post_func_no_default_true(self):
        model.db_connect()
        user1 = User(jid="user1@test.com")
        account11 = SMTPAccount(user=user1,
                                name="account11",
                                jid="account11@jmc.test.com")
        account12 = SMTPAccount(user=user1,
                                name="account12",
                                jid="account12@jmc.test.com")
        (name, field_type, field_options, post_func, default_func) = \
            SMTPAccount.get_register_fields()[7]
        value = post_func("True", None, "user1@test.com")
        self.assertTrue(value)
        model.db_disconnect()

    def test_default_account_post_func_no_default_false(self):
        model.db_connect()
        user1 = User(jid="user1@test.com")
        account11 = SMTPAccount(user=user1,
                                name="account11",
                                jid="account11@jmc.test.com")
        account12 = SMTPAccount(user=user1,
                                name="account12",
                                jid="account12@jmc.test.com")
        (name, field_type, field_options, post_func, default_func) = \
            SMTPAccount.get_register_fields()[7]
        value = post_func("False", None, "user1@test.com")
        self.assertTrue(value)
        model.db_disconnect()

    def test_default_account_post_func_true(self):
        model.db_connect()
        user1 = User(jid="user1@test.com")
        account11 = SMTPAccount(user=user1,
                                name="account11",
                                jid="account11@jmc.test.com")
        account12 = SMTPAccount(user=user1,
                                name="account12",
                                jid="account12@jmc.test.com")
        account12.default_account = True
        (name, field_type, field_options, post_func, default_func) = \
            SMTPAccount.get_register_fields()[7]
        value = post_func("True", None, "user1@test.com")
        self.assertTrue(value)
        self.assertFalse(account12.default_account)
        model.db_disconnect()

    def test_default_account_post_func_false(self):
        model.db_connect()
        user1 = User(jid="user1@test.com")
        account11 = SMTPAccount(user=user1,
                                name="account11",
                                jid="account11@jmc.test.com")
        account12 = SMTPAccount(user=user1,
                                name="account12",
                                jid="account12@jmc.test.com")
        account12.default_account = True
        (name, field_type, field_options, post_func, default_func) = \
            SMTPAccount.get_register_fields()[7]
        value = post_func("False", None, "user1@test.com")
        self.assertFalse(value)
        self.assertTrue(account12.default_account)
        model.db_disconnect()

    def test_create_email(self):
        model.db_connect()
        account11 = SMTPAccount(user=User(jid="user1@test.com"),
                                name="account11",
                                jid="account11@jmc.test.com")
        model.db_disconnect()
        email = account11.create_email("from@test.com",
                                       "to@test.com",
                                       "subject",
                                       "body")
        self.assertEqual(email['From'], "from@test.com")
        self.assertEqual(email['To'], "to@test.com")
        self.assertEqual(email['Subject'], "subject")
        self.assertEqual(email.get_payload(), "body")

    def make_test(self, responses=None, queries=None, core=None):
        def inner():
            self.server = server.DummyServer("localhost", 1025)
            thread.start_new_thread(self.server.serve, ())
            self.server.responses = []
            if responses:
                self.server.responses += responses
            self.server.responses += ["221 localhost closing connection\r\n"]
            self.server.queries = []
            if queries:
                self.server.queries += queries
            self.server.queries += ["quit\r\n"]
            if core:
                model.db_connect()
                core(self)
                model.db_disconnect()
            self.failUnless(self.server.verify_queries())
        return inner

    def test_send_email_esmtp_no_auth(self):
       model.db_connect()
       smtp_account = SMTPAccount(user=User(jid="user1@test.com"),
                                  name="account11",
                                  jid="account11@jmc.test.com")
       smtp_account.host = "localhost"
       smtp_account.port = 1025
       model.db_disconnect()
       email = smtp_account.create_email("from@test.com",
                                         "to@test.com",
                                         "subject",
                                         "body")
       test_func = self.make_test(["220 localhost ESMTP\r\n",
                                   "250-localhost Hello 127.0.0.1\r\n"
                                   + "250-SIZE 52428800\r\n"
                                   + "250-PIPELINING\r\n"
                                   + "250 HELP\r\n",
                                   "250 OK\r\n",
                                   "250 Accepted\r\n",
                                   "354 Enter message\r\n",
                                   None, None, None, None,
                                   None, None, None, None,
                                   "250 OK\r\n"],
                                  ["ehlo \[127.0.0.1\]\r\n",
                                   "mail FROM:<" + str(email['From']) + ">.*",
                                   "rcpt TO:<" + str(email['To']) + ">\r\n",
                                   "data\r\n"] +
                                   email.as_string().split("\n") + [".\r\n"],
                                  lambda self: \
                                  smtp_account.send_email(email))
       test_func()

    def test_send_email_no_auth(self):
       model.db_connect()
       smtp_account = SMTPAccount(user=User(jid="user1@test.com"),
                                  name="account11",
                                  jid="account11@jmc.test.com")
       smtp_account.host = "localhost"
       smtp_account.port = 1025
       model.db_disconnect()
       email = smtp_account.create_email("from@test.com",
                                         "to@test.com",
                                         "subject",
                                         "body")
       test_func = self.make_test(["220 localhost SMTP\r\n",
                                   "504 ESMTP not supported\r\n",
                                   "250-localhost Hello 127.0.0.1\r\n"
                                   + "250-SIZE 52428800\r\n"
                                   + "250-PIPELINING\r\n"
                                   + "250 HELP\r\n",
                                   "250 OK\r\n",
                                   "250 Accepted\r\n",
                                   "354 Enter message\r\n",
                                   None, None, None, None,
                                   None, None, None, None,
                                   "250 OK\r\n"],
                                  ["ehlo \[127.0.0.1\]\r\n",
                                   "helo \[127.0.0.1\]\r\n",
                                   "mail FROM:<" + str(email['From']) + ">.*",
                                   "rcpt TO:<" + str(email['To']) + ">\r\n",
                                   "data\r\n"] +
                                   email.as_string().split("\n") + [".\r\n"],
                                  lambda self: \
                                  smtp_account.send_email(email))
       test_func()

    def test_send_email_esmtp_auth(self):
       model.db_connect()
       smtp_account = SMTPAccount(user=User(jid="user1@test.com"),
                                  name="account11",
                                  jid="account11@jmc.test.com")
       smtp_account.host = "localhost"
       smtp_account.port = 1025
       smtp_account.login = "user"
       smtp_account.password = "pass"
       model.db_disconnect()
       email = smtp_account.create_email("from@test.com",
                                         "to@test.com",
                                         "subject",
                                         "body")
       test_func = self.make_test(["220 localhost ESMTP\r\n",
                                   "250-localhost Hello 127.0.0.1\r\n"
                                   + "250-SIZE 52428800\r\n"
                                   + "250-AUTH PLAIN LOGIN CRAM-MD5\r\n"
                                   + "250-PIPELINING\r\n"
                                   + "250 HELP\r\n",
                                   "334 ZGF4IDNmNDM2NzY0YzBhNjgyMTQ1MzhhZGNiMjE2YTYxZjRm\r\n",
                                   "235 Authentication succeeded\r\n",
                                   "250 OK\r\n",
                                   "250 Accepted\r\n",
                                   "354 Enter message\r\n",
                                   None, None, None, None,
                                   None, None, None, None,
                                   "250 OK\r\n"],
                                  ["ehlo \[127.0.0.1\]\r\n",
                                   "AUTH CRAM-MD5\r\n",
                                   ".*\r\n",
                                   "mail FROM:<" + str(email['From']) + ">.*",
                                   "rcpt TO:<" + str(email['To']) + ">\r\n",
                                   "data\r\n"] +
                                   email.as_string().split("\n") + [".\r\n"],
                                  lambda self: \
                                  smtp_account.send_email(email))
       test_func()

    def test_send_email_esmtp_auth_method2(self):
       model.db_connect()
       smtp_account = SMTPAccount(user=User(jid="user1@test.com"),
                                  name="account11",
                                  jid="account11@jmc.test.com")
       smtp_account.host = "localhost"
       smtp_account.port = 1025
       smtp_account.login = "user"
       smtp_account.password = "pass"
       model.db_disconnect()
       email = smtp_account.create_email("from@test.com",
                                         "to@test.com",
                                         "subject",
                                         "body")
       test_func = self.make_test(["220 localhost ESMTP\r\n",
                                   "250-localhost Hello 127.0.0.1\r\n"
                                   + "250-SIZE 52428800\r\n"
                                   + "250-AUTH PLAIN LOGIN CRAM-MD5\r\n"
                                   + "250-PIPELINING\r\n"
                                   + "250 HELP\r\n",
                                   "334 ZGF4IDNmNDM2NzY0YzBhNjgyMTQ1MzhhZGNiMjE2YTYxZjRm\r\n",
                                   "535 Incorrect Authentication data\r\n",
                                   "334 asd235r4\r\n",
                                   "235 Authentication succeeded\r\n",
                                   "250 OK\r\n",
                                   "250 Accepted\r\n",
                                   "354 Enter message\r\n",
                                   None, None, None, None,
                                   None, None, None, None,
                                   "250 OK\r\n"],
                                  ["ehlo \[127.0.0.1\]\r\n",
                                   "AUTH CRAM-MD5\r\n",
                                   ".*\r\n",
                                   "AUTH LOGIN .*\r\n",
                                   ".*\r\n",
                                   "mail FROM:<" + str(email['From']) + ">.*",
                                   "rcpt TO:<" + str(email['To']) + ">\r\n",
                                   "data\r\n"] +
                                   email.as_string().split("\n") + [".\r\n"],
                                  lambda self: \
                                  smtp_account.send_email(email))
       test_func()


def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(MailAccount_TestCase, 'test'))
    suite.addTest(unittest.makeSuite(POP3Account_TestCase, 'test'))
    suite.addTest(unittest.makeSuite(IMAPAccount_TestCase, 'test'))
    suite.addTest(unittest.makeSuite(SMTPAccount_TestCase, 'test'))
    return suite

if __name__ == '__main__':
    unittest.main(defaultTest='suite')