[newsletter] fixed tests

This commit is contained in:
Rushabh Mehta 2015-03-19 17:15:45 +05:30
parent 14ae4c3603
commit 611b513a2d
6 changed files with 72 additions and 70 deletions

View File

@ -30,7 +30,7 @@
"label": "Sender",
"no_copy": 1,
"permlevel": 0,
"reqd": 1
"reqd": 0
},
{
"fieldname": "email_sent",
@ -78,7 +78,7 @@
],
"icon": "icon-envelope",
"idx": 1,
"modified": "2015-03-18 08:19:24.393173",
"modified": "2015-03-19 07:39:53.550198",
"modified_by": "Administrator",
"module": "CRM",
"name": "Newsletter",

View File

@ -7,13 +7,14 @@ import frappe
import frappe.utils
from frappe import throw, _
from frappe.model.document import Document
from frappe.utils.verified_command import verify_request
import erpnext.tasks
class Newsletter(Document):
def onload(self):
if self.email_sent:
self.get("__onload").status_count = dict(frappe.db.sql("""select status, count(name)
from `tabBulk Email` where ref_doctype=%s and ref_docname=%s
from `tabBulk Email` where reference_doctype=%s and reference_name=%s
group by status""", (self.doctype, self.name))) or None
def test_send(self, doctype="Lead"):
@ -41,10 +42,6 @@ class Newsletter(Document):
frappe.db.set(self, "email_sent", 1)
def get_recipients(self):
"""Get recipients from Newsletter List"""
return frappe.db.get_all("Newsletter List Subscriber", ["email"], {"unsubscribed": 0})
def send_bulk(self):
if not self.get("recipients"):
# in case it is called via worker
@ -61,12 +58,18 @@ class Newsletter(Document):
send(recipients = self.recipients, sender = sender,
subject = self.subject, message = self.message,
ref_doctype = self.doctype, ref_docname = self.name,
unsubscribe_url = "/api/method/erpnext.crm.doctype.newsletter.newsletter.unsubscribe?name=%s&email={email}" % self.name)
reference_doctype = self.doctype, reference_name = self.name,
unsubscribe_method = "/api/method/erpnext.crm.doctype.newsletter.newsletter.unsubscribe",
unsubscribe_params = {"name": self.name})
if not frappe.flags.in_test:
frappe.db.auto_commit_on_many_writes = False
def get_recipients(self):
"""Get recipients from Newsletter List"""
return [d.email for d in frappe.db.get_all("Newsletter List Subscriber", ["email"],
{"unsubscribed": 0, "newsletter_list": self.newsletter_list})]
def validate_send(self):
if self.get("__islocal"):
throw(_("Please save the Newsletter before sending"))
@ -85,10 +88,13 @@ def get_lead_options():
def unsubscribe(email, name):
from frappe.email.bulk import return_unsubscribed_page
name = frappe.db.get_value("Newsletter List Subscriber", {"email": email, "newsletter_list": name})
if not verify_request():
return
subs_id = frappe.db.get_value("Newsletter List Subscriber", {"email": email, "newsletter_list": name})
if name:
subscriber = frappe.get_doc("Newsletter List Subscriber", name)
subscriber.unsubscribe = 1
subscriber = frappe.get_doc("Newsletter List Subscriber", subs_id)
subscriber.unsubscribed = 1
subscriber.save(ignore_permissions=True)
return_unsubscribed_page(email)

View File

