Esempio n. 1
0
    def add_students(self, num_students, num_groups=0):
        students = []
        for _ in range(num_students):
            student = UserFactory(system_role=SystemRole.student)
            students.append(student)
        self.students += students

        if num_groups > 0:
            student_per_group = int(len(self.students) /
                                    num_groups) if num_groups is not 0 else 0
            for idx in range(num_groups):
                group = self.add_group(self.course)
                # slice student list and enroll them into groups
                group_students = students[idx * student_per_group:min(
                    (idx + 1) * student_per_group, len(students))]
                for student in group_students:
                    self.enrol_user(student, self.course, CourseRole.student,
                                    group)
        else:
            for student in students:
                self.enrol_user(student, self.course, CourseRole.student)

        # add dropped student in group
        dropped_student = UserFactory(system_role=SystemRole.student)
        dropped_group = self.add_group(self.course)
        self.enrol_user(dropped_student, self.course, CourseRole.dropped,
                        dropped_group)
        self.dropped_students.append(dropped_student)

        return self
Esempio n. 2
0
 def __init__(self):
     self.root_user = DefaultFixture.ROOT_USER
     self.default_criterion = DefaultFixture.DEFAULT_CRITERION
     self.course = self.assignment = None
     self.instructor = self.ta = None
     self.students = []
     self.dropped_students = []
     self.assignments = []
     self.comparison_examples = []
     self.answers = []
     self.dropped_answers = []
     self.removed_answers = []
     self.draft_answers = []
     self.non_comparable_answers = []
     self.groups = []
     self.unauthorized_instructor = UserFactory(
         system_role=SystemRole.instructor)
     self.unauthorized_student = UserFactory(system_role=SystemRole.student)
     self.dropped_instructor = UserFactory(
         system_role=SystemRole.instructor)
     self.draft_student = None
     self.draft_group = None
     self.answer_comments = []
     self.comparisons = []
     self.self_evaluations = []
     db.session.commit()
Esempio n. 3
0
    def test_add_managers(self, _):
        """
        This file contains one diagnostic with three emails for managers. The first two
        already have an account with ma cantine, so they should be added. The third one
        has no account, so an invitation should be sent.
        All the managers would recieve an email, either a notification or an invitation.
        """
        gestionnaire_1 = UserFactory(email="*****@*****.**")
        gestionnaire_2 = UserFactory(email="*****@*****.**")

        with open("./api/tests/files/diagnostics_managers.csv") as diag_file:
            response = self.client.post(reverse("import_diagnostics"),
                                        {"file": diag_file})
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        body = response.json()
        self.assertEqual(body["count"], 1)
        canteen = Canteen.objects.get(siret="21340172201787")

        self.assertIn(authenticate.user, canteen.managers.all())
        self.assertIn(gestionnaire_1, canteen.managers.all())
        self.assertIn(gestionnaire_2, canteen.managers.all())

        self.assertTrue(ManagerInvitation.objects.count(), 1)
        self.assertEqual(ManagerInvitation.objects.first().email,
                         "*****@*****.**")

        self.assertEqual(len(mail.outbox), 3)
Esempio n. 4
0
 def create_user(self, type):
     student_number = None
     if type == SystemRole.student:
         student_number = factory.fuzzy.FuzzyText(length=4)
     user = UserFactory(system_role=type, student_number=student_number)
     db.session.commit()
     return user
Esempio n. 5
0
    def test_authenticated_add_manager_existing_user(self):
        """
        If the email matches an existing user, add the user to the canteen managers
        without going through invitations table. No email sent for now
        """
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)
        other_user = UserFactory.create(email="*****@*****.**")
        payload = {"canteenId": canteen.id, "email": other_user.email}

        response = self.client.post(reverse("add_manager"), payload)
        body = response.json()

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(canteen.managers.all().get(id=other_user.id).id,
                         other_user.id)
        with self.assertRaises(ManagerInvitation.DoesNotExist):
            ManagerInvitation.objects.get(email=other_user.email)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to[0], "*****@*****.**")
        self.assertEqual(mail.outbox[0].from_email, "*****@*****.**")
        self.assertIn("Accèder à la cantine", mail.outbox[0].body)

        self.assertEqual(len(body["managers"]), canteen.managers.all().count())
        self.assertEqual(len(body["managerInvitations"]),
                         canteen.managerinvitation_set.all().count())
