tests.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from os.path import join, exists
  2. import shutil
  3. import tempfile
  4. from django.conf import settings
  5. from django.test import TestCase, override_settings
  6. from coin.members.models import Member
  7. from .models import MaillingList, MaillingListSubscription
  8. @override_settings()
  9. class SubscriptionTestCase(TestCase):
  10. def setUp(self):
  11. self.member = Member.objects.create(
  12. first_name=u"Toto",
  13. last_name=u"L'artichaut",
  14. username='toto',
  15. email='toto@example.com',
  16. )
  17. self.member2 = Member.objects.create(
  18. first_name=u"Lolo",
  19. last_name=u"Le Bigorneau",
  20. username='lolo',
  21. email='lolo@example.com',
  22. )
  23. self.tmpdir = tempfile.mkdtemp()
  24. settings.MAILLIST_SYNC_COMMAND = 'tee {}'.format(
  25. join(self.tmpdir, 'testlist-{short_name}'))
  26. self.ml_file = join(self.tmpdir, 'testlist-blabla')
  27. self.ml = MaillingList.objects.create(
  28. short_name='blabla',
  29. email='blabla@example.com',
  30. verbose_name='Blablateries',
  31. description='',
  32. )
  33. def tearDown(self):
  34. shutil.rmtree(self.tmpdir)
  35. def test_subscription_sync(self):
  36. sub1 = MaillingListSubscription.objects.create(
  37. member=self.member,
  38. maillinglist=self.ml,
  39. )
  40. self.assertTrue(exists(self.ml_file))
  41. self.assertEqual(open(self.ml_file).read(), 'toto@example.com')
  42. MaillingListSubscription.objects.create(
  43. member=self.member2,
  44. maillinglist=self.ml,
  45. )
  46. self.assertEqual(
  47. open(self.ml_file).read(),
  48. 'toto@example.com\nlolo@example.com',
  49. )
  50. sub1.delete()
  51. self.assertEqual(
  52. open(self.ml_file).read(),
  53. 'lolo@example.com',
  54. )
  55. def test_email_change_update_subscriptions(self):
  56. MaillingListSubscription.objects.create(
  57. member=self.member,
  58. maillinglist=self.ml,
  59. )
  60. # then, change member email
  61. self.member.email = 'tata@example.com'
  62. self.member.save()
  63. self.assertEqual(open(self.ml_file).read(), 'tata@example.com')