tests.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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
  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. self.ml.subscribers.add(self.member)
  37. self.assertTrue(exists(self.ml_file))
  38. self.assertEqual(open(self.ml_file).read(), 'toto@example.com')
  39. self.ml.subscribers.add(self.member2)
  40. self.assertEqual(
  41. open(self.ml_file).read(),
  42. 'toto@example.com\nlolo@example.com',
  43. )
  44. self.ml.subscribers.remove(self.member)
  45. self.assertEqual(
  46. open(self.ml_file).read(),
  47. 'lolo@example.com',
  48. )
  49. def test_email_change_update_subscriptions(self):
  50. self.ml.subscribers.add(self.member)
  51. # then, change member email
  52. self.member.email = 'tata@example.com'
  53. self.member.save()
  54. self.assertEqual(open(self.ml_file).read(), 'tata@example.com')