def test_list_filter_is_staff_field(self): modeladmin = UserAdmin(User, site) request = self.rf.get(self.user_change_list_url, {}) request.user = self.superuser list_filter = modeladmin.get_list_filter(request) self.assertIn("is_staff", list_filter) # ensure "is_staff" not present in list_filter of staff view request.user = self.instructor1 list_filter = modeladmin.get_list_filter(request) self.assertNotIn("is_staff", list_filter)
def setUp(self): self.user_admin = UserAdmin(User, AdminSite()) self.superuser = UserFactory.create(is_superuser=True) self.organisation = OrganisationFactory.create() self.organisation_1 = OrganisationFactory.create() self.user_with_org = UserFactory.create(is_staff=True) self.user_with_org.approved_organisations.add(self.organisation) self.user_with_org.approved_organisations.add(self.organisation_1) self.user_with_org_1 = UserFactory.create(is_staff=True) self.user_with_org_1.approved_organisations.add(self.organisation)
def setUp(self): self.resource_admin = ResourceAdmin(Resource, AdminSite()) self.user_admin = UserAdmin(User, AdminSite()) self.organisation = OrganisationFactory.create() self.resource = ResourceFactory.create(organisation=self.organisation) self.resource_1 = ResourceFactory.create( organisation=self.organisation) self.resource_2 = ResourceFactory.create() self.user_without_organisation = UserFactory.create(is_staff=True) self.user_with_organisation = UserFactory.create(is_staff=True) self.user_with_organisation.approved_organisations.add( self.organisation)
class ResourceAdminTest(TestCase): def setUp(self): self.resource_admin = ResourceAdmin(Resource, AdminSite()) self.user_admin = UserAdmin(User, AdminSite()) self.organisation = OrganisationFactory.create() self.resource = ResourceFactory.create(organisation=self.organisation) self.resource_1 = ResourceFactory.create( organisation=self.organisation) self.resource_2 = ResourceFactory.create() self.user_without_organisation = UserFactory.create(is_staff=True) self.user_with_organisation = UserFactory.create(is_staff=True) self.user_with_organisation.approved_organisations.add( self.organisation) def test_permissions_with_no_organisation(self): self.user_admin.add_remove_permissions(self.user_without_organisation, Resource, 'change_resource') self.assertFalse( self.user_without_organisation.user_permissions.exists()) def test_permissions_with_organisation(self): self.user_admin.add_remove_permissions(self.user_with_organisation, Resource, 'change_resource') content_type = ContentType.objects.get_for_model(Resource) permission = Permission.objects.get(content_type=content_type, codename='change_resource') permission_string = '{app_label}.{permission}'.format( app_label=permission.content_type.app_label, permission=permission.codename, ) self.assertTrue(self.user_with_organisation.user_permissions.exists()) self.assertTrue( self.user_with_organisation.has_perm(permission_string)) def test_get_queryset(self): request = RequestFactory().request() request.user = self.user_with_organisation qs = self.resource_admin.get_queryset(request) self.assertEqual(qs.count(), 2)
def test_list_editable_is_staff_field(self): # ensuer "is_staff" not present in list_editable # notice that, list_editable must be a subset of list_display, # or it will raise an check error. modeladmin = UserAdmin(User, site) request = self.rf.get(self.user_change_list_url, {}) request.user = self.superuser changelist = self.get_changelist(request, User, modeladmin) self.assertNotIn("is_staff", changelist.list_editable) request.user = self.instructor1 changelist = self.get_changelist(request, User, modeladmin) self.assertNotIn("is_staff", changelist.list_editable)
class UserAdminTest(TestCase): def setUp(self): self.user_admin = UserAdmin(User, AdminSite()) self.superuser = UserFactory.create(is_superuser=True) self.organisation = OrganisationFactory.create() self.organisation_1 = OrganisationFactory.create() self.user_with_org = UserFactory.create(is_staff=True) self.user_with_org.approved_organisations.add(self.organisation) self.user_with_org.approved_organisations.add(self.organisation_1) self.user_with_org_1 = UserFactory.create(is_staff=True) self.user_with_org_1.approved_organisations.add(self.organisation) def test_permissions_with_no_organisation(self): self.user_admin.add_remove_permissions(self.superuser, User, 'change_user') self.assertFalse(self.superuser.user_permissions.exists()) def test_permissions_with_organisation(self): self.user_admin.add_remove_permissions(self.user_with_org, User, 'change_user') content_type = ContentType.objects.get_for_model(User) permission = Permission.objects.get(content_type=content_type, codename='change_user') permission_string = '{app_label}.{permission}'.format( app_label=permission.content_type.app_label, permission=permission.codename, ) self.assertTrue(self.user_with_org.user_permissions.exists()) self.assertTrue(self.user_with_org.has_perm(permission_string)) def test_permissions_delete_organisation(self): self.user_with_org_1.approved_organisations.remove(self.organisation) self.user_admin.add_remove_permissions(self.user_with_org_1, User, 'change_user') self.assertFalse(self.user_with_org.user_permissions.exists()) def test_get_queryset(self): request = RequestFactory().request() request.user = self.user_with_org qs = self.user_admin.get_queryset(request) self.assertEqual(qs.count(), 2)
def test_list_filter_queryset_filter(self): """ A list filter that filters the queryset by default gives the correct full_result_count. """ total_user_count = User.objects.count() modeladmin = UserAdmin(User, site) # {{{ not filtered request = self.rf.get(self.user_change_list_url, {}) request.user = self.superuser changelist = self.get_changelist(request, User, modeladmin) changelist.get_results(request) self.assertEqual(changelist.full_result_count, total_user_count) filterspec_list = self.get_filterspec_list(request, changelist) self.assertIn( ('All', self.course1.identifier, self.course2.identifier), filterspec_list) request = self.rf.get(self.user_change_list_url, {}) request.user = self.instructor1 changelist = self.get_changelist(request, User, modeladmin) changelist.get_results(request) filterspec_list = self.get_filterspec_list(request, changelist) # 2 users created in setUp 'testuser_001', 'testuser_000', # 4 non-participation users 'test_user4', 'test_user3', 'test_user2', # 'test_user1', # 1 instructor 'test_instructor' (request.user) self.assertEqual(changelist.full_result_count, 10) queryset = changelist.get_queryset(request) self.assertIn(self.instructor1, queryset) self.assertIn(self.superuser, queryset) self.assertIn(self.course1_student_participation.user, queryset) self.assertIn(self.instructor2, queryset) # Although instructor 1 attended course2, the list_filter did not have that # choice, because he/she has no view_admin_interface pperm in that course self.assertIn(('All', self.course1.identifier), filterspec_list) # }}} # {{{ filtered by course 1 request = self.rf.get(self.user_change_list_url, {"course__identifier": self.course1.identifier}) request.user = self.superuser changelist = self.get_changelist(request, User, modeladmin) queryset = changelist.get_queryset(request) self.assertEqual( queryset.count(), Participation.objects.filter( course__identifier=self.course1.identifier).count()) request = self.rf.get(self.user_change_list_url, {"course__identifier": self.course1.identifier}) request.user = self.instructor1 changelist = self.get_changelist(request, User, modeladmin) queryset = changelist.get_queryset(request) # 2 users created in setUp 'testuser_001', 'testuser_000', # 1 instructor 'test_instructor' self.assertEqual(queryset.count(), 5)
def setUp(self): self.user = UserFactory() self.site = AdminSite() self.user_admin = UserAdmin(User, self.site)