test_legacy_anon_csv.py 2.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
"""
Unit tests for instructor dashboard

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

Notes for running by hand:

./manage.py lms --settings test test lms/djangoapps/instructor
"""

from django.test.utils import override_settings

# Need access to internal func to put users in the right group
14
from django.contrib.auth.models import User
15 16 17 18 19

from django.core.urlresolvers import reverse

from courseware.tests.helpers import LoginEnrollmentTestCase
from courseware.tests.modulestore_config import TEST_DATA_MIXED_MODULESTORE
20
import instructor.views.legacy
21
from student.roles import CourseStaffRole
22 23
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.django import modulestore, clear_existing_modulestores
24
from opaque_keys.edx.locations import SlashSeparatedCourseKey
25

26
from mock import Mock, patch
27 28 29 30 31 32 33 34 35 36 37


@override_settings(MODULESTORE=TEST_DATA_MIXED_MODULESTORE)
class TestInstructorDashboardAnonCSV(ModuleStoreTestCase, LoginEnrollmentTestCase):
    '''
    Check for download of csv
    '''

    # Note -- I copied this setUp from a similar test
    def setUp(self):
        clear_existing_modulestores()
38
        self.toy = modulestore().get_course(SlashSeparatedCourseKey("edX", "toy", "2012_Fall"))
39 40 41 42 43 44 45 46 47 48

        # Create two accounts
        self.student = 'view@test.com'
        self.instructor = 'view2@test.com'
        self.password = 'foo'
        self.create_account('u1', self.student, self.password)
        self.create_account('u2', self.instructor, self.password)
        self.activate_user(self.student)
        self.activate_user(self.instructor)

49
        CourseStaffRole(self.toy.id).add_users(User.objects.get(email=self.instructor))
50 51 52 53 54

        self.logout()
        self.login(self.instructor, self.password)
        self.enroll(self.toy)

55 56
    @patch.object(instructor.views.legacy, 'anonymous_id_for_user', Mock(return_value='42'))
    @patch.object(instructor.views.legacy, 'unique_id_for_user', Mock(return_value='41'))
57 58
    def test_download_anon_csv(self):
        course = self.toy
Calen Pennington committed
59
        url = reverse('instructor_dashboard_legacy', kwargs={'course_id': course.id.to_deprecated_string()})
60
        response = self.client.post(url, {'action': 'Download CSV of all student anonymized IDs'})
61 62 63

        self.assertEqual(response['Content-Type'], 'text/csv')
        body = response.content.replace('\r', '')
64 65
        self.assertEqual(
            body,
66
            ('"User ID","Anonymized User ID","Course Specific Anonymized User ID"'
67 68
             '\n"2","41","42"\n')
        )