Commit 099f499e by Bridger Maxwell

Fixed access methods for wiki nav. Added tests.

parent 4bc4e87a
...@@ -4,7 +4,7 @@ from urlparse import urlparse ...@@ -4,7 +4,7 @@ from urlparse import urlparse
from django.http import Http404 from django.http import Http404
from django.shortcuts import redirect from django.shortcuts import redirect
from courseware.courses import check_course from courseware.courses import get_course_with_access
class Middleware(object): class Middleware(object):
...@@ -21,8 +21,6 @@ class Middleware(object): ...@@ -21,8 +21,6 @@ class Middleware(object):
""" """
def process_request(self, request): 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
referer = request.META.get('HTTP_REFERER') referer = request.META.get('HTTP_REFERER')
try: try:
...@@ -40,7 +38,7 @@ class Middleware(object): ...@@ -40,7 +38,7 @@ class Middleware(object):
# See if we are able to view the course. If we are, redirect to it # See if we are able to view the course. If we are, redirect to it
try: 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') ) return redirect("/courses/" + course.id + "/wiki/" + path_match.group('wiki_path') )
except Http404: except Http404:
...@@ -55,14 +53,13 @@ class Middleware(object): ...@@ -55,14 +53,13 @@ class Middleware(object):
course_id = course_match.group('course_id') course_id = course_match.group('course_id')
# See if we are able to view the course. If we aren't, redirect to regular wiki # See if we are able to view the course. If we aren't, redirect to regular wiki
try: 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 # Good, we can see the course. Carry on
return None return None
except Http404: except Http404:
# We can't see the course, so redirect to the regular wiki # We can't see the course, so redirect to the regular wiki
return redirect("/wiki/" + course_match.group('wiki_path')) return redirect("/wiki/" + course_match.group('wiki_path'))
return None return None
def context_processor(request): def context_processor(request):
...@@ -79,7 +76,7 @@ def context_processor(request): ...@@ -79,7 +76,7 @@ def context_processor(request):
course_id = match.group('course_id') course_id = match.group('course_id')
try: try:
course = check_course(request.user, course_id) course = get_course_with_access(request.user, course_id, 'load')
return {'course' : course} return {'course' : course}
except Http404: except Http404:
# We couldn't access the course for whatever reason. It is too late to change # We couldn't access the course for whatever reason. It is too late to change
......
...@@ -34,11 +34,13 @@ class WikiRedirectTestCase(PageLoader): ...@@ -34,11 +34,13 @@ class WikiRedirectTestCase(PageLoader):
def test_wiki_redirect(self): 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 to /wiki/some/fake/wiki/page/ will redirect to
/courses/edX/toy/2012_Fall/wiki/some/fake/wiki/page/ /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/ will be redirected to /wiki/some/fake/wiki/page/
""" """
...@@ -57,16 +59,61 @@ class WikiRedirectTestCase(PageLoader): ...@@ -57,16 +59,61 @@ class WikiRedirectTestCase(PageLoader):
self.assertEqual(resp['Location'], 'http://testserver' + redirected_to ) 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 # 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 the same # 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) resp = self.client.get( bad_course_wiki_page, HTTP_REFERER=referer)
print "redirected_to" , redirected_to
self.assertEqual(resp.status_code, 302) self.assertEqual(resp.status_code, 302)
self.assertEqual(resp['Location'], 'http://testserver' + destination ) 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 ...@@ -5,7 +5,7 @@ from django.shortcuts import redirect
from wiki.core.exceptions import NoRootURL from wiki.core.exceptions import NoRootURL
from wiki.models import URLPath, Article from wiki.models import URLPath, Article
from courseware.courses import check_course from courseware.courses import get_course_by_id
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -25,7 +25,7 @@ def course_wiki_redirect(request, course_id): ...@@ -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 as it's home page. A course's wiki must be an article on the root (for
example, "/6.002x") to keep things simple. 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 course_slug = course.wiki_slug
......
...@@ -187,7 +187,7 @@ class PageLoader(ActivateLoginTestCase): ...@@ -187,7 +187,7 @@ class PageLoader(ActivateLoginTestCase):
def unenroll(self, course): def unenroll(self, course):
"""Unenroll the currently logged-in user, and check that it worked.""" """Unenroll the currently logged-in user, and check that it worked."""
resp = self.client.post('/change_enrollment', { resp = self.client.post('/change_enrollment', {
'enrollment_action': 'enroll', 'enrollment_action': 'unenroll',
'course_id': course.id, 'course_id': course.id,
}) })
data = parse_json(resp) data = parse_json(resp)
......
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