示例#1
0
    def test_cannot_delete_learning_unit_year_with_administrative_manager_role(self):
        manager = AdministrativeManagerFactory()
        entity_version = EntityVersionFactory(entity_type=entity_type.FACULTY, acronym="SST",
                                              start_date=datetime.date(year=1990, month=1, day=1),
                                              end_date=None)
        PersonEntityFactory(person=manager, entity=entity_version.entity, with_child=True)

        # Creation UE
        learning_unit = LearningUnitFactory()
        l_containeryear = LearningContainerYearFactory(academic_year=self.academic_year,
                                                       container_type=learning_container_year_types.COURSE,
                                                       requirement_entity=entity_version.entity)
        learning_unit_year = LearningUnitYearFactory(learning_unit=learning_unit,
                                                     academic_year=self.academic_year,
                                                     learning_container_year=l_containeryear,
                                                     subtype=learning_unit_year_subtypes.FULL)

        # Cannot remove FULL COURSE
        self.assertFalse(
            base.business.learning_units.perms.is_eligible_to_delete_learning_unit_year(
                learning_unit_year,
                manager
            )
        )

        # Can remove PARTIM COURSE
        learning_unit_year.subtype = learning_unit_year_subtypes.PARTIM
        learning_unit_year.save()
        self.assertFalse(
            base.business.learning_units.perms.is_eligible_to_delete_learning_unit_year(
                learning_unit_year,
                manager
            )
        )
示例#2
0
    def test_get_learning_unit_volumes_management(self, mock_program_manager,
                                                  mock_render):
        mock_program_manager.return_value = True

        learning_unit_year = LearningUnitYearFactory(
            academic_year=self.current_academic_year,
            learning_container_year=self.learning_container_yr)
        learning_unit_year.save()

        request_factory = RequestFactory()
        url = reverse("learning_unit_volumes_management",
                      args=[learning_unit_year.id])
        # GET request
        request = request_factory.get(url)
        request.user = self.a_superuser
        from base.views.learning_unit import learning_unit_volumes_management
        learning_unit_volumes_management(request, learning_unit_year.id)
        self.assertTrue(mock_render.called)
        request, template, context = mock_render.call_args[0]
        self.assertEqual(template, 'learning_unit/volumes_management.html')
        self.assertEqual(context['tab_active'], 'components')

        # POST request
        request = request_factory.post(
            url, self._get_volumes_data([learning_unit_year]))
        request.user = mock.Mock()
        learning_unit_volumes_management(request, learning_unit_year.id)
        self.assertTrue(mock_render.called)
    def test_li_edit_date_person_test_is_eligible_to_modify_end_date_based_on_container_type(
            self):
        lu = LearningUnitFactory(existing_proposal_in_epc=False)
        learning_unit_year_without_proposal = LearningUnitYearFactory(
            academic_year=self.academic_year,
            subtype=learning_unit_year_subtypes.FULL,
            learning_unit=lu,
            learning_container_year=self.lcy)
        person_faculty_manager = FacultyManagerFactory()

        PersonEntityFactory(person=person_faculty_manager,
                            entity=self.entity_container_yr.entity)

        self.context['user'] = person_faculty_manager.user
        self.context[
            'learning_unit_year'] = learning_unit_year_without_proposal
        result = li_edit_date_lu(self.context, self.url_edit, "")

        self.assertEqual(
            result,
            self._get_result_data_expected(ID_LINK_EDIT_DATE_LU,
                                           MSG_NO_ELIGIBLE_TO_MODIFY_END_DATE))

        # allowed if _is_person_central_manager or
        #            _is_learning_unit_year_a_partim or
        #            negation(_is_container_type_course_dissertation_or_internship),
        # test 1st condition true
        self.context['user'] = self.central_manager_person.user
        result = li_edit_date_lu(self.context, self.url_edit, "")

        self.assertEqual(
            result,
            self._get_result_data_expected(ID_LINK_EDIT_DATE_LU,
                                           url=self.url_edit))
        # test 2nd condition true
        self.context['user'] = person_faculty_manager.user
        learning_unit_year_without_proposal.subtype = learning_unit_year_subtypes.PARTIM
        learning_unit_year_without_proposal.save()
        self.context[
            'learning_unit_year'] = learning_unit_year_without_proposal

        self.assertEqual(
            li_edit_date_lu(self.context, self.url_edit, ""),
            self._get_result_data_expected(ID_LINK_EDIT_DATE_LU,
                                           url=self.url_edit))
        # test 3rd condition true
        learning_unit_year_without_proposal.learning_container_year.container_type = learning_container_year_types.OTHER_COLLECTIVE
        learning_unit_year_without_proposal.learning_container_year.save()
        learning_unit_year_without_proposal.subtype = learning_unit_year_subtypes.FULL
        learning_unit_year_without_proposal.save()
        self.context[
            'learning_unit_year'] = learning_unit_year_without_proposal

        self.assertEqual(
            li_edit_date_lu(self.context, self.url_edit, ""),
            self._get_result_data_expected(ID_LINK_EDIT_DATE_LU,
                                           url=self.url_edit))
