示例#1
0
    def test_serialize_with_empty_schedule_items(self):
        user_schedule = UserSchedule(user_id=self.generate_random_user_item(),
                                     name="User Schedule C")

        serialized_user_schedule = user_schedule.serialize()

        assert serialized_user_schedule["name"] == user_schedule.name
        assert serialized_user_schedule["schedule_items"] == []
示例#2
0
    def test_serialization_contains_required_keys(self):
        user_schedule = UserSchedule(user_id=self.generate_random_user_item(),
                                     name="User Schedule B")

        serialized_user_schedule = user_schedule.serialize()

        self.assert_serialization_contains_keys(
            keys=["id", "name", "created_at", "schedule_items"],
            serialized_object=serialized_user_schedule,
        )
示例#3
0
    def test_serialize_with_schedule_items(self):
        schedule_item = self.generate_random_schedule_item()
        user_schedule = UserSchedule(
            user_id=self.generate_random_user_item(),
            name="User Schedule A",
            schedule_items=[schedule_item],
        )

        serialized_user_schedule = user_schedule.serialize()

        assert serialized_user_schedule["name"] == user_schedule.name
        assert len(serialized_user_schedule["schedule_items"]) == 1
        assert (serialized_user_schedule["schedule_items"][0] ==
                schedule_item.serialize())
示例#4
0
    def test_user_schedule_creation(self):
        created_datetime = datetime.datetime(year=2020,
                                             month=12,
                                             day=2,
                                             hour=8,
                                             minute=30)
        user = self.generate_random_user_item().save()
        schedule_item = self.generate_random_schedule_item()
        user_schedule = UserSchedule.objects().create(
            user_id=user,
            name="Dummy Schedule",
            deleted=False,
            schedule_items=[schedule_item],
            created_at=created_datetime,
        )

        user_schedules = UserSchedule.objects
        assert len(user_schedules) == 1
        assert user_schedule in user_schedules

        fetched_user_schedule = user_schedules().first()
        assert fetched_user_schedule.user_id == user
        assert fetched_user_schedule.name == "Dummy Schedule"
        assert not fetched_user_schedule.deleted
        assert len(fetched_user_schedule.schedule_items) == 1
        assert schedule_item in fetched_user_schedule.schedule_items
        assert fetched_user_schedule.created_at == created_datetime

        user_schedule.delete()
示例#5
0
    def test_add_schedule_item_should_increase_the_schedule_item(self):
        user_schedule = UserSchedule(user_id=self.generate_random_user_item(),
                                     name="User Schedule A")
        assert len(user_schedule.schedule_items) == 0

        inserted_schedule = user_schedule.add_schedule_item(name="Schedule A",
                                                            day="Day A",
                                                            room="Room A",
                                                            start="Start",
                                                            end="End")

        assert len(user_schedule.schedule_items) == 1
        assert inserted_schedule.name == "Schedule A"
        assert inserted_schedule.day == "Day A"
        assert inserted_schedule.room == "Room A"
        assert inserted_schedule.start == "Start"
        assert inserted_schedule.end == "End"
示例#6
0
def delete_user_schedule(user_id, user_schedule_id):
    user_schedule = UserSchedule.objects(user_id=user_id,
                                         id=user_schedule_id).first()
    if user_schedule is None:
        return jsonify({'message': 'Schedule not found.'}), 404
    user_schedule.deleted = True
    user_schedule.save()
    return (jsonify(), 204)
示例#7
0
def edit_user_schedule(user_id, user_schedule_id):
    user_schedule = UserSchedule.objects(id=user_schedule_id).first()
    data = request.json
    user_schedule.clear_schedule_item()
    for editedScheduleItem in data['schedule_items']:
        user_schedule.add_schedule_item(**editedScheduleItem)
    user_schedule.save()

    return (jsonify({'user_schedule': user_schedule.serialize()}), 200)
示例#8
0
def get_user_schedule_detail(user_schedule_id):
    user_schedule = UserSchedule.objects(id=user_schedule_id).first()
    request_user_id = get_user_id(request)
    return (jsonify({
        'user_schedule': {
            **user_schedule.serialize(), "has_edit_access":
            str(user_schedule.user_id.id) == request_user_id
        }
    }), 200)
示例#9
0
def rename_user_schedule(user_id, user_schedule_id):
    data = request.json
    user_schedule = UserSchedule.objects(user_id=user_id,
                                         id=user_schedule_id).first()
    if user_schedule is None:
        return jsonify({'message': 'Schedule not found.'}), 404
    user_schedule.name = html.escape(data["name"])
    user_schedule.save()
    return (jsonify({'user_schedule': user_schedule.serialize()}), 200)
示例#10
0
    def test_get_empty_user_schedules(self, auth_client):
        client, user = auth_client
        url = '{}/users/{}/user_schedules'.format(BASE_PATH, user.id)
        res = client.get(url)

        assert res.status_code == 200
        assert res.get_json()['user_schedules'] == []

        user_schedules = UserSchedule.objects(user_id=user.id).all()
        assert len(user_schedules) == 0
