Commit 4e455fd8 by Don Mitchell

Limit read access to people with write access.

Add unit tests for auth
parent e9c70633
...@@ -5,6 +5,7 @@ import json ...@@ -5,6 +5,7 @@ import json
from contentstore.tests.utils import CourseTestCase from contentstore.tests.utils import CourseTestCase
from xmodule.modulestore.django import editable_modulestore from xmodule.modulestore.django import editable_modulestore
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from student.models import CourseEnrollment
class TestOrphan(CourseTestCase): class TestOrphan(CourseTestCase):
""" """
...@@ -70,3 +71,18 @@ class TestOrphan(CourseTestCase): ...@@ -70,3 +71,18 @@ class TestOrphan(CourseTestCase):
self.client.get(url, HTTP_ACCEPT='application/json').content self.client.get(url, HTTP_ACCEPT='application/json').content
) )
self.assertEqual(len(orphans), 0, "Orphans not deleted {}".format(orphans)) self.assertEqual(len(orphans), 0, "Orphans not deleted {}".format(orphans))
def test_not_permitted(self):
"""
Test that auth restricts get and delete appropriately
"""
test_user_client, test_user = self.createNonStaffAuthedUserClient()
CourseEnrollment.enroll(test_user, self.course.location.course_id)
url = reverse(
'orphan',
kwargs={'course_id': '{}.{}'.format(self.course.location.org, self.course.location.course)}
)
response = test_user_client.get(url)
self.assertEqual(response.status_code, 403)
response = test_user_client.delete(url)
self.assertEqual(response.status_code, 403)
...@@ -65,7 +65,7 @@ class CourseTestCase(ModuleStoreTestCase): ...@@ -65,7 +65,7 @@ class CourseTestCase(ModuleStoreTestCase):
def createNonStaffAuthedUserClient(self): def createNonStaffAuthedUserClient(self):
""" """
Create a non-staff user, log them in, and return the client to use for testing. Create a non-staff user, log them in, and return the client, user to use for testing.
""" """
uname = 'teststudent' uname = 'teststudent'
password = 'foo' password = 'foo'
......
...@@ -7,7 +7,7 @@ from django.core.exceptions import PermissionDenied ...@@ -7,7 +7,7 @@ from django.core.exceptions import PermissionDenied
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from xmodule.modulestore import Location from xmodule.modulestore import Location
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore, loc_mapper
from xmodule.modulestore.inheritance import own_metadata from xmodule.modulestore.inheritance import own_metadata
from xmodule.modulestore.exceptions import ItemNotFoundError, InvalidLocationError from xmodule.modulestore.exceptions import ItemNotFoundError, InvalidLocationError
...@@ -21,6 +21,8 @@ from .access import has_access ...@@ -21,6 +21,8 @@ from .access import has_access
from .helpers import _xmodule_recurse from .helpers import _xmodule_recurse
from xmodule.x_module import XModuleDescriptor from xmodule.x_module import XModuleDescriptor
from django.views.decorators.http import require_http_methods from django.views.decorators.http import require_http_methods
from xmodule.modulestore.locator import CourseLocator
from student.models import CourseEnrollment
__all__ = ['save_item', 'create_item', 'delete_item', 'orphan'] __all__ = ['save_item', 'create_item', 'delete_item', 'orphan']
...@@ -216,10 +218,17 @@ def orphan(request, course_id): ...@@ -216,10 +218,17 @@ def orphan(request, course_id):
:param request: :param request:
:param course_id: Locator syntax course_id :param course_id: Locator syntax course_id
""" """
course_loc = CourseLocator(course_id=course_id)
if request.method == 'GET': if request.method == 'GET':
return JsonResponse(modulestore().get_orphans(course_id, DETACHED_CATEGORIES, 'draft')) if has_access(request.user, course_loc):
if request.method == 'DELETE' and request.user.is_staff: return JsonResponse(modulestore().get_orphans(course_id, DETACHED_CATEGORIES, 'draft'))
items = modulestore().get_orphans(course_id, DETACHED_CATEGORIES, 'draft') else:
for item in items: raise PermissionDenied()
modulestore('draft').delete_item(item, True) if request.method == 'DELETE':
return JsonResponse({'deleted': items}) if request.user.is_staff:
items = modulestore().get_orphans(course_id, DETACHED_CATEGORIES, 'draft')
for item in items:
modulestore('draft').delete_item(item, True)
return JsonResponse({'deleted': items})
else:
raise PermissionDenied()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment