You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

80 lines
2.8 KiB

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