Passed
Push — master ( 3684b3...aca5bb )
by Peter
02:15 queued 40s
created

Teacher.test_mail_view_students()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
'''
2
    Test cases for teacher 'GET' views and actions.
3
    They include all cases for tutors by inheritance.
4
'''
5
6
import io
7
import zipfile
8
9
from django.contrib.auth.models import User
10
11
from opensubmit.models import Submission
12
from .cases import SubmitTeacherTestCase
13
from .helpers.course import create_course
14
from .helpers.submission import create_validated_submission
15
from .helpers.submission import create_validatable_submission
16
from .helpers.assignment import create_pass_fail_grading
17
from .helpers.assignment import create_all_assignments
18
from .helpers.assignment import create_open_file_assignment
19
from .helpers.assignment import get_pass_grading
20
from .helpers.djangofiles import create_submission_file
21
from .helpers.user import create_user, get_student_dict
22
23
24
class Teacher(SubmitTeacherTestCase):
25
    def setUp(self):
26
        super(Teacher, self).setUp()
27
28
        # Prepare assignments
29
        self.course = create_course(self.user)
30
        self.student = create_user(get_student_dict(1))
31
        self.course.participants.add(self.student.profile)
32
        self.course.save()
33
        self.grading_scheme = create_pass_fail_grading()
34
        self.all_assignments = create_all_assignments(
35
            self.course, self.grading_scheme)
36
        self.assignment = create_open_file_assignment(
37
            self.course, self.grading_scheme)
38
39
    def test_new_assignment_view(self):
40
        response = self.c.get('/teacher/opensubmit/assignment/add/')
41
        self.assertEqual(response.status_code, 200)
42
43
    def test_edit_assignment_view(self):
44
        for ass in self.all_assignments:
45
            response = self.c.get(
46
                '/teacher/opensubmit/assignment/%u/change/' % ass.pk)
47
            self.assertEqual(response.status_code, 200)
48
49
    def test_grading_scheme_list_view(self):
50
        response = self.c.get('/teacher/opensubmit/gradingscheme/')
51
        self.assertEqual(response.status_code, 200)
52
53
    def test_grading_list_view(self):
54
        response = self.c.get('/teacher/opensubmit/grading/')
55
        self.assertEqual(response.status_code, 200)
56
57
    def test_assignment_list_view(self):
58
        response = self.c.get('/teacher/opensubmit/assignment/')
59
        self.assertEqual(response.status_code, 200)
60
61
    def test_course_list_view(self):
62
        response = self.c.get('/teacher/opensubmit/course/')
63
        self.assertEqual(response.status_code, 200)
64
65
    def test_grading_table_view(self):
66
        sub = create_validated_submission(self.user, self.assignment)
67
        sub.grading = get_pass_grading(self.grading_scheme)
68
        sub.state = Submission.CLOSED
69
        sub.save()
70
        response = self.c.get('/course/%u/gradingtable/' % self.course.pk)
71
        self.assertEqual(response.status_code, 200)
72
73
    def test_submission_file_list_view(self):
74
        response = self.c.get('/teacher/opensubmit/submissionfile/')
75
        self.assertEqual(response.status_code, 200)
76
77
    def test_mail_view_course(self):
78
        response = self.c.get('/mail/course=%u' % self.course.pk)
79
        self.assertEqual(response.status_code, 200)
80
81
    def test_mail_view_students(self):
82
        response = self.c.get('/mail/students=%u' % self.student.pk)
83
        self.assertEqual(response.status_code, 200)
84
85
    def test_mail_sending(self):
86
        # Mail form rendering stores the receiver data in the session
87
        s = self.c.session
88
        s['mail_receivers'] = [{'first_name': self.student.first_name,
89
                                'last_name': self.student.last_name,
90
                                'email': self.student.email}]
91
        s.save()
92
        # Submitted mail form adds subject and text as POST data
93
        # Leads to preview screen, where we start here
94
        response = self.c.post('/mail/preview/', {'subject': 'Foo', 'message': 'bar'})
95
        self.assertEqual(response.status_code, 200)
