Best Python code snippet using autotest_python
test_managers.py
Source:test_managers.py
1"""Tests for managers of projects."""2from django.test import TestCase3from django.contrib.auth.models import AnonymousUser4from django.core.exceptions import PermissionDenied5from geokey.users.tests.model_factories import UserFactory, UserGroupFactory6from .model_factories import ProjectFactory7from ..models import Project8class ProjectListTest(TestCase):9 def setUp(self):10 self.admin = UserFactory.create()11 self.moderator_view = UserFactory.create()12 self.moderator = UserFactory.create()13 self.contributor_view = UserFactory.create()14 self.contributor = UserFactory.create()15 self.viewer = UserFactory.create()16 self.some_dude = UserFactory.create()17 self.private_project = ProjectFactory.create(18 add_admins=[self.admin],19 **{20 'name': 'Private',21 'isprivate': True,22 'everyone_contributes': 'false'23 })24 self.private_moderators_view = UserGroupFactory(25 add_users=[self.moderator_view],26 **{27 'project': self.private_project,28 'can_moderate': True29 })30 self.private_contributors_view = UserGroupFactory(31 add_users=[self.contributor_view],32 **{33 'project': self.private_project,34 'can_contribute': True35 })36 self.private_moderators = UserGroupFactory(37 add_users=[self.moderator],38 **{39 'project': self.private_project,40 'can_moderate': True41 })42 self.private_contributors = UserGroupFactory(43 add_users=[self.contributor],44 **{45 'project': self.private_project,46 'can_contribute': True47 })48 self.private_viewers = UserGroupFactory(49 add_users=[self.viewer],50 **{51 'project': self.private_project,52 'can_contribute': False,53 'can_moderate': False54 })55 self.private_publicview_project = ProjectFactory.create(56 add_admins=[self.admin],57 **{58 'name': 'Private with public view',59 'isprivate': True,60 'everyone_contributes': 'false'61 })62 self.private_publicview_moderators_view = UserGroupFactory(63 add_users=[self.moderator_view],64 **{65 'project': self.private_publicview_project,66 'can_moderate': True67 })68 self.private_publicview_contributors_view = UserGroupFactory(69 add_users=[self.contributor_view],70 **{71 'project': self.private_publicview_project,72 'can_contribute': True73 })74 self.private_publicview_moderators = UserGroupFactory(75 add_users=[self.moderator],76 **{77 'project': self.private_publicview_project,78 'can_moderate': True79 })80 self.private_publicview_contributors = UserGroupFactory(81 add_users=[self.contributor],82 **{83 'project': self.private_publicview_project,84 'can_contribute': True85 })86 self.private_publicview_viewers = UserGroupFactory(87 add_users=[self.viewer],88 **{89 'project': self.private_publicview_project,90 'can_contribute': False,91 'can_moderate': False92 })93 self.private_publicviews_project = ProjectFactory.create(94 add_admins=[self.admin],95 **{96 'name': 'Private with public views',97 'isprivate': True,98 'everyone_contributes': 'false'99 })100 self.private_publicviews_moderators_view = UserGroupFactory(101 add_users=[self.moderator_view],102 **{103 'project': self.private_publicviews_project,104 'can_moderate': True105 })106 self.private_publicviews_contributors_view = UserGroupFactory(107 add_users=[self.contributor_view],108 **{109 'project': self.private_publicviews_project,110 'can_contribute': True111 })112 self.private_publicviews_moderators = UserGroupFactory(113 add_users=[self.moderator],114 **{115 'project': self.private_publicviews_project,116 'can_moderate': True117 })118 self.private_publicviews_contributors = UserGroupFactory(119 add_users=[self.contributor],120 **{121 'project': self.private_publicviews_project,122 'can_contribute': True123 })124 self.private_publicviews_viewers = UserGroupFactory(125 add_users=[self.viewer],126 **{127 'project': self.private_publicviews_project,128 'can_contribute': False,129 'can_moderate': False130 })131 self.public_project = ProjectFactory.create(132 add_admins=[self.admin],133 **{134 'name': 'Public',135 'isprivate': False,136 'everyone_contributes': 'false'137 })138 self.public_moderators_view = UserGroupFactory(139 add_users=[self.moderator_view],140 **{141 'project': self.public_project,142 'can_moderate': True143 })144 self.public_contributors_view = UserGroupFactory(145 add_users=[self.contributor_view],146 **{147 'project': self.public_project,148 'can_contribute': True149 })150 self.public_moderators = UserGroupFactory(151 add_users=[self.moderator],152 **{153 'project': self.public_project,154 'can_moderate': True155 })156 self.public_contributors = UserGroupFactory(157 add_users=[self.contributor],158 **{159 'project': self.public_project,160 'can_contribute': True161 })162 self.public_viewers = UserGroupFactory(163 add_users=[self.viewer],164 **{165 'project': self.public_project,166 'can_contribute': False,167 'can_moderate': False168 })169 self.public_publicview_project = ProjectFactory.create(170 add_admins=[self.admin],171 **{172 'name': 'Public with public view',173 'isprivate': False,174 'everyone_contributes': 'false'175 })176 self.public_publicview_moderators_view = UserGroupFactory(177 add_users=[self.moderator_view],178 **{179 'project': self.public_publicview_project,180 'can_moderate': True181 })182 self.public_publicview_contributors_view = UserGroupFactory(183 add_users=[self.contributor_view],184 **{185 'project': self.public_publicview_project,186 'can_contribute': True187 })188 self.public_publicview_moderators = UserGroupFactory(189 add_users=[self.moderator],190 **{191 'project': self.public_publicview_project,192 'can_moderate': True193 })194 self.public_publicview_contributors = UserGroupFactory(195 add_users=[self.contributor],196 **{197 'project': self.public_publicview_project,198 'can_contribute': True199 })200 self.public_publicview_viewers = UserGroupFactory(201 add_users=[self.viewer],202 **{203 'project': self.public_publicview_project,204 'can_contribute': False,205 'can_moderate': False206 })207 self.public_publicviews_project = ProjectFactory.create(208 add_admins=[self.admin],209 **{210 'name': 'public with all public views',211 'isprivate': False,212 'everyone_contributes': 'false'213 })214 self.public_publicviews_moderators_view = UserGroupFactory(215 add_users=[self.moderator_view],216 **{217 'project': self.public_publicviews_project,218 'can_moderate': True219 })220 self.public_publicviews_contributors_view = UserGroupFactory(221 add_users=[self.contributor_view],222 **{223 'project': self.public_publicviews_project,224 'can_contribute': True225 })226 self.public_publicviews_moderators = UserGroupFactory(227 add_users=[self.moderator],228 **{229 'project': self.public_publicviews_project,230 'can_moderate': True231 })232 self.public_publicviews_contributors = UserGroupFactory(233 add_users=[self.contributor],234 **{235 'project': self.public_publicviews_project,236 'can_contribute': True237 })238 self.public_publicviews_viewers = UserGroupFactory(239 add_users=[self.viewer],240 **{241 'project': self.public_publicviews_project,242 'can_contribute': False,243 'can_moderate': False244 })245 def test_get_list_with_admin(self):246 projects = Project.objects.get_list(self.admin)247 self.assertEquals(len(projects), 6)248 def test_get_list_with_moderator_view(self):249 projects = Project.objects.get_list(self.moderator_view)250 self.assertEquals(len(projects), 6)251 def test_get_list_with_moderator(self):252 projects = Project.objects.get_list(self.moderator)253 self.assertEquals(len(projects), 6)254 def test_get_list_with_contributor_view(self):255 projects = Project.objects.get_list(self.contributor_view)256 self.assertEquals(len(projects), 6)257 def test_get_list_with_contributor(self):258 projects = Project.objects.get_list(self.contributor)259 self.assertEquals(len(projects), 6)260 def test_get_list_with_viewer(self):261 projects = Project.objects.get_list(self.viewer)262 self.assertEquals(len(projects), 6)263 def test_get_list_with_some_dude(self):264 projects = Project.objects.get_list(self.some_dude)265 self.assertEquals(len(projects), 3)266 def test_get_list_with_anonymous(self):267 projects = Project.objects.get_list(AnonymousUser())268 self.assertEquals(len(projects), 3)269class ProjectAccessTest(TestCase):270 def test_as_admin(self):271 project = ProjectFactory.create()272 user = UserFactory.create(**{'is_superuser': True})273 ref = Project.objects.as_admin(user, project.id)274 self.assertEqual(project, ref)275 def test_as_contributor(self):276 admin = UserFactory.create()277 contributor = UserFactory.create()278 other = UserFactory.create()279 project = ProjectFactory.create(280 add_admins=[admin],281 add_contributors=[contributor],282 add_viewer=[other]283 )284 ref = Project.objects.as_contributor(admin, project.id)285 self.assertEqual(project, ref)286 ref = Project.objects.as_contributor(contributor, project.id)287 self.assertEqual(project, ref)288 try:289 Project.objects.as_contributor(other, project.id)290 except PermissionDenied:291 pass292 else:...
test_authz.py
Source:test_authz.py
...34 self.assertTrue(user_has_role(self.user, CourseCreatorRole()))35 def test_creator_group_enabled_nonempty(self):36 """ Tests creator group feature on, user added. """37 with mock.patch.dict('django.conf.settings.FEATURES', {"ENABLE_CREATOR_GROUP": True}):38 add_users(self.admin, CourseCreatorRole(), self.user)39 self.assertTrue(user_has_role(self.user, CourseCreatorRole()))40 # check that a user who has not been added to the group still returns false41 user_not_added = User.objects.create_user('testuser2', 'test+courses2@edx.org', 'foo2')42 self.assertFalse(user_has_role(user_not_added, CourseCreatorRole()))43 # remove first user from the group and verify that CourseCreatorRole().has_user now returns false44 remove_users(self.admin, CourseCreatorRole(), self.user)45 self.assertFalse(user_has_role(self.user, CourseCreatorRole()))46 def test_course_creation_disabled(self):47 """ Tests that the COURSE_CREATION_DISABLED flag overrides course creator group settings. """48 with mock.patch.dict('django.conf.settings.FEATURES',49 {'DISABLE_COURSE_CREATION': True, "ENABLE_CREATOR_GROUP": True}):50 # Add user to creator group.51 add_users(self.admin, CourseCreatorRole(), self.user)52 # DISABLE_COURSE_CREATION overrides (user is not marked as staff).53 self.assertFalse(user_has_role(self.user, CourseCreatorRole()))54 # Mark as staff. Now CourseCreatorRole().has_user returns true.55 self.user.is_staff = True56 self.assertTrue(user_has_role(self.user, CourseCreatorRole()))57 # Remove user from creator group. CourseCreatorRole().has_user still returns true because is_staff=True58 remove_users(self.admin, CourseCreatorRole(), self.user)59 self.assertTrue(user_has_role(self.user, CourseCreatorRole()))60 def test_add_user_not_authenticated(self):61 """62 Tests that adding to creator group fails if user is not authenticated63 """64 with mock.patch.dict(65 'django.conf.settings.FEATURES',66 {'DISABLE_COURSE_CREATION': False, "ENABLE_CREATOR_GROUP": True}67 ):68 anonymous_user = AnonymousUser()69 role = CourseCreatorRole()70 add_users(self.admin, role, anonymous_user)71 self.assertFalse(user_has_role(anonymous_user, role))72 def test_add_user_not_active(self):73 """74 Tests that adding to creator group fails if user is not active75 """76 with mock.patch.dict(77 'django.conf.settings.FEATURES',78 {'DISABLE_COURSE_CREATION': False, "ENABLE_CREATOR_GROUP": True}79 ):80 self.user.is_active = False81 add_users(self.admin, CourseCreatorRole(), self.user)82 self.assertFalse(user_has_role(self.user, CourseCreatorRole()))83 def test_add_user_to_group_requires_staff_access(self):84 with self.assertRaises(PermissionDenied):85 self.admin.is_staff = False86 add_users(self.admin, CourseCreatorRole(), self.user)87 with self.assertRaises(PermissionDenied):88 add_users(self.user, CourseCreatorRole(), self.user)89 def test_add_user_to_group_requires_active(self):90 with self.assertRaises(PermissionDenied):91 self.admin.is_active = False92 add_users(self.admin, CourseCreatorRole(), self.user)93 def test_add_user_to_group_requires_authenticated(self):94 with self.assertRaises(PermissionDenied):95 self.admin.is_authenticated = mock.Mock(return_value=False)96 add_users(self.admin, CourseCreatorRole(), self.user)97 def test_remove_user_from_group_requires_staff_access(self):98 with self.assertRaises(PermissionDenied):99 self.admin.is_staff = False100 remove_users(self.admin, CourseCreatorRole(), self.user)101 def test_remove_user_from_group_requires_active(self):102 with self.assertRaises(PermissionDenied):103 self.admin.is_active = False104 remove_users(self.admin, CourseCreatorRole(), self.user)105 def test_remove_user_from_group_requires_authenticated(self):106 with self.assertRaises(PermissionDenied):107 self.admin.is_authenticated = mock.Mock(return_value=False)108 remove_users(self.admin, CourseCreatorRole(), self.user)109class CourseGroupTest(TestCase):110 """111 Tests for instructor and staff groups for a particular course.112 """113 def setUp(self):114 """ Test case setup """115 super(CourseGroupTest, self).setUp()116 self.global_admin = AdminFactory()117 self.creator = User.objects.create_user('testcreator', 'testcreator+courses@edx.org', 'foo')118 self.staff = User.objects.create_user('teststaff', 'teststaff+courses@edx.org', 'foo')119 self.course_key = SlashSeparatedCourseKey('mitX', '101', 'test')120 def test_add_user_to_course_group(self):121 """122 Tests adding user to course group (happy path).123 """124 # Create groups for a new course (and assign instructor role to the creator).125 self.assertFalse(user_has_role(self.creator, CourseInstructorRole(self.course_key)))126 add_users(self.global_admin, CourseInstructorRole(self.course_key), self.creator)127 add_users(self.global_admin, CourseStaffRole(self.course_key), self.creator)128 self.assertTrue(user_has_role(self.creator, CourseInstructorRole(self.course_key)))129 # Add another user to the staff role.130 self.assertFalse(user_has_role(self.staff, CourseStaffRole(self.course_key)))131 add_users(self.creator, CourseStaffRole(self.course_key), self.staff)132 self.assertTrue(user_has_role(self.staff, CourseStaffRole(self.course_key)))133 def test_add_user_to_course_group_permission_denied(self):134 """135 Verifies PermissionDenied if caller of add_user_to_course_group is not instructor role.136 """137 add_users(self.global_admin, CourseInstructorRole(self.course_key), self.creator)138 add_users(self.global_admin, CourseStaffRole(self.course_key), self.creator)139 with self.assertRaises(PermissionDenied):140 add_users(self.staff, CourseStaffRole(self.course_key), self.staff)141 def test_remove_user_from_course_group(self):142 """143 Tests removing user from course group (happy path).144 """145 add_users(self.global_admin, CourseInstructorRole(self.course_key), self.creator)146 add_users(self.global_admin, CourseStaffRole(self.course_key), self.creator)147 add_users(self.creator, CourseStaffRole(self.course_key), self.staff)148 self.assertTrue(user_has_role(self.staff, CourseStaffRole(self.course_key)))149 remove_users(self.creator, CourseStaffRole(self.course_key), self.staff)150 self.assertFalse(user_has_role(self.staff, CourseStaffRole(self.course_key)))151 remove_users(self.creator, CourseInstructorRole(self.course_key), self.creator)152 self.assertFalse(user_has_role(self.creator, CourseInstructorRole(self.course_key)))153 def test_remove_user_from_course_group_permission_denied(self):154 """155 Verifies PermissionDenied if caller of remove_user_from_course_group is not instructor role.156 """157 add_users(self.global_admin, CourseInstructorRole(self.course_key), self.creator)158 another_staff = User.objects.create_user('another', 'teststaff+anothercourses@edx.org', 'foo')159 add_users(self.global_admin, CourseStaffRole(self.course_key), self.creator, self.staff, another_staff)160 with self.assertRaises(PermissionDenied):...
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!!