Learners want to have the usual course navigation when viewing a wiki, so that they can go back to the course related to the wiki and browse other tabs/sections of the course. Wiki reads the course from the `request.course`. If it's not present, i.e. None or not set on the request, it will not show the course navigation UI. It seems like `WikiAccessMiddleware` already has the code that parses course id from the request (when the request is for a wiki view) and sets the course for the request. However, it doesn't work in most scenarios, because the course id is not in the it's normal format in most requests that go to wiki. For example, when a leaner clicks on a wiki tab from the course overview, they are redirected to `/wiki/<wiki_slug>/` path. The wiki slug is taken from course's `wiki_slug` field. This slug can be used to figure out what course this wiki belongs to in most (not all) cases. This commit adds code to the `WikiAccessMiddleware` that attempts to find a course based on wiki slug, and in case of success, sets the course to the `request.course`, so that wiki can display course navigation UI.
44 lines
1.9 KiB
Python
44 lines
1.9 KiB
Python
"""
|
|
Tests for wiki middleware.
|
|
"""
|
|
|
|
|
|
from django.test.client import Client
|
|
from wiki.models import URLPath
|
|
from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE, ModuleStoreTestCase
|
|
from xmodule.modulestore.tests.factories import CourseFactory
|
|
|
|
from common.djangoapps.student.tests.factories import InstructorFactory
|
|
from lms.djangoapps.course_wiki.views import get_or_create_root
|
|
|
|
|
|
class TestWikiAccessMiddleware(ModuleStoreTestCase):
|
|
"""Tests for WikiAccessMiddleware."""
|
|
MODULESTORE = TEST_DATA_SPLIT_MODULESTORE
|
|
|
|
def setUp(self):
|
|
"""Test setup."""
|
|
super().setUp()
|
|
|
|
self.wiki = get_or_create_root()
|
|
|
|
self.course_math101 = CourseFactory.create(org='edx', number='math101', display_name='2014')
|
|
self.course_math101_instructor = InstructorFactory(course_key=self.course_math101.id, username='instructor', password='secret') # lint-amnesty, pylint: disable=line-too-long
|
|
self.wiki_math101 = URLPath.create_article(self.wiki, 'math101', title='math101')
|
|
|
|
self.client = Client()
|
|
self.client.login(username='instructor', password='secret')
|
|
|
|
def test_url_tranform(self):
|
|
"""Test that the correct prefix ('/courses/<course_id>') is added to the urls in the wiki."""
|
|
response = self.client.get('/courses/course-v1:edx+math101+2014/wiki/math101/')
|
|
self.assertContains(response, '/courses/course-v1:edx+math101+2014/wiki/math101/_edit/')
|
|
self.assertContains(response, '/courses/course-v1:edx+math101+2014/wiki/math101/_settings/')
|
|
|
|
def test_finds_course_by_wiki_slug(self):
|
|
"""Test that finds course by wiki slug, if course id is not present in the url."""
|
|
response = self.client.get('/wiki/math101/')
|
|
request = response.wsgi_request
|
|
self.assertTrue(hasattr(request, 'course'))
|
|
self.assertEqual(request.course.id, self.course_math101.id)
|