示例#1
0
class TestSave(TestCase):
    def setUp(self):
        self.person = PersonFactory()
        an_organization = OrganizationFactory(type=organization_type.MAIN)
        current_academic_year = create_current_academic_year()

        today = datetime.date.today()
        an_entity = EntityFactory(organization=an_organization)
        self.entity_version = EntityVersionFactory(
            entity=an_entity,
            entity_type=entity_type.FACULTY,
            start_date=today.replace(year=1900),
            end_date=None)
        self.an_entity_school = EntityFactory(organization=an_organization)
        self.entity_version_school = EntityVersionFactory(
            entity=self.an_entity_school,
            entity_type=entity_type.SCHOOL,
            start_date=today.replace(year=1900),
            end_date=None)

        learning_container_year = LearningContainerYearFactory(
            academic_year=current_academic_year,
            container_type=learning_container_year_types.COURSE,
            requirement_entity=self.entity_version.entity,
        )
        self.learning_unit_year = LearningUnitYearFakerFactory(
            credits=Decimal(5),
            subtype=learning_unit_year_subtypes.FULL,
            academic_year=current_academic_year,
            learning_container_year=learning_container_year,
            campus=CampusFactory(organization=an_organization,
                                 is_administration=True),
            periodicity=learning_unit_year_periodicity.ANNUAL,
            internship_subtype=None)
        self.person_entity = PersonEntityFactory(person=self.person,
                                                 entity=an_entity)
        self.language = LanguageFactory(code="EN")
        self.campus = CampusFactory(
            name="OSIS Campus",
            organization=OrganizationFactory(type=organization_type.MAIN),
            is_administration=True)

        self.form_data = {
            "academic_year": self.learning_unit_year.academic_year.id,
            "acronym_0": "L",
            "acronym_1": "OSIS1245",
            "common_title": "New common title",
            "common_title_english": "New common title english",
            "specific_title": "New title",
            "specific_title_english": "New title english",
            "container_type":
            self.learning_unit_year.learning_container_year.container_type,
            "internship_subtype": "",
            "credits": self.learning_unit_year.credits,
            "periodicity": learning_unit_year_periodicity.BIENNIAL_ODD,
            "status": False,
            "language": self.language.pk,
            "quadrimester": quadrimesters.Q1,
            "campus": self.campus.id,
            "entity": self.entity_version.id,
            "folder_id": "1",
            "state": proposal_state.ProposalState.CENTRAL.name,
            'requirement_entity': self.entity_version.id,
            'allocation_entity': self.entity_version.id,
            'additional_entity_1': self.entity_version.id,
            'additional_entity_2': self.entity_version.id,

            # Learning component year data model form
            'component-TOTAL_FORMS': '2',
            'component-INITIAL_FORMS': '0',
            'component-MAX_NUM_FORMS': '2',
            'component-0-hourly_volume_total_annual': Decimal(20),
            'component-0-hourly_volume_partial_q1': Decimal(10),
            'component-0-hourly_volume_partial_q2': Decimal(10),
            'component-1-hourly_volume_total_annual': Decimal(20),
            'component-1-hourly_volume_partial_q1': Decimal(10),
            'component-1-hourly_volume_partial_q2': Decimal(10),
            'component-0-planned_classes': 1,
            'component-1-planned_classes': 1,
        }
        FacultyManagerGroupFactory()
        CentralManagerGroupFactory()

    def test_learning_unit_proposal_form_get_as_faculty_manager(self):
        self.person.user.groups.add(
            Group.objects.get(name=FACULTY_MANAGER_GROUP))
        form = ProposalBaseForm(self.form_data, self.person,
                                self.learning_unit_year)
        self.assertTrue(form.fields['state'].disabled)

    def test_learning_unit_proposal_form_get_as_central_manager(self):
        self.person.user.groups.add(
            Group.objects.get(name=CENTRAL_MANAGER_GROUP))
        form = ProposalBaseForm(self.form_data, self.person,
                                self.learning_unit_year)
        self.assertFalse(form.fields['state'].disabled)

    def test_learning_unit_proposal_form_get_as_central_manager_with_instance(
            self):
        self.person.user.groups.add(
            Group.objects.get(name=CENTRAL_MANAGER_GROUP))
        proposal = ProposalLearningUnitFactory(
            learning_unit_year=self.learning_unit_year,
            state=ProposalState.FACULTY.name,
            entity=self.entity_version.entity)
        form = ProposalBaseForm(self.form_data,
                                self.person,
                                self.learning_unit_year,
                                proposal=proposal)
        self.assertFalse(form.fields['state'].disabled)
        self.assertEqual(form.fields['state'].initial,
                         ProposalState.FACULTY.name)

    def test_learning_unit_year_update(self):
        form = ProposalBaseForm(self.form_data, self.person,
                                self.learning_unit_year)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()
        learning_unit_year = LearningUnitYear.objects.get(
            pk=self.learning_unit_year.id)
        self._assert_acronym_has_changed_in_proposal(learning_unit_year)
        self._assert_common_titles_stored_in_container(learning_unit_year)
        self.assertFalse(learning_unit_year.status)
        self.assertEqual(learning_unit_year.credits,
                         Decimal(self.form_data['credits']))
        self.assertEqual(learning_unit_year.quadrimester,
                         self.form_data['quadrimester'])
        self.assertEqual(learning_unit_year.specific_title,
                         self.form_data["specific_title"])
        self.assertEqual(learning_unit_year.specific_title_english,
                         self.form_data["specific_title_english"])
        self.assertEqual(learning_unit_year.language, self.language)
        self.assertEqual(learning_unit_year.campus, self.campus)

    def _assert_acronym_has_changed_in_proposal(self, learning_unit_year):
        self.assertEqual(
            learning_unit_year.acronym,
            "{}{}".format(self.form_data['acronym_0'],
                          self.form_data['acronym_1']))

    def _assert_common_titles_stored_in_container(self, learning_unit_year):
        self.assertNotEqual(learning_unit_year.specific_title,
                            self.form_data['common_title'])
        self.assertNotEqual(learning_unit_year.specific_title_english,
                            self.form_data['common_title_english'])
        self.assertEqual(
            learning_unit_year.learning_container_year.common_title,
            self.form_data['common_title'])
        self.assertEqual(
            learning_unit_year.learning_container_year.common_title_english,
            self.form_data['common_title_english'])

    def test_learning_container_update(self):
        form = ProposalBaseForm(self.form_data, self.person,
                                self.learning_unit_year)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        learning_unit_year = LearningUnitYear.objects.get(
            pk=self.learning_unit_year.id)
        learning_container_year = learning_unit_year.learning_container_year

        self.assertEqual(
            learning_unit_year.acronym,
            self.form_data['acronym_0'] + self.form_data['acronym_1'])
        self.assertEqual(learning_container_year.common_title,
                         self.form_data['common_title'])
        self.assertEqual(learning_container_year.common_title_english,
                         self.form_data['common_title_english'])

    def test_requirement_entity(self):
        form = ProposalBaseForm(self.form_data, self.person,
                                self.learning_unit_year)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        container = self.learning_unit_year.learning_container_year
        container.refresh_from_db()
        self.assertEqual(container.requirement_entity,
                         self.entity_version.entity)

    def test_with_all_entities_set(self):
        today = datetime.date.today()
        entity_1 = EntityFactory(organization=OrganizationFactory(
            type=organization_type.MAIN))
        additional_entity_version_1 = EntityVersionFactory(
            entity_type=entity_type.SCHOOL,
            start_date=today.replace(year=1900),
            end_date=today.replace(year=today.year + 1),
            entity=entity_1)
        entity_2 = EntityFactory(organization=OrganizationFactory(
            type=organization_type.MAIN))
        additional_entity_version_2 = EntityVersionFactory(
            entity_type=entity_type.SCHOOL,
            start_date=today.replace(year=1900),
            end_date=today.replace(year=today.year + 1),
            entity=entity_2)
        self.form_data["allocation_entity"] = self.entity_version.id
        self.form_data["additional_entity_1"] = additional_entity_version_1.id
        self.form_data["additional_entity_2"] = additional_entity_version_2.id

        form = ProposalBaseForm(self.form_data, self.person,
                                self.learning_unit_year)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        self.learning_unit_year.learning_container_year.refresh_from_db()
        entities_by_type = self.learning_unit_year.learning_container_year.get_map_entity_by_type(
        )

        expected_entities = {
            entity_container_year_link_type.REQUIREMENT_ENTITY:
            self.entity_version.entity,
            entity_container_year_link_type.ALLOCATION_ENTITY:
            self.entity_version.entity,
            entity_container_year_link_type.ADDITIONAL_REQUIREMENT_ENTITY_1:
            additional_entity_version_1.entity,
            entity_container_year_link_type.ADDITIONAL_REQUIREMENT_ENTITY_2:
            additional_entity_version_2.entity
        }
        self.assertDictEqual(entities_by_type, expected_entities)

    def test_modify_learning_container_subtype(self):
        self.learning_unit_year.learning_container_year.container_type = learning_container_year_types.INTERNSHIP
        self.learning_unit_year.internship_subtype = internship_subtypes.CLINICAL_INTERNSHIP
        self.learning_unit_year.learning_container_year.save()
        self.learning_unit_year.save()
        self.form_data[
            "container_type"] = learning_container_year_types.INTERNSHIP
        self.form_data[
            "internship_subtype"] = internship_subtypes.TEACHING_INTERNSHIP

        form = ProposalBaseForm(self.form_data, self.person,
                                self.learning_unit_year)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        self.learning_unit_year.refresh_from_db()

        self.assertEqual(
            self.learning_unit_year.learning_container_year.container_type,
            learning_container_year_types.INTERNSHIP)
        self.assertEqual(self.learning_unit_year.internship_subtype,
                         internship_subtypes.TEACHING_INTERNSHIP)

    def test_creation_proposal_learning_unit(self):
        self.maxDiff = None
        form = ProposalBaseForm(self.form_data, self.person,
                                self.learning_unit_year)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        a_proposal_learning_unt = proposal_learning_unit.find_by_learning_unit_year(
            self.learning_unit_year)

        self.assertEqual(a_proposal_learning_unt.type, PROPOSAL_TYPE)
        self.assertEqual(a_proposal_learning_unt.state, PROPOSAL_STATE)
        self.assertEqual(a_proposal_learning_unt.author, self.person)
        self.assertDictEqual(a_proposal_learning_unt.initial_data,
                             self._get_initial_data_expected())

    def _get_initial_data_expected(self):
        initial_data_expected = build_initial_data(self.learning_unit_year,
                                                   self.entity_version.entity)
        initial_data_expected["learning_unit_year"]["credits"] = '5.00'
        initial_data_expected['entities'] = {
            entity_container_year_link_type.REQUIREMENT_ENTITY:
            self.entity_version.entity.id,
            entity_container_year_link_type.ALLOCATION_ENTITY:
            None,
            entity_container_year_link_type.ADDITIONAL_REQUIREMENT_ENTITY_1:
            None,
            entity_container_year_link_type.ADDITIONAL_REQUIREMENT_ENTITY_2:
            None
        }
        return initial_data_expected

    def test_when_setting_additional_entity_to_none(self):
        self.form_data['additional_entity_1'] = None
        form = ProposalBaseForm(self.form_data, self.person,
                                self.learning_unit_year)
        self.assertTrue(form.is_valid(), form.errors)
        form.save()

        self.learning_unit_year.learning_container_year.refresh_from_db()
        self.assertIsNone(self.learning_unit_year.learning_container_year.
                          additional_entity_1)

    def test_creation_proposal_learning_unit_with_school_entity(self):
        self.entity_version.entity_type = SCHOOL
        self.entity_version.save()
        form = ProposalBaseForm(self.form_data, self.person,
                                self.learning_unit_year)
        self.assertTrue('entity' in form.errors[0])

    def test_creation_proposal_learning_unit_with_not_linked_entity(self):
        self.person_entity.entity = self.an_entity_school
        self.person_entity.save()
        form = ProposalBaseForm(self.form_data, self.person,
                                self.learning_unit_year)
        self.assertTrue('entity' in form.errors[1])