示例#4
0
    def test_can_delete_learning_unit_year_with_faculty_manager_role(self):
        # Faculty manager can only delete other type than COURSE/INTERNSHIP/DISSERTATION
        managers = [
            create_person_with_permission_and_group(FACULTY_MANAGER_GROUP, 'can_delete_learningunit'),
            create_person_with_permission_and_group(UE_FACULTY_MANAGER_GROUP, 'can_delete_learningunit')
        ]
        entity_version = EntityVersionFactory(entity_type=entity_type.FACULTY, acronym="SST",
                                              start_date=datetime.date(year=1990, month=1, day=1),
                                              end_date=None)
        for manager in managers:
            PersonEntityFactory(person=manager, entity=entity_version.entity, with_child=True)

            # Creation UE
            learning_unit = LearningUnitFactory()
            l_containeryear = LearningContainerYearFactory(
                academic_year=self.academic_year,
                container_type=learning_container_year_types.COURSE,
                requirement_entity=entity_version.entity
            )
            learning_unit_year = LearningUnitYearFactory(learning_unit=learning_unit,
                                                         academic_year=self.academic_year,
                                                         learning_container_year=l_containeryear,
                                                         subtype=learning_unit_year_subtypes.FULL)

            # Cannot remove FULL COURSE
            self.assertFalse(
                base.business.learning_units.perms.is_eligible_to_delete_learning_unit_year(
                    learning_unit_year,
                    manager
                )
            )

            # Can remove PARTIM COURSE
            learning_unit_year.subtype = learning_unit_year_subtypes.PARTIM
            learning_unit_year.save()
            self.assertTrue(
                base.business.learning_units.perms.is_eligible_to_delete_learning_unit_year(
                    learning_unit_year,
                    manager
                )
            )

            # Invalidate cache_property
            del manager.is_central_manager
            del manager.is_faculty_manager

            # With both role, greatest is taken
            add_to_group(manager.user, CENTRAL_MANAGER_GROUP)
            learning_unit_year.subtype = learning_unit_year_subtypes.FULL
            learning_unit_year.save()
            self.assertTrue(
                base.business.learning_units.perms.is_eligible_to_delete_learning_unit_year(
                    learning_unit_year,
                    manager
                )
            )
 def test_find_first_by_exact_acronym(self):
     a_learning_unit = LearningUnitFactory()
     ldroi1000_learning_unit_year = LearningUnitYearFactory(academic_year=self.an_academic_year,
                                                            learning_unit=a_learning_unit, acronym='LDROI1000')
     ldroi1000_learning_unit_year.save()
     ldroi1000_learning_unit_year_bis = LearningUnitYearFactory(academic_year=self.an_academic_year,
                                                                learning_unit=a_learning_unit, acronym='LDROI1000')
     ldroi1000_learning_unit_year_bis.save()
     self.assertEqual(mdl_base.learning_unit_year.find_first_by_exact_acronym(self.an_academic_year, 'LDROI1000'),
                      ldroi1000_learning_unit_year)
示例#6
0
    def test_can_delete_learning_unit_year_with_faculty_manager_role(self):
        # Faculty manager can only delete other type than COURSE/INTERNSHIP/DISSERTATION
        person = PersonFactory()
        add_to_group(person.user, FACULTY_MANAGER_GROUP)
        entity_version = EntityVersionFactory(entity_type=entity_type.FACULTY,
                                              acronym="SST",
                                              start_date=datetime.date(
                                                  year=1990, month=1, day=1),
                                              end_date=None)
        PersonEntityFactory(person=person,
                            entity=entity_version.entity,
                            with_child=True)

        # Creation UE
        learning_unit = LearningUnitFactory()
        l_containeryear = LearningContainerYearFactory(
            academic_year=self.academic_year,
            container_type=learning_container_year_types.COURSE)
        EntityContainerYearFactory(
            learning_container_year=l_containeryear,
            entity=entity_version.entity,
            type=entity_container_year_link_type.REQUIREMENT_ENTITY)
        learning_unit_year = LearningUnitYearFactory(
            learning_unit=learning_unit,
            academic_year=self.academic_year,
            learning_container_year=l_containeryear,
            subtype=learning_unit_year_subtypes.FULL)

        # Cannot remove FULL COURSE
        self.assertFalse(
            base.business.learning_units.perms.
            is_eligible_to_delete_learning_unit_year(learning_unit_year,
                                                     person))

        # Can remove PARTIM COURSE
        learning_unit_year.subtype = learning_unit_year_subtypes.PARTIM
        learning_unit_year.save()
        self.assertTrue(
            base.business.learning_units.perms.
            is_eligible_to_delete_learning_unit_year(learning_unit_year,
                                                     person))

        # Invalidate cache_property
        del person.is_central_manager
        del person.is_faculty_manager

        # With both role, greatest is taken
        add_to_group(person.user, CENTRAL_MANAGER_GROUP)
        learning_unit_year.subtype = learning_unit_year_subtypes.FULL
        learning_unit_year.save()
        self.assertTrue(
            base.business.learning_units.perms.
            is_eligible_to_delete_learning_unit_year(learning_unit_year,
                                                     person))
示例#7
0
    def test_learning_unit_check_acronym(self):
        learning_unit_container_year = LearningContainerYearFactory(
            academic_year=self.academic_years[0])
        learning_unit_year = LearningUnitYearFactory(
            acronym="LCHIM1210",
            learning_container_year=learning_unit_container_year,
            subtype=learning_unit_year_subtypes.FULL,
            academic_year=self.academic_years[0])
        learning_unit_year.save()

        get_data = {
            'acronym': 'LCHIM1210',
            'year_id': self.academic_years[0].id
        }
        response = self.client.get(self.url, get_data, **KWARGS)

        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            str(response.content, encoding='utf8'), {
                'valid': True,
                'existing_acronym': True,
                'existed_acronym': False,
                'first_using': str(self.academic_years[0]),
                'last_using': ""
            })

        learning_unit_year = LearningUnitYearFactory(
            acronym="LCHIM1211",
            learning_container_year=learning_unit_container_year,
            subtype=learning_unit_year_subtypes.FULL,
            academic_year=self.academic_years[0])
        learning_unit_year.save()

        get_data = {
            'acronym': 'LCHIM1211',
            'year_id': self.academic_years[6].id
        }
        response = self.client.get(self.url, get_data, **KWARGS)

        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            str(response.content, encoding='utf8'), {
                'valid': True,
                'existing_acronym': False,
                'existed_acronym': True,
                'first_using': "",
                'last_using': str(self.academic_years[0])
            })
示例#8
0
    def test_volumes_validation(self):
        learning_unit_year = LearningUnitYearFactory(
            academic_year=self.current_academic_year,
            learning_container_year=self.learning_container_yr)
        learning_unit_year.save()

        kwargs = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
        url = reverse("volumes_validation", args=[learning_unit_year.id])

        data = self._get_volumes_data(learning_unit_year)
        # TODO inject wrong data
        response = self.client.get(url, data, **kwargs)
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(str(response.content, encoding='utf8'), {
            'errors': [],
        })
