1 | # -*- coding: utf-8 -*-
|
---|
2 |
|
---|
3 | import os, multiprocessing, xmlrpclib, time
|
---|
4 | from SimpleXMLRPCServer import SimpleXMLRPCServer
|
---|
5 | from unittest import TestCase
|
---|
6 |
|
---|
7 | from postman.daemon import Postman, PostmanXMLRPC, PostmanDaemon
|
---|
8 | from postman.models import Member, MailingList
|
---|
9 | from postman.storage import JsonStorage as Storage
|
---|
10 |
|
---|
11 |
|
---|
12 | class TestPostman(TestCase):
|
---|
13 | """
|
---|
14 | postman.daemon.Postman tests.
|
---|
15 |
|
---|
16 | Remember to call the .clear() method of postman after each test, so
|
---|
17 | the temp storage files are deleted
|
---|
18 | """
|
---|
19 | def setUp(self):
|
---|
20 | self.configfile = os.path.join(os.path.dirname(__file__), 'postman.conf')
|
---|
21 | self.mailing_list = MailingList('test_list', 'test_list@example.com',
|
---|
22 | members={}, configfile=self.configfile)
|
---|
23 | self.member = Member('test@example.com')
|
---|
24 |
|
---|
25 | def test___init__(self):
|
---|
26 | postman = Postman(configfile=self.configfile)
|
---|
27 | self.assertIsInstance(postman, Postman)
|
---|
28 | self.assertEqual(postman.mailings, {})
|
---|
29 | self.assertEqual(postman.mailings_addresses, [])
|
---|
30 | self.assertIsInstance(postman.dbs, dict)
|
---|
31 | self.assertTrue('mailings' in postman.dbs.keys())
|
---|
32 | self.assertTrue('members' in postman.dbs.keys())
|
---|
33 | self.assertIsInstance(postman.dbs['mailings'], Storage)
|
---|
34 | self.assertIsInstance(postman.dbs['members'], Storage)
|
---|
35 |
|
---|
36 | def test_save(self):
|
---|
37 | postman = Postman(configfile=self.configfile)
|
---|
38 | self.assertFalse(postman.save())
|
---|
39 | postman.add_mailing_list(self.mailing_list.info())
|
---|
40 | self.assertTrue(postman.save())
|
---|
41 | # FIXME: We have to test here that the generated json file
|
---|
42 | # contains the data it should contain
|
---|
43 |
|
---|
44 | # Clear the files created by the tests
|
---|
45 | postman.clear()
|
---|
46 |
|
---|
47 | def test_load(self):
|
---|
48 | postman = Postman(configfile=self.configfile)
|
---|
49 | self.assertFalse(postman.mailings)
|
---|
50 | self.assertFalse(postman.mailings_addresses)
|
---|
51 | self.assertFalse(postman.load())
|
---|
52 | postman.add_mailing_list(self.mailing_list.info())
|
---|
53 | self.assertTrue(postman.load())
|
---|
54 |
|
---|
55 | # Check that another postman instance is able to load the saved data
|
---|
56 | postman_load = Postman(configfile=self.configfile)
|
---|
57 | self.assertFalse(postman_load.mailings)
|
---|
58 | self.assertFalse(postman_load.mailings_addresses)
|
---|
59 | postman_load.load()
|
---|
60 | self.assertTrue(postman_load.mailings)
|
---|
61 | self.assertIsInstance(postman_load.mailings, dict)
|
---|
62 | self.assertTrue(postman_load.mailings_addresses)
|
---|
63 | self.assertIsInstance(postman_load.mailings_addresses, list)
|
---|
64 |
|
---|
65 | # Clear the files created by the tests
|
---|
66 | postman.clear()
|
---|
67 |
|
---|
68 | def test_clear(self):
|
---|
69 | postman = Postman(configfile=self.configfile)
|
---|
70 | self.assertFalse(postman.clear())
|
---|
71 | postman.add_mailing_list(self.mailing_list.info())
|
---|
72 | self.assertTrue(postman.clear())
|
---|
73 |
|
---|
74 | def test_add_mailing_list(self):
|
---|
75 | postman = Postman(configfile=self.configfile)
|
---|
76 | with self.assertRaises(TypeError):
|
---|
77 | # test improper info values
|
---|
78 | postman.add_mailing_list(['a list', 'is an', 'invalid parameter'])
|
---|
79 | postman.add_mailing_list(self.mailing_list)
|
---|
80 | with self.assertRaises(ValueError):
|
---|
81 | #test incomplete/missing info values
|
---|
82 | postman.add_mailing_list()
|
---|
83 | postman.add_mailing_list({'name': 'missing info'})
|
---|
84 | postman.add_mailing_list({'address': 'missing info'})
|
---|
85 | postman.add_mailing_list({'name': 'missing info',
|
---|
86 | 'address': 'missing info'})
|
---|
87 | # test mailing lists can be added
|
---|
88 | self.assertTrue(postman.add_mailing_list(self.mailing_list.info()))
|
---|
89 | self.assertTrue(postman.mailings)
|
---|
90 | self.assertIsInstance(postman.mailings, dict)
|
---|
91 | self.assertTrue(postman.mailings_addresses)
|
---|
92 | self.assertIsInstance(postman.mailings_addresses, list)
|
---|
93 | with self.assertRaises(IndexError):
|
---|
94 | # test what happens when the mailing has been already added
|
---|
95 | postman.add_mailing_list(self.mailing_list.info())
|
---|
96 |
|
---|
97 | # Clear the files created by the tests
|
---|
98 | postman.clear()
|
---|
99 |
|
---|
100 | def test_add_mailing_member(self):
|
---|
101 | postman = Postman(configfile=self.configfile)
|
---|
102 | postman.add_mailing_list(self.mailing_list.info())
|
---|
103 | with self.assertRaises(ValueError):
|
---|
104 | # test what happens if we call the method without proper
|
---|
105 | # parameters
|
---|
106 | postman.add_mailing_member()
|
---|
107 | postman.add_mailing_member(None, None)
|
---|
108 | postman.add_mailing_member(None, 'test_list@example.net')
|
---|
109 | postman.add_mailing_member('test@example.net', None)
|
---|
110 | postman.add_mailing_member('test@example', 'test_list@example.net')
|
---|
111 | with self.assertRaises(IndexError):
|
---|
112 | # test if we try to add a member to a non-existing mailing list
|
---|
113 | postman.add_mailing_member('test@example.net',
|
---|
114 | 'test_list_b@example.net')
|
---|
115 | # Test adding a member
|
---|
116 | self.assertTrue(postman.add_mailing_member('test@example.net',
|
---|
117 | self.mailing_list.address))
|
---|
118 | # Test trying to re-add that user
|
---|
119 | self.assertFalse(postman.add_mailing_member('test@example.net',
|
---|
120 | self.mailing_list.address))
|
---|
121 |
|
---|
122 | # Clear the files created by the tests
|
---|
123 | postman.clear()
|
---|
124 |
|
---|
125 |
|
---|
126 | class TestPostmanDaemon(TestCase):
|
---|
127 | """
|
---|
128 | postman.daemon.PostmanDaemon tests.
|
---|
129 |
|
---|
130 | Remember to call the .clear() method of postman after each test, so
|
---|
131 | the temp storage files are deleted
|
---|
132 | """
|
---|
133 | def setUp(self):
|
---|
134 | self.configfile = os.path.join(os.path.dirname(__file__),
|
---|
135 | 'postman.conf')
|
---|
136 | self.mailing_list = MailingList('test_xmlrpc',
|
---|
137 | 'test_xmlrpc@example.com', members={},
|
---|
138 | configfile=self.configfile)
|
---|
139 | self.member = Member('test@example.com')
|
---|
140 |
|
---|
141 | def test___init__(self):
|
---|
142 | daemon = PostmanDaemon(self.configfile)
|
---|
143 | self.assertIsInstance(daemon, PostmanDaemon)
|
---|
144 | self.assertFalse(daemon.ready_to_serve)
|
---|
145 | # FIXME: More tests should be added here once the configuration
|
---|
146 | # file feature is added
|
---|
147 |
|
---|
148 | def test_create_server(self):
|
---|
149 | daemon = PostmanDaemon(self.configfile)
|
---|
150 | daemon.port = 9001
|
---|
151 | self.assertTrue(daemon.create_server())
|
---|
152 | self.assertIsInstance(daemon.server, SimpleXMLRPCServer)
|
---|
153 | self.assertFalse(daemon.ready_to_serve)
|
---|
154 |
|
---|
155 | def test_add_methods(self):
|
---|
156 | daemon = PostmanDaemon(self.configfile)
|
---|
157 | daemon.port = 9002
|
---|
158 | self.assertTrue(daemon.add_methods())
|
---|
159 | self.assertTrue(daemon.ready_to_serve)
|
---|
160 |
|
---|
161 | daemon = PostmanDaemon(self.configfile)
|
---|
162 | daemon.port = 9003
|
---|
163 | daemon.create_server()
|
---|
164 | self.assertTrue(daemon.add_methods())
|
---|
165 | self.assertTrue(daemon.ready_to_serve)
|
---|
166 |
|
---|
167 | def test_run(self):
|
---|
168 | daemon = PostmanDaemon(self.configfile)
|
---|
169 | daemon.port = 9004
|
---|
170 | # start the daemon in another process, so we can start communicating
|
---|
171 | # with itjobs = []
|
---|
172 | p = multiprocessing.Process(target=daemon.run)
|
---|
173 | p.start()
|
---|
174 |
|
---|
175 | # Add a delay here to allow the server to be fully started before
|
---|
176 | # starting to send requests from the client
|
---|
177 | time.sleep(2)
|
---|
178 |
|
---|
179 | # FIXME: Hardcoded url here, should be picked from a config file
|
---|
180 | client = xmlrpclib.ServerProxy('http://localhost:9004')
|
---|
181 |
|
---|
182 | # Check that we can perform an XMLRPC call and that the list of
|
---|
183 | # available public methods contains the list of methods we have
|
---|
184 | # defined in our base XMLRPC class
|
---|
185 | set_class_methods = set(PostmanXMLRPC()._listMethods())
|
---|
186 | set_xmlrpc_methods = set(client.system.listMethods())
|
---|
187 | self.assertTrue(set_class_methods.issubset(set_xmlrpc_methods))
|
---|
188 |
|
---|
189 | # Stop the server
|
---|
190 | p.terminate()
|
---|
191 |
|
---|
192 |
|
---|