test_views.py 3.96 KB
Newer Older
1 2 3
"""
Tests for class dashboard (Metrics tab in instructor dashboard)
"""
4 5
import json

6
from django.test.client import RequestFactory
7
from mock import patch
8
from nose.plugins.attrib import attr
9 10

from class_dashboard import views
11
from student.tests.factories import AdminFactory
12 13
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory
14 15


16
@attr(shard=1)
17
class TestViews(ModuleStoreTestCase):
18 19 20 21 22
    """
    Tests related to class_dashboard/views.py
    """

    def setUp(self):
23
        super(TestViews, self).setUp()
24 25 26 27 28 29 30 31 32 33 34 35 36 37

        self.request_factory = RequestFactory()
        self.request = self.request_factory.get('')
        self.request.user = None
        self.simple_data = {'error': 'error'}

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_problem_grade_distribution_has_access(self, has_access):
        """
        Test returns proper value when have proper access
        """
        has_access.return_value = True
        response = views.all_problem_grade_distribution(self.request, 'test/test/test')

38
        self.assertEqual(json.dumps(self.simple_data), response.content)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_problem_grade_distribution_no_access(self, has_access):
        """
        Test for no access
        """
        has_access.return_value = False
        response = views.all_problem_grade_distribution(self.request, 'test/test/test')

        self.assertEqual("{\"error\": \"Access Denied: User does not have access to this course\'s data\"}", response.content)

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_sequential_open_distribution_has_access(self, has_access):
        """
        Test returns proper value when have proper access
        """
        has_access.return_value = True
        response = views.all_sequential_open_distrib(self.request, 'test/test/test')

58
        self.assertEqual(json.dumps(self.simple_data), response.content)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_sequential_open_distribution_no_access(self, has_access):
        """
        Test for no access
        """
        has_access.return_value = False
        response = views.all_sequential_open_distrib(self.request, 'test/test/test')

        self.assertEqual("{\"error\": \"Access Denied: User does not have access to this course\'s data\"}", response.content)

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_section_problem_grade_distribution_has_access(self, has_access):
        """
        Test returns proper value when have proper access
        """
        has_access.return_value = True
        response = views.section_problem_grade_distrib(self.request, 'test/test/test', '1')

78
        self.assertEqual(json.dumps(self.simple_data), response.content)
79 80 81 82 83 84 85 86 87 88

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_section_problem_grade_distribution_no_access(self, has_access):
        """
        Test for no access
        """
        has_access.return_value = False
        response = views.section_problem_grade_distrib(self.request, 'test/test/test', '1')

        self.assertEqual("{\"error\": \"Access Denied: User does not have access to this course\'s data\"}", response.content)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

    def test_sending_deprecated_id(self):

        course = CourseFactory.create()
        instructor = AdminFactory.create()
        self.request.user = instructor

        response = views.all_sequential_open_distrib(self.request, course.id.to_deprecated_string())
        self.assertEqual('[]', response.content)

        response = views.all_problem_grade_distribution(self.request, course.id.to_deprecated_string())
        self.assertEqual('[]', response.content)

        response = views.section_problem_grade_distrib(self.request, course.id.to_deprecated_string(), 'no section')
        self.assertEqual('{"error": "error"}', response.content)