示例#9
0
class LearningUnitYearFindLearningUnitYearByAcademicYearTutorAttributionsTest(
        TestCase):
    def setUp(self):
        self.current_academic_year = create_current_academic_year()
        self.learning_unit_year = LearningUnitYearFactory(
            academic_year=self.current_academic_year,
            learning_container_year__academic_year=self.current_academic_year,
        )
        self.tutor = TutorFactory()
        AttributionFactory(learning_unit_year=self.learning_unit_year,
                           tutor=self.tutor)

    def test_find_learning_unit_years_by_academic_year_tutor_attributions_case_occurrence_found(
            self):
        result = find_learning_unit_years_by_academic_year_tutor_attributions(
            academic_year=self.current_academic_year, tutor=self.tutor)
        self.assertIsInstance(result, QuerySet)
        self.assertEqual(result.count(), 1)

    def test_find_learning_unit_years_by_academic_year_tutor_attributions_case_distinct_occurrence_found(
            self):
        """In this test, we ensure that user see one line per learning unit year despite multiple attribution"""
        AttributionFactory(learning_unit_year=self.learning_unit_year,
                           tutor=self.tutor,
                           function=COORDINATOR)
        AttributionFactory(learning_unit_year=self.learning_unit_year,
                           tutor=self.tutor,
                           function=CO_HOLDER)
        AttributionFactory(learning_unit_year=self.learning_unit_year,
                           tutor=self.tutor)

        result = find_learning_unit_years_by_academic_year_tutor_attributions(
            academic_year=self.current_academic_year, tutor=self.tutor)
        self.assertIsInstance(result, QuerySet)
        self.assertEqual(result.count(), 1)

    def test_find_learning_unit_years_by_academic_year_tutor_attributions_case_no_occurrence_found(
            self):
        """In this test, we ensure that if the learning unit year as no learning container, it is not taking account"""
        self.learning_unit_year.learning_container_year = None
        self.learning_unit_year.save()

        result = find_learning_unit_years_by_academic_year_tutor_attributions(
            academic_year=self.current_academic_year, tutor=self.tutor)
        self.assertIsInstance(result, QuerySet)
        self.assertFalse(result.count())
示例#10
0
class LearningUnitYearGetEntityTest(TestCase):
    def setUp(self):
        self.learning_unit_year = LearningUnitYearFactory()
        self.requirement_entity = EntityContainerYearFactory(
            type=entity_container_year_link_type.REQUIREMENT_ENTITY,
            learning_container_year=self.learning_unit_year.
            learning_container_year)

    def test_get_entity_case_found_entity_type(self):
        result = self.learning_unit_year.get_entity(
            entity_type=entity_container_year_link_type.REQUIREMENT_ENTITY)
        self.assertEqual(result, self.requirement_entity.entity)

    def test_get_entity_case_no_learning_container_year(self):
        self.learning_unit_year.learning_container_year = None
        self.learning_unit_year.save()

        result = self.learning_unit_year.get_entity(
            entity_type=entity_container_year_link_type.REQUIREMENT_ENTITY)
        self.assertIsNone(result)
示例#11
0
    def test_learning_unit_check_acronym(self):
        kwargs = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}

        url = reverse('check_acronym', kwargs={'subtype': FULL})
        get_data = {'acronym': 'goodacronym', 'year_id': self.academic_years[0].id}
        response = self.client.get(url, get_data, **kwargs)

        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            str(response.content, encoding='utf8'),
            {'valid': False,
             'existing_acronym': False,
             'existed_acronym': False,
             'first_using': "",
             'last_using': ""}
        )

        learning_unit_container_year = LearningContainerYearFactory(
            academic_year=self.academic_years[0]
        )
        learning_unit_year = LearningUnitYearFactory(
            acronym="LCHIM1210",
            learning_container_year=learning_unit_container_year,
            subtype=learning_unit_year_subtypes.FULL,
            academic_year=self.academic_years[0]
        )
        learning_unit_year.save()

        get_data = {'acronym': 'LCHIM1210', 'year_id': self.academic_years[0].id}
        response = self.client.get(url, get_data, **kwargs)

        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            str(response.content, encoding='utf8'),
            {'valid': True,
             'existing_acronym': True,
             'existed_acronym': False,
             'first_using': str(self.academic_years[0]),
             'last_using': ""}
        )

        learning_unit_year = LearningUnitYearFactory(
            acronym="LCHIM1211",
            learning_container_year=learning_unit_container_year,
            subtype=learning_unit_year_subtypes.FULL,
            academic_year=self.academic_years[0]
        )
        learning_unit_year.save()

        get_data = {'acronym': 'LCHIM1211', 'year_id': self.academic_years[6].id}
        response = self.client.get(url, get_data, **kwargs)

        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            str(response.content, encoding='utf8'),
            {'valid': True,
             'existing_acronym': False,
             'existed_acronym': True,
             'first_using': "",
             'last_using': str(self.academic_years[0])}
        )
示例#12
0
    def test_li_edit_date_person_test_is_eligible_to_modify_end_date_based_on_container_type(
            self):
        lu = LearningUnitFactory(existing_proposal_in_epc=False)
        learning_unit_year_without_proposal = LearningUnitYearFactory(
            academic_year=self.current_academic_year,
            learning_unit=lu,
        )
        person_faculty_managers = [
            create_person_with_permission_and_group(FACULTY_MANAGER_GROUP,
                                                    'can_edit_learningunit'),
            create_person_with_permission_and_group(UE_FACULTY_MANAGER_GROUP,
                                                    'can_edit_learningunit')
        ]

        for manager in person_faculty_managers:
            manager.user.user_permissions.add(
                Permission.objects.get(codename='can_edit_learningunit_date'))
            learning_unit_year_without_proposal.subtype = learning_unit_year_subtypes.FULL
            learning_unit_year_without_proposal.learning_container_year = self.lcy
            learning_unit_year_without_proposal.learning_container_year.container_type = learning_container_year_types.COURSE
            learning_unit_year_without_proposal.learning_container_year.save()
            learning_unit_year_without_proposal.save()
            PersonEntityFactory(
                person=manager,
                entity=self.requirement_entity,
            )

            self.context['user'] = manager.user
            self.context[
                'learning_unit_year'] = learning_unit_year_without_proposal
            result = li_edit_date_lu(self.context, self.url_edit, "")

            self.assertEqual(
                result,
                self._get_result_data_expected(
                    ID_LINK_EDIT_DATE_LU, MSG_NO_ELIGIBLE_TO_MODIFY_END_DATE))

            # allowed if _is_person_central_manager or
            #            _is_learning_unit_year_a_partim or
            #            negation(_is_container_type_course_dissertation_or_internship),
            # test 1st condition true
            self.context['user'] = self.central_manager_person.user
            result = li_edit_date_lu(self.context, self.url_edit, "")

            self.assertEqual(
                result,
                self._get_result_data_expected(ID_LINK_EDIT_DATE_LU,
                                               url=self.url_edit))
            # test 2nd condition true
            self.context['user'] = manager.user
            learning_unit_year_without_proposal.subtype = learning_unit_year_subtypes.PARTIM
            learning_unit_year_without_proposal.save()
            self.context[
                'learning_unit_year'] = learning_unit_year_without_proposal

            self.assertEqual(
                li_edit_date_lu(self.context, self.url_edit, ""),
                self._get_result_data_expected(ID_LINK_EDIT_DATE_LU,
                                               url=self.url_edit))
            # test 3rd condition true
            learning_unit_year_without_proposal.learning_container_year.container_type = learning_container_year_types.OTHER_COLLECTIVE
            learning_unit_year_without_proposal.learning_container_year.save()
            learning_unit_year_without_proposal.subtype = learning_unit_year_subtypes.FULL
            learning_unit_year_without_proposal.save()
            self.context[
                'learning_unit_year'] = learning_unit_year_without_proposal

            self.assertEqual(
                li_edit_date_lu(self.context, self.url_edit, ""),
                self._get_result_data_expected(ID_LINK_EDIT_DATE_LU,
                                               url=self.url_edit))
