blob: 19255db33c524071b7b2ced07921f6be73462efc [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):
10 create_user('Supplier 1', 'supplier1@gmail.com')
11 create_user('Supplier 2', 'supplier2@gmail.com')
12 create_supplier_with_contact('Supplier1', 'All Supplier Groups', 'Supplier 1', 'supplier1@gmail.com')
13 create_supplier_with_contact('Supplier2', 'All Supplier Groups', 'Supplier 2', 'supplier2@gmail.com')
14 po1 = create_purchase_order(supplier='Supplier1')
15 po2 = create_purchase_order(supplier='Supplier2')
16
17 create_custom_doctype()
18 create_webform()
19 create_order_assignment(supplier='Supplier1', po = po1.name)
20 create_order_assignment(supplier='Supplier2', po = po2.name)
21
22 frappe.set_user("Administrator")
23 # checking if data consist of all order assignment of Supplier1 and Supplier2
24 self.assertTrue('Supplier1' and 'Supplier2' in [data.supplier for data in get_data()])
25
26 frappe.set_user("supplier1@gmail.com")
27 # checking if data only consist of order assignment of Supplier1
28 self.assertTrue('Supplier1' in [data.supplier for data in get_data()])
29 self.assertFalse([data.supplier for data in get_data() if data.supplier != 'Supplier1'])
30
31 frappe.set_user("supplier2@gmail.com")
32 # checking if data only consist of order assignment of Supplier2
33 self.assertTrue('Supplier2' in [data.supplier for data in get_data()])
34 self.assertFalse([data.supplier for data in get_data() if data.supplier != 'Supplier2'])
35
36 frappe.set_user("Administrator")
37
38def get_data():
39 webform_list_contexts = frappe.get_hooks('webform_list_context')
40 if webform_list_contexts:
41 context = frappe._dict(frappe.get_attr(webform_list_contexts[0])('Buying') or {})
42 kwargs = dict(doctype='Order Assignment', order_by = 'modified desc')
43 return context.get_list(**kwargs)
44
45def create_user(name, email):
46 frappe.get_doc({
47 'doctype': 'User',
48 'send_welcome_email': 0,
49 'user_type': 'Website User',
50 'first_name': name,
51 'email': email,
52 'roles': [{"doctype": "Has Role", "role": "Supplier"}]
53 }).insert(ignore_if_duplicate = True)
54
55def create_supplier_with_contact(name, group, contact_name, contact_email):
56 supplier = frappe.get_doc({
57 'doctype': 'Supplier',
58 'supplier_name': name,
59 'supplier_group': group
60 }).insert(ignore_if_duplicate = True)
61
62 if not frappe.db.exists('Contact', contact_name+'-1-'+name):
63 new_contact = frappe.new_doc("Contact")
64 new_contact.first_name = contact_name
65 new_contact.is_primary_contact = True,
66 new_contact.append('links', {
67 "link_doctype": "Supplier",
68 "link_name": supplier.name
69 })
70 new_contact.append('email_ids', {
71 "email_id": contact_email,
72 "is_primary": 1
73 })
74
75 new_contact.insert(ignore_mandatory=True)
76
77def create_custom_doctype():
78 frappe.get_doc({
79 'doctype': 'DocType',
80 'name': 'Order Assignment',
81 'module': 'Buying',
82 'custom': 1,
83 'autoname': 'field:po',
84 'fields': [
85 {'label': 'PO', 'fieldname': 'po', 'fieldtype': 'Link', 'options': 'Purchase Order'},
86 {'label': 'Supplier', 'fieldname': 'supplier', 'fieldtype': 'Data', "fetch_from": "po.supplier"}
87 ],
88 'permissions': [
89 {
90 "create": 1,
91 "delete": 1,
92 "email": 1,
93 "export": 1,
94 "print": 1,
95 "read": 1,
96 "report": 1,
97 "role": "System Manager",
98 "share": 1,
99 "write": 1
100 },
101 {
102 "read": 1,
103 "role": "Supplier"
104 }
105 ]
106 }).insert(ignore_if_duplicate = True)
107
108def create_webform():
109 frappe.get_doc({
110 'doctype': 'Web Form',
111 'module': 'Buying',
112 'title': 'SO Schedule',
113 'route': 'so-schedule',
114 'doc_type': 'Order Assignment',
115 'web_form_fields': [
116 {
117 'doctype': 'Web Form Field',
118 'fieldname': 'po',
119 'fieldtype': 'Link',
120 'options': 'Purchase Order',
121 'label': 'PO'
122 },
123 {
124 'doctype': 'Web Form Field',
125 'fieldname': 'supplier',
126 'fieldtype': 'Data',
127 'label': 'Supplier'
128 }
129 ]
130
131 }).insert(ignore_if_duplicate = True)
132
133def create_order_assignment(supplier, po):
134 frappe.get_doc({
135 'doctype': 'Order Assignment',
136 'po': po,
137 'supplier': supplier,
138 }).insert(ignore_if_duplicate = True)