""" test views """ import datetime import json import re import pytz from mock import patch from nose.plugins.attrib import attr from capa.tests.response_xml_factory import StringResponseXMLFactory from courseware.field_overrides import OverrideFieldData # pylint: disable=import-error from courseware.tests.factories import StudentModuleFactory # pylint: disable=import-error from courseware.tests.helpers import LoginEnrollmentTestCase # pylint: disable=import-error from django.core.urlresolvers import reverse from django.test.utils import override_settings from edxmako.shortcuts import render_to_response # pylint: disable=import-error from student.roles import CourseCcxCoachRole # pylint: disable=import-error from student.tests.factories import ( # pylint: disable=import-error AdminFactory, CourseEnrollmentFactory, UserFactory, ) from xmodule.x_module import XModuleMixin from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import ( CourseFactory, ItemFactory, ) from ..models import ( CustomCourseForEdX, CcxMembership, CcxFutureMembership, ) from ..overrides import get_override_for_ccx, override_field_for_ccx from .. import ACTIVE_CCX_KEY from .factories import ( CcxFactory, CcxMembershipFactory, CcxFutureMembershipFactory, ) def intercept_renderer(path, context): """ Intercept calls to `render_to_response` and attach the context dict to the response for examination in unit tests. """ # I think Django already does this for you in their TestClient, except # we're bypassing that by using edxmako. Probably edxmako should be # integrated better with Django's rendering and event system. response = render_to_response(path, context) response.mako_context = context response.mako_template = path return response @attr('shard_1') class TestCoachDashboard(ModuleStoreTestCase, LoginEnrollmentTestCase): """ Tests for Custom Courses views. """ def setUp(self): """ Set up tests """ super(TestCoachDashboard, self).setUp() self.course = course = CourseFactory.create() # Create instructor account self.coach = coach = AdminFactory.create() self.client.login(username=coach.username, password="test") # Create a course outline self.mooc_start = start = datetime.datetime( 2010, 5, 12, 2, 42, tzinfo=pytz.UTC) self.mooc_due = due = datetime.datetime( 2010, 7, 7, 0, 0, tzinfo=pytz.UTC) chapters = [ItemFactory.create(start=start, parent=course) for _ in xrange(2)] sequentials = flatten([ [ ItemFactory.create(parent=chapter) for _ in xrange(2) ] for chapter in chapters ]) verticals = flatten([ [ ItemFactory.create( due=due, parent=sequential, graded=True, format='Homework' ) for _ in xrange(2) ] for sequential in sequentials ]) blocks = flatten([ # pylint: disable=unused-variable [ ItemFactory.create(parent=vertical) for _ in xrange(2) ] for vertical in verticals ]) def make_coach(self): """ create coach user """ role = CourseCcxCoachRole(self.course.id) role.add_users(self.coach) def make_ccx(self): """ create ccx """ ccx = CcxFactory(course_id=self.course.id, coach=self.coach) return ccx def get_outbox(self): """ get fake outbox """ from django.core import mail return mail.outbox def test_not_a_coach(self): """ User is not a coach, should get Forbidden response. """ url = reverse( 'ccx_coach_dashboard', kwargs={'course_id': self.course.id.to_deprecated_string()}) response = self.client.get(url) self.assertEqual(response.status_code, 403) def test_no_ccx_created(self): """ No CCX is created, coach should see form to add a CCX. """ self.make_coach() url = reverse( 'ccx_coach_dashboard', kwargs={'course_id': self.course.id.to_deprecated_string()}) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue(re.search( '