示例#13
0
class HomeTest(TestCase):
    def setUp(self):
        Group.objects.get_or_create(name='tutors')
        self.person = PersonFactory()
        self.tutor = TutorFactory(person=self.person)

        attribution_permission = Permission.objects.get(
            codename='can_access_attribution')
        self.person.user.user_permissions.add(attribution_permission)

        today = datetime.datetime.today()
        self.academic_year = AcademicYearFactory(
            year=today.year,
            start_date=today - datetime.timedelta(days=5),
            end_date=today + datetime.timedelta(days=5))
        self.learning_unit_year = LearningUnitYearFactory(
            academic_year=self.academic_year)
        self.learning_unit_year.learning_container_year = LearningContainerYearFactory(
            academic_year=self.learning_unit_year.academic_year,
            in_charge=True)
        self.learning_unit_year.save()
        self.attribution = AttributionFactory(
            function=function.CO_HOLDER,
            learning_unit_year=self.learning_unit_year,
            tutor=self.tutor,
            external_id=ATTRIBUTION_EXTERNAL_ID)

        self.url = reverse('attribution_home')
        self.client.force_login(self.person.user)

    def test_user_not_logged(self):
        self.client.logout()
        response = self.client.get(self.url)

        self.assertRedirects(response, "/login/?next={}".format(self.url))

    def test_user_without_permission(self):
        a_person = PersonFactory()
        self.client.force_login(a_person.user)

        response = self.client.get(self.url)

        self.assertEqual(response.status_code, ACCESS_DENIED)
        self.assertTemplateUsed(response, "access_denied.html")

    def test_person_without_global_id(self):
        self.person.global_id = None
        self.person.save()

        response = self.client.get(self.url)

        self.assertTemplateUsed(response, 'tutor_charge.html')

        self.assertEqual(response.context['user'], self.person.user)
        self.assertEqual(response.context['attributions'], None)
        self.assertEqual(response.context['year'],
                         int(self.academic_year.year))
        self.assertEqual(response.context['tot_lecturing'], 0)
        self.assertEqual(response.context['tot_practical'], 0)
        self.assertEqual(response.context['academic_year'], self.academic_year)
        self.assertEqual(response.context['global_id'], None)
        self.assertEqual(response.context['error'], True)

        self.assertIsInstance(response.context['formset'], BaseFormSet)

    def test_user_without_person(self):
        self.person.delete()

        response = self.client.get(self.url)

        self.assertTemplateUsed(response, 'tutor_charge.html')

        self.assertEqual(response.context['user'], None)
        self.assertEqual(response.context['attributions'], None)
        self.assertEqual(response.context['year'],
                         int(self.academic_year.year))
        self.assertEqual(response.context['tot_lecturing'], None)
        self.assertEqual(response.context['tot_practical'], None)
        self.assertEqual(response.context['academic_year'], self.academic_year)
        self.assertEqual(response.context['global_id'], None)
        self.assertEqual(response.context['error'], False)

        self.assertIsInstance(response.context['formset'], BaseFormSet)

    def test_user_without_tutor(self):
        self.tutor.delete()

        response = self.client.get(self.url)

        self.assertTemplateUsed(response, 'tutor_charge.html')

        self.assertEqual(response.context['user'], self.person.user)
        self.assertEqual(response.context['attributions'], None)
        self.assertEqual(response.context['year'],
                         int(self.academic_year.year))
        self.assertEqual(response.context['tot_lecturing'], None)
        self.assertEqual(response.context['tot_practical'], None)
        self.assertEqual(response.context['academic_year'], self.academic_year)
        self.assertEqual(response.context['global_id'], None)
        self.assertEqual(response.context['error'], False)

        self.assertIsInstance(response.context['formset'], BaseFormSet)

    def test_without_current_academic_year(self):
        self.academic_year.year -= 1
        self.academic_year.end_date = datetime.datetime.today(
        ) - datetime.timedelta(days=3)
        self.academic_year.save()

        response = self.client.get(self.url)

        self.assertTemplateUsed(response, 'tutor_charge.html')

        self.assertEqual(response.context['user'], self.person.user)
        self.assertEqual(response.context['attributions'], None)
        self.assertEqual(response.context['year'],
                         int(datetime.datetime.now().year))
        self.assertEqual(response.context['tot_lecturing'], 0)
        self.assertEqual(response.context['tot_practical'], 0)
        self.assertEqual(response.context['academic_year'], None)
        self.assertEqual(response.context['global_id'], None)
        self.assertEqual(response.context['error'], True)

        self.assertIsInstance(response.context['formset'], BaseFormSet)

    @mock.patch('requests.get',
                side_effect=mock_request_none_attribution_charge)
    def test_without_attributions(self, mock_requests_get):
        response = self.client.get(self.url, mock_requests_get)

        self.assertTrue(mock_requests_get.called)

        self.assertTemplateUsed(response, 'tutor_charge.html')

        self.assertEqual(response.context['user'], self.person.user)
        self.assertEqual(response.context['attributions'], None)
        self.assertEqual(response.context['year'],
                         int(self.academic_year.year))
        self.assertEqual(response.context['tot_lecturing'], 0)
        self.assertEqual(response.context['tot_practical'], 0)
        self.assertEqual(response.context['academic_year'], self.academic_year)
        self.assertEqual(response.context['global_id'], self.person.global_id)
        self.assertEqual(response.context['error'], False)

        self.assertIsInstance(response.context['formset'], BaseFormSet)

    @override_settings()
    def test_when_not_configuration_for_attribution(self):
        del settings.ATTRIBUTION_CONFIG

        response = self.client.get(self.url)

        self.assertTemplateUsed(response, 'tutor_charge.html')

        self.assertEqual(response.context['user'], self.person.user)
        self.assertEqual(response.context['attributions'], None)
        self.assertEqual(response.context['year'],
                         int(self.academic_year.year))
        self.assertEqual(response.context['tot_lecturing'], 0)
        self.assertEqual(response.context['tot_practical'], 0)
        self.assertEqual(response.context['academic_year'], self.academic_year)
        self.assertEqual(response.context['global_id'], self.person.global_id)
        self.assertEqual(response.context['error'], True)

        self.assertIsInstance(response.context['formset'], BaseFormSet)

    @mock.patch('requests.get', side_effect=RequestException)
    def test_when_exception_occured_during_request_of_webservice(
            self, mock_requests_get):
        response = self.client.get(self.url)

        self.assertTrue(mock_requests_get.called)

        self.assertTemplateUsed(response, 'tutor_charge.html')

        self.assertEqual(response.context['user'], self.person.user)
        self.assertEqual(response.context['attributions'], None)
        self.assertEqual(response.context['year'],
                         int(self.academic_year.year))
        self.assertEqual(response.context['tot_lecturing'], 0)
        self.assertEqual(response.context['tot_practical'], 0)
        self.assertEqual(response.context['academic_year'], self.academic_year)
        self.assertEqual(response.context['global_id'], self.person.global_id)
        self.assertEqual(response.context['error'], True)

        self.assertIsInstance(response.context['formset'], BaseFormSet)

    @override_settings(ATTRIBUTION_CONFIG=get_attribution_config_settings())
    @mock.patch('requests.get',
                side_effect=mock_request_single_attribution_charge)
    def test_for_one_attribution(self, mock_requests_get):

        response = self.client.get(self.url)

        self.assertTrue(mock_requests_get.called)

        self.assertTemplateUsed(response, 'tutor_charge.html')

        self.assertEqual(response.context['user'], self.person.user)
        self.assertEqual(response.context['year'],
                         int(self.academic_year.year))
        self.assertEqual(response.context['tot_lecturing'],
                         LEARNING_UNIT_LECTURING_DURATION)
        self.assertEqual(response.context['tot_practical'],
                         LEARNING_UNIT_PRACTICAL_EXERCISES_DURATION)
        self.assertEqual(response.context['academic_year'], self.academic_year)
        self.assertEqual(response.context['global_id'], self.person.global_id)
        self.assertEqual(response.context['error'], False)

        self.assertIsInstance(response.context['formset'], BaseFormSet)

        self.assertEqual(len(response.context['attributions']), 1)
        attribution = response.context['attributions'][0]
        self.assertEqual(attribution['acronym'],
                         self.learning_unit_year.acronym)
        self.assertEqual(attribution['title'],
                         self.learning_unit_year.specific_title)
        self.assertEqual(attribution['start_year'],
                         self.attribution.start_year)
        self.assertEqual(attribution['lecturing_allocation_charge'],
                         str(LEARNING_UNIT_LECTURING_DURATION))
        self.assertEqual(attribution['practice_allocation_charge'],
                         str(LEARNING_UNIT_PRACTICAL_EXERCISES_DURATION))
        self.assertEqual(attribution['percentage_allocation_charge'], '75.0')
        self.assertEqual(attribution['weight'],
                         self.learning_unit_year.credits)
        self.assertEqual(attribution['url_schedule'], "")
        self.assertEqual(
            attribution['url_students_list_email'],
            'mailto:{}-{}@listes-student.uclouvain.be'.format(
                self.learning_unit_year.acronym.lower(),
                self.academic_year.year))
        self.assertEqual(attribution['function'], self.attribution.function)
        self.assertEqual(attribution['year'], self.academic_year.year)
        self.assertEqual(attribution['learning_unit_year_url'], "")
        self.assertEqual(attribution['learning_unit_year'],
                         self.learning_unit_year)
        self.assertEqual(attribution['tutor_id'], self.tutor.id)

    @mock.patch('requests.get',
                side_effect=mock_request_multiple_attributions_charge)
    def test_for_multiple_attributions(self, mock_requests_get):
        an_other_attribution = AttributionFactory(
            function=function.CO_HOLDER,
            learning_unit_year=self.learning_unit_year,
            tutor=self.tutor,
            external_id=OTHER_ATTRIBUTION_EXTERNAL_ID)

        response = self.client.get(self.url)

        self.assertTrue(mock_requests_get.called)

        self.assertTemplateUsed(response, 'tutor_charge.html')

        self.assertEqual(response.context['user'], self.person.user)
        self.assertEqual(response.context['year'],
                         int(self.academic_year.year))
        self.assertEqual(response.context['tot_lecturing'],
                         LEARNING_UNIT_LECTURING_DURATION)
        self.assertEqual(response.context['tot_practical'],
                         LEARNING_UNIT_PRACTICAL_EXERCISES_DURATION)
        self.assertEqual(response.context['academic_year'], self.academic_year)
        self.assertEqual(response.context['global_id'], self.person.global_id)
        self.assertEqual(response.context['error'], False)

        self.assertIsInstance(response.context['formset'], BaseFormSet)

        self.assertEqual(len(response.context['attributions']), 2)

    @mock.patch('requests.get',
                side_effect=mock_request_multiple_attributions_charge)
    def test_with_attribution_not_recognized(self, mock_requests_get):
        an_other_attribution = AttributionFactory(
            learning_unit_year=self.learning_unit_year,
            tutor=self.tutor,
            external_id=OTHER_ATTRIBUTION_EXTERNAL_ID)

        inexisting_external_id = "osis.attribution_8082"
        attribution_not_in_json = AttributionFactory(
            learning_unit_year=self.learning_unit_year,
            tutor=self.tutor,
            external_id=inexisting_external_id)

        response = self.client.get(self.url)

        self.assertTrue(mock_requests_get.called)

        self.assertTemplateUsed(response, 'tutor_charge.html')

        self.assertEqual(response.context['user'], self.person.user)
        self.assertEqual(response.context['year'],
                         int(self.academic_year.year))
        self.assertEqual(response.context['tot_lecturing'],
                         LEARNING_UNIT_LECTURING_DURATION)
        self.assertEqual(response.context['tot_practical'],
                         LEARNING_UNIT_PRACTICAL_EXERCISES_DURATION)
        self.assertEqual(response.context['academic_year'], self.academic_year)
        self.assertEqual(response.context['global_id'], self.person.global_id)
        self.assertEqual(response.context['error'], False)

        self.assertIsInstance(response.context['formset'], BaseFormSet)

        self.assertEqual(len(response.context['attributions']), 2)

    @mock.patch(
        'requests.get',
        side_effect=mock_request_multiple_attributions_charge_with_missing_values
    )
    def test_with_missing_values(self, mock_requests_get):
        an_other_attribution = AttributionFactory(
            learning_unit_year=self.learning_unit_year,
            tutor=self.tutor,
            external_id=OTHER_ATTRIBUTION_EXTERNAL_ID)

        response = self.client.get(self.url)

        self.assertTrue(mock_requests_get.called)

        self.assertTemplateUsed(response, 'tutor_charge.html')

        self.assertEqual(response.context['user'], self.person.user)
        self.assertEqual(response.context['year'],
                         int(self.academic_year.year))
        self.assertEqual(response.context['tot_lecturing'],
                         LEARNING_UNIT_LECTURING_DURATION)
        self.assertEqual(response.context['tot_practical'],
                         LEARNING_UNIT_PRACTICAL_EXERCISES_DURATION)
        self.assertEqual(response.context['academic_year'], self.academic_year)
        self.assertEqual(response.context['global_id'], self.person.global_id)
        self.assertEqual(response.context['error'], False)

        self.assertIsInstance(response.context['formset'], BaseFormSet)

        attributions = response.context['attributions']
        reduced_list_attributions = map(
            lambda attribution: [
                attribution["lecturing_allocation_charge"], attribution[
                    'practice_allocation_charge'], attribution[
                        'percentage_allocation_charge']
            ], attributions)
        self.assertIn([str(LEARNING_UNIT_LECTURING_DURATION), None, "25.0"],
                      reduced_list_attributions)
class TestLearningUnitModificationForm(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.current_academic_year = create_current_academic_year()

        cls.organization = OrganizationFactory(type=organization_type.MAIN)
        a_campus = CampusFactory(organization=cls.organization)
        an_entity = EntityFactory(organization=cls.organization)
        cls.an_entity_version = EntityVersionFactory(
            entity=an_entity,
            entity_type=entity_type.SCHOOL,
            parent=None,
            end_date=None,
            start_date=datetime.date.today() - datetime.timedelta(days=5))
        cls.person = PersonEntityFactory(entity=an_entity).person

        language = LanguageFactory()
        cls.form_data = {
            "academic_year": str(cls.current_academic_year.id),
            "container_type": str(learning_container_year_types.COURSE),
            "subtype": str(learning_unit_year_subtypes.FULL),
            "acronym": "OSIS1452",
            "credits": "45",
            "specific_title": "OSIS",
            "first_letter": "L",
            "periodicity": learning_unit_periodicity.ANNUAL,
            "campus": str(a_campus.id),
            "requirement_entity": str(cls.an_entity_version.id),
            "allocation_entity": str(cls.an_entity_version.id),
            "language": language.pk
        }

        cls.initial_data = {
            "academic_year": str(cls.current_academic_year.id),
            "container_type": str(learning_container_year_types.COURSE),
            "subtype": str(learning_unit_year_subtypes.FULL),
            "acronym": "OSIS1452",
            "first_letter": "L",
            "status": True
        }

        cls.faculty_user = UserFactory()
        cls.faculty_user.groups.add(
            Group.objects.get(name=FACULTY_MANAGER_GROUP))
        cls.faculty_person = PersonFactory(user=cls.faculty_user)
        cls.faculty_user.user_permissions.add(
            Permission.objects.get(codename='can_propose_learningunit'))
        PersonEntityFactory(entity=an_entity, person=cls.faculty_person)

    def setUp(self):
        self.learning_container_year = LearningContainerYearFactory(
            academic_year=self.current_academic_year, container_type=COURSE)
        self.learning_unit_year = LearningUnitYearFactory(
            academic_year=self.current_academic_year,
            learning_container_year=self.learning_container_year,
            learning_unit__periodicity=ANNUAL,
            subtype=FULL,
            credits=25,
            status=False)
        self.learning_unit_year_partim_1 = LearningUnitYearFactory(
            academic_year=self.current_academic_year,
            learning_container_year=self.learning_container_year,
            learning_unit__periodicity=ANNUAL,
            subtype=PARTIM,
            credits=20,
            status=False)
        self.learning_unit_year_partim_2 = LearningUnitYearFactory(
            academic_year=self.current_academic_year,
            learning_container_year=self.learning_container_year,
            learning_unit__periodicity=ANNUAL,
            subtype=PARTIM,
            credits=18,
            status=False)

    def test_disabled_fields_in_case_of_learning_unit_of_type_full(self):

        form = LearningUnitModificationForm(
            person=self.person,
            learning_unit_year_instance=self.learning_unit_year)
        disabled_fields = ("first_letter", "acronym", "academic_year",
                           "container_type", "subtype")
        for field in disabled_fields:
            self.assertTrue(form.fields[field].disabled)

    def test_disabled_fields_in_case_of_learning_unit_of_type_partim(self):

        form = LearningUnitModificationForm(
            person=self.person,
            learning_unit_year_instance=self.learning_unit_year_partim_1)
        disabled_fields = ('first_letter', 'acronym', 'common_title',
                           'common_title_english', 'requirement_entity',
                           'allocation_entity', 'language', 'campus',
                           'container_type', "academic_year",
                           'internship_subtype',
                           'additional_requirement_entity_1',
                           'additional_requirement_entity_2', 'is_vacant',
                           'team', 'type_declaration_vacant',
                           'attribution_procedure', "subtype", "status")
        for field in form.fields:
            if field in disabled_fields:
                self.assertTrue(form.fields[field].disabled, field)
            else:
                self.assertFalse(form.fields[field].disabled, field)

    def test_disabled_internship_subtype_in_case_of_container_type_different_than_internship(
            self):
        form = LearningUnitModificationForm(
            person=self.person,
            learning_unit_year_instance=self.learning_unit_year)

        self.assertTrue(form.fields["internship_subtype"].disabled)

        self.learning_unit_year.learning_container_year.container_type = learning_container_year_types.INTERNSHIP

        form = LearningUnitModificationForm(
            person=self.person,
            learning_unit_year_instance=self.learning_unit_year)

        self.assertFalse(form.fields["internship_subtype"].disabled)

    def test_entity_does_not_exist_for_lifetime_of_learning_unit(self):
        an_other_entity = EntityFactory(organization=self.organization)
        an_other_entity_version = EntityVersionFactory(
            entity=an_other_entity,
            entity_type=entity_type.SCHOOL,
            parent=None,
            end_date=self.current_academic_year.end_date -
            datetime.timedelta(days=5),
            start_date=datetime.date.today() - datetime.timedelta(days=5))
        PersonEntityFactory(person=self.person, entity=an_other_entity)

        form_data_with_invalid_requirement_entity = self.form_data.copy()
        form_data_with_invalid_requirement_entity["requirement_entity"] = str(
            an_other_entity_version.id)

        form = LearningUnitModificationForm(
            form_data_with_invalid_requirement_entity,
            person=self.person,
            end_date=self.current_academic_year.end_date,
            learning_unit_year_instance=self.learning_unit_year)
        self.assertFalse(form.is_valid())

    def test_set_status_value(self):
        form = LearningUnitModificationForm(
            learning_unit_year_instance=self.learning_unit_year_partim_1,
            person=self.person)
        self.assertEqual(form.fields["status"].initial, False)
        self.assertTrue(form.fields["status"].disabled)

    def test_partim_can_modify_periodicity(self):
        initial_data_with_subtype_partim = self.initial_data.copy()
        initial_data_with_subtype_partim[
            "subtype"] = learning_unit_year_subtypes.PARTIM
        form = LearningUnitModificationForm(
            learning_unit_year_instance=self.learning_unit_year_partim_1,
            person=self.person)
        self.assertFalse(form.fields["periodicity"].disabled)

    def test_do_not_set_minimum_credits_for_full_learning_unit_year_if_no_partims(
            self):
        learning_unit_year_with_no_partims = LearningUnitYearFactory(
            academic_year=self.current_academic_year,
            learning_unit__periodicity=ANNUAL,
            subtype=FULL)
        form = LearningUnitModificationForm(
            person=self.person,
            learning_unit_year_instance=learning_unit_year_with_no_partims)
        self.assertEqual(form.fields["credits"].min_value, None)

    def test_entity_does_not_exist_for_lifetime_of_learning_unit_with_no_planned_end(
            self):
        an_other_entity = EntityFactory(organization=self.organization)
        an_other_entity_version = EntityVersionFactory(
            entity=an_other_entity,
            entity_type=entity_type.SCHOOL,
            parent=None,
            end_date=self.current_academic_year.end_date -
            datetime.timedelta(days=5),
            start_date=datetime.date.today() - datetime.timedelta(days=5))
        PersonEntityFactory(person=self.person, entity=an_other_entity)

        form_data_with_invalid_requirement_entity = self.form_data.copy()
        form_data_with_invalid_requirement_entity["requirement_entity"] = str(
            an_other_entity_version.id)
        form = LearningUnitModificationForm(
            form_data_with_invalid_requirement_entity,
            person=self.person,
            learning_unit_year_instance=self.learning_unit_year)
        self.assertFalse(form.is_valid())

    def test_when_requirement_and_attribution_entities_are_different_for_disseration_and_internship_subtype(
            self):
        an_other_entity_version = EntityVersionFactory(
            entity__organization=self.organization,
            entity_type=entity_type.SCHOOL,
            parent=None,
            end_date=None,
            start_date=datetime.date.today() - datetime.timedelta(days=5))
        form_data_with_different_allocation_entity = self.form_data.copy()
        form_data_with_different_allocation_entity["allocation_entity"] = str(
            an_other_entity_version.id)

        for container_type in (learning_container_year_types.DISSERTATION,
                               learning_container_year_types.INTERNSHIP):
            self.learning_container_year.container_type = container_type
            self.learning_container_year.save()

            form = LearningUnitModificationForm(
                form_data_with_different_allocation_entity,
                person=self.person,
                learning_unit_year_instance=self.learning_unit_year)
            self.assertFalse(form.is_valid(), container_type)

    def test_when_partim_active_but_modify_parent_to_inactive(self):
        # Set status to parent inactive
        form_data_parent_with_status_inactive = self.form_data.copy()
        form_data_parent_with_status_inactive['status'] = False

        # Set status to partim active
        self.learning_unit_year_partim_1.status = True
        self.learning_unit_year_partim_1.save()

        form = LearningUnitModificationForm(
            form_data_parent_with_status_inactive,
            person=self.person,
            end_date=self.current_academic_year.end_date,
            learning_unit_year_instance=self.learning_unit_year)
        self.assertFalse(form.is_valid())
        self.assertEqual(
            form.errors['status'],
            [_('The parent must be active because there are partim active')])

    def test_valid_form(self):
        form = LearningUnitModificationForm(
            self.form_data,
            person=self.person,
            learning_unit_year_instance=self.learning_unit_year)
        self.assertTrue(form.is_valid(), form.errors)

    def test_valid_form_with_faculty_manager(self):
        form = LearningUnitModificationForm(
            self.form_data,
            person=self.person,
            learning_unit_year_instance=self.learning_unit_year)
        self.assertTrue(form.is_valid(), form.errors)

    def test_deactivated_fields_in_learning_unit_modification_form(self):
        form = LearningUnitModificationForm(
            person=self.person,
            learning_unit_year_instance=self.learning_unit_year)
        self.assertFalse(form.fields["common_title"].disabled)
        self.assertFalse(form.fields["common_title_english"].disabled)
        self.assertFalse(form.fields["specific_title"].disabled)
        self.assertFalse(form.fields["specific_title_english"].disabled)
        self.assertFalse(form.fields["faculty_remark"].disabled)
        self.assertFalse(form.fields["other_remark"].disabled)
        self.assertFalse(form.fields["campus"].disabled)
        self.assertFalse(form.fields["status"].disabled)
        self.assertFalse(form.fields["credits"].disabled)
        self.assertFalse(form.fields["language"].disabled)
        self.assertFalse(form.fields["requirement_entity"].disabled)
        self.assertFalse(form.fields["allocation_entity"].disabled)
        self.assertFalse(
            form.fields["additional_requirement_entity_2"].disabled)
        self.assertFalse(form.fields["is_vacant"].disabled)
        self.assertFalse(form.fields["type_declaration_vacant"].disabled)
        self.assertFalse(form.fields["attribution_procedure"].disabled)
        self.assertTrue(form.fields["subtype"].disabled)

    def test_deactivated_fields_in_learning_unit_modification_form_with_faculty_manager(
            self):
        form = LearningUnitModificationForm(
            person=self.faculty_person,
            learning_unit_year_instance=self.learning_unit_year)
        self.assertTrue(form.fields["common_title"].disabled)
        self.assertTrue(form.fields["common_title_english"].disabled)
        self.assertTrue(form.fields["specific_title"].disabled)
        self.assertTrue(form.fields["specific_title_english"].disabled)
        self.assertFalse(form.fields["faculty_remark"].disabled)
        self.assertFalse(form.fields["other_remark"].disabled)
        self.assertTrue(form.fields["campus"].disabled)
        self.assertTrue(form.fields["status"].disabled)
        self.assertTrue(form.fields["credits"].disabled)
        self.assertTrue(form.fields["language"].disabled)
        self.assertTrue(form.fields["requirement_entity"].disabled)
        self.assertTrue(form.fields["allocation_entity"].disabled)
        self.assertTrue(
            form.fields["additional_requirement_entity_2"].disabled)
        self.assertTrue(form.fields["is_vacant"].disabled)
        self.assertTrue(form.fields["type_declaration_vacant"].disabled)
        self.assertTrue(form.fields["attribution_procedure"].disabled)
        self.assertTrue(form.fields["subtype"].disabled)
示例#15
0
class TestQuadriConsistency(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.ay = create_current_academic_year()

    def setUp(self):
        self.luy_full = LearningUnitYearFactory(
            academic_year=self.ay,
            subtype=learning_unit_year_subtypes.FULL,
            quadrimester=None)
        self.learning_component_year_full_lecturing = LearningComponentYearFactory(
            learning_unit_year=self.luy_full)

    @patch.multiple(LearningComponentYearQuadriStrategy,
                    __abstractmethods__=set())
    def test_is_valid_present(self):
        instance = LearningComponentYearQuadriStrategy(
            lcy=self.learning_component_year_full_lecturing)
        with self.assertRaises(NotImplementedError):
            self.assertRaises(NotImplementedError, instance.is_valid())

    def test_no_strategy(self):
        self.luy_full.credits = self.luy_full.credits + 1
        self.luy_full.save()

        test_cases = [
            {
                'vol_q1': 20,
                'vol_q2': None,
                'vol_tot_annual': 20,
                'planned_classes': 1,
                'vol_tot_global': 20
            },
            {
                'vol_q1': None,
                'vol_q2': 20,
                'vol_tot_annual': 20,
                'planned_classes': 1,
                'vol_tot_global': 20
            },
            {
                'vol_q1': 10,
                'vol_q2': 10,
                'vol_tot_annual': 20,
                'planned_classes': 1,
                'vol_tot_global': 20
            },
            {
                'vol_q1': None,
                'vol_q2': None,
                'vol_tot_annual': None,
                'planned_classes': None,
                'vol_tot_global': None
            },
        ]

        for case in test_cases:
            self._update_lecturing_component_volumes(case)

        self.assertTrue(
            LearningComponentYearQuadriNoStrategy(
                lcy=self.learning_component_year_full_lecturing).is_valid())

    def test_ok_volumes_for_Q1(self):
        self.luy_full.credits = self.luy_full.credits + 1
        self.luy_full.quadrimester = quadrimesters.Q1
        self.luy_full.save()

        test_cases = [
            {
                'vol_q1': 20,
                'vol_q2': None,
                'vol_tot_annual': 20,
                'planned_classes': 1,
                'vol_tot_global': 20
            },
        ]

        self._update_lecturing_component_volumes(test_cases[0])

        self.assertTrue(
            LearningComponentYearQ1Strategy(
                lcy=self.learning_component_year_full_lecturing).is_valid())

    def test_warning_volumes_for_Q1(self):
        self.luy_full.credits = self.luy_full.credits + 1
        self.luy_full.quadrimester = quadrimesters.Q1
        self.luy_full.save()

        test_cases = [
            {
                'vol_q1': None,
                'vol_q2': 20,
                'vol_tot_annual': 20,
                'planned_classes': 1,
                'vol_tot_global': 20
            },
        ]

        for case in test_cases:
            self._update_lecturing_component_volumes(case)

        excepted_error = "{} ({})".format(
            _('Volumes of {} are inconsistent').format(
                self.learning_component_year_full_lecturing.complete_acronym),
            _('Only the volume Q1 must have a value'))

        self.assertIn(excepted_error, self.luy_full.warnings)

    def _update_lecturing_component_volumes(self, case):
        with self.subTest(case=case):
            self.learning_component_year_full_lecturing.hourly_volume_partial_q1 = case.get(
                'vol_q1')
            self.learning_component_year_full_lecturing.hourly_volume_partial_q2 = case.get(
                'vol_q2')
            self.learning_component_year_full_lecturing.hourly_volume_total_annual = case.get(
                'vol_tot_annual')
            self.learning_component_year_full_lecturing.planned_classes = case.get(
                'planned_classes')
            self.learning_component_year_full_lecturing.repartition_volume_requirement_entity = case.get(
                'vol_tot_global')
            self.learning_component_year_full_lecturing.save()