<commit_before># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
# License: GNU General Public License v3. See license.txt

import webnotes
import unittest

from hr.doctype.leave_application.leave_application import LeaveDayBlockedError, OverlapError

class TestLeaveApplication(unittest.TestCase):
	def _clear_roles(self):
		webnotes.conn.sql("""delete from `tabUserRole` where parent in 
			("<EMAIL>", "<EMAIL>", "<EMAIL>")""")
			
	def _clear_applications(self):
		webnotes.conn.sql("""delete from `tabLeave Application`""")
		
	def _add_employee_leave_approver(self, employee, leave_approver):
		webnotes.session.user = "Administrator"
		employee = webnotes.bean("Employee", employee)
		employee.doclist.append({
			"doctype": "Employee Leave Approver",
			"parentfield": "employee_leave_approvers",
			"leave_approver": leave_approver
		})
		employee.save()
	
	def get_application(self, doclist):
		application = webnotes.bean(copy=doclist)
		application.doc.from_date = "2013-01-01"
		application.doc.to_date = "2013-01-05"
		return application

	def test_block_list(self):
		webnotes.session.user = "Administrator"
		self._clear_roles()
		
		from webnotes.profile import add_role
		add_role("<EMAIL>", "HR User")
			
		webnotes.conn.set_value("Department", "_Test Department", 
			"leave_block_list", "_Test Leave Block List")
		
		application = self.get_application(test_records[1])
		application.insert()
		application.doc.status = "Approved"
		self.assertRaises(LeaveDayBlockedError, application.submit)
		
		webnotes.session.user = "<EMAIL>"

		# clear other applications
		webnotes.conn.sql("delete from `tabLeave Application`")
		
		application = self.get_application(test_records[1])
		self.assertTrue(application.insert())
		
	def test_overlap(self):
		webnotes.session.user = "Administrator"
		self._clear_roles()
		self._clear_applications()
		
		from webnotes.profile import add_role
		add_role("<EMAIL>", "Employee")
		add_role("<EMAIL>", "Leave Approver")
		
		webnotes.session.user = "<EMAIL>"
		application = self.get_application(test_records[1])
		application.doc.leave_approver = "<EMAIL>"
		application.insert()
		
		application = self.get_application(test_records[1])
		application.doc.leave_approver = "<EMAIL>"
		self.assertRaises(OverlapError, application.insert)
		
	def test_global_block_list(self):
		webnotes.session.user = "Administrator"
		self._clear_roles()

		from webnotes.profile import add_role
		add_role("<EMAIL>", "Employee")
		add_role("<EMAIL>", "Leave Approver")
				
		application = self.get_application(test_records[3])
		application.doc.leave_approver = "<EMAIL>"
		
		webnotes.conn.set_value("Leave Block List", "_Test Leave Block List", 
			"applies_to_all_departments", 1)
		webnotes.conn.set_value("Employee", "_T-Employee-0002", "department", 
			"_Test Department")
		
		webnotes.session.user = "<EMAIL>"
		application.insert()
		
		webnotes.session.user = "<EMAIL>"
		application.doc.status = "Approved"
		self.assertRaises(LeaveDayBlockedError, application.submit)
		
		webnotes.conn.set_value("Leave Block List", "_Test Leave Block List", 
			"applies_to_all_departments", 0)
		
	def test_leave_approval(self):
		webnotes.session.user = "Administrator"
		self._clear_roles()
		
		from webnotes.profile import add_role
		add_role("<EMAIL>", "Employee")
		add_role("<EMAIL>", "Leave Approver")
		add_role("<EMAIL>", "Leave Approver")
		
		self._test_leave_approval_basic_case_1()
		self._test_leave_approval_basic_case_2()
		self._test_leave_approval_invalid_leave_approver_insert()
		self._test_leave_approval_invalid_leave_approver_submit()
		self._test_leave_approval_valid_leave_approver_insert()
		
	def _test_leave_approval_basic_case_1(self):
		self._clear_applications()
		
		# create leave application as Employee
		webnotes.session.user = "<EMAIL>"
		application = self.get_application(test_records[1])
		application.doc.leave_approver = "<EMAIL>"
		application.insert()
		
		# submit leave application by Leave Approver
		webnotes.session.user = "<EMAIL>"
		application.doc.status = "Approved"
		application.submit()
		self.assertEqual(webnotes.conn.get_value("Leave Application", application.doc.name,
			"docstatus"), 1)
		
	def _test_leave_approval_basic_case_2(self):
		self._clear_applications()
		
		# create leave application by any leave approver, 
		# when no leave approver specified in employee's leave approvers list
		application = self.get_application(test_records[1])
		application.doc.leave_approver = "<EMAIL>"
		application.insert()
		application.doc.status = "Approved"
		application.submit()
		self.assertEqual(webnotes.conn.get_value("Leave Application", application.doc.name,
			"docstatus"), 1)
		
	def _test_leave_approval_invalid_leave_approver_insert(self):
		from hr.doctype.leave_application.leave_application import InvalidLeaveApproverError
		
		self._clear_applications()
		
		# add a different leave approver in the employee's list
		# should raise exception if not a valid leave approver
		self._add_employee_leave_approver("_T-Employee-0001", "<EMAIL>")
		
		# TODO - add <EMAIL> leave approver in employee's leave approvers list
		application = self.get_application(test_records[1])
		webnotes.session.user = "<EMAIL>"
		
		application.doc.leave_approver = "<EMAIL>"
		self.assertRaises(InvalidLeaveApproverError, application.insert)
		
		webnotes.conn.sql("""delete from `tabEmployee Leave Approver` where parent=%s""",
			"_T-Employee-0001")
		
	def _test_leave_approval_invalid_leave_approver_submit(self):
		self._clear_applications()
		self._add_employee_leave_approver("_T-Employee-0001", "<EMAIL>")
		
		# create leave application as employee
		# but submit as invalid leave approver - should raise exception
		webnotes.session.user = "<EMAIL>"
		application = self.get_application(test_records[1])
		application.doc.leave_approver = "<EMAIL>"
		application.insert()
		webnotes.session.user = "<EMAIL>"
		application.doc.status = "Approved"
		
		from webnotes.model.bean import BeanPermissionError
		self.assertRaises(BeanPermissionError, application.submit)
		
		webnotes.conn.sql("""delete from `tabEmployee Leave Approver` where parent=%s""",
			"_T-Employee-0001")
		
	def _test_leave_approval_valid_leave_approver_insert(self):
		self._clear_applications()
		self._add_employee_leave_approver("_T-Employee-0001", "<EMAIL>")
		
		original_department = webnotes.conn.get_value("Employee", "_T-Employee-0001", "department")
		webnotes.conn.set_value("Employee", "_T-Employee-0001", "department", None)
		
		# change to valid leave approver and try to create and submit leave application
		webnotes.session.user = "<EMAIL>"
		application = self.get_application(test_records[1])
		application.doc.leave_approver = "<EMAIL>"
		application.insert()
		application.doc.status = "Approved"
		application.submit()
		self.assertEqual(webnotes.conn.get_value("Leave Application", application.doc.name,
			"docstatus"), 1)
			
		webnotes.conn.sql("""delete from `tabEmployee Leave Approver` where parent=%s""",<commit_msg>[fix] [minor] get_url and fixes due to addition of create permission check<commit_after># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
# License: GNU General Public License v3. See license.txt

import webnotes
import unittest

from hr.doctype.leave_application.leave_application import LeaveDayBlockedError, OverlapError

class TestLeaveApplication(unittest.TestCase):
	def _clear_roles(self):
		webnotes.conn.sql("""delete from `tabUserRole` where parent in 
			("<EMAIL>", "<EMAIL>", "<EMAIL>")""")
			
	def _clear_applications(self):
		webnotes.conn.sql("""delete from `tabLeave Application`""")
		
	def _add_employee_leave_approver(self, employee, leave_approver):
		webnotes.session.user = "Administrator"
		employee = webnotes.bean("Employee", employee)
		employee.doclist.append({
			"doctype": "Employee Leave Approver",
			"parentfield": "employee_leave_approvers",
			"leave_approver": leave_approver
		})
		employee.save()
	
	def get_application(self, doclist):
		application = webnotes.bean(copy=doclist)
		application.doc.from_date = "2013-01-01"
		application.doc.to_date = "2013-01-05"
		return application

	def test_block_list(self):
		webnotes.session.user = "Administrator"
		self._clear_roles()
		
		from webnotes.profile import add_role
		add_role("<EMAIL>", "HR User")
			
		webnotes.conn.set_value("Department", "_Test Department", 
			"leave_block_list", "_Test Leave Block List")
		
		application = self.get_application(test_records[1])
		application.insert()
		application.doc.status = "Approved"
		self.assertRaises(LeaveDayBlockedError, application.submit)
		
		webnotes.session.user = "<EMAIL>"

		# clear other applications
		webnotes.conn.sql("delete from `tabLeave Application`")
		
		application = self.get_application(test_records[1])
		self.assertTrue(application.insert())
		
	def test_overlap(self):
		webnotes.session.user = "Administrator"
		self._clear_roles()
		self._clear_applications()
		
		from webnotes.profile import add_role
		add_role("<EMAIL>", "Employee")
		add_role("<EMAIL>", "Leave Approver")
		
		webnotes.session.user = "<EMAIL>"
		application = self.get_application(test_records[1])
		application.doc.leave_approver = "<EMAIL>"
		application.insert()
		
		application = self.get_application(test_records[1])
		application.doc.leave_approver = "<EMAIL>"
		self.assertRaises(OverlapError, application.insert)
		
	def test_global_block_list(self):
		webnotes.session.user = "Administrator"
		self._clear_roles()

		from webnotes.profile import add_role
		add_role("<EMAIL>", "Employee")
		add_role("<EMAIL>", "Leave Approver")
				
		application = self.get_application(test_records[3])
		application.doc.leave_approver = "<EMAIL>"
		
		webnotes.conn.set_value("Leave Block List", "_Test Leave Block List", 
			"applies_to_all_departments", 1)
		webnotes.conn.set_value("Employee", "_T-Employee-0002", "department", 
			"_Test Department")
		
		webnotes.session.user = "<EMAIL>"
		application.insert()
		
		webnotes.session.user = "<EMAIL>"
		application.doc.status = "Approved"
		self.assertRaises(LeaveDayBlockedError, application.submit)
		
		webnotes.conn.set_value("Leave Block List", "_Test Leave Block List", 
			"applies_to_all_departments", 0)
		
	def test_leave_approval(self):
		webnotes.session.user = "Administrator"
		self._clear_roles()
		
		from webnotes.profile import add_role
		add_role("<EMAIL>", "Employee")
		add_role("<EMAIL>", "Leave Approver")
		add_role("<EMAIL>", "Leave Approver")
		
		self._test_leave_approval_basic_case()
		self._test_leave_approval_invalid_leave_approver_insert()
		self._test_leave_approval_invalid_leave_approver_submit()
		self._test_leave_approval_valid_leave_approver_insert()
		
	def _test_leave_approval_basic_case(self):
		self._clear_applications()
		
		# create leave application as Employee
		webnotes.session.user = "<EMAIL>"
		application = self.get_application(test_records[1])
		application.doc.leave_approver = "<EMAIL>"
		application.insert()
		
		# submit leave application by Leave Approver
		webnotes.session.user = "<EMAIL>"
		application.doc.status = "Approved"
		application.submit()
		self.assertEqual(webnotes.conn.get_value("Leave Application", application.doc.name,
			"docstatus"), 1)
		
	def _test_leave_approval_invalid_leave_approver_insert(self):
		from hr.doctype.leave_application.leave_application import InvalidLeaveApproverError
		
		self._clear_applications()
		
		# add a different leave approver in the employee's list
		# should raise exception if not a valid leave approver
		self._add_employee_leave_approver("_T-Employee-0001", "<EMAIL>")
		
		# TODO - add <EMAIL> leave approver in employee's leave approvers list
		application = self.get_application(test_records[1])
		webnotes.session.user = "<EMAIL>"
		
		application.doc.leave_approver = "<EMAIL>"
		self.assertRaises(InvalidLeaveApproverError, application.insert)
		
		webnotes.conn.sql("""delete from `tabEmployee Leave Approver` where parent=%s""",
			"_T-Employee-0001")
		
	def _test_leave_approval_invalid_leave_approver_submit(self):
		self._clear_applications()
		self._add_employee_leave_approver("_T-Employee-0001", "<EMAIL>")
		
		# create leave application as employee
		# but submit as invalid leave approver - should raise exception
		webnotes.session.user = "<EMAIL>"
		application = self.get_application(test_records[1])
		application.doc.leave_approver = "<EMAIL>"
		application.insert()
		webnotes.session.user = "<EMAIL>"
		application.doc.status = "Approved"
		
		from webnotes.model.bean import BeanPermissionError
		self.assertRaises(BeanPermissionError, application.submit)
		
		webnotes.conn.sql("""delete from `tabEmployee Leave Approver` where parent=%s""",
			"_T-Employee-0001")
		
	def _test_leave_approval_valid_leave_approver_insert(self):
		self._clear_applications()
		self._add_employee_leave_approver("_T-Employee-0001", "<EMAIL>")
		
		original_department = webnotes.conn.get_value("Employee", "_T-Employee-0001", "department")
		webnotes.conn.set_value("Employee", "_T-Employee-0001", "department", None)
		
		webnotes.session.user = "<EMAIL>"
		application = self.get_application(test_records[1])
		application.doc.leave_approver = "<EMAIL>"
		application.insert()

		# change to valid leave approver and try to submit leave application
		webnotes.session.user = "<EMAIL>"
		application.doc.status = "Approved"
		application.submit()
		self.assertEqual(webnotes.conn.get_value("Leave Application", application.doc.name,
			"docstatus"), 1)
			
		webnotes.conn.sql("""delete from `tabEmployee Leave Approver` where parent=%s""",
{'fraction_non_alphanumeric': 0.11923840329339117, 'fraction_numerical': 0.010144820995368668, 'mean_word_length': 5.339235787511649, 'pattern_counts': {'":': 6, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7245325', 'n_tokens_mistral': 4878, 'n_tokens_neox': 4190, 'n_words': 822}