source: mailjam/postman/tests/daemon.py@ 10:d5329a2a05b7

Last change on this file since 10:d5329a2a05b7 was 10:d5329a2a05b7, checked in by Borja Lopez <borja@…>, 12 years ago

Fully added support for the configuration file. Now all the code uses
postman.conf to read the configuration parameters from it

Added a tests/postman.conf configuration file with specific configurtion
to use while testing (mostly tmp paths)

Updated the run_tests script to delete the temporary directory where
data is saved while running tests

Updated the tests modules/files so they use the tests config file

File size: 7.9 KB
RevLine 
[5]1# -*- coding: utf-8 -*-
2
[10]3import os, multiprocessing, xmlrpclib, time
[7]4from SimpleXMLRPCServer import SimpleXMLRPCServer
[5]5from unittest import TestCase
[7]6
7from postman.daemon import Postman, PostmanXMLRPC, PostmanDaemon
[5]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):
[10]20 self.configfile = os.path.join(os.path.dirname(__file__), 'postman.conf')
[5]21 self.mailing_list = MailingList('test_list', 'test_list@example.com',
[10]22 members={}, configfile=self.configfile)
[5]23 self.member = Member('test@example.com')
24
25 def test___init__(self):
[10]26 postman = Postman(configfile=self.configfile)
[5]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):
[10]37 postman = Postman(configfile=self.configfile)
[5]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):
[10]48 postman = Postman(configfile=self.configfile)
[5]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
[10]56 postman_load = Postman(configfile=self.configfile)
[5]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):
[10]69 postman = Postman(configfile=self.configfile)
[5]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):
[10]75 postman = Postman(configfile=self.configfile)
[5]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):
[10]101 postman = Postman(configfile=self.configfile)
[5]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()
[6]124
125
126class TestPostmanDaemon(TestCase):
127 """
128 postman.daemon.PostmanDaemon tests.
[7]129
130 Remember to call the .clear() method of postman after each test, so
131 the temp storage files are deleted
[6]132 """
133 def setUp(self):
[10]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)
[6]139 self.member = Member('test@example.com')
[7]140
141 def test___init__(self):
[10]142 daemon = PostmanDaemon(self.configfile)
[7]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):
[10]149 daemon = PostmanDaemon(self.configfile)
[7]150 daemon.port = 9001
151 self.assertTrue(daemon.create_server())
152 self.assertIsInstance(daemon.server, SimpleXMLRPCServer)
153 self.assertFalse(daemon.ready_to_serve)
[6]154
[7]155 def test_add_methods(self):
[10]156 daemon = PostmanDaemon(self.configfile)
[7]157 daemon.port = 9002
158 self.assertTrue(daemon.add_methods())
159 self.assertTrue(daemon.ready_to_serve)
160
[10]161 daemon = PostmanDaemon(self.configfile)
[7]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):
[10]168 daemon = PostmanDaemon(self.configfile)
[7]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()
[10]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)
[7]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
[9]192
Note: See TracBrowser for help on using the repository browser.