|
| 1 | +from django.urls import reverse |
| 2 | +from django_email_learning.models import ( |
| 3 | + AssignmentSubmission, |
| 4 | + ContentDelivery, |
| 5 | + Enrollment, |
| 6 | + Learner, |
| 7 | +) |
| 8 | +import pytest |
| 9 | +import uuid |
| 10 | + |
| 11 | + |
| 12 | +def get_url(organization_id: int, course_id: int) -> str: |
| 13 | + return reverse( |
| 14 | + "django_email_learning:api_platform:submitted_assignments_view", |
| 15 | + kwargs={"organization_id": organization_id, "course_id": course_id}, |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.parametrize( |
| 20 | + "client,expected_status", |
| 21 | + [ |
| 22 | + ("org_admin", 200), |
| 23 | + ("instructor", 200), |
| 24 | + ("editor", 403), |
| 25 | + ("viewer", 403), |
| 26 | + ("anonymous", 401), |
| 27 | + ], |
| 28 | + indirect=["client"], |
| 29 | +) |
| 30 | +def test_submitted_assignments_view_get_role_access( |
| 31 | + client, expected_status, course_assignment_content, enrollment |
| 32 | +): |
| 33 | + delivery = ContentDelivery.objects.create( |
| 34 | + enrollment=enrollment, |
| 35 | + course_content=course_assignment_content, |
| 36 | + ) |
| 37 | + AssignmentSubmission.objects.create( |
| 38 | + delivery=delivery, |
| 39 | + text_submission="My answer", |
| 40 | + ) |
| 41 | + |
| 42 | + response = client.get( |
| 43 | + get_url( |
| 44 | + organization_id=course_assignment_content.course.organization_id, |
| 45 | + course_id=course_assignment_content.course_id, |
| 46 | + ) |
| 47 | + ) |
| 48 | + |
| 49 | + assert response.status_code == expected_status |
| 50 | + |
| 51 | + |
| 52 | +def test_submitted_assignments_view_returns_expected_list_fields( |
| 53 | + org_admin_client, course_assignment_content, enrollment |
| 54 | +): |
| 55 | + delivery = ContentDelivery.objects.create( |
| 56 | + enrollment=enrollment, |
| 57 | + course_content=course_assignment_content, |
| 58 | + ) |
| 59 | + submission = AssignmentSubmission.objects.create( |
| 60 | + delivery=delivery, |
| 61 | + text_submission="My answer", |
| 62 | + ) |
| 63 | + |
| 64 | + response = org_admin_client.get( |
| 65 | + get_url( |
| 66 | + organization_id=course_assignment_content.course.organization_id, |
| 67 | + course_id=course_assignment_content.course_id, |
| 68 | + ) |
| 69 | + ) |
| 70 | + |
| 71 | + assert response.status_code == 200 |
| 72 | + payload = response.json() |
| 73 | + assert "submissions" in payload |
| 74 | + assert len(payload["submissions"]) == 1 |
| 75 | + |
| 76 | + item = payload["submissions"][0] |
| 77 | + assert set(item.keys()) == { |
| 78 | + "id", |
| 79 | + "assignment_title", |
| 80 | + "submitted_at", |
| 81 | + "status", |
| 82 | + "reviewed_at", |
| 83 | + "reviewed_by", |
| 84 | + } |
| 85 | + assert item["id"] == submission.id |
| 86 | + assert item["assignment_title"] == course_assignment_content.assignment.title |
| 87 | + assert item["status"] == AssignmentSubmission.SubmissionStatus.PENDING_REVIEW |
| 88 | + assert item["reviewed_at"] is None |
| 89 | + assert item["reviewed_by"] is None |
| 90 | + |
| 91 | + |
| 92 | +def test_submitted_assignments_view_filters_by_status( |
| 93 | + org_admin_client, course_assignment_content, enrollment |
| 94 | +): |
| 95 | + delivery_approved = ContentDelivery.objects.create( |
| 96 | + enrollment=enrollment, |
| 97 | + course_content=course_assignment_content, |
| 98 | + ) |
| 99 | + approved_submission = AssignmentSubmission.objects.create( |
| 100 | + delivery=delivery_approved, |
| 101 | + text_submission="Approved answer", |
| 102 | + status=AssignmentSubmission.SubmissionStatus.APPROVED, |
| 103 | + ) |
| 104 | + |
| 105 | + second_learner = Learner.objects.create( |
| 106 | + email=f"{uuid.uuid4().hex}@example.com", |
| 107 | + organization_id=course_assignment_content.course.organization_id, |
| 108 | + ) |
| 109 | + second_enrollment = Enrollment.objects.create( |
| 110 | + learner=second_learner, |
| 111 | + course=course_assignment_content.course, |
| 112 | + ) |
| 113 | + delivery_pending = ContentDelivery.objects.create( |
| 114 | + enrollment=second_enrollment, |
| 115 | + course_content=course_assignment_content, |
| 116 | + ) |
| 117 | + AssignmentSubmission.objects.create( |
| 118 | + delivery=delivery_pending, |
| 119 | + text_submission="Pending answer", |
| 120 | + status=AssignmentSubmission.SubmissionStatus.PENDING_REVIEW, |
| 121 | + ) |
| 122 | + |
| 123 | + response_without_status = org_admin_client.get( |
| 124 | + get_url( |
| 125 | + organization_id=course_assignment_content.course.organization_id, |
| 126 | + course_id=course_assignment_content.course_id, |
| 127 | + ) |
| 128 | + ) |
| 129 | + assert response_without_status.status_code == 200 |
| 130 | + assert len(response_without_status.json()["submissions"]) == 2 |
| 131 | + |
| 132 | + response = org_admin_client.get( |
| 133 | + get_url( |
| 134 | + organization_id=course_assignment_content.course.organization_id, |
| 135 | + course_id=course_assignment_content.course_id, |
| 136 | + ), |
| 137 | + {"status": AssignmentSubmission.SubmissionStatus.APPROVED}, |
| 138 | + ) |
| 139 | + |
| 140 | + assert response.status_code == 200 |
| 141 | + submissions = response.json()["submissions"] |
| 142 | + assert len(submissions) == 1 |
| 143 | + assert submissions[0]["id"] == approved_submission.id |
| 144 | + assert submissions[0]["status"] == AssignmentSubmission.SubmissionStatus.APPROVED |
| 145 | + |
| 146 | + |
| 147 | +def test_submitted_assignments_view_filters_by_learner_id( |
| 148 | + org_admin_client, course_assignment_content, enrollment |
| 149 | +): |
| 150 | + delivery_first = ContentDelivery.objects.create( |
| 151 | + enrollment=enrollment, |
| 152 | + course_content=course_assignment_content, |
| 153 | + ) |
| 154 | + first_submission = AssignmentSubmission.objects.create( |
| 155 | + delivery=delivery_first, |
| 156 | + text_submission="First learner answer", |
| 157 | + ) |
| 158 | + |
| 159 | + second_learner = Learner.objects.create( |
| 160 | + email=f"{uuid.uuid4().hex}@example.com", |
| 161 | + organization_id=course_assignment_content.course.organization_id, |
| 162 | + ) |
| 163 | + second_enrollment = Enrollment.objects.create( |
| 164 | + learner=second_learner, |
| 165 | + course=course_assignment_content.course, |
| 166 | + ) |
| 167 | + delivery_second = ContentDelivery.objects.create( |
| 168 | + enrollment=second_enrollment, |
| 169 | + course_content=course_assignment_content, |
| 170 | + ) |
| 171 | + AssignmentSubmission.objects.create( |
| 172 | + delivery=delivery_second, |
| 173 | + text_submission="Second learner answer", |
| 174 | + ) |
| 175 | + |
| 176 | + response_without_learner_id = org_admin_client.get( |
| 177 | + get_url( |
| 178 | + organization_id=course_assignment_content.course.organization_id, |
| 179 | + course_id=course_assignment_content.course_id, |
| 180 | + ) |
| 181 | + ) |
| 182 | + assert response_without_learner_id.status_code == 200 |
| 183 | + assert len(response_without_learner_id.json()["submissions"]) == 2 |
| 184 | + |
| 185 | + response = org_admin_client.get( |
| 186 | + get_url( |
| 187 | + organization_id=course_assignment_content.course.organization_id, |
| 188 | + course_id=course_assignment_content.course_id, |
| 189 | + ), |
| 190 | + {"learner_id": enrollment.learner.id}, |
| 191 | + ) |
| 192 | + |
| 193 | + assert response.status_code == 200 |
| 194 | + submissions = response.json()["submissions"] |
| 195 | + assert len(submissions) == 1 |
| 196 | + assert submissions[0]["id"] == first_submission.id |
0 commit comments