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