Esempio n. 6
0
    def add_course(self,
                   num_students=5,
                   num_assignments=1,
                   num_additional_criteria=0,
                   num_groups=0,
                   num_answers='#',
                   with_comments=False,
                   with_draft_student=False,
                   with_comparisons=False,
                   with_self_eval=False):
        self.course = CourseFactory()
        self.instructor = UserFactory(system_role=SystemRole.instructor)
        self.enrol_user(self.instructor, self.course, CourseRole.instructor)
        self.ta = UserFactory(system_role=SystemRole.student)
        self.dropped_instructor = UserFactory(
            system_role=SystemRole.instructor)
        self.enrol_user(self.dropped_instructor, self.course,
                        CourseRole.dropped)
        self.enrol_user(self.ta, self.course, CourseRole.teaching_assistant)

        self.add_students(num_students, num_groups)

        self.add_assignments(num_assignments,
                             num_additional_criteria=num_additional_criteria,
                             with_self_eval=with_self_eval)
        # create a shortcut for first assignment as it is frequently used
        self.assignment = self.assignments[0]

        self.add_answers(num_answers, with_comments=with_comments)

        if with_comparisons:
            self.add_comparisons(with_comments=with_comments,
                                 with_self_eval=with_self_eval)

        if with_draft_student:
            self.add_students(1)
            self.draft_student = self.students[-1]
            self.add_draft_answers([self.draft_student])

        for assignment in self.course.assignments:
            assignment.calculate_grades()
        self.course.calculate_grades()

        return self
Esempio n. 7
0
 def create_cas_user_auth(self, type):
     student_number = None
     if type == SystemRole.student:
         student_number = factory.fuzzy.FuzzyText(length=4)
     user = UserFactory(system_role=type,
                        student_number=student_number,
                        username=None,
                        password=None)
     third_party_user = self.create_third_party_user(user=user)
     db.session.commit()
     return third_party_user
Esempio n. 8
0
    def test_generate_pdf_unauthorized(self):
        """
        Only managers of the canteen can get PDF documents
        """
        manager = UserFactory.create()
        canteen = CanteenFactory.create()
        canteen.managers.add(manager)
        diagnostic = DiagnosticFactory.create(canteen=canteen, year=2020)
        teledeclaration = Teledeclaration.createFromDiagnostic(diagnostic, manager)

        response = self.client.get(reverse("teledeclaration_pdf", kwargs={"pk": teledeclaration.id}))
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Esempio n. 9
0
    def test_create_unauthorized(self):
        """
        Only managers of the canteen can create teledeclarations
        """
        manager = UserFactory.create()
        canteen = CanteenFactory.create()
        canteen.managers.add(manager)
        diagnostic = DiagnosticFactory.create(canteen=canteen, year=2020)
        payload = {"diagnosticId": diagnostic.id}

        response = self.client.post(reverse("teledeclaration_create"), payload)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Esempio n. 10
0
    def test_send_message(self):
        canteen = CanteenFactory.create()
        managers = [UserFactory.create(), UserFactory.create()]
        canteen.managers.set(managers)
        message = MessageFactory.create(destination_canteen=canteen,
                                        sender_email="*****@*****.**")
        self.assertEqual(message.status, Message.Status.PENDING)

        message.send()
        message.refresh_from_db()

        email = mail.outbox[0]
        self.assertEqual(len(email.to), canteen.managers.all().count() + 1)
        self.assertIn(managers[0].email, email.to)
        self.assertIn(managers[1].email, email.to)
        self.assertIn("*****@*****.**", email.to)
        self.assertEqual(email.from_email, "*****@*****.**")
        self.assertEqual(len(email.reply_to), 1)
        self.assertEqual(email.reply_to[0], "*****@*****.**")

        self.assertIsNotNone(message.sent_date)
        self.assertEqual(message.status, Message.Status.SENT)
Esempio n. 11
0
 def __init__(self):
     self.default_criterion = Criterion.query.get(1)
     self.course = self.assignment = None
     self.instructor = self.ta = None
     self.students = []
     self.dropped_students = []
     self.assignments = []
     self.comparison_examples = []
     self.answers = []
     self.dropped_answers = []
     self.removed_answers = []
     self.draft_answers = []
     self.groups = []
     self.unauthorized_instructor = UserFactory(
         system_role=SystemRole.instructor)
     self.unauthorized_student = UserFactory(system_role=SystemRole.student)
     self.dropped_instructor = UserFactory(
         system_role=SystemRole.instructor)
     self.draft_student = None
     self.answer_comments = []
     self.comparisons = []
     self.self_evaluations = []
     db.session.commit()
Esempio n. 12
0
    def test_get_someone_elses_purchases(self):
        """
        This endpoint can only return the purchases of canteens the logged user manages
        """
        other_user = UserFactory.create()
        other_user_canteen = CanteenFactory.create()
        other_user_canteen.managers.add(other_user)

        PurchaseFactory.create(canteen=other_user_canteen)

        response = self.client.get(reverse("purchase_list_create"))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        body = response.json().get("results", [])
        self.assertEqual(len(body), 0)
Esempio n. 13
0
    def test_authenticated_remove_nonexistent_manager(self):
        """
        When trying to remove a manager that does not manage a canteen, we will
        respond 200 OK.
        """
        coworker = UserFactory.create()
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)

        payload = {"canteenId": canteen.id, "email": coworker.email}

        response = self.client.post(reverse("remove_manager"), payload)
        body = response.json()

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            canteen.managers.filter(id=authenticate.user.id).count(), 1)
        self.assertEqual(len(body["managers"]), canteen.managers.all().count())
Esempio n. 14
0
    def test_authenticated_remove_manager(self):
        """
        It should be possible to remove a given manager from a canteen
        """
        coworker = UserFactory.create()
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)
        canteen.managers.add(coworker)

        payload = {"canteenId": canteen.id, "email": coworker.email}

        response = self.client.post(reverse("remove_manager"), payload)
        body = response.json()

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(canteen.managers.filter(id=coworker.id).count(), 0)
        self.assertEqual(
            canteen.managers.filter(id=authenticate.user.id).count(), 1)
        self.assertEqual(len(body["managers"]), canteen.managers.all().count())
Esempio n. 15
0
    def test_create_purchase_someone_elses_canteen(self):
        """
        A user can only create a purchase of a canteen they manage
        """
        other_user = UserFactory.create()
        other_user_canteen = CanteenFactory.create()
        other_user_canteen.managers.add(other_user)

        payload = {
            "date": "2022-01-13",
            "canteen": other_user_canteen.id,
            "description": "Saumon",
            "provider": "Test provider",
            "category": "PRODUITS_DE_LA_MER",
            "characteristics": ["BIO"],
            "price_ht": 15.23,
        }
        response = self.client.post(reverse("purchase_list_create"),
                                    payload,
                                    format="json")
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
    def test_serialization(self):
        user = UserFactory(username='******')
        allegation = AllegationFactory(crid='456')
        attachment = AttachmentFileFactory(
            id=123,
            allegation=allegation,
            title='CR document',
            text_content='CHICAGO POLICE DEPARTMENT RD I HT334604',
            url='http://foo.com',
            preview_image_url=
            'https://assets.documentcloud.org/CRID-456-CR-p1-normal.gif',
            original_url=
            'https://www.documentcloud.org/documents/1-CRID-123456-CR.html',
            source_type='DOCUMENTCLOUD',
            show=True,
            file_type='document',
            pages=10,
            last_updated_by=user,
            views_count=100)
        attachment.created_at = datetime(2017,
                                         8,
                                         4,
                                         14,
                                         30,
                                         00,
                                         tzinfo=pytz.utc)
        with freeze_time('2017-08-05 12:00:01'):
            attachment.save()

        AttachmentFileFactory(
            id=124,
            allegation=allegation,
            show=True,
            file_type='document',
            preview_image_url=
            'https://assets.documentcloud.org/124/CRID-456-CR-p1-normal.gif',
            original_url=
            'https://www.documentcloud.org/documents/1-CRID-123-CR.html',
        )
        AttachmentFileFactory(
            id=125,
            allegation=allegation,
            show=True,
            file_type='document',
            preview_image_url=
            'https://assets.documentcloud.org/125/CRID-456-CR-p1-normal.gif',
            original_url=
            'https://www.documentcloud.org/documents/1-CRID-123-CR.html',
        )
        AttachmentFileFactory(
            id=126,
            allegation=allegation,
            show=False,
            file_type='document',
            preview_image_url=
            'https://assets.documentcloud.org/125/CRID-456-CR-p1-normal.gif',
            original_url=
            'https://www.documentcloud.org/documents/1-CRID-123-CR.html',
        )
        AttachmentFileFactory(
            id=127,
            allegation=allegation,
            show=True,
            file_type='audio',
            preview_image_url='',
            original_url='http://audio_link',
        )

        expected_data = {
            'id':
            123,
            'crid':
            '456',
            'title':
            'CR document',
            'text_content':
            'CHICAGO POLICE DEPARTMENT RD I HT334604',
            'url':
            'http://foo.com',
            'preview_image_url':
            'https://assets.documentcloud.org/CRID-456-CR-p1-normal.gif',
            'original_url':
            'https://www.documentcloud.org/documents/1-CRID-123456-CR.html',
            'created_at':
            '2017-08-04T09:30:00-05:00',
            'updated_at':
            '2017-08-05T07:00:01-05:00',
            'crawler_name':
            'Document Cloud',
            'linked_documents': [{
                'id':
                124,
                'preview_image_url':
                'https://assets.documentcloud.org/124/CRID-456-CR-p1-normal.gif',
            }, {
                'id':
                125,
                'preview_image_url':
                'https://assets.documentcloud.org/125/CRID-456-CR-p1-normal.gif',
            }],
            'pages':
            10,
            'last_updated_by':
            'test user'
        }

        data = AttachmentFileSerializer(attachment).data

        data['linked_documents'] = sorted(data['linked_documents'],
                                          key=itemgetter('id'))

        expect(data).to.eq(expected_data)
