[1] | 1 | # -*- coding: utf-8 -*-
|
---|
| 2 |
|
---|
| 3 | from unittest import TestCase
|
---|
| 4 | from postman.models import Member, MailingList
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | class TestMember(TestCase):
|
---|
| 8 | def setUp(self):
|
---|
| 9 | self.member = Member('test@example.com')
|
---|
| 10 |
|
---|
| 11 | def test__validate_address(self):
|
---|
| 12 | self.assertEqual(self.member._validate_address(self.member.address),
|
---|
| 13 | self.member.address)
|
---|
| 14 | with self.assertRaises(ValueError):
|
---|
| 15 | self.member._validate_address('notavalidemail')
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | class TestMailingList(TestCase):
|
---|
| 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__validate_member_object(self):
|
---|
| 27 | self.assertEqual(self.mailing_list._validate_member_object(self.member),
|
---|
| 28 | self.member)
|
---|
| 29 | with self.assertRaises(TypeError):
|
---|
| 30 | self.mailing_list._validate_member_object(None)
|
---|
| 31 | self.mailing_list._validate_member_object('Not a member object')
|
---|
| 32 | self.mailing_list._validate_member_object(self.mailing_list)
|
---|
| 33 |
|
---|
| 34 | def test__validate_member(self):
|
---|
| 35 | # At first the member object is not a member of the list
|
---|
| 36 | self.assertFalse(self.mailing_list._validate_member(self.member))
|
---|
| 37 | self.mailing_list.add_member(self.member)
|
---|
| 38 | self.assertTrue(self.mailing_list._validate_member(self.member))
|
---|
| 39 | with self.assertRaises(TypeError):
|
---|
| 40 | self.mailing_list._validate_member(None)
|
---|
| 41 | self.mailing_list._validate_member('Not a member object')
|
---|
| 42 | self.mailing_list._validate_member(self.mailing_list)
|
---|
| 43 |
|
---|
| 44 | def test__validate_member_by_address(self):
|
---|
| 45 | self.assertFalse(self.mailing_list._validate_member_by_address(self.member.address))
|
---|
| 46 | self.mailing_list.add_member(self.member)
|
---|
| 47 | self.assertTrue(self.mailing_list._validate_member_by_address(self.member.address))
|
---|
| 48 | with self.assertRaises(ValueError):
|
---|
| 49 | self.mailing_list._validate_member_by_address(self.member)
|
---|
| 50 | self.mailing_list._validate_member_by_address(None)
|
---|
| 51 | self.mailing_list._validate_member_by_address('Not a member object')
|
---|
| 52 | self.mailing_list._validate_member_by_address(self.mailing_list)
|
---|
| 53 |
|
---|
| 54 | def test_members_addresses(self):
|
---|
| 55 | self.assertEqual(self.mailing_list.members_addresses(), [])
|
---|
| 56 | self.mailing_list.add_member(self.member)
|
---|
| 57 | self.assertTrue(self.member.address in \
|
---|
| 58 | self.mailing_list.members_addresses())
|
---|
| 59 |
|
---|
| 60 | def test_add_member(self):
|
---|
| 61 | self.assertTrue(self.mailing_list.add_member(self.member))
|
---|
| 62 | self.assertTrue(self.member.address in \
|
---|
| 63 | self.mailing_list.members_addresses())
|
---|
| 64 | # check what happens if the member is already there:
|
---|
| 65 | self.assertFalse(self.mailing_list.add_member(self.member))
|
---|
| 66 | # check what happens if we try to add something that is not
|
---|
| 67 | # a valid Member instance
|
---|
| 68 | with self.assertRaises(TypeError):
|
---|
| 69 | self.mailing_list.add_member(None)
|
---|
| 70 | self.mailing_list.add_member('Not a member object')
|
---|
| 71 | self.mailing_list.add_member(self.mailing_list)
|
---|
| 72 |
|
---|
| 73 | def test_add_member_by_address(self):
|
---|
| 74 | self.assertTrue(self.mailing_list.add_member_by_address(self.member.address))
|
---|
| 75 | self.assertTrue(self.member.address in \
|
---|
| 76 | self.mailing_list.members_addresses())
|
---|
| 77 | # check what happens if the member is already there:
|
---|
| 78 | self.assertFalse(self.mailing_list.add_member_by_address(self.member.address))
|
---|
| 79 | # check what happens if we try to add something that is not
|
---|
| 80 | # a valid Member instance
|
---|
| 81 | with self.assertRaises(ValueError):
|
---|
| 82 | self.mailing_list.add_member_by_address(self.member)
|
---|
| 83 | self.mailing_list.add_member_by_address(None)
|
---|
| 84 | self.mailing_list.add_member_by_address('Not a member object')
|
---|
| 85 | self.mailing_list.add_member_by_address(self.mailing_list)
|
---|
| 86 |
|
---|
| 87 | def test_delete_member(self):
|
---|
| 88 | self.assertFalse(self.mailing_list.delete_member(self.member))
|
---|
| 89 | self.mailing_list.add_member(self.member)
|
---|
| 90 | self.assertTrue(self.member.address in \
|
---|
| 91 | self.mailing_list.members_addresses())
|
---|
| 92 | self.assertTrue(self.mailing_list.delete_member(self.member))
|
---|
| 93 | self.assertFalse(self.member.address in \
|
---|
| 94 | self.mailing_list.members_addresses())
|
---|
| 95 | with self.assertRaises(TypeError):
|
---|
| 96 | self.mailing_list.delete_member(None)
|
---|
| 97 | self.mailing_list.delete_member('Not a member object')
|
---|
| 98 | self.mailing_list.delete_member(self.mailing_list)
|
---|