# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import os, sys

import unittest, frappe

from frappe.test_runner import make_test_records
make_test_records("User")

class TestEmail(unittest.TestCase):
	def setUp(self):
		frappe.db.sql("""update tabUser set unsubscribed=0""")
		frappe.db.sql("""delete from `tabBulk Email`""")
		
	def test_send(self):
		from frappe.utils.email_lib import sendmail
		#sendmail('test@example.com', subject='Test Mail', msg="Test Content")

	def test_bulk(self):
		from frappe.utils.email_lib.bulk import send
		send(recipients = ['test@example.com', 'test1@example.com'], 
			sender="admin@example.com",
			doctype='User', email_field='email',
			subject='Testing Bulk', message='This is a bulk mail!')
		
		bulk = frappe.db.sql("""select * from `tabBulk Email` where status='Not Sent'""", as_dict=1)
		self.assertEquals(len(bulk), 2)
		self.assertTrue('test@example.com' in [d['recipient'] for d in bulk])
		self.assertTrue('test1@example.com' in [d['recipient'] for d in bulk])
		self.assertTrue('Unsubscribe' in bulk[0]['message'])

	def test_flush(self):
		self.test_bulk()
		from frappe.utils.email_lib.bulk import flush
		flush(from_test=True)
		bulk = frappe.db.sql("""select * from `tabBulk Email` where status='Sent'""", as_dict=1)
		self.assertEquals(len(bulk), 2)
		self.assertTrue('test@example.com' in [d['recipient'] for d in bulk])
		self.assertTrue('test1@example.com' in [d['recipient'] for d in bulk])
		
	def test_unsubscribe(self):
		from frappe.utils.email_lib.bulk import unsubscribe, send
		frappe.local.form_dict = {
			'email':'test@example.com',
			'type':'User',
			'email_field':'email',
			"from_test": True
		}
		unsubscribe()

		send(recipients = ['test@example.com', 'test1@example.com'], 
			sender="admin@example.com",
			doctype='User', email_field='email', 
			subject='Testing Bulk', message='This is a bulk mail!')
		
		bulk = frappe.db.sql("""select * from `tabBulk Email` where status='Not Sent'""", 
			as_dict=1)
		self.assertEquals(len(bulk), 1)
		self.assertFalse('test@example.com' in [d['recipient'] for d in bulk])
		self.assertTrue('test1@example.com' in [d['recipient'] for d in bulk])
		self.assertTrue('Unsubscribe' in bulk[0]['message'])
	
	def test_bulk_limit(self):
		from frappe.utils.email_lib.bulk import unsubscribe, send, BulkLimitCrossedError
		self.assertRaises(BulkLimitCrossedError, send,
			recipients=['test@example.com']*1000,
			sender="admin@example.com",
			doctype='User', email_field='email',
			subject='Testing Bulk', message='This is a bulk mail!')
		
		
if __name__=='__main__':
	frappe.connect()
	unittest.main()
@@ -416,18 +416,16 @@
 il%60%22%22%22)%0A
-%09%09
 %0A%09def te
@@ -682,33 +682,32 @@
 1@example.com'%5D,
- 
 %0A%09%09%09sender=%22admi
@@ -813,34 +813,32 @@
  a bulk mail!')%0A
-%09%09
 %0A%09%09bulk = frappe
@@ -1536,18 +1536,16 @@
  bulk%5D)%0A
-%09%09
 %0A%09def te
@@ -1653,16 +1653,29 @@
 _dict = 
+frappe._dict(
 %7B%0A%09%09%09'em
@@ -1767,16 +1767,17 @@
 True%0A%09%09%7D
+)
 %0A%09%09unsub
@@ -1848,17 +1848,16 @@
 e.com'%5D,
- 
 %0A%09%09%09send
@@ -1919,17 +1919,16 @@
 'email',
- 
 %0A%09%09%09subj
@@ -1979,18 +1979,16 @@
 mail!')%0A
-%09%09
 %0A%09%09bulk 
@@ -2063,17 +2063,16 @@
 ent'%22%22%22,
- 
 %0A%09%09%09as_d
@@ -2313,17 +2313,16 @@
 sage'%5D)%0A
-%09
 %0A%09def te
@@ -2649,13 +2649,9 @@
 !')%0A
-%09%09%0A%09%09
+%0A
 %0Aif 
@@ -2706,8 +2706,9 @@
 t.main()
+%0A

test case fix