Esempio n. 17
0
    def add_course(self,
                   num_students=5,
                   num_assignments=1,
                   num_group_assignments=0,
                   num_additional_criteria=0,
                   num_groups=0,
                   num_answers='#',
                   num_group_answers='#',
                   with_comments=False,
                   with_draft_student=False,
                   with_comparisons=False,
                   with_self_eval=False,
                   num_non_comparable_ans=1):
        self.course = CourseFactory()
        self.instructor = UserFactory(system_role=SystemRole.instructor)
        self.enrol_user(self.instructor, self.course, CourseRole.instructor)
        self.ta = UserFactory(system_role=SystemRole.student)
        self.dropped_instructor = UserFactory(
            system_role=SystemRole.instructor)
        self.enrol_user(self.dropped_instructor, self.course,
                        CourseRole.dropped)
        self.enrol_user(self.ta, self.course, CourseRole.teaching_assistant)

        self.add_students(num_students, num_groups)

        if num_assignments:
            self.add_assignments(
                num_assignments,
                num_additional_criteria=num_additional_criteria,
                with_self_eval=with_self_eval)

        if num_group_assignments:
            self.add_assignments(
                num_group_assignments,
                num_additional_criteria=num_additional_criteria,
                with_self_eval=with_self_eval,
                with_group_answers=True)

        # create a shortcut for first assignment as it is frequently used
        self.assignment = self.assignments[0]

        self.add_answers(num_answers,
                         num_group_answers,
                         with_comments=with_comments)
        self.add_non_comparable_answers(num_non_comparable_ans)

        if with_comparisons:
            self.add_comparisons(with_comments=with_comments,
                                 with_self_eval=with_self_eval)

        if with_draft_student:
            self.draft_student = UserFactory(system_role=SystemRole.student)
            db.session.add(self.draft_student)
            db.session.commit()
            self.draft_group = self.add_group(self.course)
            self.students.append(self.draft_student)
            self.enrol_user(self.draft_student, self.course,
                            CourseRole.student, self.draft_group)
            for assignment in self.assignments:
                answer = AnswerFactory(assignment=assignment,
                                       draft=True,
                                       submission_date=None)
                if assignment.enable_group_answers:
                    answer.group = self.draft_group
                    answer.user = None
                else:
                    answer.user = self.draft_student
                    answer.group = None
                self.draft_answers.append(answer)
            db.session.add_all(self.draft_answers)
            db.session.commit()

        for assignment in self.assignments:
            assignment.calculate_grades()
        self.course.calculate_grades()

        return self