92 lines
3.2 KiB
Python
92 lines
3.2 KiB
Python
import json
|
|
|
|
from rest_framework.test import APITestCase
|
|
|
|
from vbv_lernwelt.assignment.models import Assignment, AssignmentCompletion
|
|
from vbv_lernwelt.core.create_default_users import create_default_users
|
|
from vbv_lernwelt.core.models import User
|
|
from vbv_lernwelt.core.utils import find_first
|
|
from vbv_lernwelt.course.consts import COURSE_TEST_ID
|
|
from vbv_lernwelt.course.creators.test_course import create_test_course
|
|
from vbv_lernwelt.course.models import CourseSession, CourseSessionUser
|
|
|
|
|
|
class AssignmentApiStudentTestCase(APITestCase):
|
|
def setUp(self) -> None:
|
|
create_default_users()
|
|
create_test_course(include_vv=False)
|
|
|
|
self.assignment = Assignment.objects.first()
|
|
self.assignment_subtasks = self.assignment.filter_user_subtasks()
|
|
|
|
self.cs = CourseSession.objects.create(
|
|
course_id=COURSE_TEST_ID,
|
|
title="Test Lehrgang Session",
|
|
)
|
|
self.user = User.objects.get(username="student")
|
|
csu = CourseSessionUser.objects.create(
|
|
course_session=self.cs,
|
|
user=self.user,
|
|
)
|
|
self.client.login(username="student", password="test")
|
|
|
|
def test_can_updateAssignmentCompletion_asStudent(self):
|
|
url = f"/api/assignment/update/"
|
|
|
|
user_text_input = find_first(
|
|
self.assignment_subtasks, pred=lambda x: x["type"] == "user_text_input"
|
|
)
|
|
|
|
response = self.client.post(
|
|
url,
|
|
{
|
|
"assignment_id": self.assignment.id,
|
|
"course_session_id": self.cs.id,
|
|
"completion_status": "in_progress",
|
|
"completion_data": {
|
|
user_text_input["id"]: {"user_data": {"text": "Hallo via API"}},
|
|
},
|
|
},
|
|
format="json",
|
|
)
|
|
response_json = response.json()
|
|
print(json.dumps(response.json(), indent=2))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertEqual(response_json["user"], self.user.id)
|
|
self.assertEqual(response_json["assignment"], self.assignment.id)
|
|
self.assertEqual(response_json["completion_status"], "in_progress")
|
|
self.assertDictEqual(
|
|
response_json["completion_data"],
|
|
{
|
|
user_text_input["id"]: {"user_data": {"text": "Hallo via API"}},
|
|
},
|
|
)
|
|
|
|
db_entry = AssignmentCompletion.objects.get(
|
|
user=self.user,
|
|
course_session_id=self.cs.id,
|
|
assignment_id=self.assignment.id,
|
|
)
|
|
self.assertEqual(db_entry.completion_status, "in_progress")
|
|
self.assertDictEqual(
|
|
db_entry.completion_data,
|
|
{
|
|
user_text_input["id"]: {"user_data": {"text": "Hallo via API"}},
|
|
},
|
|
)
|
|
|
|
# read data via request api
|
|
response = self.client.get(
|
|
f"/api/assignment/{self.assignment.id}/{self.cs.id}/",
|
|
format="json",
|
|
)
|
|
response_json = response.json()
|
|
print(json.dumps(response.json(), indent=2))
|
|
self.assertDictEqual(
|
|
response_json["completion_data"],
|
|
{
|
|
user_text_input["id"]: {"user_data": {"text": "Hallo via API"}},
|
|
},
|
|
)
|