blob: 202467b545060caa120f05dedcc915b5f1748782 [file] [log] [blame]
Shariq Ansari9aa6f522021-09-14 12:49:08 +05301import unittest
2
3import frappe
4
5from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order
6
7
8class TestWebsite(unittest.TestCase):
9 def test_permission_for_custom_doctype(self):
Ankush Menat494bd9e2022-03-28 18:52:46 +053010 create_user("Supplier 1", "supplier1@gmail.com")
11 create_user("Supplier 2", "supplier2@gmail.com")
12 create_supplier_with_contact(
13 "Supplier1", "All Supplier Groups", "Supplier 1", "supplier1@gmail.com"
14 )
15 create_supplier_with_contact(
16 "Supplier2", "All Supplier Groups", "Supplier 2", "supplier2@gmail.com"
17 )
18 po1 = create_purchase_order(supplier="Supplier1")
19 po2 = create_purchase_order(supplier="Supplier2")
Shariq Ansari9aa6f522021-09-14 12:49:08 +053020
21 create_custom_doctype()
22 create_webform()
Ankush Menat494bd9e2022-03-28 18:52:46 +053023 create_order_assignment(supplier="Supplier1", po=po1.name)
24 create_order_assignment(supplier="Supplier2", po=po2.name)
Shariq Ansari9aa6f522021-09-14 12:49:08 +053025
26 frappe.set_user("Administrator")
27 # checking if data consist of all order assignment of Supplier1 and Supplier2
Ankush Menat494bd9e2022-03-28 18:52:46 +053028 self.assertTrue("Supplier1" and "Supplier2" in [data.supplier for data in get_data()])
Shariq Ansari9aa6f522021-09-14 12:49:08 +053029
30 frappe.set_user("supplier1@gmail.com")
31 # checking if data only consist of order assignment of Supplier1
Ankush Menat494bd9e2022-03-28 18:52:46 +053032 self.assertTrue("Supplier1" in [data.supplier for data in get_data()])
33 self.assertFalse([data.supplier for data in get_data() if data.supplier != "Supplier1"])
Shariq Ansari9aa6f522021-09-14 12:49:08 +053034
35 frappe.set_user("supplier2@gmail.com")
36 # checking if data only consist of order assignment of Supplier2
Ankush Menat494bd9e2022-03-28 18:52:46 +053037 self.assertTrue("Supplier2" in [data.supplier for data in get_data()])
38 self.assertFalse([data.supplier for data in get_data() if data.supplier != "Supplier2"])
Shariq Ansari9aa6f522021-09-14 12:49:08 +053039
40 frappe.set_user("Administrator")
41
Ankush Menat494bd9e2022-03-28 18:52:46 +053042
Shariq Ansari9aa6f522021-09-14 12:49:08 +053043def get_data():
Ankush Menat494bd9e2022-03-28 18:52:46 +053044 webform_list_contexts = frappe.get_hooks("webform_list_context")
Shariq Ansari9aa6f522021-09-14 12:49:08 +053045 if webform_list_contexts:
Ankush Menat494bd9e2022-03-28 18:52:46 +053046 context = frappe._dict(frappe.get_attr(webform_list_contexts[0])("Buying") or {})
47 kwargs = dict(doctype="Order Assignment", order_by="modified desc")
Shariq Ansari9aa6f522021-09-14 12:49:08 +053048 return context.get_list(**kwargs)
49
Ankush Menat494bd9e2022-03-28 18:52:46 +053050
Shariq Ansari9aa6f522021-09-14 12:49:08 +053051def create_user(name, email):
Ankush Menat494bd9e2022-03-28 18:52:46 +053052 frappe.get_doc(
53 {
54 "doctype": "User",
55 "send_welcome_email": 0,
56 "user_type": "Website User",
57 "first_name": name,
58 "email": email,
59 "roles": [{"doctype": "Has Role", "role": "Supplier"}],
60 }
61 ).insert(ignore_if_duplicate=True)
62
Shariq Ansari9aa6f522021-09-14 12:49:08 +053063
64def create_supplier_with_contact(name, group, contact_name, contact_email):
Ankush Menat494bd9e2022-03-28 18:52:46 +053065 supplier = frappe.get_doc(
66 {"doctype": "Supplier", "supplier_name": name, "supplier_group": group}
67 ).insert(ignore_if_duplicate=True)
Shariq Ansari9aa6f522021-09-14 12:49:08 +053068
Ankush Menat494bd9e2022-03-28 18:52:46 +053069 if not frappe.db.exists("Contact", contact_name + "-1-" + name):
Shariq Ansari9aa6f522021-09-14 12:49:08 +053070 new_contact = frappe.new_doc("Contact")
71 new_contact.first_name = contact_name
Ankush Menat494bd9e2022-03-28 18:52:46 +053072 new_contact.is_primary_contact = (True,)
73 new_contact.append("links", {"link_doctype": "Supplier", "link_name": supplier.name})
74 new_contact.append("email_ids", {"email_id": contact_email, "is_primary": 1})
Shariq Ansari9aa6f522021-09-14 12:49:08 +053075
76 new_contact.insert(ignore_mandatory=True)
77
Ankush Menat494bd9e2022-03-28 18:52:46 +053078
Shariq Ansari9aa6f522021-09-14 12:49:08 +053079def create_custom_doctype():
Ankush Menat494bd9e2022-03-28 18:52:46 +053080 frappe.get_doc(
81 {
82 "doctype": "DocType",
83 "name": "Order Assignment",
84 "module": "Buying",
85 "custom": 1,
86 "autoname": "field:po",
87 "fields": [
88 {"label": "PO", "fieldname": "po", "fieldtype": "Link", "options": "Purchase Order"},
89 {
90 "label": "Supplier",
91 "fieldname": "supplier",
92 "fieldtype": "Data",
93 "fetch_from": "po.supplier",
94 },
95 ],
96 "permissions": [
97 {
98 "create": 1,
99 "delete": 1,
100 "email": 1,
101 "export": 1,
102 "print": 1,
103 "read": 1,
104 "report": 1,
105 "role": "System Manager",
106 "share": 1,
107 "write": 1,
108 },
109 {"read": 1, "role": "Supplier"},
110 ],
111 }
112 ).insert(ignore_if_duplicate=True)
113
Shariq Ansari9aa6f522021-09-14 12:49:08 +0530114
115def create_webform():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530116 frappe.get_doc(
117 {
118 "doctype": "Web Form",
119 "module": "Buying",
120 "title": "SO Schedule",
121 "route": "so-schedule",
122 "doc_type": "Order Assignment",
123 "web_form_fields": [
124 {
125 "doctype": "Web Form Field",
126 "fieldname": "po",
127 "fieldtype": "Link",
128 "options": "Purchase Order",
129 "label": "PO",
130 },
131 {
132 "doctype": "Web Form Field",
133 "fieldname": "supplier",
134 "fieldtype": "Data",
135 "label": "Supplier",
136 },
137 ],
138 }
139 ).insert(ignore_if_duplicate=True)
Shariq Ansari9aa6f522021-09-14 12:49:08 +0530140
Shariq Ansari9aa6f522021-09-14 12:49:08 +0530141
142def create_order_assignment(supplier, po):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530143 frappe.get_doc(
144 {
145 "doctype": "Order Assignment",
146 "po": po,
147 "supplier": supplier,
148 }
149 ).insert(ignore_if_duplicate=True)