@ -4,40 +4,41 @@ from __future__ import unicode_literals
import frappe, unittest
from erpnext.crm.doctype.newsletter.newsletter import unsubscribe
from urllib import unquote
class TestNewsletter(unittest.TestCase):
def test_get_recipients_lead(self):
w = frappe.get_doc(test_records[0])
w.insert()
self.assertTrue("test_lead@example.com" in w.get_recipients())
frappe.db.sql("""delete from `tabBulk Email`""")
w.send_emails()
self.assertTrue(frappe.db.get_value("Bulk Email", {"recipient": "test_lead@example.com"}))
def setUp(self):
frappe.db.sql("update `tabNewsletter List Subscriber` set unsubscribed = 0")
def test_get_recipients_lead_by_status(self):
w = frappe.get_doc(test_records[0])
w.lead_status="Converted"
w.insert()
self.assertTrue("test_lead3@example.com" in w.get_recipients())
def test_send(self):
self.send_newsletter()
self.assertEquals(len(frappe.get_all("Bulk Email")), 3)
def test_get_recipients_contact_customer(self):
w = frappe.get_doc(test_records[1])
w.insert()
self.assertTrue("test_contact_customer@example.com" in w.get_recipients())
def test_unsubscribe(self):
# test unsubscribe
self.send_newsletter()
def test_get_recipients_contact_supplier(self):
w = frappe.get_doc(test_records[1])
w.contact_type="Supplier"
w.insert()
self.assertTrue("test_contact_supplier@example.com" in w.get_recipients())
email = unquote(frappe.local.flags.signed_query_string.split("email=")[1].split("&")[0])
def test_get_recipients_custom(self):
w = frappe.get_doc(test_records[2])
w.insert()
self.assertTrue("test_custom2@example.com" in w.get_recipients())
self.assertTrue(frappe.db.get("Lead",
{"email_id": "test_custom2@example.com"}))
unsubscribe(email, "_Test Newsletter List")
self.send_newsletter()
self.assertEquals(len(frappe.get_all("Bulk Email")), 2)
def send_newsletter(self):
frappe.db.sql("delete from `tabBulk Email`")
frappe.delete_doc("Newsletter", "_Test Newsletting")
newsletter = frappe.get_doc({
"doctype": "Newsletter",
"subject": "_Test Newsletting",
"newsletter_list": "_Test Newsletter List",
"send_from": "Test Sender <test_sender@example.com>",
"message": "Testing my news."
}).insert(ignore_permissions=True)
newsletter.send_emails()
test_dependencies = ["Lead", "Contact"]
test_records = frappe.get_test_records('Newsletter')
test_dependencies = ["Newsletter List"]

View File

@ -1,26 +0,0 @@
[
{
"doctype": "Newsletter",
"lead_source": "All",
"message": "This is a test newsletter",
"send_from": "admin@example.com",
"send_to_type": "Lead",
"subject": "_Test Newsletter to Lead"
},
{
"contact_type": "Customer",
"doctype": "Newsletter",
"message": "This is a test newsletter",
"send_from": "admin@example.com",
"send_to_type": "Contact",
"subject": "_Test Newsletter to Contact"
},
{
"doctype": "Newsletter",
"email_list": "test_custom@example.com, test_custom1@example.com, test_custom2@example.com",
"message": "This is a test newsletter",
"send_from": "admin@example.com",
"send_to_type": "Custom",
"subject": "_Test Newsletter to Custom"
}
]

View File

@ -0,0 +1,21 @@
[
{
"doctype": "Newsletter List",
"title": "_Test Newsletter List"
},
{
"doctype": "Newsletter List Subscriber",
"email": "test_subscriber1@example.com",
"newsletter_list": "_Test Newsletter List"
},
{
"doctype": "Newsletter List Subscriber",
"email": "test_subscriber2@example.com",
"newsletter_list": "_Test Newsletter List"
},
{
"doctype": "Newsletter List Subscriber",
"email": "test_subscriber3@example.com",
"newsletter_list": "_Test Newsletter List"
}
]

View File

@ -78,9 +78,9 @@ class TransactionBase(StatusUpdater):
self.compare_values(ref_doc, val["compare_fields"])
def compare_values(self, ref_doc, fields, doc=None):
for ref_doctype, ref_dn_list in ref_doc.items():
for ref_docname in ref_dn_list:
prevdoc_values = frappe.db.get_value(ref_doctype, ref_docname,
for reference_doctype, ref_dn_list in ref_doc.items():
for reference_name in ref_dn_list:
prevdoc_values = frappe.db.get_value(reference_doctype, reference_name,
[d[0] for d in fields], as_dict=1)
for field, condition in fields: