def test_org_permission_required(self): view_func = Mock(return_value='success') request = self.mk_request('get', '/') user = self.mk_user() request.user = user # test login redirect wrapped_view_func = org_permission_required( perm='organizations.change_organization', login_url='/login/')(view_func) response = wrapped_view_func(request) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, '/login/?next=http%3A//testserver/') # test that we can pass a list of perms wrapped_view_func = org_permission_required( perm=[ 'organizations.change_organization', 'organizations.add_organization' ], raise_exception=True)(view_func) self.assertRaises(PermissionDenied, wrapped_view_func, request) # test exception is raised wrapped_view_func = org_permission_required( perm='organizations.change_organization', raise_exception=True)(view_func) self.assertRaises(PermissionDenied, wrapped_view_func, request) self.grant_perms(user, 'organizations.change_organization') User = get_user_model() request.user = User.objects.get(id=user.pk) self.assertEqual(wrapped_view_func(request), 'success') self.revoke_perms(user, 'organizations.change_organization') request.user = User.objects.get(id=user.pk) self.assertRaises(PermissionDenied, wrapped_view_func, request) organization = self.mk_organization(users=[user]) self.assertEqual(wrapped_view_func(request), 'success') request.session[ORGANIZATION_SESSION_KEY] = organization.pk self.assertEqual(wrapped_view_func(request), 'success') organization.organizationuserrelation_set.filter(user=user).update( is_admin=False) self.assertRaises(PermissionDenied, wrapped_view_func, request)
def test_org_permission_required(self): view_func = Mock(return_value='success') request = self.mk_request('get', '/') user = self.mk_user() request.user = user # test login redirect wrapped_view_func = org_permission_required( perm='organizations.change_organization', login_url='/login/')(view_func) response = wrapped_view_func(request) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, '/login/?next=http%3A//testserver/') # test that we can pass a list of perms wrapped_view_func = org_permission_required( perm=['organizations.change_organization', 'organizations.add_organization'], raise_exception=True)(view_func) self.assertRaises(PermissionDenied, wrapped_view_func, request) # test exception is raised wrapped_view_func = org_permission_required( perm='organizations.change_organization', raise_exception=True)(view_func) self.assertRaises(PermissionDenied, wrapped_view_func, request) self.grant_perms(user, 'organizations.change_organization') User = get_user_model() request.user = User.objects.get(id=user.pk) self.assertEqual(wrapped_view_func(request), 'success') self.revoke_perms(user, 'organizations.change_organization') request.user = User.objects.get(id=user.pk) self.assertRaises(PermissionDenied, wrapped_view_func, request) organization = self.mk_organization(users=[user]) self.assertEqual(wrapped_view_func(request), 'success') request.session[ORGANIZATION_SESSION_KEY] = organization.pk self.assertEqual(wrapped_view_func(request), 'success') organization.organizationuserrelation_set.filter( user=user).update(is_admin=False) self.assertRaises(PermissionDenied, wrapped_view_func, request)
def as_view(cls): view = super(ControllerViewMixin, cls).as_view() if cls.social_auth: view = user_passes_test( lambda u: u.social_auth.filter( provider=cls.social_auth).exists(), login_url=reverse_lazy( 'social:begin', args=(cls.social_auth,)))(view) if cls.permissions: view = org_permission_required(cls.permissions)(view) return login_required(view)
def as_view(cls): view = super(ControllerViewMixin, cls).as_view() if cls.social_auth: view = user_passes_test(lambda u: u.social_auth.filter( provider=cls.social_auth).exists(), login_url=reverse_lazy( 'social:begin', args=(cls.social_auth, )))(view) if cls.permissions: view = org_permission_required(cls.permissions)(view) return login_required(view)