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 |
Gursheen Kaur Anand | 5113a41 | 2023-06-28 11:22:40 +0530 | [diff] [blame] | 6 | from erpnext.buying.doctype.supplier.test_supplier import create_supplier |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 7 | |
| 8 | |
| 9 | class TestWebsite(unittest.TestCase): |
| 10 | def test_permission_for_custom_doctype(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 11 | create_user("Supplier 1", "supplier1@gmail.com") |
| 12 | create_user("Supplier 2", "supplier2@gmail.com") |
Gursheen Kaur Anand | 5113a41 | 2023-06-28 11:22:40 +0530 | [diff] [blame] | 13 | |
| 14 | supplier1 = create_supplier(supplier_name="Supplier1") |
| 15 | supplier2 = create_supplier(supplier_name="Supplier2") |
| 16 | supplier1.append("portal_users", {"user": "supplier1@gmail.com"}) |
| 17 | supplier1.save() |
| 18 | supplier2.append("portal_users", {"user": "supplier2@gmail.com"}) |
| 19 | supplier2.save() |
| 20 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 21 | po1 = create_purchase_order(supplier="Supplier1") |
| 22 | po2 = create_purchase_order(supplier="Supplier2") |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 23 | |
| 24 | create_custom_doctype() |
| 25 | create_webform() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 26 | create_order_assignment(supplier="Supplier1", po=po1.name) |
| 27 | create_order_assignment(supplier="Supplier2", po=po2.name) |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 28 | |
| 29 | frappe.set_user("Administrator") |
| 30 | # checking if data consist of all order assignment of Supplier1 and Supplier2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 31 | 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] | 32 | |
| 33 | frappe.set_user("supplier1@gmail.com") |
| 34 | # checking if data only consist of order assignment of Supplier1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 35 | self.assertTrue("Supplier1" in [data.supplier for data in get_data()]) |
| 36 | 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] | 37 | |
| 38 | frappe.set_user("supplier2@gmail.com") |
| 39 | # checking if data only consist of order assignment of Supplier2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 40 | self.assertTrue("Supplier2" in [data.supplier for data in get_data()]) |
| 41 | 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] | 42 | |
| 43 | frappe.set_user("Administrator") |
| 44 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 45 | |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 46 | def get_data(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 47 | webform_list_contexts = frappe.get_hooks("webform_list_context") |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 48 | if webform_list_contexts: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 49 | context = frappe._dict(frappe.get_attr(webform_list_contexts[0])("Buying") or {}) |
| 50 | kwargs = dict(doctype="Order Assignment", order_by="modified desc") |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 51 | return context.get_list(**kwargs) |
| 52 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 53 | |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 54 | def create_user(name, email): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 55 | frappe.get_doc( |
| 56 | { |
| 57 | "doctype": "User", |
| 58 | "send_welcome_email": 0, |
| 59 | "user_type": "Website User", |
| 60 | "first_name": name, |
| 61 | "email": email, |
| 62 | "roles": [{"doctype": "Has Role", "role": "Supplier"}], |
| 63 | } |
| 64 | ).insert(ignore_if_duplicate=True) |
| 65 | |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 66 | |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 67 | def create_custom_doctype(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 68 | frappe.get_doc( |
| 69 | { |
| 70 | "doctype": "DocType", |
| 71 | "name": "Order Assignment", |
| 72 | "module": "Buying", |
| 73 | "custom": 1, |
| 74 | "autoname": "field:po", |
| 75 | "fields": [ |
| 76 | {"label": "PO", "fieldname": "po", "fieldtype": "Link", "options": "Purchase Order"}, |
| 77 | { |
| 78 | "label": "Supplier", |
| 79 | "fieldname": "supplier", |
| 80 | "fieldtype": "Data", |
| 81 | "fetch_from": "po.supplier", |
| 82 | }, |
| 83 | ], |
| 84 | "permissions": [ |
| 85 | { |
| 86 | "create": 1, |
| 87 | "delete": 1, |
| 88 | "email": 1, |
| 89 | "export": 1, |
| 90 | "print": 1, |
| 91 | "read": 1, |
| 92 | "report": 1, |
| 93 | "role": "System Manager", |
| 94 | "share": 1, |
| 95 | "write": 1, |
| 96 | }, |
| 97 | {"read": 1, "role": "Supplier"}, |
| 98 | ], |
| 99 | } |
| 100 | ).insert(ignore_if_duplicate=True) |
| 101 | |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 102 | |
| 103 | def create_webform(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 104 | frappe.get_doc( |
| 105 | { |
| 106 | "doctype": "Web Form", |
| 107 | "module": "Buying", |
| 108 | "title": "SO Schedule", |
| 109 | "route": "so-schedule", |
| 110 | "doc_type": "Order Assignment", |
| 111 | "web_form_fields": [ |
| 112 | { |
| 113 | "doctype": "Web Form Field", |
| 114 | "fieldname": "po", |
| 115 | "fieldtype": "Link", |
| 116 | "options": "Purchase Order", |
| 117 | "label": "PO", |
| 118 | }, |
| 119 | { |
| 120 | "doctype": "Web Form Field", |
| 121 | "fieldname": "supplier", |
| 122 | "fieldtype": "Data", |
| 123 | "label": "Supplier", |
| 124 | }, |
| 125 | ], |
| 126 | } |
| 127 | ).insert(ignore_if_duplicate=True) |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 128 | |
Shariq Ansari | 9aa6f52 | 2021-09-14 12:49:08 +0530 | [diff] [blame] | 129 | |
| 130 | def create_order_assignment(supplier, po): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 131 | frappe.get_doc( |
| 132 | { |
| 133 | "doctype": "Order Assignment", |
| 134 | "po": po, |
| 135 | "supplier": supplier, |
| 136 | } |
| 137 | ).insert(ignore_if_duplicate=True) |