From d45beaebface9f40a0d09e79c401d226eb8c723d Mon Sep 17 00:00:00 2001 From: Don Mitchell Date: Wed, 16 Oct 2013 13:45:48 -0400 Subject: [PATCH] Test the new RESTful course index and outline views. --- .../contentstore/tests/test_course_index.py | 43 +++++++++++++++++++ cms/djangoapps/contentstore/tests/utils.py | 18 ++++++++ 2 files changed, 61 insertions(+) create mode 100644 cms/djangoapps/contentstore/tests/test_course_index.py diff --git a/cms/djangoapps/contentstore/tests/test_course_index.py b/cms/djangoapps/contentstore/tests/test_course_index.py new file mode 100644 index 0000000000..f268eaf5f1 --- /dev/null +++ b/cms/djangoapps/contentstore/tests/test_course_index.py @@ -0,0 +1,43 @@ +""" +Unit tests for getting the list of courses and the course outline. +""" +from django.core.urlresolvers import reverse +import lxml + +from contentstore.tests.utils import CourseTestCase +from xmodule.modulestore.django import loc_mapper +from django.core.exceptions import PermissionDenied + +class TestCourseIndex(CourseTestCase): + """ + Unit tests for getting the list of courses and the course outline. + """ + def test_index(self): + """ + Test getting the list of courses and then pulling up their outlines + """ + index_url = reverse('contentstore.views.index') + index_response = self.client.get(index_url, {}, HTTP_ACCEPT='text/html') + parsed_html = lxml.html.fromstring(index_response.content) + course_link_eles = parsed_html.find_class('course-link') + for link in course_link_eles: + self.assertRegexpMatches(link.get("href"), r'course/\w+\.\w+\.\w+.*/branch/\w+/block/.*') + # now test that url + outline_response = self.client.get(link.get("href"), {}, HTTP_ACCEPT='text/html') + # ensure it has the expected 2 self referential links + outline_parsed = lxml.html.fromstring(outline_response.content) + outline_link = outline_parsed.find_class('course-link')[0] + self.assertEqual(outline_link.get("href"), link.get("href")) + course_menu_link = outline_parsed.find_class('nav-course-courseware-outline')[0] + self.assertEqual(course_menu_link.find("a").get("href"), link.get("href")) + + def test_negative_conditions(self): + """ + Test the error conditions for the access + """ + locator = loc_mapper().translate_location(self.course.location.course_id, self.course.location, False, True) + outline_url = reverse('contentstore.views.course_handler', kwargs={'course_url': unicode(locator)}) + # register a non-staff member and try to delete the course branch + non_staff_client = self.createNonStaffAuthedUserClient() + response = non_staff_client.delete(outline_url, {}, HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 403) diff --git a/cms/djangoapps/contentstore/tests/utils.py b/cms/djangoapps/contentstore/tests/utils.py index 8b3f4cf4b1..6a227d9a80 100644 --- a/cms/djangoapps/contentstore/tests/utils.py +++ b/cms/djangoapps/contentstore/tests/utils.py @@ -61,3 +61,21 @@ class CourseTestCase(ModuleStoreTestCase): number='999', display_name='Robot Super Course', ) + + def createNonStaffAuthedUserClient(self): + """ + Create a non-staff user, log them in, and return the client to use for testing. + """ + uname = 'teststudent' + password = 'foo' + nonstaff = User.objects.create_user(uname, 'test+student@edx.org', password) + + # Note that we do not actually need to do anything + # for registration if we directly mark them active. + nonstaff.is_active = True + nonstaff.is_staff = False + nonstaff.save() + + client = Client() + client.login(username=uname, password=password) + return client