Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

76 rader
2.7 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import os, sys
  5. import unittest, webnotes
  6. from webnotes.test_runner import make_test_records
  7. make_test_records("Profile")
  8. class TestEmail(unittest.TestCase):
  9. def setUp(self):
  10. webnotes.conn.sql("""update tabProfile set unsubscribed=0""")
  11. webnotes.conn.sql("""delete from `tabBulk Email`""")
  12. def test_send(self):
  13. from webnotes.utils.email_lib import sendmail
  14. #sendmail('test@example.com', subject='Test Mail', msg="Test Content")
  15. def test_bulk(self):
  16. from webnotes.utils.email_lib.bulk import send
  17. send(recipients = ['test@example.com', 'test1@example.com'],
  18. sender="admin@example.com",
  19. doctype='Profile', email_field='email',
  20. subject='Testing Bulk', message='This is a bulk mail!')
  21. bulk = webnotes.conn.sql("""select * from `tabBulk Email` where status='Not Sent'""", as_dict=1)
  22. self.assertEquals(len(bulk), 2)
  23. self.assertTrue('test@example.com' in [d['recipient'] for d in bulk])
  24. self.assertTrue('test1@example.com' in [d['recipient'] for d in bulk])
  25. self.assertTrue('Unsubscribe' in bulk[0]['message'])
  26. def test_flush(self):
  27. self.test_bulk()
  28. from webnotes.utils.email_lib.bulk import flush
  29. flush(from_test=True)
  30. bulk = webnotes.conn.sql("""select * from `tabBulk Email` where status='Sent'""", as_dict=1)
  31. self.assertEquals(len(bulk), 2)
  32. self.assertTrue('test@example.com' in [d['recipient'] for d in bulk])
  33. self.assertTrue('test1@example.com' in [d['recipient'] for d in bulk])
  34. def test_unsubscribe(self):
  35. from webnotes.utils.email_lib.bulk import unsubscribe, send
  36. webnotes.local.form_dict = {
  37. 'email':'test@example.com',
  38. 'type':'Profile',
  39. 'email_field':'email',
  40. "from_test": True
  41. }
  42. unsubscribe()
  43. send(recipients = ['test@example.com', 'test1@example.com'],
  44. sender="admin@example.com",
  45. doctype='Profile', email_field='email',
  46. subject='Testing Bulk', message='This is a bulk mail!')
  47. bulk = webnotes.conn.sql("""select * from `tabBulk Email` where status='Not Sent'""",
  48. as_dict=1)
  49. self.assertEquals(len(bulk), 1)
  50. self.assertFalse('test@example.com' in [d['recipient'] for d in bulk])
  51. self.assertTrue('test1@example.com' in [d['recipient'] for d in bulk])
  52. self.assertTrue('Unsubscribe' in bulk[0]['message'])
  53. def test_bulk_limit(self):
  54. from webnotes.utils.email_lib.bulk import unsubscribe, send, BulkLimitCrossedError
  55. self.assertRaises(BulkLimitCrossedError, send,
  56. recipients=['test@example.com']*1000,
  57. sender="admin@example.com",
  58. doctype='Profile', email_field='email',
  59. subject='Testing Bulk', message='This is a bulk mail!')
  60. if __name__=='__main__':
  61. webnotes.connect()
  62. unittest.main()