示例#11
0
    def test_save_user_schedule(self, auth_client):
        client, user = auth_client
        url = '{}/users/{}/user_schedule'.format(BASE_PATH, user.id)
        res = client.post(url, json={'schedule_items': self.schedule_items})

        assert res.status_code == 201
        assert res.get_json()['id']

        user_schedules = UserSchedule.objects(user_id=user.id).all()
        assert len(user_schedules) == 1
示例#12
0
 def create_user_schedule(self, user):
     """Create user schedule directly from db"""
     user_schedule = UserSchedule(user_id=user.id)
     for item in self.schedule_items:
         user_schedule.add_schedule_item(**item)
     user_schedule.save()
     return user_schedule
示例#13
0
    def test_user_schedule_deletion(self):
        user_schedule = UserSchedule.objects().create(
            user_id=self.generate_random_user_item().save(),
            name=self.get_random_string(128),
            schedule_items=[],
        )

        assert len(UserSchedule.objects) == 1
        assert user_schedule in UserSchedule.objects

        user_schedule.delete()
        assert len(UserSchedule.objects) == 0
        assert user_schedule not in UserSchedule.objects
示例#14
0
def save_user_schedule(user_id):
    data = request.json
    user_schedule = UserSchedule(user_id=user_id)
    for item in data['schedule_items']:
        user_schedule.add_schedule_item(**item)
    user_schedule.save()

    return (jsonify({
        'id': str(user_schedule.id),
    }), 201)
示例#15
0
    def test_user_schedule_fields_validation(self):
        schedule_item = self.generate_random_schedule_item()
        user = self.generate_random_user_item()
        now = datetime.datetime.now()
        test_cases = [
            {
                "user_id": schedule_item,
                "name": "Name",
                "schedule_items": [schedule_item],
                "created_at": now,
            },
            {
                "user_id": schedule_item,
                "name": "Name" * 40,
                "schedule_items": [schedule_item],
                "created_at": now,
            },
            {
                "user_id": schedule_item,
                "name": "Name" * 40,
                "schedule_items": user,
                "created_at": now,
            },
            {
                "user_id": schedule_item,
                "name": "Name" * 40,
                "schedule_items": user,
                "created_at": "Now",
            },
        ]

        for case in test_cases:
            with pytest.raises(ValidationError):
                UserSchedule(
                    user_id=case["user_id"],
                    name=case["name"],
                    schedule_items=case["schedule_items"],
                    created_at=case["created_at"],
                ).validate()
示例#16
0
    def test_user_schedule_update(self):
        user_schedule = UserSchedule.objects().create(user_id=None,
                                                      name="Old Schedule",
                                                      schedule_items=[])

        new_user = self.generate_random_user_item().save()
        new_schedule_item = self.generate_random_schedule_item()
        new_created_at = datetime.datetime(year=2020, month=12, day=3, hour=8)
        user_schedule.user_id = new_user
        user_schedule.name = "New Schedule"
        user_schedule.schedule_items = [new_schedule_item]
        user_schedule.deleted = True
        user_schedule.created_at = new_created_at
        user_schedule.save()
        user_schedule.reload()

        assert user_schedule.user_id == new_user
        assert user_schedule.name == "New Schedule"
        assert new_schedule_item in user_schedule.schedule_items
        assert user_schedule.deleted
        assert user_schedule.created_at == new_created_at

        user_schedule.delete()
示例#17
0
def rename_user_schedule(user_id, user_schedule_id):
    data = request.json
    user_schedule = UserSchedule.objects(id=user_schedule_id).first()
    user_schedule.name = html.escape(data["name"])
    user_schedule.save()
    return (jsonify({'user_schedule': user_schedule.serialize()}), 200)
示例#18
0
def delete_user_schedule(user_id, user_schedule_id):
    user_schedule = UserSchedule.objects(id=user_schedule_id).first()
    user_schedule.deleted = True
    user_schedule.save()
    return (jsonify(), 204)
示例#19
0
def get_user_schedule_list(user_id):
    schedules = UserSchedule.objects(user_id=user_id, deleted=False).all()
    data = []
    for schedule in schedules:
        data.append(schedule.serialize())
    return (jsonify({'user_schedules': data}), 200)
示例#20
0
def edit_user_schedule(user_id, user_schedule_id):
    user_schedule = UserSchedule.objects(id=user_schedule_id).first()
    # If the schedule doesn't exist or the user is mismatched,
    # create a new one with the same items.
    if user_schedule is None:
        user_schedule = UserSchedule(user_id=user_id)
    elif str(user_schedule.user_id.id) != user_id:
        name = f'{user_schedule.name} (copied)'
        user_schedule = UserSchedule(user_id=user_id, name=name)
    data = request.json
    user_schedule.clear_schedule_item()
    for editedScheduleItem in data['schedule_items']:
        user_schedule.add_schedule_item(**editedScheduleItem)
    user_schedule.save()

    return (jsonify({'user_schedule': user_schedule.serialize()}), 200)
示例#21
0
def get_user_schedule_detail(user_schedule_id):
    user_schedule = UserSchedule.objects(id=user_schedule_id).first()
    return (jsonify({'user_schedule': user_schedule.serialize()}), 200)