96
        # POST without parameters leads to sending of data stored in session
97
        # Expect redirect to overview
98
        response = self.c.post('/mail/send/')
99
        self.assertEqual(response.status_code, 302)
100
101
    def test_perf_data_view(self):
102
        sub1 = create_validated_submission(self.user, self.assignment)
103
        sub2 = create_validated_submission(self.user, self.assignment)
104
        response = self.c.get('/assignments/%u/perftable/' %
105
                              sub1.assignment.pk)
106
        # Resulting CSV should have header line + 2 result lines + empty final line
107
        csv = response.content.decode(response.charset)
108
        self.assertEqual(response.status_code, 200)
109
        # content type
110
        self.assertIn('text/csv', response['Content-Type'])
111
        self.assertEqual(len(csv.split('\n')), 3 + 1)
112
113
    def test_course_archive_view(self):
114
        # add some student upload to be stored in the archive
115
        create_validated_submission(self.user, self.assignment)
116
        response = self.c.get('/course/%u/archive/' % self.course.pk)
117
        self.assertEqual(response.status_code, 200)
118
        # Test if the download is really a ZIP file
119
        f = io.BytesIO(response.content)
120
        zipped_file = zipfile.ZipFile(f, 'r')
121
        try:
122
            # Check ZIP file validity
123
            self.assertIsNone(zipped_file.testzip())
124
            # Try to find a file some student stored in a
125
            # sub-folder on it's own, targets #18
126
            found_stud_subfile = False
127
            for entry in zipped_file.filelist:
128
                if "subdir" in entry.filename:
129
                    found_stud_subfile = True
130
            assert(found_stud_subfile)
131
        finally:
132
            zipped_file.close()
133
            f.close()
134
135
    def test_course_archive_with_non_zip_view(self):
136
        # add some student upload to be stored in the archive
137
        non_zip = create_submission_file(
138
            relpath="/submfiles/validation/1000fff/helloworld.c")
139
        create_validatable_submission(
140
            self.user, self.assignment, non_zip)
141
        response = self.c.get('/course/%u/archive/' % self.course.pk)
142
        self.assertEqual(response.status_code, 200)
143
144
    def test_assignment_archive_view(self):
145
        # add some student upload to be stored in the archive
146
        zip_file = create_submission_file(
147
            relpath="/submfiles/validation/1000tff/packed.zip")
148
        create_validatable_submission(
149
            self.user, self.assignment, zip_file)
150
        response = self.c.get('/assignments/%u/archive/' %
151
                              self.assignment.pk)
152
        self.assertEqual(response.status_code, 200)
153
        # Test if the download is really a ZIP file
154
        f = io.BytesIO(response.content)
155
        zipped_file = zipfile.ZipFile(f, 'r')
156
        try:
157
            # Check ZIP file validity
158
            self.assertIsNone(zipped_file.testzip())
159
        finally:
160
            zipped_file.close()
161
            f.close()
162
163
    def test_assignment_archive_with_non_zip_view(self):
164
        # add some student upload to be stored in the archive
165
        non_zip = create_submission_file(
166
            relpath="/submfiles/validation/1000fff/helloworld.c")
167
        create_validatable_submission(
168
            self.user, self.assignment, non_zip)
169
        response = self.c.get('/assignments/%u/archive/' %
170
                              self.assignment.pk)
171
        self.assertEqual(response.status_code, 200)
172
173
    def test_change_course_owner_signal_handler(self):
174
        # Get a course with some owner
175
        # Assign new owner who had no backend rights before
176
        new_owner = User(username='foo')
177
        new_owner.save()
178
        assert(not new_owner.is_staff)
179
        old_owner_name = self.course.owner.username
180
        self.course.owner = new_owner
181
        self.course.save()
182
        # Make sure the old one has no more rights
183
        old_owner = User.objects.get(username=old_owner_name)
184
        assert(not old_owner.is_staff)
185
        # Make sure the new one has now backend rights
186
        new_owner = User.objects.get(username='foo')
187
        assert(new_owner.is_staff)
188