Best Python code snippet using localstack_python
test_views.py
Source:test_views.py
...34 def _request_role_delete_view(self):35 return self.post(36 viewname='permissions:role_delete', args=(self.role_2.pk,),37 )38 def _create_role(self):39 self.role_2 = Role.objects.create(name=TEST_ROLE_2_LABEL)40 def test_role_delete_view_no_access(self):41 self._create_role()42 response = self._request_role_delete_view()43 self.assertEqual(response.status_code, 403)44 self.assertEqual(Role.objects.count(), 2)45 self.assertTrue(TEST_ROLE_2_LABEL in Role.objects.values_list('name', flat=True))46 def test_role_delete_view_with_access(self):47 self._create_role()48 self.grant_access(permission=permission_role_delete, obj=self.role_2)49 response = self._request_role_delete_view()50 self.assertEqual(response.status_code, 302)51 self.assertEqual(Role.objects.count(), 1)52 self.assertFalse(TEST_ROLE_2_LABEL in Role.objects.values_list('name', flat=True))53 def _request_role_edit_view(self):54 return self.post(55 viewname='permissions:role_edit', args=(self.role_2.pk,), data={56 'name': TEST_ROLE_LABEL_EDITED,57 }58 )59 def test_role_edit_view_no_access(self):60 self._create_role()61 response = self._request_role_edit_view()62 self.assertEqual(response.status_code, 403)63 self.role_2.refresh_from_db()64 self.assertEqual(Role.objects.count(), 2)65 self.assertEqual(self.role_2.name, TEST_ROLE_2_LABEL)66 def test_role_edit_view_with_access(self):67 self._create_role()68 self.grant_access(permission=permission_role_edit, obj=self.role_2)69 response = self._request_role_edit_view()70 self.assertEqual(response.status_code, 302)71 self.role_2.refresh_from_db()72 self.assertEqual(Role.objects.count(), 2)73 self.assertEqual(self.role_2.name, TEST_ROLE_LABEL_EDITED)74 def _request_role_list_view(self):75 return self.get(viewname='permissions:role_list')76 def test_role_list_view_no_access(self):77 self._create_role()78 response = self._request_role_list_view()79 self.assertEqual(response.status_code, 200)80 self.assertNotContains(response, text=TEST_ROLE_2_LABEL, status_code=200)81 def test_role_list_view_with_access(self):82 self._create_role()83 self.grant_access(permission=permission_role_view, obj=self.role_2)84 response = self._request_role_list_view()85 self.assertContains(response, text=TEST_ROLE_2_LABEL, status_code=200)86 def _request_role_permissions_view(self):87 return self.get(88 viewname='permissions:role_permissions', args=(self.role_2.pk,)89 )90 def test_role_permissions_view_no_access(self):91 self._create_role()92 response = self._request_role_permissions_view()93 self.assertEqual(response.status_code, 403)94 def test_role_permissions_view_with_permission_grant(self):95 self._create_role()96 self.grant_access(permission=permission_permission_grant, obj=self.role_2)97 response = self._request_role_permissions_view()98 self.assertEqual(response.status_code, 200)99 def test_role_permissions_view_with_permission_revoke(self):100 self._create_role()101 self.grant_access(permission=permission_permission_revoke, obj=self.role_2)102 response = self._request_role_permissions_view()103 self.assertEqual(response.status_code, 200)104 def _request_role_groups_view(self):105 return self.get(106 viewname='permissions:role_groups', args=(self.role_2.pk,)107 )108 def test_role_groups_view_no_access(self):109 self._create_role()110 response = self._request_role_groups_view()111 self.assertEqual(response.status_code, 403)112 def test_role_groups_view_with_access(self):113 self._create_role()114 self.grant_access(permission=permission_role_edit, obj=self.role_2)115 response = self._request_role_groups_view()116 self.assertEqual(response.status_code, 200)117 def _create_group(self):118 self.group_2 = Group.objects.create(name=TEST_GROUP_2_NAME)119 def _request_group_roles_view(self):120 return self.get(121 viewname='permissions:group_roles', args=(self.group_2.pk,)122 )123 def test_group_roles_view_no_access(self):124 self._create_group()125 response = self._request_group_roles_view()126 self.assertEqual(response.status_code, 403)127 def test_group_roles_view_with_access(self):...
test_role.py
Source:test_role.py
2from __future__ import absolute_import3from .helper import DashboardTestCase4class RoleTest(DashboardTestCase):5 @classmethod6 def _create_role(cls, name=None, description=None, scopes_permissions=None):7 data = {}8 if name:9 data['name'] = name10 if description:11 data['description'] = description12 if scopes_permissions:13 data['scopes_permissions'] = scopes_permissions14 cls._post('/api/role', data)15 def test_crud_role(self):16 self._create_role(name='role1',17 description='Description 1',18 scopes_permissions={'osd': ['read']})19 self.assertStatus(201)20 self.assertJsonBody({21 'name': 'role1',22 'description': 'Description 1',23 'scopes_permissions': {'osd': ['read']},24 'system': False25 })26 self._get('/api/role/role1')27 self.assertStatus(200)28 self.assertJsonBody({29 'name': 'role1',30 'description': 'Description 1',31 'scopes_permissions': {'osd': ['read']},32 'system': False33 })34 self._put('/api/role/role1', {35 'description': 'Description 2',36 'scopes_permissions': {'osd': ['read', 'update']},37 })38 self.assertStatus(200)39 self.assertJsonBody({40 'name': 'role1',41 'description': 'Description 2',42 'scopes_permissions': {'osd': ['read', 'update']},43 'system': False44 })45 self._delete('/api/role/role1')46 self.assertStatus(204)47 def test_list_roles(self):48 roles = self._get('/api/role')49 self.assertStatus(200)50 self.assertGreaterEqual(len(roles), 1)51 for role in roles:52 self.assertIn('name', role)53 self.assertIn('description', role)54 self.assertIn('scopes_permissions', role)55 self.assertIn('system', role)56 def test_get_role_does_not_exist(self):57 self._get('/api/role/role2')58 self.assertStatus(404)59 def test_create_role_already_exists(self):60 self._create_role(name='read-only',61 description='Description 1',62 scopes_permissions={'osd': ['read']})63 self.assertStatus(400)64 self.assertError(code='role_already_exists',65 component='role')66 def test_create_role_no_name(self):67 self._create_role(description='Description 1',68 scopes_permissions={'osd': ['read']})69 self.assertStatus(400)70 self.assertError(code='name_required',71 component='role')72 def test_create_role_invalid_scope(self):73 self._create_role(name='role1',74 description='Description 1',75 scopes_permissions={'invalid-scope': ['read']})76 self.assertStatus(400)77 self.assertError(code='invalid_scope',78 component='role')79 def test_create_role_invalid_permission(self):80 self._create_role(name='role1',81 description='Description 1',82 scopes_permissions={'osd': ['invalid-permission']})83 self.assertStatus(400)84 self.assertError(code='invalid_permission',85 component='role')86 def test_delete_role_does_not_exist(self):87 self._delete('/api/role/role2')88 self.assertStatus(404)89 def test_delete_system_role(self):90 self._delete('/api/role/read-only')91 self.assertStatus(400)92 self.assertError(code='cannot_delete_system_role',93 component='role')94 def test_delete_role_associated_with_user(self):95 self.create_user("user", "user", ['read-only'])96 self._create_role(name='role1',97 description='Description 1',98 scopes_permissions={'user': ['create', 'read', 'update', 'delete']})99 self.assertStatus(201)100 self._put('/api/user/user', {'roles': ['role1']})101 self.assertStatus(200)102 self._delete('/api/role/role1')103 self.assertStatus(400)104 self.assertError(code='role_is_associated_with_user',105 component='role')106 self._put('/api/user/user', {'roles': ['administrator']})107 self.assertStatus(200)108 self._delete('/api/role/role1')109 self.assertStatus(204)110 self.delete_user("user")...
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!!