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 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")