Exemplo n.º 1
0
class DissertationViewTestCase(TestCase):
    fixtures = [
        'dissertation/fixtures/message_template.json',
    ]

    def setUp(self):
        self.maxDiff = None
        self.manager = AdviserManagerFactory()
        a_person_teacher = PersonFactory.create(
            first_name='Pierre',
            last_name='Dupont',
            email='*****@*****.**')
        self.teacher = AdviserTeacherFactory(person=a_person_teacher)
        a_person_teacher2 = PersonFactory.create(
            first_name='Marco',
            last_name='Millet',
            email='*****@*****.**')
        self.teacher2 = AdviserTeacherFactory(person=a_person_teacher2)
        a_person_student = PersonFactory.create(
            last_name="Durant",
            user=None,
            email='*****@*****.**')
        self.student = StudentFactory.create(person=a_person_student)
        self.offer1 = OfferFactory(title="test_offer1")
        self.offer2 = OfferFactory(title="test_offer2")
        self.academic_year1 = AcademicYearFactory()
        self.academic_year2 = AcademicYearFactory(
            year=self.academic_year1.year - 1)
        self.offer_year_start1 = OfferYearFactory(
            acronym="test_offer1",
            offer=self.offer1,
            academic_year=self.academic_year1)
        self.offer_year_start2 = OfferYearFactory(
            acronym="test_offer2",
            offer=self.offer2,
            academic_year=self.academic_year1)
        self.offer_proposition1 = OfferPropositionFactory(
            offer=self.offer1,
            global_email_to_commission=True,
            evaluation_first_year=True)
        self.offer_proposition2 = OfferPropositionFactory(
            offer=self.offer2, global_email_to_commission=False)
        self.proposition_dissertation = PropositionDissertationFactory(
            author=self.teacher,
            creator=a_person_teacher,
            title='Proposition 1212121')
        FacultyAdviserFactory(adviser=self.manager, offer=self.offer1)
        self.dissertation_test_email = DissertationFactory(
            author=self.student,
            title='Dissertation_test_email',
            offer_year_start=self.offer_year_start1,
            proposition_dissertation=self.proposition_dissertation,
            status='DRAFT',
            active=True,
            dissertation_role__adviser=self.teacher,
            dissertation_role__status='PROMOTEUR')

        FacultyAdviserFactory(adviser=self.manager, offer=self.offer1)
        self.manager2 = AdviserManagerFactory()
        FacultyAdviserFactory(adviser=self.manager, offer=self.offer2)
        roles = [
            'PROMOTEUR', 'CO_PROMOTEUR', 'READER', 'PROMOTEUR', 'ACCOMPANIST',
            'PRESIDENT'
        ]
        status = [
            'DRAFT', 'COM_SUBMIT', 'EVA_SUBMIT', 'TO_RECEIVE', 'DIR_SUBMIT',
            'DIR_SUBMIT'
        ]
        self.dissertations_list = list()
        for x in range(0, 6):
            proposition_dissertation = PropositionDissertationFactory(
                author=self.teacher,
                creator=a_person_teacher,
                title='Proposition {}'.format(x))
            PropositionOfferFactory(
                proposition_dissertation=proposition_dissertation,
                offer_proposition=self.offer_proposition1)

            self.dissertations_list.append(
                DissertationFactory(
                    author=self.student,
                    title='Dissertation {}'.format(x),
                    offer_year_start=self.offer_year_start1,
                    proposition_dissertation=proposition_dissertation,
                    status=status[x],
                    active=True,
                    dissertation_role__adviser=self.teacher,
                    dissertation_role__status=roles[x]))
        self.dissertation_1 = DissertationFactory(
            author=self.student,
            title='Dissertation 2017',
            offer_year_start=self.offer_year_start1,
            proposition_dissertation=proposition_dissertation,
            status='COM_SUBMIT',
            active=True,
            dissertation_role__adviser=self.teacher2,
            dissertation_role__status='PROMOTEUR')

    def test_get_dissertations_list_for_teacher(self):
        self.client.force_login(self.teacher.person.user)
        url = reverse('dissertations_list')
        response = self.client.get(url)
        self.assertEqual(
            response.context[-1]['adviser_list_dissertations'].count(),
            1)  # only 1 because 1st is DRAFT
        self.assertEqual(
            response.context[-1]['adviser_list_dissertations_copro'].count(),
            1)
        self.assertEqual(
            response.context[-1]['adviser_list_dissertations_reader'].count(),
            1)
        self.assertEqual(
            response.context[-1]
            ['adviser_list_dissertations_accompanist'].count(), 1)
        self.assertEqual(
            response.context[-1]
            ['adviser_list_dissertations_president'].count(), 1)

    def test_get_dissertations_list_for_manager(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_list')
        response = self.client.get(url)
        self.assertEqual(response.context[-1]['dissertations'].count(), 8)
        self.assertCountEqual(
            response.context[-1]['dissertations'], [self.dissertation_1] +
            [self.dissertation_test_email] + self.dissertations_list)

    def test_search_dissertations_for_manager_1(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(url, data={"search": "no result search"})
        self.assertEqual(response.status_code, HTTP_OK)

    def test_search_dissertations_for_manager_2(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(url, data={"search": "Dissertation 2"})
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(response.context[-1]['dissertations'].count(), 2)
        self.assertCountEqual(
            response.context[-1]['dissertations'],
            [self.dissertation_1, self.dissertations_list[2]])

    def test_search_dissertations_for_manager_3(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(url, data={"search": "Proposition 3"})
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(response.context[-1]['dissertations'].count(), 1)
        self.assertCountEqual(response.context[-1]['dissertations'],
                              [self.dissertations_list[3]])

    def test_search_dissertations_for_manager_4(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(url, data={"search": "Dissertation"})
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(response.context[-1]['dissertations'].count(), 8)
        self.assertCountEqual(
            response.context[-1]['dissertations'], [self.dissertation_1] +
            [self.dissertation_test_email] + self.dissertations_list)

    def test_search_dissertations_for_manager_5(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(url,
                                   data={
                                       "search":
                                       "Dissertation",
                                       "offer_prop_search":
                                       self.offer_proposition1.id
                                   })
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(response.context[-1]['dissertations'].count(), 8)
        self.assertCountEqual(
            response.context[-1]['dissertations'], [self.dissertation_1] +
            [self.dissertation_test_email] + self.dissertations_list)

    def test_search_dissertations_for_manager_6(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(url,
                                   data={
                                       "search":
                                       "Dissertation",
                                       "offer_prop_search":
                                       self.offer_proposition2.id
                                   })
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(response.context[-1]['dissertations'].count(), 0)

    def test_search_dissertations_for_manager_7(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(
            url, data={"academic_year": self.academic_year1.id})
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(response.context[-1]['dissertations'].count(), 8)
        self.assertCountEqual(
            response.context[-1]['dissertations'], [self.dissertation_1] +
            [self.dissertation_test_email] + self.dissertations_list)

    def test_search_dissertations_for_manager_8(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(
            url, data={"academic_year": self.academic_year2.id})
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(response.context[-1]['dissertations'].count(), 0)

    def test_search_dissertations_for_manager_9(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(url, data={"status_search": "COM_SUBMIT"})
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(response.context[-1]['dissertations'].count(), 2)

    def test_search_dissertations_for_manager_10(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(url, data={"search": "test_offer"})
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(response.context[-1]['dissertations'].count(), 8)
        self.assertCountEqual(
            response.context[-1]['dissertations'], [self.dissertation_1] +
            [self.dissertation_test_email] + self.dissertations_list)

    def test_search_dissertations_for_manager_11(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(url, data={"search": "Durant"})
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(response.context[-1]['dissertations'].count(), 8)
        self.assertCountEqual(
            response.context[-1]['dissertations'], [self.dissertation_1] +
            [self.dissertation_test_email] + self.dissertations_list)

    def test_search_dissertations_for_manager_12(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_dissertations_search')
        response = self.client.get(url, data={"search": "Dupont"})
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(response.context[-1]['dissertations'].count(), 8)
        self.assertCountEqual(
            response.context[-1]['dissertations'], [self.dissertation_1] +
            [self.dissertation_test_email] + self.dissertations_list)

    def test_manager_accept(self):
        self.dissertation_test_email.status = 'EVA_SUBMIT'
        self.dissertation_test_email.manager_accept()
        self.assertEqual(self.dissertation_test_email.status, 'TO_RECEIVE')
        self.dissertation_test_email.status = 'EVA_KO'
        self.dissertation_test_email.manager_accept()
        self.assertEqual(self.dissertation_test_email.status, 'TO_RECEIVE')
        self.dissertation_test_email.status = 'DEFENDED'
        self.dissertation_test_email.manager_accept()
        self.assertEqual(self.dissertation_test_email.status, 'ENDED_WIN')
        self.dissertation_test_email.status = 'DRAFT'
        self.dissertation_test_email.manager_accept()
        self.assertEqual(self.dissertation_test_email.status, 'DRAFT')

    def test_adviser_can_manage_dissertation(self):
        manager = AdviserManagerFactory()
        manager2 = AdviserManagerFactory()
        a_person_teacher = PersonFactory.create(first_name='Pierre',
                                                last_name='Dupont')
        a_person_teacher2 = PersonFactory.create(first_name='Marco',
                                                 last_name='Millet')
        teacher = AdviserTeacherFactory(person=a_person_teacher)
        a_person_student = PersonFactory.create(last_name="Durant", user=None)
        student = StudentFactory.create(person=a_person_student)
        offer_year_start = OfferYearFactory(academic_year=self.academic_year1,
                                            acronym="test_offer2")
        offer_year_start2 = OfferYearFactory(
            acronym="test_offer3",
            academic_year=offer_year_start.academic_year)
        offer = offer_year_start.offer
        offer2 = offer_year_start2.offer
        FacultyAdviserFactory(adviser=manager, offer=offer)
        create_faculty_adviser(manager, offer)
        create_faculty_adviser(manager2, offer2)
        proposition_dissertation = PropositionDissertationFactory(
            author=teacher, creator=a_person_teacher, title='Proposition1')
        dissertation = DissertationFactory(
            author=student,
            title='Dissertation 2017',
            offer_year_start=offer_year_start,
            proposition_dissertation=proposition_dissertation,
            status='DIR_SUBMIT',
            active=True,
            dissertation_role__adviser=teacher,
            dissertation_role__status='PROMOTEUR')
        self.assertEqual(adviser_can_manage(dissertation, manager), True)
        self.assertEqual(adviser_can_manage(dissertation, manager2), False)
        self.assertEqual(adviser_can_manage(dissertation, teacher), False)

    def test_email_new_dissert(self):
        self.client.force_login(self.manager.person.user)
        count_messages_before_status_change = message_history.find_my_messages(
            self.teacher.person.id).count()
        self.dissertation_test_email.status = 'DRAFT'
        self.dissertation_test_email.go_forward()
        message_history_result = message_history.find_my_messages(
            self.teacher.person.id)
        self.assertEqual(count_messages_before_status_change + 1,
                         len(message_history_result))
        self.assertIsNotNone(
            message_template.find_by_reference(
                'dissertation_adviser_new_project_dissertation_txt'))
        self.assertIsNotNone(
            message_template.find_by_reference(
                'dissertation_adviser_new_project_dissertation_html'))
        self.assertIn('Vous avez reçu une demande d\'encadrement de mémoire',
                      message_history_result.last().subject)

    def test_email_new_dissert_refuse(self):
        count_messages_before_status_change = message_history.find_my_messages(
            self.dissertation_test_email.author.person.id).count()
        self.dissertation_test_email.status = 'DIR_SUBMIT'
        self.dissertation_test_email.refuse()
        message_history_result = message_history.find_my_messages(
            self.dissertation_test_email.author.person.id)
        self.assertEqual(count_messages_before_status_change + 1,
                         len(message_history_result))
        self.assertIn(
            'Votre projet de mémoire n\'a pas été validé par votre promoteur',
            message_history_result.last().subject)

    def test_email_new_dissert_accept(self):
        count_messages_before_status_change = message_history.find_my_messages(
            self.dissertation_test_email.author.person.id).count()
        self.dissertation_test_email.status = 'DIR_SUBMIT'
        self.dissertation_test_email.go_forward()
        self.dissertation_test_email.manager_accept()
        message_history_result_after = message_history.find_my_messages(
            self.dissertation_test_email.author.person.id)
        self.assertIn('Votre projet de mémoire est validé par votre promoteur',
                      message_history_result_after.last().subject)
        self.assertEqual(count_messages_before_status_change + 1,
                         len(message_history_result_after))

    def test_email_dissert_commission_refuse(self):
        count_message_history_result_author = message_history. \
            find_my_messages(self.dissertation_test_email.author.person.id).count()
        count_message_history_result_promotor = len(
            message_history.find_my_messages(self.teacher.person.id))
        self.dissertation_test_email.status = 'COM_SUBMIT'
        self.dissertation_test_email.refuse()
        message_history_result_author_after_change = message_history.find_my_messages(
            self.dissertation_test_email.author.person.id)
        message_history_result_promotor_after_change = message_history.find_my_messages(
            self.teacher.person.id)
        self.assertEqual(count_message_history_result_author + 1,
                         len(message_history_result_author_after_change))
        self.assertEqual(count_message_history_result_promotor + 1,
                         len(message_history_result_promotor_after_change))
        self.assertIn(
            'La commission Mémoires n\'a pas validé le projet de mémoire',
            message_history_result_promotor_after_change.last().subject)
        self.assertIn(
            'La commission Mémoires n\'a pas validé votre projet de mémoire',
            message_history_result_author_after_change.last().subject)

    def test_email_dissert_commission_accept_1(self):
        count_message_history_result_author = message_history. \
            find_my_messages(self.dissertation_test_email.author.person.id).count()
        count_message_history_result_promotor = len(
            message_history.find_my_messages(self.teacher.person.id))
        self.dissertation_test_email.status = 'COM_KO'
        self.dissertation_test_email.manager_accept()
        message_history_result_author_after_change = message_history.find_my_messages(
            self.dissertation_test_email.author.person.id)
        message_history_result_promotor_after_change = message_history.find_my_messages(
            self.teacher.person.id)
        self.assertEqual(count_message_history_result_author + 1,
                         len(message_history_result_author_after_change))
        self.assertEqual(count_message_history_result_promotor + 1,
                         len(message_history_result_promotor_after_change))
        self.assertIn(
            'La commission Mémoires a accepté le projet de Mémoire :',
            message_history_result_promotor_after_change.last().subject)
        self.assertIn(
            'La commission Mémoires a accepté votre projet de mémoire',
            message_history_result_author_after_change.last().subject)

    def test_email_dissert_commission_accept_2(self):
        count_message_history_result_author = message_history. \
            find_my_messages(self.dissertation_test_email.author.person.id).count()
        count_message_history_result_promotor = len(
            message_history.find_my_messages(self.teacher.person.id))
        self.dissertation_test_email.status = 'COM_SUBMIT'
        self.dissertation_test_email.manager_accept()
        message_history_result_author_after_change = message_history.find_my_messages(
            self.dissertation_test_email.author.person.id)
        message_history_result_promotor_after_change = message_history.find_my_messages(
            self.teacher.person.id)
        self.assertEqual(count_message_history_result_author + 1,
                         len(message_history_result_author_after_change))
        self.assertEqual(count_message_history_result_promotor + 1,
                         len(message_history_result_promotor_after_change))
        self.assertIn(
            'La commission Mémoires a accepté le projet de Mémoire :',
            message_history_result_promotor_after_change.last().subject)
        self.assertIn(
            'La commission Mémoires a accepté votre projet de mémoire',
            message_history_result_author_after_change.last().subject)

    def test_email_dissert_commission_accept_3(self):
        dissert = DissertationFactory(
            author=self.student,
            title='Dissertation_test_email',
            offer_year_start=self.offer_year_start2,
            proposition_dissertation=self.proposition_dissertation,
            status='COM_SUBMIT',
            active=True,
            dissertation_role__adviser=self.teacher,
            dissertation_role__status='PROMOTEUR')
        count_message_history_result_author = \
            message_history.find_my_messages(dissert.author.person.id).count()
        count_message_history_result_promotor = \
            message_history.find_my_messages(self.teacher.person.id).count()
        dissert.manager_accept()
        message_history_result_author_after_change = message_history.find_my_messages(
            dissert.author.person.id)
        message_history_result_promotor_after_change = message_history.find_my_messages(
            self.teacher.person.id)
        self.assertEqual(count_message_history_result_author + 1,
                         len(message_history_result_author_after_change))
        self.assertEqual(count_message_history_result_promotor,
                         len(message_history_result_promotor_after_change))
        self.assertIn(
            'La commission Mémoires a accepté votre projet de mémoire',
            message_history_result_author_after_change.last().subject)

    def test_email_dissert_acknowledgement(self):
        count_message_history_author = message_history. \
            find_my_messages(self.dissertation_test_email.author.person.id).count()
        self.dissertation_test_email.status = 'TO_RECEIVE'
        self.dissertation_test_email.go_forward()
        message_history_result_author_after_change = message_history.find_my_messages(
            self.dissertation_test_email.author.person.id)
        count_message_history_result_author = len(
            message_history_result_author_after_change)
        self.assertEqual(count_message_history_author + 1,
                         count_message_history_result_author)
        self.assertIn(
            'bien été réceptionné',
            message_history_result_author_after_change.last().subject)

    def test_get_all_advisers(self):
        res = adviser.find_all_advisers()
        self.assertEqual(res.count(), 4)

    def test_find_by_last_name_or_email(self):
        res = adviser.find_advisers_last_name_email('Dupont',
                                                    MAXIMUM_IN_REQUEST)
        self.assertEqual(len(res), 1)
        self.assertEqual(res[0].person.last_name, 'Dupont')
        res = adviser.find_advisers_last_name_email(None, MAXIMUM_IN_REQUEST)
        self.assertEqual(len(res), 0)

    def test_get_adviser_list_json(self):
        self.client.force_login(self.manager.person.user)
        response = self.client.get('/dissertation/find_adviser_list/',
                                   {'term': 'Dupont'})
        self.assertEqual(response.status_code, HTTP_OK)
        data_json = response.json()
        self.assertNotEqual(len(data_json), 0)
        for data in data_json:
            self.assertEqual(data['last_name'], 'Dupont')

    def test_manager_dissert_jury_new_by_ajax1(self):
        self.client.force_login(self.manager.person.user)
        dissert_role_count = dissertation_role.count_by_dissertation(
            self.dissertation_1)
        response = self.client.post(
            '/dissertation/manager_dissertations_jury_new_ajax/',
            {'pk_dissertation': str(self.dissertation_1.id)})
        self.assertEqual(response.status_code, ERROR_405_BAD_REQUEST)
        response = self.client.get(
            '/dissertation/manager_dissertations_jury_new_ajax/',
            {'pk_dissertation': str(self.dissertation_1.id)})
        self.assertEqual(response.status_code, ERROR_405_BAD_REQUEST)
        response = self.client.post(
            '/dissertation/manager_dissertations_jury_new_ajax/', {
                'pk_dissertation': str(self.dissertation_1.id),
                'status_choice': 'READER'
            })
        self.assertEqual(response.status_code, ERROR_405_BAD_REQUEST)
        response = self.client.post(
            '/dissertation/manager_dissertations_jury_new_ajax/', {
                'pk_dissertation': str(self.dissertation_1.id),
                'status_choice': 'READER',
                'adviser_pk': str(self.teacher.id)
            })
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(
            dissert_role_count + 1,
            dissertation_role.count_by_dissertation(self.dissertation_1))

    def test_manager_dissert_jury_del_by_ajax(self):
        self.client.force_login(self.manager.person.user)
        response = self.client.post(
            '/dissertation/manager_dissertations_jury_new_ajax/', {
                'pk_dissertation': str(self.dissertation_1.id),
                'status_choice': 'READER',
                'adviser_pk': str(self.teacher.id)
            })
        self.assertEqual(response.status_code, HTTP_OK)
        liste_dissert_roles = dissertation_role.search_by_dissertation_and_role(
            self.dissertation_1, 'READER')
        self.assertNotEqual(len(liste_dissert_roles), 0)
        for element in liste_dissert_roles:
            dissert_role_count = dissertation_role.count_by_dissertation(
                self.dissertation_1)
            url = "/dissertation/manager_dissertations_role_delete_by_ajax/{role}"
            response2 = self.client.get(url.format(role=str(element.id)))
            self.assertEqual(response2.status_code, HTTP_OK)
            self.assertEqual(
                dissertation_role.count_by_dissertation(self.dissertation_1),
                dissert_role_count - 1)

    def test_manager_dissert_wait_comm_jsonlist(self):
        self.client.force_login(self.manager.person.user)
        response = self.client.post(
            '/dissertation/manager_dissertations_wait_comm_json_list', )
        response_data = json.loads(response.content.decode('utf-8'))
        self.assertEqual(response.status_code, HTTP_OK)
        self.assertEqual(len(response_data), 2)

    def test_manager_dissert_jury_security_ajax(self):
        self.client.force_login(self.manager2.person.user)
        response = self.client.post(
            '/dissertation/manager_dissertations_jury_new_ajax/', {
                'pk_dissertation': str(self.dissertation_1.id),
                'status_choice': 'READER',
                'adviser_pk': str(self.teacher.id)
            })
        self.assertEqual(response.status_code, ERROR_403_NOT_AUTORIZED)
        liste_dissert_roles = dissertation_role.search_by_dissertation_and_role(
            self.dissertation_1, 'READER')
        for element in liste_dissert_roles:
            url = "/dissertation/manager_dissertations_role_delete_by_ajax/{role}"
            response = self.client.get(url.format(role=str(element.id)))
            self.assertEqual(response.status_code, ERROR_403_NOT_AUTORIZED)

    def test_manager_students_list(self):
        self.client.force_login(self.manager.person.user)
        url = reverse('manager_students_list')
        response = self.client.get(url)
        self.assertEqual(response.status_code, HTTP_OK)
Exemplo n.º 2
0
class DissertationModelTestCase(TestCase):
    fixtures = ['dissertation/fixtures/message_template.json', ]

    @classmethod
    def setUpTestData(cls):
        a_person_teacher = PersonFactory(
            first_name='Pierre',
            last_name='Dupont'
        )
        cls.teacher = AdviserTeacherFactory(person=a_person_teacher)
        a_person_student = PersonWithoutUserFactory(
            last_name="Durant",
            first_name='jean'
        )
        cls.student = StudentFactory(person=a_person_student)
        cls.education_group = EducationGroupFactory()
        cls.education_group2 = EducationGroupFactory()
        cls.offer_prop = OfferPropositionFactory(education_group=cls.education_group,
                                                 acronym="test_offer1",
                                                 validation_commission_exists=True)
        cls.proposition_dissertation = PropositionDissertationFactory(author=cls.teacher,
                                                                      title='proposition_x',
                                                                      creator=a_person_teacher)

        cls.academic_year1 = AcademicYearFactory()
        cls.education_group_year = EducationGroupYearFactory(acronym="test_offer1",
                                                             education_group=cls.education_group,
                                                             academic_year=cls.academic_year1)
        cls.education_group_year2 = EducationGroupYearFactory(acronym="test_offer11",
                                                              education_group=cls.education_group2,
                                                              academic_year=cls.academic_year1)
        cls.dissertation_test_email = DissertationFactory(author=cls.student,
                                                          title='Dissertation_test_email',
                                                          education_group_year=cls.education_group_year,
                                                          proposition_dissertation=cls.proposition_dissertation,
                                                          status=dissertation_status.DRAFT,
                                                          active=True,
                                                          dissertation_role__adviser=cls.teacher,
                                                          dissertation_role__status='PROMOTEUR')
        cls.dissertation = DissertationFactory(author=cls.student,
                                               title='Dissertation_1',
                                               education_group_year=cls.education_group_year,
                                               proposition_dissertation=cls.proposition_dissertation,
                                               status=dissertation_status.DIR_SUBMIT,
                                               active=True,
                                               description='les phobies',
                                               dissertation_role__adviser=cls.teacher,
                                               dissertation_role__status='PROMOTEUR')

    def test_deactivate(self):
        self.dissertation = DissertationFactory(active=True)
        self.dissertation.deactivate()
        self.assertEqual(self.dissertation.active, False)

    def test_str(self):
        self.dissertation = DissertationFactory(title="dissert1")
        self.assertEqual(self.dissertation.title, str(self.dissertation))

    def test_set_status(self):
        self.dissertation = DissertationFactory(status=dissertation_status.DRAFT)
        self.dissertation.set_status(dissertation_status.DIR_SUBMIT)
        self.assertEqual(self.dissertation.status, dissertation_status.DIR_SUBMIT)

    def test_go_forward_status(self):
        self.dissertation = DissertationFactory(status=dissertation_status.DRAFT, active=True)
        self.dissertation.go_forward()
        self.assertEqual(dissertation_status.DIR_SUBMIT, self.dissertation.status)
        self.dissertation = DissertationFactory(status=dissertation_status.DIR_KO)
        self.dissertation.go_forward()
        self.assertEqual(dissertation_status.DIR_SUBMIT, self.dissertation.status)
        self.dissertation = DissertationFactory(status=dissertation_status.TO_RECEIVE)
        self.dissertation.go_forward()
        self.assertEqual(dissertation_status.TO_DEFEND, self.dissertation.status)

    def test_go_forward_emails_acknowledge(self):
        self.dissertation_test_email.status = dissertation_status.TO_RECEIVE
        count_message_history_author = message_history.find_my_messages(
            self.dissertation_test_email.author.person.id
        ).count()
        self.dissertation_test_email.go_forward()
        message_history_result_author_after_change = message_history.find_my_messages(
            self.dissertation_test_email.author.person.id
        )
        count_message_history_result_author = len(message_history_result_author_after_change)
        self.assertEqual(count_message_history_author + 1, count_message_history_result_author)
        self.assertIn('bien été réceptionné', message_history_result_author_after_change.last().subject)

    def test_go_forward_emails_new_dissert_1(self):
        self.dissertation_test_email.status = dissertation_status.DRAFT
        count_messages_before_status_change = message_history.find_my_messages(self.teacher.person.id).count()
        self.dissertation_test_email.go_forward()
        message_history_result = message_history.find_my_messages(self.teacher.person.id)
        self.assertEqual(count_messages_before_status_change + 1, len(message_history_result))
        self.assertIsNotNone(message_template.find_by_reference('dissertation_adviser_new_project_dissertation_txt'))
        self.assertIsNotNone(message_template.find_by_reference('dissertation_adviser_new_project_dissertation_html'))
        self.assertIn('Vous avez reçu une demande d\'encadrement de mémoire', message_history_result.last().subject)

    def test_go_forward_emails_new_dissert_2(self):
        count_messages_before_status_change = message_history.find_my_messages(self.teacher.person.id).count()
        self.dissertation_test_email.status = dissertation_status.DIR_KO
        self.dissertation_test_email.go_forward()
        message_history_result = message_history.find_my_messages(self.teacher.person.id)
        self.assertEqual(count_messages_before_status_change + 1, len(message_history_result))
        self.assertIsNotNone(message_template.find_by_reference('dissertation_adviser_new_project_dissertation_txt'))
        self.assertIsNotNone(message_template.find_by_reference('dissertation_adviser_new_project_dissertation_html'))
        self.assertIn('Vous avez reçu une demande d\'encadrement de mémoire', message_history_result.last().subject)

    def test_manager_accept_commission_exist(self):
        self.offer_prop2 = OfferPropositionFactory(
            education_group=self.education_group2,
            validation_commission_exists=True
        )
        self.dissertation1 = DissertationFactory(
            status=dissertation_status.DIR_SUBMIT,
            education_group_year=self.education_group_year2
        )
        self.dissertation1.manager_accept()
        self.assertEqual(self.dissertation1.status, dissertation_status.COM_SUBMIT)

    def test_manager_accept_commission_exist_2(self):
        self.offer_prop2 = OfferPropositionFactory(
            education_group=self.education_group2,
            validation_commission_exists=True,
            evaluation_first_year=True
        )
        self.dissertation1 = DissertationFactory(
            status=dissertation_status.COM_KO,
            education_group_year=self.education_group_year2
        )
        self.dissertation1.manager_accept()
        self.assertEqual(self.dissertation1.status, dissertation_status.EVA_SUBMIT)

    def test_manager_accept_not_commission_exist(self):
        self.offer_prop2 = OfferPropositionFactory(education_group=self.education_group2,
                                                   validation_commission_exists=False,
                                                   evaluation_first_year=False)
        self.dissertation1 = DissertationFactory(status=dissertation_status.DIR_SUBMIT,
                                                 education_group_year=self.education_group_year2)
        self.dissertation1.manager_accept()
        self.assertEqual(self.dissertation1.status, dissertation_status.TO_RECEIVE)

    def test_manager_accept_not_commission_yes_eval(self):
        self.offer_prop2 = OfferPropositionFactory(education_group=self.education_group2,
                                                   validation_commission_exists=False,
                                                   evaluation_first_year=True)
        self.dissertation1 = DissertationFactory(status=dissertation_status.DIR_SUBMIT,
                                                 education_group_year=self.education_group_year2)
        self.dissertation1.manager_accept()
        self.assertEqual(self.dissertation1.status, dissertation_status.EVA_SUBMIT)

    def test_manager_accept_eval_submit(self):
        self.offer_prop2 = OfferPropositionFactory(education_group=self.education_group2,
                                                   validation_commission_exists=False,
                                                   evaluation_first_year=True)
        self.dissertation1 = DissertationFactory(status=dissertation_status.EVA_SUBMIT,
                                                 education_group_year=self.education_group_year2)
        self.dissertation1.manager_accept()
        self.assertEqual(self.dissertation1.status, dissertation_status.TO_RECEIVE)

    def test_manager_accept_eval_KO(self):
        self.offer_prop2 = OfferPropositionFactory(education_group=self.education_group2,
                                                   validation_commission_exists=False,
                                                   evaluation_first_year=True)
        self.dissertation1 = DissertationFactory(status=dissertation_status.EVA_KO,
                                                 education_group_year=self.education_group_year2, )
        self.dissertation1.manager_accept()
        self.assertEqual(self.dissertation1.status, dissertation_status.TO_RECEIVE)

    def test_teacher_accept_1(self):
        count_messages_before_status_change = message_history.find_my_messages(self.student.person.id).count()
        self.offer_prop2 = OfferPropositionFactory(education_group=self.education_group2,
                                                   validation_commission_exists=True,
                                                   evaluation_first_year=True)
        self.dissertation1 = DissertationFactory(status=dissertation_status.DIR_SUBMIT,
                                                 education_group_year=self.education_group_year2,
                                                 author=self.student)
        self.dissertation1.teacher_accept()
        message_history_result = message_history.find_my_messages(self.student.person.id)
        self.assertEqual(count_messages_before_status_change + 1, len(message_history_result))
        self.assertEqual(self.dissertation1.status, dissertation_status.COM_SUBMIT)
        self.assertIn('Votre projet de mémoire est validé par votre promoteur', message_history_result.last().subject)

    def test_teacher_accept_2(self):
        self.dissertation1 = DissertationFactory(status=dissertation_status.DRAFT)
        self.assertEqual(self.dissertation1.teacher_accept(), None)

    def test_refuse_DIR_SUBMIT(self):
        count_messages_before_status_change = message_history.find_my_messages(self.student.person.id).count()
        self.offer_prop2 = OfferPropositionFactory(education_group=self.education_group2, )
        self.dissertation1 = DissertationFactory(status=dissertation_status.DIR_SUBMIT,
                                                 education_group_year=self.education_group_year2,
                                                 author=self.student)
        self.dissertation1.refuse()
        message_history_result = message_history.find_my_messages(self.student.person.id)
        self.assertEqual(count_messages_before_status_change + 1, len(message_history_result))
        self.assertEqual(self.dissertation1.status, dissertation_status.DIR_KO)
        self.assertIn('Votre projet de mémoire n\'a pas été validé par votre promoteur',
                      message_history_result.last().subject)

    def test_refuse_COM_SUBMIT(self):
        count_messages_before_status_change = message_history.find_my_messages(self.student.person.id).count()
        self.offer_prop2 = OfferPropositionFactory(education_group=self.education_group2,
                                                   validation_commission_exists=True,
                                                   evaluation_first_year=True)
        self.dissertation1 = DissertationFactory(status=dissertation_status.COM_SUBMIT,
                                                 education_group_year=self.education_group_year2,
                                                 author=self.student)
        self.dissertation1.refuse()
        message_history_result = message_history.find_my_messages(self.student.person.id)
        self.assertEqual(count_messages_before_status_change + 1, len(message_history_result))
        self.assertEqual(self.dissertation1.status, dissertation_status.COM_KO)
        self.assertIn('n\'a pas validé',
                      message_history_result.last().subject)

    def test_refuse_COM_SUBMIT_2(self):
        count_messages_before_status_change = message_history.find_my_messages(self.teacher.person.id).count()
        self.offer_prop2 = OfferPropositionFactory(education_group=self.education_group2,
                                                   validation_commission_exists=True,
                                                   evaluation_first_year=True)
        self.dissertation1 = DissertationFactory(status=dissertation_status.COM_SUBMIT,
                                                 education_group_year=self.education_group_year2,
                                                 author=self.student,
                                                 dissertation_role__adviser=self.teacher,
                                                 dissertation_role__status='PROMOTEUR'
                                                 )
        self.dissertation1.refuse()
        message_history_result = message_history.find_my_messages(self.teacher.person.id)
        self.assertEqual(count_messages_before_status_change + 1, len(message_history_result))
        self.assertEqual(self.dissertation1.status, dissertation_status.COM_KO)
        self.assertIn('n\'a pas validé le projet de mémoire',
                      message_history_result.last().subject)

    def test_search_with_title_name_first_name(self):
        self.assertCountEqual(dissertation.search('Dissertation_1'), [self.dissertation])
        self.assertCountEqual(
            dissertation.search(dissertation_status.DIR_SUBMIT),
            [self.dissertation]
        )
        self.assertCountEqual(
            dissertation.search('jean'),
            [self.dissertation, self.dissertation_test_email]
        )
        self.assertCountEqual(
            dissertation.search('durant'),
            [self.dissertation, self.dissertation_test_email]
        )
        self.assertCountEqual(
            dissertation.search('Pierre'),
            [self.dissertation, self.dissertation_test_email]
        )

    def test_search_with_subject_offer(self):
        self.assertCountEqual(
            dissertation.search('les phobies'),
            [self.dissertation]
        )
        self.assertCountEqual(
            dissertation.search('proposition_x'),
            [self.dissertation, self.dissertation_test_email]
        )
        self.assertCountEqual(
            dissertation.search('test_offer1'),
            [self.dissertation, self.dissertation_test_email]
        )

    def test_search_by_proposition_author(self):
        self.assertCountEqual(
            dissertation.search_by_proposition_author(None, True, self.teacher),
            [self.dissertation, self.dissertation_test_email]
        )

    def test_search_by_offer(self):
        self.assertCountEqual(dissertation.search_by_education_group([self.education_group]),
                              [self.dissertation, self.dissertation_test_email]
                              )

    def test_search_by_offer_and_status(self):
        self.assertCountEqual(
            dissertation.search_by_education_group_and_status([self.education_group], dissertation_status.DIR_SUBMIT),
            [self.dissertation]
        )

    def test_get_next_status_goforward(self):
        self.dissertation_x = DissertationFactory(status=dissertation_status.DRAFT, active=True)
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "go_forward"),
                         dissertation_status.DIR_SUBMIT)
        self.dissertation_x.status = dissertation_status.DIR_KO
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "go_forward"),
                         dissertation_status.DIR_SUBMIT)
        self.dissertation_x.status = dissertation_status.TO_RECEIVE
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "go_forward"),
                         dissertation_status.TO_DEFEND)
        self.dissertation_x.status = dissertation_status.TO_DEFEND
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "go_forward"),
                         dissertation_status.DEFENDED)
        self.dissertation_x.status = dissertation_status.DIR_SUBMIT
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "go_forward"),
                         dissertation_status.DIR_SUBMIT)

    def test_get_next_status_accept_1(self):
        self.offer_prop2 = OfferPropositionFactory(
            education_group=self.education_group2,
            validation_commission_exists=True,
            evaluation_first_year=True
        )
        self.dissertation_x = DissertationFactory(status=dissertation_status.DIR_SUBMIT,
                                                  education_group_year=self.education_group_year2)
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "accept"),
                         dissertation_status.COM_SUBMIT)

    def test_get_next_status_accept_2(self):
        self.offer_prop2 = OfferPropositionFactory(education_group=self.education_group2,
                                                   validation_commission_exists=False,
                                                   evaluation_first_year=True)
        self.dissertation_x = DissertationFactory(status=dissertation_status.DIR_SUBMIT,
                                                  education_group_year=self.education_group_year2)
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "accept"),
                         dissertation_status.EVA_SUBMIT)
        self.dissertation_x.status = dissertation_status.COM_SUBMIT
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "accept"),
                         dissertation_status.EVA_SUBMIT)
        self.dissertation_x.status = dissertation_status.COM_KO
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "accept"),
                         dissertation_status.EVA_SUBMIT)

    def test_get_next_status_accept_3(self):
        self.offer_prop2 = OfferPropositionFactory(education_group=self.education_group2,
                                                   validation_commission_exists=False,
                                                   evaluation_first_year=True)
        self.dissertation_x = DissertationFactory(status=dissertation_status.EVA_SUBMIT,
                                                  education_group_year=self.education_group_year2)
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "accept"),
                         dissertation_status.TO_RECEIVE)
        self.dissertation_x.status = dissertation_status.DEFENDED
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "accept"), dissertation_status.ENDED_WIN)

        self.dissertation_x.status = dissertation_status.DIR_SUBMIT
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "accept"), dissertation_status.EVA_SUBMIT)

    def test_get_next_status_accept_4(self):
        self.offer_prop2 = OfferPropositionFactory(education_group=self.education_group2,
                                                   validation_commission_exists=False,
                                                   evaluation_first_year=False)
        self.dissertation_x = DissertationFactory(status=dissertation_status.DIR_SUBMIT,
                                                  education_group_year=self.education_group_year2)
        self.dissertation_x.status = dissertation_status.DIR_SUBMIT
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "accept"),
                         dissertation_status.TO_RECEIVE)
        self.dissertation_x.status = dissertation_status.DIR_SUBMIT
        self.assertEqual(dissertation.get_next_status(self.dissertation_x, "go"), dissertation_status.DIR_SUBMIT)

    def test_get_next_status_refuse(self):
        self.dissertation_a = DissertationFactory(status=dissertation_status.DIR_SUBMIT)
        self.assertEqual(dissertation.get_next_status(self.dissertation_a, "refuse"), dissertation_status.DIR_KO)
        self.dissertation_a.status = dissertation_status.COM_SUBMIT
        self.assertEqual(dissertation.get_next_status(self.dissertation_a, "refuse"), dissertation_status.COM_KO)
        self.dissertation_a.status = dissertation_status.EVA_SUBMIT
        self.assertEqual(dissertation.get_next_status(self.dissertation_a, "refuse"), dissertation_status.EVA_KO)
        self.dissertation_a.status = dissertation_status.DEFENDED
        self.assertEqual(dissertation.get_next_status(self.dissertation_a, "refuse"), dissertation_status.ENDED_LOS)
        self.dissertation_a.status = dissertation_status.DRAFT
        self.assertEqual(dissertation.get_next_status(self.dissertation_a, "refuse"), dissertation_status.DRAFT)
        self.dissertation_a.status = dissertation_status.TO_DEFEND
        self.assertEqual(dissertation.get_next_status(self.dissertation_a, "refuse"), dissertation_status.TO_DEFEND)

    def test_find_by_id(self):
        self.dissertation_a = DissertationFactory(id=666)
        result = dissertation.find_by_id(666)
        self.assertEqual(self.dissertation_a, result)
        result = dissertation.find_by_id(999)
        self.assertEqual(None, result)

    def test_count_by_proposition(self):
        self.prop_dissert = PropositionDissertationFactory()
        self.starting_academic_year = create_current_academic_year()
        self.dissertation_active = DissertationFactory(
            active=True,
            status=dissertation_status.COM_SUBMIT,
            proposition_dissertation=self.prop_dissert,
            education_group_year__academic_year=self.starting_academic_year
        )
        DissertationFactory(active=False, proposition_dissertation=self.prop_dissert)
        DissertationFactory(active=True, status=dissertation_status.DRAFT, proposition_dissertation=self.prop_dissert)
        DissertationFactory(active=True, status=dissertation_status.DIR_KO, proposition_dissertation=self.prop_dissert)

        self.assertEqual(dissertation.count_by_proposition(self.prop_dissert), 1)

    def test_search_by_education_group(self):
        dissert = dissertation.search_by_education_group([self.education_group_year.education_group])
        self.assertEqual(dissert[0], self.dissertation)