1 | # -*- coding: utf-8 -*-
|
---|
2 |
|
---|
3 | import os, sys, multiprocessing, time
|
---|
4 | from unittest import TestCase
|
---|
5 | from postman.mta import MTAClient
|
---|
6 | from postman.daemon import PostmanDaemon
|
---|
7 | from postman.models import Member, MailingList
|
---|
8 |
|
---|
9 |
|
---|
10 | class TestMTAClient(TestCase):
|
---|
11 | """
|
---|
12 | FIXME: These are dummy tests, they cover almost nothing from the
|
---|
13 | real postman mta client (yet)
|
---|
14 | """
|
---|
15 | def setUp(self):
|
---|
16 | self.mta_configfile = os.path.join(os.path.dirname(__file__),
|
---|
17 | 'postman-mta.conf')
|
---|
18 | self.configfile = os.path.join(os.path.dirname(__file__),
|
---|
19 | 'postman.conf')
|
---|
20 | self.mailing_list = MailingList('test_list', 'test_list@example.com',
|
---|
21 | members={}, configfile=self.configfile)
|
---|
22 | self.member = Member('test@example.com')
|
---|
23 | self.raw_email_file = os.path.join(os.path.dirname(__file__),
|
---|
24 | 'sample_raw_email.txt')
|
---|
25 | tmp_to_read = open(self.raw_email_file, 'r')
|
---|
26 | self.raw_email = tmp_to_read.read()
|
---|
27 | tmp_to_read.close()
|
---|
28 |
|
---|
29 | def test___init__(self):
|
---|
30 | # in order to start mta client instances, we need to have a postman
|
---|
31 | # daemon running on the background
|
---|
32 | # This should be added to the setUp() method, but then it would be
|
---|
33 | # more difficult to terminate the process after all the methods
|
---|
34 | # were called
|
---|
35 | daemon = PostmanDaemon(self.configfile)
|
---|
36 | daemon.port = 9100
|
---|
37 | p = multiprocessing.Process(target=daemon.run)
|
---|
38 | p.start()
|
---|
39 | # Add a delay here to allow the server to be fully started before
|
---|
40 | # starting to send requests from the client
|
---|
41 | time.sleep(1)
|
---|
42 |
|
---|
43 | with self.assertRaises(ValueError):
|
---|
44 | mta = MTAClient(self.mailing_list, self.mta_configfile)
|
---|
45 | mta = MTAClient(self.member, self.mta_configfile)
|
---|
46 | mta = MTAClient(None, self.mta_configfile)
|
---|
47 | mta = MTAClient(self.mailing_list.address, self.mta_configfile)
|
---|
48 | self.assertTrue(isinstance(mta, MTAClient))
|
---|
49 | self.assertEqual(mta.address, self.mailing_list.address)
|
---|
50 | self.assertEqual(mta.suscriptors, self.mailing_list.members_addresses)
|
---|
51 | self.assertEqual(mta.reply_to, self.mailing_list.address)
|
---|
52 |
|
---|
53 | p.terminate()
|
---|
54 |
|
---|
55 | def test_get_raw_email(self):
|
---|
56 | # in order to start mta client instances, we need to have a postman
|
---|
57 | # daemon running on the background
|
---|
58 | # This should be added to the setUp() method, but then it would be
|
---|
59 | # more difficult to terminate the process after all the methods
|
---|
60 | # were called
|
---|
61 | daemon = PostmanDaemon(self.configfile)
|
---|
62 | daemon.port = 9100
|
---|
63 | p = multiprocessing.Process(target=daemon.run)
|
---|
64 | p.start()
|
---|
65 | # Add a delay here to allow the server to be fully started before
|
---|
66 | # starting to send requests from the client
|
---|
67 | time.sleep(1)
|
---|
68 |
|
---|
69 | mta = MTAClient(self.mailing_list.address, self.mta_configfile)
|
---|
70 |
|
---|
71 | sys_stdin = sys.stdin
|
---|
72 | sys.stdin = open(self.raw_email_file, 'r')
|
---|
73 | self.assertEqual(mta.get_raw_email(),
|
---|
74 | self.raw_email)
|
---|
75 | sys.stdin.close()
|
---|
76 | with self.assertRaises(IOError):
|
---|
77 | mta.get_raw_email()
|
---|
78 |
|
---|
79 | tmp_file= open(self.raw_email_file('r'))
|
---|
80 | raw_data = tmp_file.read()
|
---|
81 | tmp_file.close()
|
---|
82 | self.assertEqual(mta.get_raw_email(raw_data),
|
---|
83 | self.raw_email)
|
---|
84 |
|
---|
85 | p.terminate()
|
---|
86 |
|
---|
87 | #def save_raw_email(self):
|
---|
88 | # sys_stdin = sys.stdin
|
---|
89 | # sys.stdin = open(self.raw_email_file, 'r')
|
---|
90 | # self.mta.save_raw_email.filename = '/tmp/postman-test-mta-save-raw-email'
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 |
|
---|
95 |
|
---|
96 |
|
---|
97 |
|
---|