Best Python code snippet using autotest_python
test_permissions.py
Source:test_permissions.py
...28 frappe.flags.permission_user_setup_done = True29 reset('Blogger')30 reset('Blog Post')31 frappe.db.sql('delete from `tabUser Permission`')32 frappe.set_user("test1@example.com")33 def tearDown(self):34 frappe.set_user("Administrator")35 frappe.db.set_value("Blogger", "_Test Blogger 1", "user", None)36 clear_user_permissions_for_doctype("Blog Category")37 clear_user_permissions_for_doctype("Blog Post")38 clear_user_permissions_for_doctype("Blogger")39 @staticmethod40 def set_strict_user_permissions(ignore):41 ss = frappe.get_doc("System Settings")42 ss.apply_strict_user_permissions = ignore43 ss.flags.ignore_mandatory = 144 ss.save()45 def test_basic_permission(self):46 post = frappe.get_doc("Blog Post", "-test-blog-post")47 self.assertTrue(post.has_permission("read"))48 def test_user_permissions_in_doc(self):49 add_user_permission("Blog Category", "_Test Blog Category 1",50 "test2@example.com")51 frappe.set_user("test2@example.com")52 post = frappe.get_doc("Blog Post", "-test-blog-post")53 self.assertFalse(post.has_permission("read"))54 self.assertFalse(get_doc_permissions(post).get("read"))55 post1 = frappe.get_doc("Blog Post", "-test-blog-post-1")56 self.assertTrue(post1.has_permission("read"))57 self.assertTrue(get_doc_permissions(post1).get("read"))58 def test_user_permissions_in_report(self):59 add_user_permission("Blog Category", "_Test Blog Category 1", "test2@example.com")60 frappe.set_user("test2@example.com")61 names = [d.name for d in frappe.get_list("Blog Post", fields=["name", "blog_category"])]62 self.assertTrue("-test-blog-post-1" in names)63 self.assertFalse("-test-blog-post" in names)64 def test_default_values(self):65 add_user_permission("Blog Category", "_Test Blog Category 1", "test2@example.com")66 frappe.set_user("test2@example.com")67 doc = frappe.new_doc("Blog Post")68 self.assertEqual(doc.get("blog_category"), "_Test Blog Category 1")69 def test_user_link_match_doc(self):70 blogger = frappe.get_doc("Blogger", "_Test Blogger 1")71 blogger.user = "test2@example.com"72 blogger.save()73 frappe.set_user("test2@example.com")74 post = frappe.get_doc("Blog Post", "-test-blog-post-2")75 self.assertTrue(post.has_permission("read"))76 post1 = frappe.get_doc("Blog Post", "-test-blog-post-1")77 self.assertFalse(post1.has_permission("read"))78 def test_user_link_match_report(self):79 blogger = frappe.get_doc("Blogger", "_Test Blogger 1")80 blogger.user = "test2@example.com"81 blogger.save()82 frappe.set_user("test2@example.com")83 names = [d.name for d in frappe.get_list("Blog Post", fields=["name", "owner"])]84 self.assertTrue("-test-blog-post-2" in names)85 self.assertFalse("-test-blog-post-1" in names)86 def test_set_user_permissions(self):87 frappe.set_user("test1@example.com")88 add_user_permission("Blog Post", "-test-blog-post", "test2@example.com")89 def test_not_allowed_to_set_user_permissions(self):90 frappe.set_user("test2@example.com")91 # this user can't add user permissions92 self.assertRaises(frappe.PermissionError, add_user_permission,93 "Blog Post", "-test-blog-post", "test2@example.com")94 def test_read_if_explicit_user_permissions_are_set(self):95 self.test_set_user_permissions()96 frappe.set_user("test2@example.com")97 # user can only access permitted blog post98 doc = frappe.get_doc("Blog Post", "-test-blog-post")99 self.assertTrue(doc.has_permission("read"))100 # and not this one101 doc = frappe.get_doc("Blog Post", "-test-blog-post-1")102 self.assertFalse(doc.has_permission("read"))103 def test_not_allowed_to_remove_user_permissions(self):104 self.test_set_user_permissions()105 frappe.set_user("test2@example.com")106 # user cannot remove their own user permissions107 self.assertRaises(frappe.PermissionError, remove_user_permission,108 "Blog Post", "-test-blog-post", "test2@example.com")109 def test_user_permissions_if_applied_on_doc_being_evaluated(self):110 frappe.set_user("test2@example.com")111 doc = frappe.get_doc("Blog Post", "-test-blog-post-1")112 self.assertTrue(doc.has_permission("read"))113 frappe.set_user("test1@example.com")114 add_user_permission("Blog Post", "-test-blog-post", "test2@example.com")115 frappe.set_user("test2@example.com")116 doc = frappe.get_doc("Blog Post", "-test-blog-post-1")117 self.assertFalse(doc.has_permission("read"))118 doc = frappe.get_doc("Blog Post", "-test-blog-post")119 self.assertTrue(doc.has_permission("read"))120 def test_set_only_once(self):121 blog_post = frappe.get_meta("Blog Post")122 doc = frappe.get_doc("Blog Post", "-test-blog-post-1")123 doc.db_set('title', 'Old')124 blog_post.get_field("title").set_only_once = 1125 doc.title = "New"126 self.assertRaises(frappe.CannotChangeConstantError, doc.save)127 blog_post.get_field("title").set_only_once = 0128 def test_set_only_once_child_table_rows(self):129 doctype_meta = frappe.get_meta("DocType")130 doctype_meta.get_field("fields").set_only_once = 1131 doc = frappe.get_doc("DocType", "Blog Post")132 # remove last one133 doc.fields = doc.fields[:-1]134 self.assertRaises(frappe.CannotChangeConstantError, doc.save)135 frappe.clear_cache(doctype='DocType')136 def test_set_only_once_child_table_row_value(self):137 doctype_meta = frappe.get_meta("DocType")138 doctype_meta.get_field("fields").set_only_once = 1139 doc = frappe.get_doc("DocType", "Blog Post")140 # change one property from the child table141 doc.fields[-1].fieldtype = 'HTML'142 self.assertRaises(frappe.CannotChangeConstantError, doc.save)143 frappe.clear_cache(doctype='DocType')144 def test_set_only_once_child_table_okay(self):145 doctype_meta = frappe.get_meta("DocType")146 doctype_meta.get_field("fields").set_only_once = 1147 doc = frappe.get_doc("DocType", "Blog Post")148 doc.load_doc_before_save()149 self.assertFalse(doc.validate_set_only_once())150 frappe.clear_cache(doctype='DocType')151 def test_user_permission_doctypes(self):152 add_user_permission("Blog Category", "_Test Blog Category 1",153 "test2@example.com")154 add_user_permission("Blogger", "_Test Blogger 1",155 "test2@example.com")156 frappe.set_user("test2@example.com")157 frappe.model.meta.clear_cache("Blog Post")158 doc = frappe.get_doc("Blog Post", "-test-blog-post")159 self.assertFalse(doc.has_permission("read"))160 doc = frappe.get_doc("Blog Post", "-test-blog-post-2")161 self.assertTrue(doc.has_permission("read"))162 frappe.model.meta.clear_cache("Blog Post")163 def if_owner_setup(self):164 update('Blog Post', 'Blogger', 0, 'if_owner', 1)165 add_user_permission("Blog Category", "_Test Blog Category 1",166 "test2@example.com")167 add_user_permission("Blogger", "_Test Blogger 1",168 "test2@example.com")169 frappe.model.meta.clear_cache("Blog Post")170 def test_insert_if_owner_with_user_permissions(self):171 """If `If Owner` is checked for a Role, check if that document172 is allowed to be read, updated, submitted, etc. except be created,173 even if the document is restricted based on User Permissions."""174 frappe.delete_doc('Blog Post', '-test-blog-post-title')175 self.if_owner_setup()176 frappe.set_user("test2@example.com")177 doc = frappe.get_doc({178 "doctype": "Blog Post",179 "blog_category": "_Test Blog Category",180 "blogger": "_Test Blogger 1",181 "title": "_Test Blog Post Title",182 "content": "_Test Blog Post Content"183 })184 self.assertRaises(frappe.PermissionError, doc.insert)185 frappe.set_user('test1@example.com')186 add_user_permission("Blog Category", "_Test Blog Category",187 "test2@example.com")188 frappe.set_user("test2@example.com")189 doc.insert()190 frappe.set_user("Administrator")191 remove_user_permission("Blog Category", "_Test Blog Category",192 "test2@example.com")193 frappe.set_user("test2@example.com")194 doc = frappe.get_doc(doc.doctype, doc.name)195 self.assertTrue(doc.has_permission("read"))196 self.assertTrue(doc.has_permission("write"))197 self.assertFalse(doc.has_permission("create"))198 def test_ignore_user_permissions_if_missing(self):199 """If there are no user permissions, then allow as per role"""200 add_user_permission("Blog Category", "_Test Blog Category",201 "test2@example.com")202 frappe.set_user("test2@example.com")203 doc = frappe.get_doc({204 "doctype": "Blog Post",205 "blog_category": "_Test Blog Category 2",206 "blogger": "_Test Blogger 1",207 "title": "_Test Blog Post Title",208 "content": "_Test Blog Post Content"209 })210 self.assertFalse(doc.has_permission("write"))211 frappe.set_user("Administrator")212 remove_user_permission("Blog Category", "_Test Blog Category",213 "test2@example.com")214 frappe.set_user("test2@example.com")215 self.assertTrue(doc.has_permission('write'))216 def test_strict_user_permissions(self):217 """If `Strict User Permissions` is checked in System Settings,218 show records even if User Permissions are missing for a linked219 doctype"""220 frappe.set_user('Administrator')221 frappe.db.sql('delete from tabContact')222 reset('Salutation')223 reset('Contact')224 make_test_records_for_doctype('Contact', force=True)225 add_user_permission("Salutation", "Mr", "test3@example.com")226 self.set_strict_user_permissions(0)227 allowed_contact = frappe.get_doc('Contact', '_Test Contact for _Test Customer')228 other_contact = frappe.get_doc('Contact', '_Test Contact for _Test Supplier')229 frappe.set_user("test3@example.com")230 self.assertTrue(allowed_contact.has_permission('read'))231 self.assertTrue(other_contact.has_permission('read'))232 self.assertEqual(len(frappe.get_list("Contact")), 2)233 frappe.set_user("Administrator")234 self.set_strict_user_permissions(1)235 frappe.set_user("test3@example.com")236 self.assertTrue(allowed_contact.has_permission('read'))237 self.assertFalse(other_contact.has_permission('read'))238 self.assertTrue(len(frappe.get_list("Contact")), 1)239 frappe.set_user("Administrator")240 self.set_strict_user_permissions(0)241 clear_user_permissions_for_doctype("Salutation")242 clear_user_permissions_for_doctype("Contact")243 def test_user_permissions_not_applied_if_user_can_edit_user_permissions(self):244 add_user_permission('Blogger', '_Test Blogger 1', 'test1@example.com')245 # test1@example.com has rights to create user permissions246 # so it should not matter if explicit user permissions are not set247 self.assertTrue(frappe.get_doc('Blogger', '_Test Blogger').has_permission('read'))248 def test_user_permission_is_not_applied_if_user_roles_does_not_have_permission(self):249 add_user_permission('Blog Post', '-test-blog-post-1', 'test3@example.com')250 frappe.set_user("test3@example.com")251 doc = frappe.get_doc("Blog Post", "-test-blog-post-1")252 self.assertFalse(doc.has_permission("read"))253 frappe.set_user("Administrator")254 user = frappe.get_doc("User", "test3@example.com")255 user.add_roles("Blogger")256 frappe.set_user("test3@example.com")257 self.assertTrue(doc.has_permission("read"))258 frappe.set_user("Administrator")...
test_docshare.py
Source:test_docshare.py
...10 "subject": "test share event",11 "starts_on": "2015-01-01 10:00:00",12 "event_type": "Private"}).insert()13 def tearDown(self):14 frappe.set_user("Administrator")15 self.event.delete()16 def test_add(self):17 # user not shared18 self.assertTrue(self.event.name not in frappe.share.get_shared("Event", self.user))19 frappe.share.add("Event", self.event.name, self.user)20 self.assertTrue(self.event.name in frappe.share.get_shared("Event", self.user))21 def test_doc_permission(self):22 frappe.set_user(self.user)23 self.assertFalse(self.event.has_permission())24 frappe.set_user("Administrator")25 frappe.share.add("Event", self.event.name, self.user)26 frappe.set_user(self.user)27 self.assertTrue(self.event.has_permission())28 def test_share_permission(self):29 frappe.share.add("Event", self.event.name, self.user, share=1)30 frappe.set_user(self.user)31 self.assertTrue(self.event.has_permission("share"))32 # test cascade33 self.assertTrue(self.event.has_permission("read"))34 self.assertTrue(self.event.has_permission("write"))35 def test_set_permission(self):36 frappe.share.add("Event", self.event.name, self.user)37 frappe.set_user(self.user)38 self.assertFalse(self.event.has_permission("share"))39 frappe.set_user("Administrator")40 frappe.share.set_permission("Event", self.event.name, self.user, "share")41 frappe.set_user(self.user)42 self.assertTrue(self.event.has_permission("share"))43 def test_permission_to_share(self):44 frappe.set_user(self.user)45 self.assertRaises(frappe.PermissionError, frappe.share.add, "Event", self.event.name, self.user)46 frappe.set_user("Administrator")47 frappe.share.add("Event", self.event.name, self.user, share=1)48 # test not raises49 frappe.set_user(self.user)50 frappe.share.add("Event", self.event.name, "test1@example.com", share=1)51 def test_remove_share(self):52 frappe.share.add("Event", self.event.name, self.user, share=1)53 frappe.set_user(self.user)54 self.assertTrue(self.event.has_permission("share"))55 frappe.set_user("Administrator")56 frappe.share.remove("Event", self.event.name, self.user)57 frappe.set_user(self.user)58 self.assertFalse(self.event.has_permission("share"))59 def test_share_with_everyone(self):60 self.assertTrue(self.event.name not in frappe.share.get_shared("Event", self.user))61 frappe.share.set_permission("Event", self.event.name, None, "read", everyone=1)62 self.assertTrue(self.event.name in frappe.share.get_shared("Event", self.user))63 self.assertTrue(self.event.name in frappe.share.get_shared("Event", "test1@example.com"))64 self.assertTrue(self.event.name not in frappe.share.get_shared("Event", "Guest"))65 frappe.share.set_permission("Event", self.event.name, None, "read", value=0, everyone=1)66 self.assertTrue(self.event.name not in frappe.share.get_shared("Event", self.user))67 self.assertTrue(self.event.name not in frappe.share.get_shared("Event", "test1@example.com"))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!