StudentWithdraw.test_can_withdraw()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
1
'''
2
    Tets cases focusing on the frontend withdraw operations of students.
3
'''
4
5
from opensubmit.models import Submission
6
from opensubmit.tests.cases import SubmitStudentScenarioTestCase
7
from .helpers.user import get_student_dict
8
from .helpers.submission import create_submission
9
10
11
class StudentWithdraw(SubmitStudentScenarioTestCase):
12
13
    def test_can_withdraw(self):
14
        self.create_submissions()
15
        cases = {
16
            self.open_assignment_sub: (True, (200, 302, )),
17
            self.soft_deadline_passed_assignment_sub: (True, (200, 302, )),
18
            self.hard_deadline_passed_assignment_sub: (False, (403, )),
19
        }
20
        for submission in cases:
21
            response = self.c.post('/withdraw/%s/'
22
                                   % submission.pk, {'confirm': '1', })
23
            expect_submission_withdrawn, expected_responses = cases[submission]
24
            self.assertIn(response.status_code, expected_responses)
25
26
            submission = Submission.objects.get(pk__exact=submission.pk)
27
            if expect_submission_withdrawn:
28
                self.assertEqual(submission.state,
29
                                 Submission.WITHDRAWN, submission)
30
            else:
31
                self.assertNotEqual(
32
                    submission.state, Submission.WITHDRAWN, submission)
33
34
    def test_cannot_withdraw_other_users(self):
35
        '''
36
            Create submissions as one user and check
37
            that another user cannot withdraw them.
38
        '''
39
        self.create_submissions()
40
41
        self.create_and_login_user(get_student_dict(1))
42
        cases = {
43
            self.open_assignment_sub: 403,
44
            self.soft_deadline_passed_assignment_sub: 403,
45
            self.hard_deadline_passed_assignment_sub: 403,
46
        }
47
        for submission in cases:
48
            response = self.c.post('/withdraw/%s/' %
49
                                   submission.pk, {'confirm': '1', })
50
            self.assertEqual(response.status_code, cases[submission])
51
            submission = Submission.objects.get(pk__exact=submission.pk)
52
            self.assertNotEqual(submission.state, Submission.WITHDRAWN)
53
54
    def test_cannot_withdraw_graded(self):
55
        for state in (Submission.GRADED,
56
                      Submission.CLOSED,
57
                      Submission.CLOSED_TEST_FULL_PENDING):
58
            sub = create_submission(self.user, self.open_assignment)
59
            sub.state = state
60
            sub.save()
61
62
            response = self.c.post('/withdraw/%s/' %
63
                                   sub.pk, {'confirm': '1', })
64
            self.assertIn(response.status_code, (403, ))
65
            sub = Submission.objects.get(pk__exact=sub.pk)
66
            self.assertNotEqual(sub.state, Submission.WITHDRAWN)
67
68
    def testCanWithdrawSubmission(self):
69
        self.create_submissions()
70
        self.assertEqual(
71
            self.open_assignment_sub.can_withdraw(self.user), True)
72
        self.assertEqual(
73
            self.soft_deadline_passed_assignment_sub.can_withdraw(
74
                self.user), True)
75
76
    def testCannotWithdrawSubmissionAfterDeadline(self):
77
        self.create_submissions()
78
        self.assertEqual(
79
            self.hard_deadline_passed_assignment_sub.can_withdraw(
80
                self.user), False)
81