class TestLearningUnitModificationProposal(TestCase):
    def setUp(self):
        today = datetime.date.today()

        self.person = PersonFactory()
        an_organization = OrganizationFactory(type=organization_type.MAIN)
        self.learning_unit_year = LearningUnitYearFakerFactory(acronym="LOSIS1212",
                                                               subtype=learning_unit_year_subtypes.FULL)
        self.learning_unit_year.academic_year.start_date = today - datetime.timedelta(days=15)
        self.learning_unit_year.academic_year.end_date = today + datetime.timedelta(days=15)
        self.learning_unit_year.academic_year.year = today.year
        self.learning_unit_year.academic_year.save()
        self.learning_unit_year.learning_container_year.container_type = learning_container_year_types.COURSE
        self.learning_unit_year.learning_container_year.save()
        self.learning_unit_year.learning_container_year.campus.organization = an_organization
        self.learning_unit_year.learning_container_year.campus.is_administration = True
        self.learning_unit_year.learning_container_year.campus.save()

        an_entity = EntityFactory(organization=an_organization)
        self.entity_version = EntityVersionFactory(entity=an_entity, entity_type=entity_type.SCHOOL,
                                                   start_date=today - datetime.timedelta(days=25),
                                                   end_date=today.replace(year=today.year + 1))
        self.requirement_entity = EntityContainerYearFactory(
            learning_container_year=self.learning_unit_year.learning_container_year,
            entity=self.entity_version.entity,
            type=entity_container_year_link_type.REQUIREMENT_ENTITY
        )
        self.allocation_entity = EntityContainerYearFactory(
            learning_container_year=self.learning_unit_year.learning_container_year,
            entity=self.entity_version.entity,
            type=entity_container_year_link_type.ALLOCATION_ENTITY
        )
        self.additional_entity_1= EntityContainerYearFactory(
            learning_container_year=self.learning_unit_year.learning_container_year,
            entity=self.entity_version.entity,
            type=entity_container_year_link_type.ADDITIONAL_REQUIREMENT_ENTITY_1
        )
        self.additional_entity_2 = EntityContainerYearFactory(
            learning_container_year=self.learning_unit_year.learning_container_year,
            entity=self.entity_version.entity,
            type=entity_container_year_link_type.ADDITIONAL_REQUIREMENT_ENTITY_2
        )

        self.person_entity = PersonEntityFactory(person=self.person, entity=an_entity, with_child=True)

        self.client.force_login(self.person.user)
        self.url = reverse('learning_unit_modification_proposal', args=[self.learning_unit_year.id])

        self.form_data = {
            "academic_year": self.learning_unit_year.academic_year.id,
            "first_letter": self.learning_unit_year.acronym[0],
            "acronym": self.learning_unit_year.acronym[1:],
            "title": self.learning_unit_year.title,
            "title_english": self.learning_unit_year.title_english,
            "container_type": self.learning_unit_year.learning_container_year.container_type,
            "internship_subtype": "",
            "credits": self.learning_unit_year.credits,
            "periodicity": self.learning_unit_year.learning_unit.periodicity,
            "status": self.learning_unit_year.status,
            "language": self.learning_unit_year.learning_container_year.language.id,
            "quadrimester": "",
            "campus": self.learning_unit_year.learning_container_year.campus.id,
            "requirement_entity": self.entity_version.id,
            "allocation_entity": self.entity_version.id,
            "additional_entity_1": self.entity_version.id,
            "additional_entity_2": self.entity_version.id,
            "folder_entity": self.entity_version.id,
            "folder_id": "1",
        }

    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_with_non_existent_learning_unit_year(self):
        self.learning_unit_year.delete()
        response = self.client.get(self.url)

        self.assertEqual(response.status_code, HttpResponseNotFound.status_code)
        self.assertTemplateUsed(response, "page_not_found.html")

    def test_with_none_person(self):
        self.person.delete()
        response = self.client.get(self.url)

        self.assertEqual(response.status_code, HttpResponseNotFound.status_code)
        self.assertTemplateUsed(response, "page_not_found.html")

    def test_get_request(self):
        response = self.client.get(self.url)

        self.assertEqual(response.status_code, HttpResponse.status_code)
        self.assertTemplateUsed(response, 'proposal/learning_unit_modification.html')
        self.assertEqual(response.context['learning_unit_year'], self.learning_unit_year)
        self.assertEqual(response.context['experimental_phase'], True)
        self.assertEqual(response.context['person'], self.person)

        self.assertIsInstance(response.context['form'], LearningUnitProposalModificationForm)
        form_initial = response.context['form'].initial
        self.assertEqual(form_initial['academic_year'], self.learning_unit_year.academic_year.pk)
        self.assertEqual(form_initial['first_letter'], self.learning_unit_year.acronym[0])
        self.assertEqual(form_initial['acronym'], self.learning_unit_year.acronym[1:])
        self.assertEqual(form_initial['title'], self.learning_unit_year.title)
        self.assertEqual(form_initial['container_type'], self.learning_unit_year.
                         learning_container_year.container_type)
        self.assertEqual(form_initial['subtype'], self.learning_unit_year.subtype)
        self.assertEqual(form_initial['credits'], self.learning_unit_year.credits)
        self.assertEqual(form_initial['periodicity'], self.learning_unit_year.learning_unit.periodicity)
        self.assertEqual(form_initial['status'], self.learning_unit_year.status)
        self.assertEqual(form_initial['language'], self.learning_unit_year.learning_container_year.language)
        self.assertEqual(form_initial['requirement_entity'], self.entity_version)
        self.assertEqual(form_initial['allocation_entity'], self.entity_version)
        self.assertEqual(form_initial['additional_entity_1'], self.entity_version)
        self.assertEqual(form_initial['additional_entity_2'], self.entity_version)
        self.assertEqual(form_initial['campus'], self.learning_unit_year.learning_container_year.campus)

    def test_post_request_with_invalid_form(self):
        response = self.client.post(self.url, data={})

        self.assertEqual(response.status_code, HttpResponse.status_code)
        self.assertTemplateUsed(response, 'proposal/learning_unit_modification.html')
        self.assertEqual(response.context['learning_unit_year'], self.learning_unit_year)
        self.assertEqual(response.context['experimental_phase'], True)
        self.assertEqual(response.context['person'], self.person)
        self.assertIsInstance(response.context['form'], LearningUnitProposalModificationForm)

    def test_post_request(self):
        response = self.client.post(self.url, data=self.form_data)

        redirected_url = reverse('learning_unit', args=[self.learning_unit_year.id])
        self.assertRedirects(response, redirected_url, fetch_redirect_response=False)

        folder = proposal_folder.find_by_entity_and_folder_id(self.entity_version.entity, 1)
        a_proposal_learning_unit = proposal_learning_unit.find_by_learning_unit_year(self.learning_unit_year)
        self.assertTrue(folder)
        self.assertTrue(a_proposal_learning_unit)
        self.assertEqual(a_proposal_learning_unit.author, self.person)

        messages = [str(message) for message in get_messages(response.wsgi_request)]
        self.assertIn(_("success_modification_proposal").format(proposal_type.ProposalType.MODIFICATION.name,
                                                                self.learning_unit_year.acronym),
                      list(messages))

    def test_transformation_proposal_request(self):
        self.form_data["acronym"] = "OSIS1452"
        self.client.post(self.url, data=self.form_data)

        a_proposal_learning_unit = proposal_learning_unit.find_by_learning_unit_year(self.learning_unit_year)
        self.assertEqual(a_proposal_learning_unit.type, proposal_type.ProposalType.TRANSFORMATION.name)

    def test_modification_proposal_request(self):
        self.form_data["title"] = "New title"
        self.form_data["title_english"] = "New english title"
        self.client.post(self.url, data=self.form_data)

        a_proposal_learning_unit = proposal_learning_unit.find_by_learning_unit_year(self.learning_unit_year)
        self.assertEqual(a_proposal_learning_unit.type, proposal_type.ProposalType.MODIFICATION.name)

    def test_transformation_and_modification_proposal_request(self):
        self.form_data["acronym"] = "OSIS1452"
        self.form_data["title"] = "New title"
        self.form_data["title_english"] = "New english title"
        self.client.post(self.url, data=self.form_data)

        a_proposal_learning_unit = proposal_learning_unit.find_by_learning_unit_year(self.learning_unit_year)
        self.assertEqual(a_proposal_learning_unit.type, proposal_type.ProposalType.TRANSFORMATION_AND_MODIFICATION.name)

    def test_learning_unit_must_be_full(self):
        self.learning_unit_year.subtype = learning_unit_year_subtypes.PARTIM
        self.learning_unit_year.save()

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

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

    def test_proposal_already_exists(self):
        ProposalLearningUnitFactory(learning_unit_year=self.learning_unit_year)
        response = self.client.get(self.url)

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

    def test_academic_year_inferior_to_current(self):
        today = datetime.date.today()
        self.learning_unit_year.academic_year = \
            AcademicYearFakerFactory(year=today.year-1, start_date=today.replace(day=1, year=today.year-1),
                                     end_date=today.replace(day=20, year=today.year-1))
        self.learning_unit_year.save()

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

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

    def test_not_linked_to_entity(self):
        self.person_entity.delete()
        response = self.client.get(self.url)

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

    def test_not_linked_to_requirement_entity(self):
        today = datetime.date.today()
        an_entity = EntityFactory(organization=OrganizationFactory(type=organization_type.MAIN))
        an_entity_version = EntityVersionFactory(entity=an_entity, entity_type=entity_type.SCHOOL,
                                                 start_date=today - datetime.timedelta(days=25),
                                                 end_date=today.replace(year=today.year + 1))

        self.requirement_entity.entity = an_entity_version.entity
        self.requirement_entity.save()

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

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

    def test_linked_to_parent_entity(self):
        today = datetime.date.today()
        parent_entity = EntityFactory(organization=OrganizationFactory(type=organization_type.MAIN))
        EntityVersionFactory(entity=parent_entity, entity_type=entity_type.SCHOOL,
                             start_date=today - datetime.timedelta(days=25),
                             end_date=today.replace(year=today.year + 1))

        self.entity_version.parent = parent_entity
        self.entity_version.save()

        self.person_entity.entity = parent_entity
        self.person_entity.save()

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

        self.assertEqual(response.status_code, HttpResponse.status_code)
        self.assertTemplateUsed(response, 'proposal/learning_unit_modification.html')

    def test_linked_to_child_entity(self):
        today = datetime.date.today()
        child_entity = EntityFactory(organization=OrganizationFactory(type=organization_type.MAIN))
        EntityVersionFactory(entity=child_entity, entity_type=entity_type.SCHOOL,
                             start_date=today - datetime.timedelta(days=25),
                             end_date=today.replace(year=today.year + 1),
                             parent=self.entity_version.entity)

        self.person_entity.entity = child_entity
        self.person_entity.save()

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

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