Changeset 10:d5329a2a05b7 in mailjam


Ignore:
Timestamp:
May 20, 2012, 9:39:51 AM (13 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

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

Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • bin/run_tests

    r1 r10  
    33# Run the tests for the postman package
    44
    5 import os, sys, unittest
     5import os, shutil, sys, unittest
    66
    77try:
     
    1313    except ImportError:
    1414        raise SystemExit('Could not find postman on your filesystem')
    15    
    16 unittest.main()
     15
     16try:
     17    unittest.main()
     18finally:
     19    # FIXME: hardcoded path to the tmp directory, we should read this path
     20    # from postman/tests/postman.conf
     21    shutil.rmtree('/tmp/postman-tests')
  • postman/daemon.py

    r9 r10  
    9191           'address' not in info.keys() or \
    9292           'members' not in info.keys() or \
    93            'config' not in info.keys():
     93           'configfile' not in info.keys():
    9494            raise ValueError(info, ' does not seem to be a valid configuration')
    9595       
     
    9999
    100100        mailing = MailingList(info['name'], info['address'],
    101                               info['members'], info['config'])       
     101                              info['members'], info['configfile'])
    102102        self.mailings[mailing.address] = mailing
    103103        self.mailings_addresses.append(mailing.address)
  • postman/models.py

    r5 r10  
    3333    """
    3434
    35     def __init__(self, name, address, members={}, config={}):
     35    def __init__(self, name, address, members={}, configfile=None):
     36        self.configfile = configfile
     37        self.storage_config = config.get_config_parameters('storage',
     38                                                           configfile)
     39        self.archive_config = config.get_config_parameters('archive',
     40                                                           configfile)
     41        self.mailing_config = config.get_config_parameters('mailing_lists',
     42                                                           configfile)
    3643        self.name = name
    3744        self.address = address
    3845        self.members = members
    39         self.config = config
    40         self._validate_config() # validate the config parameters
    4146       
    4247    def __repr__(self):
     
    4550    def __str__(self):
    4651        return self.address
    47 
    48     def _validate_config(self):
    49         if not 'storage' in self.config.keys():
    50             self.config['storage'] = os.path.join(config.storage_path,
    51                                                   'mailings/')
    52         if not 'archive' in self.config.keys():
    53             self.config['archive'] = config.archive_path
    54         if not 'private' in self.config.keys():
    55             self.config['private'] = config.private_mailing
    5652       
    5753    def _validate_member_object(self, member=None):
     
    7167    @property
    7268    def storage(self):
    73         return Storage(os.path.join(self.config['storage'], self.address))
     69        return Storage(os.path.join(self.storage_config['path'], self.address))
    7470
    7571    @property
    7672    def archive(self):
    77         return Storage(os.path.join(self.config['archive'], self.address))
     73        return Storage(os.path.join(self.archive_config['path'], self.address))
    7874   
    7975    def members_addresses(self):
     
    109105        # automagically generates the dict from the instance attributes
    110106        return {'name': self.name, 'address': self.address,
    111                 'members': self.members, 'config': self.config}
     107                'members': self.members, 'configfile': self.configfile}
    112108
    113109    def load(self):
  • postman/mta.py

    r2 r10  
    88class Sendmail():
    99   
    10     def __init__(self, mailing_list=None):
     10    def __init__(self, mailing_list=None, configfile=None):
    1111        if not isinstance(mailing_list, MailingList):
    1212            raise ValueError(mailing_list, ' is not a valid mailing list')
     
    1616        self.raw_email = None
    1717        self.queue = []
    18         self.archive = self.mailing_list.config.get('archive',
    19                                                     config.archive_path)
     18        self.archive_config = config.get_config_parameters('archive',
     19                                                           configfile)
    2020       
    2121    def get_raw_email(self):
     
    3131            # check, would be nice here
    3232            self.get_raw_email
    33         filename = os.path.join(self.archive,
     33        filename = os.path.join(self.archive_config['path'],
    3434                                datetime.today().strftime('%Y%d%m%H%M%S%f'))
    3535        tmpfile = file(filename, 'w')
  • postman/tests/daemon.py

    r9 r10  
    11# -*- coding: utf-8 -*-
    22
    3 import os, multiprocessing, xmlrpclib
     3import os, multiprocessing, xmlrpclib, time
    44from SimpleXMLRPCServer import SimpleXMLRPCServer
    55from unittest import TestCase
     
    1818    """
    1919    def setUp(self):
    20         config = {'private': False, 'archive': '/tmp/postman-tests/archive',
    21                   'storage': '/tmp/postman-tests/storage'}
     20        self.configfile = os.path.join(os.path.dirname(__file__), 'postman.conf')
    2221        self.mailing_list = MailingList('test_list', 'test_list@example.com',
    23                                         members={}, config=config)
     22                                        members={}, configfile=self.configfile)
    2423        self.member =  Member('test@example.com')
    2524
    2625    def test___init__(self):
    27         postman = Postman()
     26        postman = Postman(configfile=self.configfile)
    2827        self.assertIsInstance(postman, Postman)
    2928        self.assertEqual(postman.mailings, {})
     
    3635
    3736    def test_save(self):
    38         postman = Postman()
     37        postman = Postman(configfile=self.configfile)
    3938        self.assertFalse(postman.save())
    4039        postman.add_mailing_list(self.mailing_list.info())
     
    4746
    4847    def test_load(self):
    49         postman = Postman()
     48        postman = Postman(configfile=self.configfile)
    5049        self.assertFalse(postman.mailings)
    5150        self.assertFalse(postman.mailings_addresses)
     
    5554
    5655        # Check that another postman instance is able to load the saved data
    57         postman_load = Postman()
     56        postman_load = Postman(configfile=self.configfile)
    5857        self.assertFalse(postman_load.mailings)
    5958        self.assertFalse(postman_load.mailings_addresses)
     
    6867
    6968    def test_clear(self):
    70         postman = Postman()
     69        postman = Postman(configfile=self.configfile)
    7170        self.assertFalse(postman.clear())
    7271        postman.add_mailing_list(self.mailing_list.info())
     
    7473
    7574    def test_add_mailing_list(self):
    76         postman = Postman()
     75        postman = Postman(configfile=self.configfile)
    7776        with self.assertRaises(TypeError):
    7877            # test improper info values
     
    10099
    101100    def test_add_mailing_member(self):
    102         postman = Postman()
     101        postman = Postman(configfile=self.configfile)
    103102        postman.add_mailing_list(self.mailing_list.info())
    104103        with self.assertRaises(ValueError):
     
    133132    """
    134133    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)
     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)
    139139        self.member =  Member('test@example.com')
    140140
    141141    def test___init__(self):
    142         daemon = PostmanDaemon()
     142        daemon = PostmanDaemon(self.configfile)
    143143        self.assertIsInstance(daemon, PostmanDaemon)
    144144        self.assertFalse(daemon.ready_to_serve)
     
    147147
    148148    def test_create_server(self):
    149         daemon = PostmanDaemon()
     149        daemon = PostmanDaemon(self.configfile)
    150150        daemon.port = 9001
    151151        self.assertTrue(daemon.create_server())
     
    154154       
    155155    def test_add_methods(self):
    156         daemon = PostmanDaemon()
     156        daemon = PostmanDaemon(self.configfile)
    157157        daemon.port = 9002
    158158        self.assertTrue(daemon.add_methods())
    159159        self.assertTrue(daemon.ready_to_serve)
    160160
    161         daemon = PostmanDaemon()
     161        daemon = PostmanDaemon(self.configfile)
    162162        daemon.port = 9003       
    163163        daemon.create_server()
     
    166166       
    167167    def test_run(self):
    168         daemon = PostmanDaemon()
     168        daemon = PostmanDaemon(self.configfile)
    169169        daemon.port = 9004
    170170        # start the daemon in another process, so we can start communicating
     
    172172        p = multiprocessing.Process(target=daemon.run)
    173173        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)
    174178       
    175179        # FIXME: Hardcoded url here, should be picked from a config file       
  • postman/tests/models.py

    r1 r10  
    11# -*- coding: utf-8 -*-
    22
     3import os
    34from unittest import TestCase
    45from postman.models import Member, MailingList
     
    1819class TestMailingList(TestCase):
    1920    def setUp(self):
    20         config = {'private': False, 'archive': '/tmp/postman-tests/archive',
    21                   'storage': '/tmp/postman-tests/storage'}
     21        configfile = os.path.join(os.path.dirname(__file__), 'postman.conf')
    2222        self.mailing_list = MailingList('test_list', 'test_list@example.com',
    23                                         members={}, config=config)
     23                                        members={}, configfile=configfile)
    2424        self.member =  Member('test@example.com')
    2525
  • postman/tests/mta.py

    r5 r10  
    66from postman.models import Member, MailingList
    77
    8 
    98class TestSendmail(TestCase):
    109    def setUp(self):
    11         config = {'private': False, 'archive': '/tmp/postman-tests/archive',
    12                   'storage': '/tmp/postman-tests/storage'}
     10        self.configfile = os.path.join(os.path.dirname(__file__),
     11                                       'postman.conf')
    1312        self.mailing_list = MailingList('test_list', 'test_list@example.com',
    14                                         members={}, config=config)
     13                                        members={}, configfile=self.configfile)
    1514        self.member =  Member('test@example.com')
    1615        self.mta = Sendmail(self.mailing_list)
Note: See TracChangeset for help on using the changeset viewer.