Example #1
0
class TestLearningUnitSummaryEdit(TestCase):
    def setUp(self):
        today = datetime.date.today()
        academic_year = AcademicYearFakerFactory(start_date=today-datetime.timedelta(days=3), year=today.year,
                                                 end_date=today + datetime.timedelta(days=5))
        self.summary_course_submission_calendar = \
            AcademicCalendarFactory(academic_year=academic_year,
                                    start_date=academic_year.start_date,
                                    end_date=academic_year.end_date,
                                    reference=academic_calendar_type.SUMMARY_COURSE_SUBMISSION)

        self.tutor = TutorFactory()

        learning_container_year = LearningContainerYearFactory(academic_year=academic_year)
        self.learning_unit_year = LearningUnitYearFakerFactory(academic_year=academic_year,
                                                               learning_container_year=learning_container_year)
        self.attribution = AttributionFactory(learning_unit_year=self.learning_unit_year, summary_responsible=True,
                                              tutor=self.tutor)

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

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

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

    def test_summary_course_submission_calendar_is_not_opened(self):
        today = datetime.date.today()
        self.summary_course_submission_calendar.start_date = today-datetime.timedelta(days=5)
        self.summary_course_submission_calendar.end_date = today - datetime.timedelta(days=3)
        self.summary_course_submission_calendar.save()
        response = self.client.get(self.url)

        self.assertRedirects(response, reverse("outside_summary_submission_period"))

    def test_summary_course_submission_calendar_is_not_set(self):
        self.summary_course_submission_calendar.delete()
        response = self.client.get(self.url)

        self.assertRedirects(response, reverse("outside_summary_submission_period"))

    def test_user_is_not_a_tutor(self):
        person = PersonFactory()
        self.client.force_login(person.user)

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

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

    def test_when_learning_unit_year_does_not_exist(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_when_user_is_not_attributed_to_the_learning_unit(self):
        self.attribution.delete()
        response = self.client.get(self.url)

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

    def test_when_user_is_not_summary_responsible_of_the_learning_unit(self):
        self.attribution.summary_responsible = False
        self.attribution.save()
        response = self.client.get(self.url)

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

    def test_valid_get_request(self):
        language = "en"
        text_label = TextLabelFactory()
        response = self.client.get(self.url, data={"language": language, "label": text_label.label})

        self.assertTemplateUsed(response, "my_osis/educational_information_edit.html")

        context = response.context
        self.assertEqual(context["learning_unit_year"], self.learning_unit_year)
        self.assertTrue(context["form"])
        self.assertEqual(context["text_label_translated"], None)
        self.assertEqual(context["language_translated"], ('en', _('English')))

    def test_valid_get_request_with_translated_text_labels(self):
        language = "fr-be"
        self.tutor.person.language = language
        self.tutor.person.save()
        trans_text_label = TranslatedTextLabelFactory()
        response = self.client.get(self.url, data={"language": language, "label": trans_text_label.text_label.label})

        self.assertTemplateUsed(response, "my_osis/educational_information_edit.html")

        context = response.context
        self.assertEqual(context["learning_unit_year"], self.learning_unit_year)
        self.assertTrue(context["form"])
        self.assertEqual(context["text_label_translated"], trans_text_label)
        self.assertEqual(context["language_translated"], ('fr-be', _('French')))

    def test_invalid_post_request(self):
        response = self.client.post(self.url, data={"trans_text": "Hello world!!"})
        self.assertRedirects(response, reverse("learning_unit_summary", args=[self.learning_unit_year.id]))

    def test_valid_post_request(self):
        new_text = "Hello world!!"
        translated_text = TranslatedTextFactory()
        response = self.client.post(self.url, data={"trans_text": new_text, "cms_id": translated_text.id})

        self.assertRedirects(response, reverse("learning_unit_summary", args=[self.learning_unit_year.id]))
        translated_text.refresh_from_db()
        self.assertEqual(translated_text.text, new_text)
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,
            learning_container_year__academic_year=self.academic_year,
            learning_container_year__in_charge=True)
        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(len(response.context['attributions']), 1)
        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(len(response.context['attributions']), 1)
        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):
        self.attribution.delete()
        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(ATTRIBUTION_CONFIG={})
    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(len(response.context['attributions']), 1)
        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(len(response.context['attributions']), 1)
        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.complete_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']), 3)

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