test_masquerade.py 4.24 KB
Newer Older
ichuang committed
1 2 3 4 5 6 7
"""
Unit tests for masquerade

Based on (and depends on) unit tests for courseware.

Notes for running by hand:

8
./manage.py lms --settings test test lms/djangoapps/courseware
ichuang committed
9 10 11 12 13 14
"""

from django.test.utils import override_settings

from django.core.urlresolvers import reverse

15
from django.contrib.auth.models import Group, User
ichuang committed
16
from courseware.access import _course_staff_group_name
17
from courseware.tests.helpers import LoginEnrollmentTestCase
18
from modulestore_config import TEST_DATA_XML_MODULESTORE
ichuang committed
19 20 21 22
from xmodule.modulestore.django import modulestore
import xmodule.modulestore.django
import json

23

ichuang committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
@override_settings(MODULESTORE=TEST_DATA_XML_MODULESTORE)
class TestStaffMasqueradeAsStudent(LoginEnrollmentTestCase):
    '''
    Check for staff being able to masquerade as student
    '''

    def setUp(self):
        xmodule.modulestore.django._MODULESTORES = {}

        #self.full = modulestore().get_course("edX/full/6.002_Spring_2012")
        #self.toy = modulestore().get_course("edX/toy/2012_Fall")
        self.graded_course = modulestore().get_course("edX/graded/2012_Fall")

        # Create staff account
        self.instructor = 'view2@test.com'
        self.password = 'foo'
        self.create_account('u2', self.instructor, self.password)
        self.activate_user(self.instructor)

        def make_instructor(course):
            group_name = _course_staff_group_name(course.location)
            g = Group.objects.create(name=group_name)
46
            g.user_set.add(User.objects.get(email=self.instructor))
ichuang committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

        make_instructor(self.graded_course)

        self.logout()
        self.login(self.instructor, self.password)
        self.enroll(self.graded_course)
        # self.factory = RequestFactory()

    def get_cw_section(self):
        url = reverse('courseware_section',
                      kwargs={'course_id': self.graded_course.id,
                              'chapter': 'GradedChapter',
                              'section': 'Homework1'})

        resp = self.client.get(url)

        print "url ", url
        return resp

    def test_staff_debug_for_staff(self):
        resp = self.get_cw_section()
        sdebug = '<div><a href="#i4x_edX_graded_problem_H1P1_debug" id="i4x_edX_graded_problem_H1P1_trig">Staff Debug Info</a></div>'
69

ichuang committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        self.assertTrue(sdebug in resp.content)

    def toggle_masquerade(self):
        '''
        Toggle masquerade state
        '''
        masq_url = reverse('masquerade-switch', kwargs={'marg': 'toggle'})
        print "masq_url ", masq_url
        resp = self.client.get(masq_url)
        return resp

    def test_no_staff_debug_for_student(self):
        togresp = self.toggle_masquerade()
        print "masq now ", togresp.content
        self.assertEqual(togresp.content, '{"status": "student"}', '')

        resp = self.get_cw_section()
        sdebug = '<div><a href="#i4x_edX_graded_problem_H1P1_debug" id="i4x_edX_graded_problem_H1P1_trig">Staff Debug Info</a></div>'
88

ichuang committed
89
        self.assertFalse(sdebug in resp.content)
90

ichuang committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    def get_problem(self):
        pun = 'H1P1'
        problem_location = "i4x://edX/graded/problem/%s" % pun

        modx_url = reverse('modx_dispatch',
                           kwargs={'course_id': self.graded_course.id,
                                   'location': problem_location,
                                   'dispatch': 'problem_get', })

        resp = self.client.get(modx_url)

        print "modx_url ", modx_url
        return resp

    def test_showanswer_for_staff(self):
        resp = self.get_problem()
        html = json.loads(resp.content)['html']
        print html
109
        sabut = '<button class="show"><span class="show-label">Show Answer(s)</span> <span class="sr">(for question(s) above - adjacent to each field)</span></button>'
ichuang committed
110 111 112 113 114 115 116 117 118 119
        self.assertTrue(sabut in html)

    def test_no_showanswer_for_student(self):
        togresp = self.toggle_masquerade()
        print "masq now ", togresp.content
        self.assertEqual(togresp.content, '{"status": "student"}', '')

        resp = self.get_problem()
        html = json.loads(resp.content)['html']
        print html
120
        sabut = '<button class="show"><span class="show-label">Show Answer(s)</span> <span class="sr">(for question(s) above - adjacent to each field)</span></button>'
ichuang committed
121
        self.assertFalse(sabut in html)