Fixed access methods for wiki nav. Added tests.
This commit is contained in:
@@ -4,7 +4,7 @@ from urlparse import urlparse
|
||||
from django.http import Http404
|
||||
from django.shortcuts import redirect
|
||||
|
||||
from courseware.courses import check_course
|
||||
from courseware.courses import get_course_with_access
|
||||
|
||||
|
||||
class Middleware(object):
|
||||
@@ -20,9 +20,7 @@ class Middleware(object):
|
||||
same page on the regular wiki.
|
||||
"""
|
||||
|
||||
def process_request(self, request):
|
||||
#TODO: We should also redirect people who can't see the class to the regular wiki, so urls don't break
|
||||
|
||||
def process_request(self, request):
|
||||
referer = request.META.get('HTTP_REFERER')
|
||||
|
||||
try:
|
||||
@@ -30,7 +28,7 @@ class Middleware(object):
|
||||
referer_path = parsed_referer.path
|
||||
except:
|
||||
referer_path =""
|
||||
|
||||
|
||||
path_match = re.match(r'^/wiki/(?P<wiki_path>.*|)$', request.path)
|
||||
if path_match:
|
||||
# We are going to the wiki. Check if we came from a course
|
||||
@@ -40,7 +38,7 @@ class Middleware(object):
|
||||
|
||||
# See if we are able to view the course. If we are, redirect to it
|
||||
try:
|
||||
course = check_course(request.user, course_id)
|
||||
course = get_course_with_access(request.user, course_id, 'load')
|
||||
return redirect("/courses/" + course.id + "/wiki/" + path_match.group('wiki_path') )
|
||||
|
||||
except Http404:
|
||||
@@ -55,14 +53,13 @@ class Middleware(object):
|
||||
course_id = course_match.group('course_id')
|
||||
# See if we are able to view the course. If we aren't, redirect to regular wiki
|
||||
try:
|
||||
course = check_course(request.user, course_id)
|
||||
course = get_course_with_access(request.user, course_id, 'load')
|
||||
# Good, we can see the course. Carry on
|
||||
return None
|
||||
except Http404:
|
||||
# We can't see the course, so redirect to the regular wiki
|
||||
return redirect("/wiki/" + course_match.group('wiki_path'))
|
||||
|
||||
|
||||
return None
|
||||
|
||||
def context_processor(request):
|
||||
@@ -79,7 +76,7 @@ def context_processor(request):
|
||||
course_id = match.group('course_id')
|
||||
|
||||
try:
|
||||
course = check_course(request.user, course_id)
|
||||
course = get_course_with_access(request.user, course_id, 'load')
|
||||
return {'course' : course}
|
||||
except Http404:
|
||||
# We couldn't access the course for whatever reason. It is too late to change
|
||||
|
||||
@@ -34,11 +34,13 @@ class WikiRedirectTestCase(PageLoader):
|
||||
|
||||
def test_wiki_redirect(self):
|
||||
"""
|
||||
Test that an enrolled in student going from /courses/edX/toy/2012_Fall/profile
|
||||
Test that requesting wiki URLs redirect properly to or out of classes.
|
||||
|
||||
An enrolled in student going from /courses/edX/toy/2012_Fall/profile
|
||||
to /wiki/some/fake/wiki/page/ will redirect to
|
||||
/courses/edX/toy/2012_Fall/wiki/some/fake/wiki/page/
|
||||
|
||||
Test that an unenrolled student going to /courses/edX/toy/2012_Fall/wiki/some/fake/wiki/page/
|
||||
An unenrolled student going to /courses/edX/toy/2012_Fall/wiki/some/fake/wiki/page/
|
||||
will be redirected to /wiki/some/fake/wiki/page/
|
||||
|
||||
"""
|
||||
@@ -57,16 +59,61 @@ class WikiRedirectTestCase(PageLoader):
|
||||
self.assertEqual(resp['Location'], 'http://testserver' + redirected_to )
|
||||
|
||||
|
||||
# Now we test that the student will be redirected away from that page if they are unenrolled
|
||||
# We do this in the same test because we want to make sure the redirected_to is the same
|
||||
# Now we test that the student will be redirected away from that page if the course doesn't exist
|
||||
# We do this in the same test because we want to make sure the redirected_to is constructed correctly
|
||||
|
||||
self.unenroll(self.toy)
|
||||
# This is a location like /courses/*/wiki/* , but with an invalid course ID
|
||||
bad_course_wiki_page = redirected_to.replace( self.toy.location.course, "bad_course" )
|
||||
|
||||
resp = self.client.get( redirected_to, HTTP_REFERER=referer)
|
||||
print "redirected_to" , redirected_to
|
||||
resp = self.client.get( bad_course_wiki_page, HTTP_REFERER=referer)
|
||||
self.assertEqual(resp.status_code, 302)
|
||||
self.assertEqual(resp['Location'], 'http://testserver' + destination )
|
||||
|
||||
|
||||
def create_course_page(self, course):
|
||||
"""
|
||||
Test that loading the course wiki page creates the wiki page.
|
||||
The user must be enrolled in the course to see the page.
|
||||
"""
|
||||
|
||||
course_wiki_home = reverse('course_wiki', kwargs={'course_id' : course.id})
|
||||
referer = reverse("profile", kwargs={ 'course_id' : self.toy.id })
|
||||
|
||||
resp = self.client.get(course_wiki_home, follow=True, HTTP_REFERER=referer)
|
||||
|
||||
course_wiki_page = referer.replace('profile', 'wiki/' + self.toy.wiki_slug + "/")
|
||||
|
||||
ending_location = resp.redirect_chain[-1][0]
|
||||
ending_status = resp.redirect_chain[-1][1]
|
||||
|
||||
self.assertEquals(ending_location, 'http://testserver' + course_wiki_page )
|
||||
self.assertEquals(resp.status_code, 200)
|
||||
|
||||
self.has_course_navigator(resp)
|
||||
|
||||
def has_course_navigator(self, resp):
|
||||
"""
|
||||
Ensure that the response has the course navigator.
|
||||
"""
|
||||
self.assertTrue( "course info" in resp.content.lower() )
|
||||
self.assertTrue( "courseware" in resp.content.lower() )
|
||||
|
||||
|
||||
def test_course_navigator(self):
|
||||
""""
|
||||
Test that going from a course page to a wiki page contains the course navigator.
|
||||
"""
|
||||
|
||||
self.login(self.student, self.password)
|
||||
self.enroll(self.toy)
|
||||
self.create_course_page(self.toy)
|
||||
|
||||
|
||||
course_wiki_page = reverse('wiki:get', kwargs={'path' : self.toy.wiki_slug + '/'})
|
||||
referer = reverse("courseware", kwargs={ 'course_id' : self.toy.id })
|
||||
|
||||
resp = self.client.get(course_wiki_page, follow=True, HTTP_REFERER=referer)
|
||||
|
||||
self.has_course_navigator(resp)
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ from django.shortcuts import redirect
|
||||
from wiki.core.exceptions import NoRootURL
|
||||
from wiki.models import URLPath, Article
|
||||
|
||||
from courseware.courses import check_course
|
||||
from courseware.courses import get_course_by_id
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -25,7 +25,7 @@ def course_wiki_redirect(request, course_id):
|
||||
as it's home page. A course's wiki must be an article on the root (for
|
||||
example, "/6.002x") to keep things simple.
|
||||
"""
|
||||
course = check_course(request.user, course_id)
|
||||
course = get_course_by_id(course_id)
|
||||
|
||||
course_slug = course.wiki_slug
|
||||
|
||||
|
||||
@@ -187,7 +187,7 @@ class PageLoader(ActivateLoginTestCase):
|
||||
def unenroll(self, course):
|
||||
"""Unenroll the currently logged-in user, and check that it worked."""
|
||||
resp = self.client.post('/change_enrollment', {
|
||||
'enrollment_action': 'enroll',
|
||||
'enrollment_action': 'unenroll',
|
||||
'course_id': course.id,
|
||||
})
|
||||
data = parse_json(resp)
|
||||
|
||||
Reference in New Issue
Block a user