[5] | 1 | # -*- coding: utf-8 -*-
|
---|
| 2 |
|
---|
| 3 | import os
|
---|
| 4 | from unittest import TestCase
|
---|
| 5 | from postman.daemon import Postman
|
---|
| 6 | from postman.models import Member, MailingList
|
---|
| 7 | from postman.storage import JsonStorage as Storage
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | class TestPostman(TestCase):
|
---|
| 11 | """
|
---|
| 12 | postman.daemon.Postman tests.
|
---|
| 13 |
|
---|
| 14 | Remember to call the .clear() method of postman after each test, so
|
---|
| 15 | the temp storage files are deleted
|
---|
| 16 | """
|
---|
| 17 | def setUp(self):
|
---|
| 18 | config = {'private': False, 'archive': '/tmp/postman-tests/archive',
|
---|
| 19 | 'storage': '/tmp/postman-tests/storage'}
|
---|
| 20 | self.mailing_list = MailingList('test_list', 'test_list@example.com',
|
---|
| 21 | members={}, config=config)
|
---|
| 22 | self.member = Member('test@example.com')
|
---|
| 23 |
|
---|
| 24 | def test___init__(self):
|
---|
| 25 | postman = Postman()
|
---|
| 26 | self.assertIsInstance(postman, Postman)
|
---|
| 27 | self.assertEqual(postman.mailings, {})
|
---|
| 28 | self.assertEqual(postman.mailings_addresses, [])
|
---|
| 29 | self.assertIsInstance(postman.dbs, dict)
|
---|
| 30 | self.assertTrue('mailings' in postman.dbs.keys())
|
---|
| 31 | self.assertTrue('members' in postman.dbs.keys())
|
---|
| 32 | self.assertIsInstance(postman.dbs['mailings'], Storage)
|
---|
| 33 | self.assertIsInstance(postman.dbs['members'], Storage)
|
---|
| 34 |
|
---|
| 35 | def test_save(self):
|
---|
| 36 | postman = Postman()
|
---|
| 37 | self.assertFalse(postman.save())
|
---|
| 38 | postman.add_mailing_list(self.mailing_list.info())
|
---|
| 39 | self.assertTrue(postman.save())
|
---|
| 40 | # FIXME: We have to test here that the generated json file
|
---|
| 41 | # contains the data it should contain
|
---|
| 42 |
|
---|
| 43 | # Clear the files created by the tests
|
---|
| 44 | postman.clear()
|
---|
| 45 |
|
---|
| 46 | def test_load(self):
|
---|
| 47 | postman = Postman()
|
---|
| 48 | self.assertFalse(postman.mailings)
|
---|
| 49 | self.assertFalse(postman.mailings_addresses)
|
---|
| 50 | self.assertFalse(postman.load())
|
---|
| 51 | postman.add_mailing_list(self.mailing_list.info())
|
---|
| 52 | self.assertTrue(postman.load())
|
---|
| 53 |
|
---|
| 54 | # Check that another postman instance is able to load the saved data
|
---|
| 55 | postman_load = Postman()
|
---|
| 56 | self.assertFalse(postman_load.mailings)
|
---|
| 57 | self.assertFalse(postman_load.mailings_addresses)
|
---|
| 58 | postman_load.load()
|
---|
| 59 | self.assertTrue(postman_load.mailings)
|
---|
| 60 | self.assertIsInstance(postman_load.mailings, dict)
|
---|
| 61 | self.assertTrue(postman_load.mailings_addresses)
|
---|
| 62 | self.assertIsInstance(postman_load.mailings_addresses, list)
|
---|
| 63 |
|
---|
| 64 | # Clear the files created by the tests
|
---|
| 65 | postman.clear()
|
---|
| 66 |
|
---|
| 67 | def test_clear(self):
|
---|
| 68 | postman = Postman()
|
---|
| 69 | self.assertFalse(postman.clear())
|
---|
| 70 | postman.add_mailing_list(self.mailing_list.info())
|
---|
| 71 | self.assertTrue(postman.clear())
|
---|
| 72 |
|
---|
| 73 | def test_add_mailing_list(self):
|
---|
| 74 | postman = Postman()
|
---|
| 75 | with self.assertRaises(TypeError):
|
---|
| 76 | # test improper info values
|
---|
| 77 | postman.add_mailing_list(['a list', 'is an', 'invalid parameter'])
|
---|
| 78 | postman.add_mailing_list(self.mailing_list)
|
---|
| 79 | with self.assertRaises(ValueError):
|
---|
| 80 | #test incomplete/missing info values
|
---|
| 81 | postman.add_mailing_list()
|
---|
| 82 | postman.add_mailing_list({'name': 'missing info'})
|
---|
| 83 | postman.add_mailing_list({'address': 'missing info'})
|
---|
| 84 | postman.add_mailing_list({'name': 'missing info',
|
---|
| 85 | 'address': 'missing info'})
|
---|
| 86 | # test mailing lists can be added
|
---|
| 87 | self.assertTrue(postman.add_mailing_list(self.mailing_list.info()))
|
---|
| 88 | self.assertTrue(postman.mailings)
|
---|
| 89 | self.assertIsInstance(postman.mailings, dict)
|
---|
| 90 | self.assertTrue(postman.mailings_addresses)
|
---|
| 91 | self.assertIsInstance(postman.mailings_addresses, list)
|
---|
| 92 | with self.assertRaises(IndexError):
|
---|
| 93 | # test what happens when the mailing has been already added
|
---|
| 94 | postman.add_mailing_list(self.mailing_list.info())
|
---|
| 95 |
|
---|
| 96 | # Clear the files created by the tests
|
---|
| 97 | postman.clear()
|
---|
| 98 |
|
---|
| 99 | def test_add_mailing_member(self):
|
---|
| 100 | postman = Postman()
|
---|
| 101 | postman.add_mailing_list(self.mailing_list.info())
|
---|
| 102 | with self.assertRaises(ValueError):
|
---|
| 103 | # test what happens if we call the method without proper
|
---|
| 104 | # parameters
|
---|
| 105 | postman.add_mailing_member()
|
---|
| 106 | postman.add_mailing_member(None, None)
|
---|
| 107 | postman.add_mailing_member(None, 'test_list@example.net')
|
---|
| 108 | postman.add_mailing_member('test@example.net', None)
|
---|
| 109 | postman.add_mailing_member('test@example', 'test_list@example.net')
|
---|
| 110 | with self.assertRaises(IndexError):
|
---|
| 111 | # test if we try to add a member to a non-existing mailing list
|
---|
| 112 | postman.add_mailing_member('test@example.net',
|
---|
| 113 | 'test_list_b@example.net')
|
---|
| 114 | # Test adding a member
|
---|
| 115 | self.assertTrue(postman.add_mailing_member('test@example.net',
|
---|
| 116 | self.mailing_list.address))
|
---|
| 117 | # Test trying to re-add that user
|
---|
| 118 | self.assertFalse(postman.add_mailing_member('test@example.net',
|
---|
| 119 | self.mailing_list.address))
|
---|
| 120 |
|
---|
| 121 | # Clear the files created by the tests
|
---|
| 122 | postman.clear()
|
---|