Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 1 | import unittest |
| 2 | |
| 3 | import frappe |
| 4 | |
| 5 | from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order |
| 6 | |
| 7 | |
| 8 | class TestWebsite(unittest.TestCase): |
| 9 | def test_permission_for_custom_doctype(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 10 | 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 Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 20 | |
| 21 | create_custom_doctype() |
| 22 | create_webform() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 23 | create_order_assignment(supplier="Supplier1", po=po1.name) |
| 24 | create_order_assignment(supplier="Supplier2", po=po2.name) |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 25 | |
| 26 | frappe.set_user("Administrator") |
| 27 | # checking if data consist of all order assignment of Supplier1 and Supplier2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 28 | self.assertTrue("Supplier1" and "Supplier2" in [data.supplier for data in get_data()]) |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 29 | |
| 30 | frappe.set_user("supplier1@gmail.com") |
| 31 | # checking if data only consist of order assignment of Supplier1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 32 | 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 Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 34 | |
| 35 | frappe.set_user("supplier2@gmail.com") |
| 36 | # checking if data only consist of order assignment of Supplier2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 37 | 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 Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 39 | |
| 40 | frappe.set_user("Administrator") |
| 41 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 42 | |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 43 | def get_data(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 44 | webform_list_contexts = frappe.get_hooks("webform_list_context") |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 45 | if webform_list_contexts: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 46 | context = frappe._dict(frappe.get_attr(webform_list_contexts[0])("Buying") or {}) |
| 47 | kwargs = dict(doctype="Order Assignment", order_by="modified desc") |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 48 | return context.get_list(**kwargs) |
| 49 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 50 | |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 51 | def create_user(name, email): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 52 | 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 Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 63 | |
| 64 | def create_supplier_with_contact(name, group, contact_name, contact_email): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 65 | supplier = frappe.get_doc( |
| 66 | {"doctype": "Supplier", "supplier_name": name, "supplier_group": group} |
| 67 | ).insert(ignore_if_duplicate=True) |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 68 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 69 | if not frappe.db.exists("Contact", contact_name + "-1-" + name): |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 70 | new_contact = frappe.new_doc("Contact") |
| 71 | new_contact.first_name = contact_name |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 72 | 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 Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 75 | |
| 76 | new_contact.insert(ignore_mandatory=True) |
| 77 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 78 | |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 79 | def create_custom_doctype(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 80 | 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 Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 114 | |
| 115 | def create_webform(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 116 | 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 Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 140 | |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 141 | |
| 142 | def create_order_assignment(supplier, po): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 143 | frappe.get_doc( |
| 144 | { |
| 145 | "doctype": "Order Assignment", |
| 146 | "po": po, |
| 147 | "supplier": supplier, |
| 148 | } |
| 149 | ).insert(ignore_if_duplicate=True) |