source: mailjam/postman/tests/daemon.py@ 7:39e2245da71c

Last change on this file since 7:39e2245da71c was 7:39e2245da71c, checked in by Francisco de Borja Lopez Rio <borja@…>, 12 years ago

Added some fixes to the XMLRPC server code

Added more tests for the XMLRPC server

File size: 7.5 KB
Line 
1# -*- coding: utf-8 -*-
2
3import os, multiprocessing, xmlrpclib
4from SimpleXMLRPCServer import SimpleXMLRPCServer
5from unittest import TestCase
6
7from postman.daemon import Postman, PostmanXMLRPC, PostmanDaemon
8from postman.models import Member, MailingList
9from postman.storage import JsonStorage as Storage
10
11
12class 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 config = {'private': False, 'archive': '/tmp/postman-tests/archive',
21 'storage': '/tmp/postman-tests/storage'}
22 self.mailing_list = MailingList('test_list', 'test_list@example.com',
23 members={}, config=config)
24 self.member = Member('test@example.com')
25
26 def test___init__(self):
27 postman = Postman()
28 self.assertIsInstance(postman, Postman)
29 self.assertEqual(postman.mailings, {})
30 self.assertEqual(postman.mailings_addresses, [])
31 self.assertIsInstance(postman.dbs, dict)
32 self.assertTrue('mailings' in postman.dbs.keys())
33 self.assertTrue('members' in postman.dbs.keys())
34 self.assertIsInstance(postman.dbs['mailings'], Storage)
35 self.assertIsInstance(postman.dbs['members'], Storage)
36
37 def test_save(self):
38 postman = Postman()
39 self.assertFalse(postman.save())
40 postman.add_mailing_list(self.mailing_list.info())
41 self.assertTrue(postman.save())
42 # FIXME: We have to test here that the generated json file
43 # contains the data it should contain
44
45 # Clear the files created by the tests
46 postman.clear()
47
48 def test_load(self):
49 postman = Postman()
50 self.assertFalse(postman.mailings)
51 self.assertFalse(postman.mailings_addresses)
52 self.assertFalse(postman.load())
53 postman.add_mailing_list(self.mailing_list.info())
54 self.assertTrue(postman.load())
55
56 # Check that another postman instance is able to load the saved data
57 postman_load = Postman()
58 self.assertFalse(postman_load.mailings)
59 self.assertFalse(postman_load.mailings_addresses)
60 postman_load.load()
61 self.assertTrue(postman_load.mailings)
62 self.assertIsInstance(postman_load.mailings, dict)
63 self.assertTrue(postman_load.mailings_addresses)
64 self.assertIsInstance(postman_load.mailings_addresses, list)
65
66 # Clear the files created by the tests
67 postman.clear()
68
69 def test_clear(self):
70 postman = Postman()
71 self.assertFalse(postman.clear())
72 postman.add_mailing_list(self.mailing_list.info())
73 self.assertTrue(postman.clear())
74
75 def test_add_mailing_list(self):
76 postman = Postman()
77 with self.assertRaises(TypeError):
78 # test improper info values
79 postman.add_mailing_list(['a list', 'is an', 'invalid parameter'])
80 postman.add_mailing_list(self.mailing_list)
81 with self.assertRaises(ValueError):
82 #test incomplete/missing info values
83 postman.add_mailing_list()
84 postman.add_mailing_list({'name': 'missing info'})
85 postman.add_mailing_list({'address': 'missing info'})
86 postman.add_mailing_list({'name': 'missing info',
87 'address': 'missing info'})
88 # test mailing lists can be added
89 self.assertTrue(postman.add_mailing_list(self.mailing_list.info()))
90 self.assertTrue(postman.mailings)
91 self.assertIsInstance(postman.mailings, dict)
92 self.assertTrue(postman.mailings_addresses)
93 self.assertIsInstance(postman.mailings_addresses, list)
94 with self.assertRaises(IndexError):
95 # test what happens when the mailing has been already added
96 postman.add_mailing_list(self.mailing_list.info())
97
98 # Clear the files created by the tests
99 postman.clear()
100
101 def test_add_mailing_member(self):
102 postman = Postman()
103 postman.add_mailing_list(self.mailing_list.info())
104 with self.assertRaises(ValueError):
105 # test what happens if we call the method without proper
106 # parameters
107 postman.add_mailing_member()
108 postman.add_mailing_member(None, None)
109 postman.add_mailing_member(None, 'test_list@example.net')
110 postman.add_mailing_member('test@example.net', None)
111 postman.add_mailing_member('test@example', 'test_list@example.net')
112 with self.assertRaises(IndexError):
113 # test if we try to add a member to a non-existing mailing list
114 postman.add_mailing_member('test@example.net',
115 'test_list_b@example.net')
116 # Test adding a member
117 self.assertTrue(postman.add_mailing_member('test@example.net',
118 self.mailing_list.address))
119 # Test trying to re-add that user
120 self.assertFalse(postman.add_mailing_member('test@example.net',
121 self.mailing_list.address))
122
123 # Clear the files created by the tests
124 postman.clear()
125
126
127class TestPostmanDaemon(TestCase):
128 """
129 postman.daemon.PostmanDaemon tests.
130
131 Remember to call the .clear() method of postman after each test, so
132 the temp storage files are deleted
133 """
134 def setUp(self):
135 config = {'private': False, 'archive': '/tmp/postman-tests/archive',
136 'storage': '/tmp/postman-tests/storage'}
137 self.mailing_list = MailingList('test_xmlrpc', 'test_xmlrpc@example.com',
138 members={}, config=config)
139 self.member = Member('test@example.com')
140
141 def test___init__(self):
142 daemon = PostmanDaemon()
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()
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()
157 daemon.port = 9002
158 self.assertTrue(daemon.add_methods())
159 self.assertTrue(daemon.ready_to_serve)
160
161 daemon = PostmanDaemon()
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()
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 # FIXME: Hardcoded url here, should be picked from a config file
176 client = xmlrpclib.ServerProxy('http://localhost:9004')
177
178 # Check that we can perform an XMLRPC call and that the list of
179 # available public methods contains the list of methods we have
180 # defined in our base XMLRPC class
181 set_class_methods = set(PostmanXMLRPC()._listMethods())
182 set_xmlrpc_methods = set(client.system.listMethods())
183 self.assertTrue(set_class_methods.issubset(set_xmlrpc_methods))
184
185 # Stop the server
186 p.terminate()
187
Note: See TracBrowser for help on using the repository browser.