Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

150 righe
4.1 KiB

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