from django.test import TestCase from django.urls import reverse from rest_framework.test import APITestCase from vbv_lernwelt.assignment.models import ( Assignment, AssignmentCompletion, AssignmentCompletionStatus, ) from vbv_lernwelt.core.constants import ( TEST_COURSE_SESSION_BERN_ID, TEST_COURSE_SESSION_ZURICH_ID, TEST_STUDENT1_USER_ID, TEST_STUDENT2_USER_ID, TEST_STUDENT3_USER_ID, TEST_SUPERVISOR1_USER_ID, TEST_TRAINER1_USER_ID, TEST_TRAINER2_USER_ID, ) from vbv_lernwelt.core.create_default_users import create_default_users from vbv_lernwelt.core.models import User from vbv_lernwelt.course.creators.test_course import create_test_course from vbv_lernwelt.course.creators.test_utils import ( add_course_session_group_supervisor, add_course_session_user, create_course, create_course_session, create_course_session_group, create_user, ) from vbv_lernwelt.course.models import Course, CourseSession, CourseSessionUser from vbv_lernwelt.dashboard.views import ( _get_course_sessions_with_roles_for_user, _get_mentee_count, _get_mentor_open_tasks_count, _get_permitted_circles_ids_for_user_and_course_session, get_course_config, get_course_sessions_with_roles_for_user, ) from vbv_lernwelt.learning_mentor.models import LearningMentor from vbv_lernwelt.learnpath.models import Circle, LearningUnit from vbv_lernwelt.self_evaluation_feedback.models import SelfEvaluationFeedback class GetCourseSessionsForUserTestCase(TestCase): def setUp(self): self.course, _ = create_course("Test Course") self.course_session = create_course_session( course=self.course, title="Test Session" ) def test_participant_get_sessions(self): # participant gets all his sessions marked with role "MEMBER" participant = create_user("participant") add_course_session_user( self.course_session, participant, role=CourseSessionUser.Role.MEMBER, ) # WHEN sessions = get_course_sessions_with_roles_for_user(participant) # THEN self.assertEqual(len(sessions), 1) self.assertEqual(sessions[0].title, "Test Session") self.assertSetEqual(sessions[0].roles, {"MEMBER"}) def test_trainer_get_sessions(self): # GIVEN # trainer gets all his sessions marked with role "EXPERT" trainer = create_user("trainer") add_course_session_user( self.course_session, trainer, role=CourseSessionUser.Role.EXPERT, ) # WHEN sessions = get_course_sessions_with_roles_for_user(trainer) # THEN self.assertEqual(len(sessions), 1) self.assertEqual(sessions[0].title, "Test Session") self.assertSetEqual(sessions[0].roles, {"EXPERT"}) def test_supervisor_get_sessions(self): supervisor = create_user("supervisor") group = create_course_session_group(course_session=self.course_session) add_course_session_group_supervisor(group=group, user=supervisor) sessions = get_course_sessions_with_roles_for_user(supervisor) # THEN self.assertEqual(len(sessions), 1) self.assertEqual(sessions[0].title, "Test Session") self.assertEqual(sessions[0].roles, {"SUPERVISOR"}) def test_learning_mentor_get_sessions(self): mentor = create_user("mentor") LearningMentor.objects.create(mentor=mentor, course_session=self.course_session) participant = create_user("participant") add_course_session_user( self.course_session, participant, role=CourseSessionUser.Role.MEMBER, ) sessions = get_course_sessions_with_roles_for_user(mentor) # THEN self.assertEqual(len(sessions), 1) self.assertEqual(sessions[0].title, "Test Session") self.assertEqual(sessions[0].roles, {"LEARNING_MENTOR"}) class GetDashboardConfig(TestCase): def setUp(self): self.course, _ = create_course("Test Course") self.course_session = create_course_session( course=self.course, title="Test Session" ) def _test_config(self, user, role, is_uk, is_vv, is_mentor, has_preview, widgets): # WHEN sessions = get_course_sessions_with_roles_for_user(user) course_configs = get_course_config(sessions) # THEN self.assertEqual(len(course_configs), 1) self.assertEqual(course_configs[0].course_title, self.course.title) self.assertEqual(course_configs[0].is_uk, is_uk) self.assertEqual(course_configs[0].is_vv, is_vv) self.assertEqual(course_configs[0].is_mentor, is_mentor) self.assertEqual(course_configs[0].has_preview, has_preview) self.assertEqual( course_configs[0].session_to_continue_id, str(self.course_session.id) ) self.assertEqual(course_configs[0].role_key, role) self.assertEqual(course_configs[0].widgets, widgets) def test_participant_uk_get_config(self): participant = create_user("participant") add_course_session_user( self.course_session, participant, role=CourseSessionUser.Role.MEMBER, ) self.course.configuration.is_uk = True self.course.configuration.save() self._test_config( user=participant, role="Member", is_uk=True, is_vv=False, is_mentor=False, has_preview=False, widgets=[ "ProgressWidget", "CompetenceWidget", "CompetenceCertificateWidget", ], ) def test_participant_vv_get_config(self): participant = create_user("participant") add_course_session_user( self.course_session, participant, role=CourseSessionUser.Role.MEMBER, ) self.course.configuration.is_vv = True self.course.configuration.save() self._test_config( user=participant, role="Member", is_uk=False, is_vv=True, is_mentor=False, has_preview=False, widgets=["ProgressWidget", "CompetenceWidget"], ) def test_mentor_uk_get_config(self): # GIVEN mentor = create_user("mentor") LearningMentor.objects.create(mentor=mentor, course_session=self.course_session) self.course.configuration.is_uk = True self.course.configuration.save() self._test_config( user=mentor, role="MentorUK", is_uk=True, is_vv=False, is_mentor=True, has_preview=True, widgets=["MentorPersonWidget", "MentorCompetenceWidget"], ) def test_mentor_vv_get_config(self): # GIVEN mentor = create_user("mentor") LearningMentor.objects.create(mentor=mentor, course_session=self.course_session) self.course.configuration.is_vv = True self.course.configuration.save() self._test_config( user=mentor, role="MentorVV", is_uk=False, is_vv=True, is_mentor=True, has_preview=True, widgets=["MentorPersonWidget", "MentorTasksWidget"], ) def test_participant_as_mentor_vv_get_config(self): # GIVEN mentor = create_user("mentor") add_course_session_user( self.course_session, mentor, role=CourseSessionUser.Role.MEMBER, ) LearningMentor.objects.create(mentor=mentor, course_session=self.course_session) self.course.configuration.is_vv = True self.course.configuration.save() self._test_config( user=mentor, role="Member", is_uk=False, is_vv=True, is_mentor=True, has_preview=False, widgets=[ "ProgressWidget", "CompetenceWidget", "MentorPersonWidget", "MentorTasksWidget", ], ) # test supervisor # test expert class GetMenteeCountTestCase(TestCase): def setUp(self): self.course, _ = create_course("Test Course") self.course_session = create_course_session( course=self.course, title="Test Session" ) def test_get_mentee_count(self): # GIVEN participants_with_mentor = [create_user(f"participant{i}") for i in range(2)] participant = create_user("participant") mentor = create_user("mentor") lm = LearningMentor.objects.create( mentor=mentor, course_session=self.course_session ) # WHEN for p in participants_with_mentor: csu = add_course_session_user( self.course_session, p, role=CourseSessionUser.Role.MEMBER, ) lm.participants.add(csu) add_course_session_user( self.course_session, participant, role=CourseSessionUser.Role.MEMBER, ) # THEN count = _get_mentee_count(str(self.course.id), mentor) self.assertEqual(count, 2) class BaseMentorAssignmentTestCase(TestCase): def setUp(self): create_default_users() create_test_course(include_vv=False, with_sessions=True) self.course = Course.objects.first() self.course_session = CourseSession.objects.first() self.lu = LearningUnit.objects.first() self.assignment = Assignment.objects.first() class GetMentorOpenTasksTestCase(BaseMentorAssignmentTestCase): def setUp(self): super().setUp() self.course.configuration.is_vv = True self.course.configuration.save() self.mentor = create_user("mentor") self.lm = LearningMentor.objects.create( mentor=self.mentor, course_session=self.course_session ) self.participants = [create_user(f"participant{i}") for i in range(2)] def create_and_test_count( self, assignment_user, evaluation_user, mentor, completion_status, count ): AssignmentCompletion.objects.create( course_session=self.course_session, assignment_user=assignment_user, assignment=self.assignment, completion_status=completion_status, evaluation_user=evaluation_user, ) self.assertEqual( _get_mentor_open_tasks_count(str(self.course.id), mentor), count ) def test_get_uk_count(self): self.course.configuration.is_uk = True self.course.configuration.save() count = _get_mentor_open_tasks_count(str(self.course.id), self.mentor) self.assertEqual(count, 0) def test_get_vv_count_when_assignment_evaluated(self): # GIVEN csu = add_course_session_user( self.course_session, self.participants[0], role=CourseSessionUser.Role.MEMBER, ) self.lm.participants.add(csu) add_course_session_user( self.course_session, self.participants[1], role=CourseSessionUser.Role.MEMBER, ) AssignmentCompletion.objects.create( course_session=self.course_session, assignment_user=self.participants[0], evaluation_user=self.participants[0], assignment=self.assignment, completion_status=AssignmentCompletionStatus.SUBMITTED.value, ) self.create_and_test_count( assignment_user=self.participants[0], mentor=self.mentor, evaluation_user=self.mentor, completion_status=AssignmentCompletionStatus.EVALUATION_SUBMITTED.value, count=0, ) def test_get_vv_count_when_assignment_submitted(self): csu = add_course_session_user( self.course_session, self.participants[0], role=CourseSessionUser.Role.MEMBER, ) self.lm.participants.add(csu) add_course_session_user( self.course_session, self.participants[1], role=CourseSessionUser.Role.MEMBER, ) self.create_and_test_count( assignment_user=self.participants[0], mentor=self.mentor, evaluation_user=self.mentor, completion_status=AssignmentCompletionStatus.SUBMITTED.value, count=1, ) def test_get_vv_count_with_feedback_not_submitted(self): csu = add_course_session_user( self.course_session, self.participants[0], role=CourseSessionUser.Role.MEMBER, ) self.lm.participants.add(csu) SelfEvaluationFeedback.objects.create( feedback_submitted=False, feedback_requester_user=self.participants[0], feedback_provider_user=self.mentor, learning_unit=self.lu, ) self.create_and_test_count( assignment_user=self.participants[0], mentor=self.mentor, evaluation_user=self.mentor, completion_status=AssignmentCompletionStatus.SUBMITTED.value, count=2, ) def test_get_vv_count_with_feedback_submitted(self): csu = add_course_session_user( self.course_session, self.participants[0], role=CourseSessionUser.Role.MEMBER, ) self.lm.participants.add(csu) SelfEvaluationFeedback.objects.create( feedback_submitted=True, feedback_requester_user=self.participants[0], feedback_provider_user=self.mentor, learning_unit=self.lu, ) self.create_and_test_count( assignment_user=self.participants[0], mentor=self.mentor, evaluation_user=self.mentor, completion_status=AssignmentCompletionStatus.SUBMITTED.value, count=1, ) def test_get_vv_count_without_lm(self): add_course_session_user( self.course_session, self.participants[0], role=CourseSessionUser.Role.MEMBER, ) SelfEvaluationFeedback.objects.create( feedback_submitted=False, feedback_requester_user=self.participants[0], feedback_provider_user=self.mentor, learning_unit=self.lu, ) self.create_and_test_count( assignment_user=self.participants[0], mentor=self.mentor, evaluation_user=self.mentor, completion_status=AssignmentCompletionStatus.SUBMITTED.value, count=0, ) class ExportXlsTestCase(TestCase): def setUp(self): create_default_users() create_test_course(include_vv=True, with_sessions=True) self.ALLOWED_ROLES = ["EXPERT", "SUPERVISOR"] def test_can_export_cs_dats(self): # supervisor sees all cs in region supervisor = User.objects.get(id=TEST_SUPERVISOR1_USER_ID) requested_cs_ids = [ TEST_COURSE_SESSION_ZURICH_ID, TEST_COURSE_SESSION_BERN_ID, ] allowed_csrs_ids = _get_course_sessions_with_roles_for_user( supervisor, self.ALLOWED_ROLES, requested_cs_ids ) self.assertCountEqual(requested_cs_ids, [csr.id for csr in allowed_csrs_ids]) def test_student_cannot_export_data(self): # student cannot export any data student = User.objects.get(id=TEST_STUDENT1_USER_ID) requested_cs_ids = [str(TEST_COURSE_SESSION_ZURICH_ID)] allowed_csrs_ids = _get_course_sessions_with_roles_for_user( student, self.ALLOWED_ROLES, requested_cs_ids ) self.assertCountEqual([], allowed_csrs_ids) def test_trainer_cannot_export_other_cs(self): # trainer can only export cs where she is assigned trainer = User.objects.get(email="test-trainer2@example.com") requested_cs_ids = [ TEST_COURSE_SESSION_BERN_ID, TEST_COURSE_SESSION_ZURICH_ID, ] allowed_csrs_ids = _get_course_sessions_with_roles_for_user( trainer, self.ALLOWED_ROLES, requested_cs_ids ) self.assertCountEqual( [TEST_COURSE_SESSION_ZURICH_ID], [csr.id for csr in allowed_csrs_ids] ) def test_trainer_can_get_circles_where_expert(self): trainer = User.objects.get(email="test-trainer2@example.com") circle = Circle.objects.get(slug="test-lehrgang-lp-circle-fahrzeug") requested_cs_ids = [TEST_COURSE_SESSION_ZURICH_ID] allowed_csrs_ids = _get_course_sessions_with_roles_for_user( trainer, self.ALLOWED_ROLES, requested_cs_ids ) allowed_circles = _get_permitted_circles_ids_for_user_and_course_session( trainer, allowed_csrs_ids, [circle.id] ) self.assertEqual( [(TEST_COURSE_SESSION_ZURICH_ID, [circle.id])], allowed_circles ) def test_trainer_cannot_get_circles_where_not_expert(self): trainer = User.objects.get(email="test-trainer2@example.com") circle = Circle.objects.get(slug="test-lehrgang-lp-circle-reisen") requested_cs_ids = [TEST_COURSE_SESSION_ZURICH_ID] allowed_csrs_ids = _get_course_sessions_with_roles_for_user( trainer, self.ALLOWED_ROLES, requested_cs_ids ) allowed_circles = _get_permitted_circles_ids_for_user_and_course_session( trainer, allowed_csrs_ids, [circle.id] ) self.assertEqual([(TEST_COURSE_SESSION_ZURICH_ID, [])], allowed_circles) def test_supervisor_can_get_all_circles(self): supervisor = User.objects.get(id=TEST_SUPERVISOR1_USER_ID) circle_reisen = Circle.objects.get(slug="test-lehrgang-lp-circle-reisen") circle_fahrzeug = Circle.objects.get(slug="test-lehrgang-lp-circle-fahrzeug") requested_cs_ids = [TEST_COURSE_SESSION_ZURICH_ID] allowed_csrs_ids = _get_course_sessions_with_roles_for_user( supervisor, self.ALLOWED_ROLES, requested_cs_ids ) allowed_circles = _get_permitted_circles_ids_for_user_and_course_session( supervisor, allowed_csrs_ids, [ circle_fahrzeug.id, circle_reisen.id, ], ) self.assertEqual( [(TEST_COURSE_SESSION_ZURICH_ID, [circle_fahrzeug.id, circle_reisen.id])], allowed_circles, ) class PersonsTestCase(APITestCase): def setUp(self): create_default_users() create_test_course(include_uk=True, include_vv=False, with_sessions=True) self.student1 = User.objects.get(id=TEST_STUDENT1_USER_ID) self.csu1_student1 = CourseSessionUser.objects.get( user=self.student1, course_session__id=TEST_COURSE_SESSION_BERN_ID ) self.student2 = User.objects.get(id=TEST_STUDENT2_USER_ID) self.csu1_student2 = CourseSessionUser.objects.get( user=self.student2, course_session__id=TEST_COURSE_SESSION_ZURICH_ID ) def test_get_course_sessions_with_roles_for_trainer(self): trainer = User.objects.get(id=TEST_TRAINER1_USER_ID) self.client.force_login(trainer) url = reverse( "get_dashboard_persons", ) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 4) user_ids = [str(user["user_id"]) for user in response.data] self.assertCountEqual( user_ids, [ TEST_TRAINER1_USER_ID, TEST_STUDENT1_USER_ID, TEST_STUDENT2_USER_ID, TEST_STUDENT3_USER_ID, ], ) user1_index = user_ids.index(TEST_STUDENT1_USER_ID) self.assertEqual( response.data[user1_index]["course_sessions"][0]["id"], str(CourseSession.objects.get(id=TEST_COURSE_SESSION_BERN_ID).id), ) def test_get_course_sessions_with_roles_for_supervisor(self): supervisor = User.objects.get(id=TEST_SUPERVISOR1_USER_ID) self.client.force_login(supervisor) url = reverse( "get_dashboard_persons", ) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 5) user_ids = [str(user["user_id"]) for user in response.data] self.assertCountEqual( user_ids, [ TEST_TRAINER1_USER_ID, TEST_STUDENT1_USER_ID, TEST_STUDENT2_USER_ID, TEST_STUDENT3_USER_ID, TEST_TRAINER2_USER_ID, ], ) user2_index = user_ids.index(TEST_STUDENT2_USER_ID) user2_cs = response.data[user2_index]["course_sessions"] self.assertEqual(len(user2_cs), 2) user2_cs_ids = [cs["id"] for cs in user2_cs] self.assertCountEqual( user2_cs_ids, [str(TEST_COURSE_SESSION_ZURICH_ID), str(TEST_COURSE_SESSION_BERN_ID)], )