How to use test_has_perm method in Kiwi

Best Python code snippet using Kiwi_python

auth_backends.py

Source:auth_backends.py Github

copy

Full Screen

...18 # The custom_perms test messes with ContentTypes, which will19 # be cached; flush the cache to ensure there are no side effects20 # Refs #14975, #1492521 ContentType.objects.clear_cache()22 def test_has_perm(self):23 user = User.objects.get(username='test')24 self.assertEqual(user.has_perm('auth.test'), False)25 user.is_staff = True26 user.save()27 self.assertEqual(user.has_perm('auth.test'), False)28 user.is_superuser = True29 user.save()30 self.assertEqual(user.has_perm('auth.test'), True)31 user.is_staff = False32 user.is_superuser = False33 user.save()34 self.assertEqual(user.has_perm('auth.test'), False)35 user.is_staff = True36 user.is_superuser = True37 user.is_active = False38 user.save()39 self.assertEqual(user.has_perm('auth.test'), False)40 def test_custom_perms(self):41 user = User.objects.get(username='test')42 content_type=ContentType.objects.get_for_model(Group)43 perm = Permission.objects.create(name='test', content_type=content_type, codename='test')44 user.user_permissions.add(perm)45 user.save()46 # reloading user to purge the _perm_cache47 user = User.objects.get(username='test')48 self.assertEqual(user.get_all_permissions() == set([u'auth.test']), True)49 self.assertEqual(user.get_group_permissions(), set([]))50 self.assertEqual(user.has_module_perms('Group'), False)51 self.assertEqual(user.has_module_perms('auth'), True)52 perm = Permission.objects.create(name='test2', content_type=content_type, codename='test2')53 user.user_permissions.add(perm)54 user.save()55 perm = Permission.objects.create(name='test3', content_type=content_type, codename='test3')56 user.user_permissions.add(perm)57 user.save()58 user = User.objects.get(username='test')59 self.assertEqual(user.get_all_permissions(), set([u'auth.test2', u'auth.test', u'auth.test3']))60 self.assertEqual(user.has_perm('test'), False)61 self.assertEqual(user.has_perm('auth.test'), True)62 self.assertEqual(user.has_perms(['auth.test2', 'auth.test3']), True)63 perm = Permission.objects.create(name='test_group', content_type=content_type, codename='test_group')64 group = Group.objects.create(name='test_group')65 group.permissions.add(perm)66 group.save()67 user.groups.add(group)68 user = User.objects.get(username='test')69 exp = set([u'auth.test2', u'auth.test', u'auth.test3', u'auth.test_group'])70 self.assertEqual(user.get_all_permissions(), exp)71 self.assertEqual(user.get_group_permissions(), set([u'auth.test_group']))72 self.assertEqual(user.has_perms(['auth.test3', 'auth.test_group']), True)73 user = AnonymousUser()74 self.assertEqual(user.has_perm('test'), False)75 self.assertEqual(user.has_perms(['auth.test2', 'auth.test3']), False)76 def test_has_no_object_perm(self):77 """Regressiontest for #12462"""78 user = User.objects.get(username='test')79 content_type=ContentType.objects.get_for_model(Group)80 perm = Permission.objects.create(name='test', content_type=content_type, codename='test')81 user.user_permissions.add(perm)82 user.save()83 self.assertEqual(user.has_perm('auth.test', 'object'), False)84 self.assertEqual(user.get_all_permissions('object'), set([]))85 self.assertEqual(user.has_perm('auth.test'), True)86 self.assertEqual(user.get_all_permissions(), set(['auth.test']))87 def test_get_all_superuser_permissions(self):88 "A superuser has all permissions. Refs #14795"89 user = User.objects.get(username='test2')90 self.assertEqual(len(user.get_all_permissions()), len(Permission.objects.all()))91BackendTest = unittest.skipIf(not connection.features.supports_joins,92 'Requires JOIN support')(BackendTest)93class TestObj(object):94 pass95class SimpleRowlevelBackend(object):96 supports_object_permissions = True97 supports_inactive_user = False98 # This class also supports tests for anonymous user permissions, and99 # inactive user permissions via subclasses which just set the100 # 'supports_anonymous_user' or 'supports_inactive_user' attribute.101 def has_perm(self, user, perm, obj=None):102 if not obj:103 return # We only support row level perms104 if isinstance(obj, TestObj):105 if user.username == 'test2':106 return True107 elif user.is_anonymous() and perm == 'anon':108 # not reached due to supports_anonymous_user = False109 return True110 elif not user.is_active and perm == 'inactive':111 return True112 return False113 def has_module_perms(self, user, app_label):114 if not user.is_anonymous() and not user.is_active:115 return False116 return app_label == "app1"117 def get_all_permissions(self, user, obj=None):118 if not obj:119 return [] # We only support row level perms120 if not isinstance(obj, TestObj):121 return ['none']122 if user.is_anonymous():123 return ['anon']124 if user.username == 'test2':125 return ['simple', 'advanced']126 else:127 return ['simple']128 def get_group_permissions(self, user, obj=None):129 if not obj:130 return # We only support row level perms131 if not isinstance(obj, TestObj):132 return ['none']133 if 'test_group' in [group.name for group in user.groups.all()]:134 return ['group_perm']135 else:136 return ['none']137class RowlevelBackendTest(TestCase):138 """139 Tests for auth backend that supports object level permissions140 """141 backend = 'django.contrib.auth.tests.auth_backends.SimpleRowlevelBackend'142 def setUp(self):143 self.curr_auth = settings.AUTHENTICATION_BACKENDS144 settings.AUTHENTICATION_BACKENDS = tuple(self.curr_auth) + (self.backend,)145 self.user1 = User.objects.create_user('test', 'test@example.com', 'test')146 self.user2 = User.objects.create_user('test2', 'test2@example.com', 'test')147 self.user3 = User.objects.create_user('test3', 'test3@example.com', 'test')148 self.save_warnings_state()149 warnings.filterwarnings('ignore', category=DeprecationWarning,150 module='django.contrib.auth')151 def tearDown(self):152 settings.AUTHENTICATION_BACKENDS = self.curr_auth153 self.restore_warnings_state()154 # The get_group_permissions test messes with ContentTypes, which will155 # be cached; flush the cache to ensure there are no side effects156 # Refs #14975, #14925157 ContentType.objects.clear_cache()158 def test_has_perm(self):159 self.assertEqual(self.user1.has_perm('perm', TestObj()), False)160 self.assertEqual(self.user2.has_perm('perm', TestObj()), True)161 self.assertEqual(self.user2.has_perm('perm'), False)162 self.assertEqual(self.user2.has_perms(['simple', 'advanced'], TestObj()), True)163 self.assertEqual(self.user3.has_perm('perm', TestObj()), False)164 self.assertEqual(self.user3.has_perm('anon', TestObj()), False)165 self.assertEqual(self.user3.has_perms(['simple', 'advanced'], TestObj()), False)166 def test_get_all_permissions(self):167 self.assertEqual(self.user1.get_all_permissions(TestObj()), set(['simple']))168 self.assertEqual(self.user2.get_all_permissions(TestObj()), set(['simple', 'advanced']))169 self.assertEqual(self.user2.get_all_permissions(), set([]))170 def test_get_group_permissions(self):171 content_type=ContentType.objects.get_for_model(Group)172 group = Group.objects.create(name='test_group')173 self.user3.groups.add(group)174 self.assertEqual(self.user3.get_group_permissions(TestObj()), set(['group_perm']))175RowlevelBackendTest = unittest.skipIf(not connection.features.supports_joins,176 'Requires JOIN support')(RowlevelBackendTest)177class AnonymousUserBackend(SimpleRowlevelBackend):178 supports_anonymous_user = True179 supports_inactive_user = False180class NoAnonymousUserBackend(SimpleRowlevelBackend):181 supports_anonymous_user = False182 supports_inactive_user = False183class AnonymousUserBackendTest(TestCase):184 """185 Tests for AnonymousUser delegating to backend if it has 'supports_anonymous_user' = True186 """187 backend = 'django.contrib.auth.tests.auth_backends.AnonymousUserBackend'188 def setUp(self):189 self.curr_auth = settings.AUTHENTICATION_BACKENDS190 settings.AUTHENTICATION_BACKENDS = (self.backend,)191 self.user1 = AnonymousUser()192 def tearDown(self):193 settings.AUTHENTICATION_BACKENDS = self.curr_auth194 def test_has_perm(self):195 self.assertEqual(self.user1.has_perm('perm', TestObj()), False)196 self.assertEqual(self.user1.has_perm('anon', TestObj()), True)197 def test_has_perms(self):198 self.assertEqual(self.user1.has_perms(['anon'], TestObj()), True)199 self.assertEqual(self.user1.has_perms(['anon', 'perm'], TestObj()), False)200 def test_has_module_perms(self):201 self.assertEqual(self.user1.has_module_perms("app1"), True)202 self.assertEqual(self.user1.has_module_perms("app2"), False)203 def test_get_all_permissions(self):204 self.assertEqual(self.user1.get_all_permissions(TestObj()), set(['anon']))205class NoAnonymousUserBackendTest(TestCase):206 """207 Tests that AnonymousUser does not delegate to backend if it has 'supports_anonymous_user' = False208 """209 backend = 'django.contrib.auth.tests.auth_backends.NoAnonymousUserBackend'210 def setUp(self):211 self.curr_auth = settings.AUTHENTICATION_BACKENDS212 settings.AUTHENTICATION_BACKENDS = tuple(self.curr_auth) + (self.backend,)213 self.user1 = AnonymousUser()214 def tearDown(self):215 settings.AUTHENTICATION_BACKENDS = self.curr_auth216 def test_has_perm(self):217 self.assertEqual(self.user1.has_perm('perm', TestObj()), False)218 self.assertEqual(self.user1.has_perm('anon', TestObj()), False)219 def test_has_perms(self):220 self.assertEqual(self.user1.has_perms(['anon'], TestObj()), False)221 def test_has_module_perms(self):222 self.assertEqual(self.user1.has_module_perms("app1"), False)223 self.assertEqual(self.user1.has_module_perms("app2"), False)224 def test_get_all_permissions(self):225 self.assertEqual(self.user1.get_all_permissions(TestObj()), set())226class NoBackendsTest(TestCase):227 """228 Tests that an appropriate error is raised if no auth backends are provided.229 """230 def setUp(self):231 self.old_AUTHENTICATION_BACKENDS = settings.AUTHENTICATION_BACKENDS232 settings.AUTHENTICATION_BACKENDS = []233 self.user = User.objects.create_user('test', 'test@example.com', 'test')234 def tearDown(self):235 settings.AUTHENTICATION_BACKENDS = self.old_AUTHENTICATION_BACKENDS236 def test_raises_exception(self):237 self.assertRaises(ImproperlyConfigured, self.user.has_perm, ('perm', TestObj(),))238class InActiveUserBackend(SimpleRowlevelBackend):239 supports_anonymous_user = False240 supports_inactive_user = True241class NoInActiveUserBackend(SimpleRowlevelBackend):242 supports_anonymous_user = False243 supports_inactive_user = False244class InActiveUserBackendTest(TestCase):245 """246 Tests for a inactive user delegating to backend if it has 'supports_inactive_user' = True247 """248 backend = 'django.contrib.auth.tests.auth_backends.InActiveUserBackend'249 def setUp(self):250 self.curr_auth = settings.AUTHENTICATION_BACKENDS251 settings.AUTHENTICATION_BACKENDS = (self.backend,)252 self.user1 = User.objects.create_user('test', 'test@example.com', 'test')253 self.user1.is_active = False254 self.user1.save()255 def tearDown(self):256 settings.AUTHENTICATION_BACKENDS = self.curr_auth257 def test_has_perm(self):258 self.assertEqual(self.user1.has_perm('perm', TestObj()), False)259 self.assertEqual(self.user1.has_perm('inactive', TestObj()), True)260 def test_has_module_perms(self):261 self.assertEqual(self.user1.has_module_perms("app1"), False)262 self.assertEqual(self.user1.has_module_perms("app2"), False)263class NoInActiveUserBackendTest(TestCase):264 """265 Tests that an inactive user does not delegate to backend if it has 'supports_inactive_user' = False266 """267 backend = 'django.contrib.auth.tests.auth_backends.NoInActiveUserBackend'268 def setUp(self):269 self.curr_auth = settings.AUTHENTICATION_BACKENDS270 settings.AUTHENTICATION_BACKENDS = tuple(self.curr_auth) + (self.backend,)271 self.user1 = User.objects.create_user('test', 'test@example.com', 'test')272 self.user1.is_active = False273 self.user1.save()274 def tearDown(self):275 settings.AUTHENTICATION_BACKENDS = self.curr_auth276 def test_has_perm(self):277 self.assertEqual(self.user1.has_perm('perm', TestObj()), False)278 self.assertEqual(self.user1.has_perm('inactive', TestObj()), True)279 def test_has_module_perms(self):280 self.assertEqual(self.user1.has_module_perms("app1"), False)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kiwi automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful