test(membership): add test for invoicing and validation
diff --git a/erpnext/non_profit/doctype/membership/membership.py b/erpnext/non_profit/doctype/membership/membership.py
index ae4df4a..ac3b89a 100644
--- a/erpnext/non_profit/doctype/membership/membership.py
+++ b/erpnext/non_profit/doctype/membership/membership.py
@@ -86,6 +86,20 @@
 		invoice = make_invoice(self, member, plan, settings)
 		self.invoice = invoice.name
 
+		if with_payment_entry:
+			if not settings.payment_account:
+				frappe.throw(_("You need to set <b>Payment Account</b> in Membership Settings"))
+
+			from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
+			frappe.flags.ignore_account_permission=True
+			pe = get_payment_entry(dt='Sales Invoice', dn=invoice.name, bank_amount=invoice.grand_total)
+			frappe.flags.ignore_account_permission=False
+			pe.paid_to = settings.payment_account
+			pe.reference_no = self.name
+			pe.reference_date = getdate()
+			pe.save(ignore_permissions=True)
+			pe.submit()
+
 		if save:
 			self.save()
 
@@ -97,7 +111,7 @@
 			frappe.throw(_("You need to enable <b>Send Acknowledge Email</b> in Membership Settings"))
 
 		member = frappe.get_doc("Member", self.member)
-		
+
 		if not member.email_id:
 			frappe.throw(_("Email address of member {0} is missing").format(frappe.utils.get_link_to_form("Member", self.member)))
 
diff --git a/erpnext/non_profit/doctype/membership/test_membership.py b/erpnext/non_profit/doctype/membership/test_membership.py
index b62f19b..6e4885d 100644
--- a/erpnext/non_profit/doctype/membership/test_membership.py
+++ b/erpnext/non_profit/doctype/membership/test_membership.py
@@ -3,7 +3,10 @@
 # See license.txt
 from __future__ import unicode_literals
 import unittest
+import frappe
+import erpnext
 from erpnext.non_profit.doctype.member.member import create_member
+from frappe.utils import nowdate, getdate, add_months
 from erpnext.stock.doctype.item.test_item import create_item
 
 class TestMembership(unittest.TestCase):
@@ -21,15 +24,16 @@
 		settings.enable_invoicing = 1
 		settings.make_payment_entry = 1
 		settings.company = company.name
-		settings.payment_to = company.default_cash_account
+		settings.payment_account = company.default_cash_account
 		settings.debit_account = company.default_receivable_account
 		settings.save()
 
 		# make test plan
 		plan = frappe.new_doc("Membership Type")
+		plan.membership_type = "_rzpy_test_milythm"
 		plan.amount = 100
 		plan.razorpay_plan_id = "_rzpy_test_milythm"
-		plan.linked_item = create_item("_Test Item for Non Profit Membership")
+		plan.linked_item = create_item("_Test Item for Non Profit Membership").name
 		plan.insert()
 
 		# make test member
@@ -38,15 +42,71 @@
 				'email': "_test_member_erpnext@example.com",
 				'plan_id': plan.name
 		}))
+		self.member_doc.make_customer_and_link()
+		self.member = "self.member_doc.name"
 
 	def test_auto_generate_invoice_and_payment_entry(self):
-		pass
+		entry = make_membership(self.member)
 
-	def test_renew within_30_days(self):
-		pass
+		# Naive test to see if at all invoice was generated and attached to member
+		# In any case if details were missing, the invoicing would throw an error
+		invoice = entry.generate_invoice(save=True)
+		self.assertEqual(invoice.name, entry.invoice)
+		# entry.delete()
 
-	def test_from_to_dates(self):
-		pass
+		# # Remove customer
+		# old_customer = self.member_doc.customer
+		# self.member_doc.customer = None
+		# self.member_doc.save()
 
-	def test_razorpay_webook(self):
-		pass
+		# entry = make_membership(self.member)
+		# self.assertRaises(frappe.ValidationError, entry.generate_invoice)
+		
+		# # Add customer value back
+		# self.member_doc.customer = old_customer
+		# self.member_doc.save()
+
+		# # Remove company
+		# set_config(company, None)
+		# self.assertRaises(frappe.ValidationError, entry.generate_invoice)
+
+	def test_renew_within_30_days(self):
+		# create a membership for two months
+		# Should work fine
+		make_membership(self.member, { "from_date": nowdate() })
+		make_membership(self.member, { "from_date": add_months(nowdate(), 1) })
+		
+		from frappe.utils.user import add_role
+		add_role("test@example.com", "Non Profit Manager")
+		frappe.set_user("test@example.com")
+		
+		# create next membership with expiry not within 30 days
+		self.assertRaises(frappe.ValidationError, make_membership, self.member, {
+			"from_date": add_months(nowdate(), 2),
+		})
+
+		frappe.set_user("Administrator")
+		# create the same membership but as administrator
+		new_entry = make_membership(self.member, {
+			"from_date": add_months(nowdate(), 2),
+			"to_date": add_months(nowdate(), 3),
+		})
+
+def set_config(key, value):
+	frappe.db.set_value("Membership Settings", None, key, value)
+
+def make_membership(member, payload={}):
+	data = {
+		"doctype": "Membership",
+		"member": member,
+		"membership_status": "Current",
+		"membership_type": "_rzpy_test_milythm",
+		"currency": "INR",
+		"paid": 1,
+		"from_date": nowdate(),
+		"amount": 100
+	}
+	data.update(payload)
+	membership = frappe.get_doc(data)
+	membership.insert(ignore_permissions=True, ignore_if_duplicate=True)
+	return membership
\ No newline at end of file