Exemplo n.º 1
0
def test_update_page_errors(db):
    user, token = generate_user()
    with session_scope() as session:
        c_id = create_community(session, 0, 2, "Root node", [user], [],
                                None).id

    with pages_session(token) as api:
        page_id = api.CreatePlace(
            pages_pb2.CreatePlaceReq(
                title="dummy title",
                content="dummy content",
                address="dummy address",
                location=pages_pb2.Coordinate(
                    lat=1,
                    lng=1,
                ),
            )).page_id

        with pytest.raises(grpc.RpcError) as e:
            api.UpdatePage(
                pages_pb2.UpdatePageReq(
                    page_id=page_id,
                    title=wrappers_pb2.StringValue(value=""),
                ))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.MISSING_PAGE_TITLE

        with pytest.raises(grpc.RpcError) as e:
            api.UpdatePage(
                pages_pb2.UpdatePageReq(
                    page_id=page_id,
                    content=wrappers_pb2.StringValue(value=""),
                ))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.MISSING_PAGE_CONTENT

        with pytest.raises(grpc.RpcError) as e:
            api.UpdatePage(
                pages_pb2.UpdatePageReq(
                    page_id=page_id,
                    address=wrappers_pb2.StringValue(value=""),
                ))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.MISSING_PAGE_ADDRESS
Exemplo n.º 2
0
def test_page_photo(db):
    user, token = generate_user()
    with session_scope() as session:
        c_id = create_community(session, 0, 2, "Root node", [user], [],
                                None).id

    with pages_session(token) as api:
        key1 = random_hex(32)
        filename1 = random_hex(32)

        with session_scope() as session:
            session.add(
                Upload(
                    key=key1,
                    filename=filename1,
                    creator_user_id=user.id,
                ))

        res = api.CreatePlace(
            pages_pb2.CreatePlaceReq(
                title="dummy title",
                content="dummy content",
                photo_key=key1,
                address="dummy address",
                location=pages_pb2.Coordinate(
                    lat=1,
                    lng=1,
                ),
            ))

        assert filename1 in res.photo_url

        # can't create with non-existent photo
        with pytest.raises(grpc.RpcError) as e:
            api.CreatePlace(
                pages_pb2.CreatePlaceReq(
                    title="dummy title",
                    content="dummy content",
                    photo_key="nonexisten",
                    address="dummy address",
                    location=pages_pb2.Coordinate(
                        lat=1,
                        lng=1,
                    ),
                ))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.PHOTO_NOT_FOUND

        # can create page with no photo
        res = api.CreatePlace(
            pages_pb2.CreatePlaceReq(
                title="dummy title",
                content="dummy content",
                address="dummy address",
                location=pages_pb2.Coordinate(
                    lat=1,
                    lng=1,
                ),
            ))

        assert res.photo_url == ""

        page_id = res.page_id

        # can't set it to non-existent stuff
        with pytest.raises(grpc.RpcError) as e:
            api.UpdatePage(
                pages_pb2.UpdatePageReq(
                    page_id=page_id,
                    photo_key=wrappers_pb2.StringValue(
                        value="non-existent id"),
                ))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.PHOTO_NOT_FOUND

        key2 = random_hex(32)
        filename2 = random_hex(32)

        with session_scope() as session:
            session.add(
                Upload(
                    key=key2,
                    filename=filename2,
                    creator_user_id=user.id,
                ))

        res = api.UpdatePage(
            pages_pb2.UpdatePageReq(
                page_id=page_id,
                photo_key=wrappers_pb2.StringValue(value=key2),
            ))

        assert filename2 in res.photo_url

        # can unset it

        res = api.UpdatePage(
            pages_pb2.UpdatePageReq(
                page_id=page_id,
                photo_key=wrappers_pb2.StringValue(value=""),
            ))

        assert res.photo_url == ""
Exemplo n.º 3
0
def test_update_page(db):
    user, token = generate_user()
    with session_scope() as session:
        c_id = create_community(session, 0, 2, "Root node", [user], [],
                                None).id

    with pages_session(token) as api:
        time_before_create = now()
        page_id = api.CreatePlace(
            pages_pb2.CreatePlaceReq(
                title="dummy title",
                content="dummy content",
                address="dummy address",
                location=pages_pb2.Coordinate(
                    lat=1,
                    lng=1,
                ),
            )).page_id

        time_before_update = now()
        api.UpdatePage(
            pages_pb2.UpdatePageReq(
                page_id=page_id,
                title=wrappers_pb2.StringValue(value="test title"),
            ))

        time_before_get = now()
        res = api.GetPage(pages_pb2.GetPageReq(page_id=page_id))
        assert res.title == "test title"
        assert res.content == "dummy content"
        assert res.address == "dummy address"
        assert res.location.lat == 1
        assert res.location.lng == 1
        assert res.slug == "test-title"
        assert time_before_create < to_aware_datetime(
            res.created) < time_before_update
        assert time_before_update < to_aware_datetime(
            res.last_edited) < time_before_get
        assert to_aware_datetime(res.created) < to_aware_datetime(
            res.last_edited)
        assert res.last_editor_user_id == user.id
        assert res.creator_user_id == user.id
        assert res.owner_user_id == user.id
        assert not res.owner_community_id
        assert not res.owner_group_id
        assert res.editor_user_ids == [user.id]
        assert res.can_edit
        assert res.can_moderate

        time_before_second_update = now()
        api.UpdatePage(
            pages_pb2.UpdatePageReq(
                page_id=page_id,
                content=wrappers_pb2.StringValue(value="test content"),
            ))

        time_before_second_get = now()
        res = api.GetPage(pages_pb2.GetPageReq(page_id=page_id))
        assert res.title == "test title"
        assert res.content == "test content"
        assert res.address == "dummy address"
        assert res.location.lat == 1
        assert res.location.lng == 1
        assert res.slug == "test-title"
        assert time_before_create < to_aware_datetime(
            res.created) < time_before_update
        assert time_before_second_update < to_aware_datetime(
            res.last_edited) < time_before_second_get
        assert to_aware_datetime(res.created) < to_aware_datetime(
            res.last_edited)
        assert res.last_editor_user_id == user.id
        assert res.creator_user_id == user.id
        assert res.owner_user_id == user.id
        assert not res.owner_community_id
        assert not res.owner_group_id
        assert res.editor_user_ids == [user.id]
        assert res.can_edit
        assert res.can_moderate

        time_before_third_update = now()
        api.UpdatePage(
            pages_pb2.UpdatePageReq(
                page_id=page_id,
                address=wrappers_pb2.StringValue(value="test address"),
            ))

        time_before_third_get = now()
        res = api.GetPage(pages_pb2.GetPageReq(page_id=page_id))
        assert res.title == "test title"
        assert res.content == "test content"
        assert res.address == "test address"
        assert res.location.lat == 1
        assert res.location.lng == 1
        assert res.slug == "test-title"
        assert time_before_create < to_aware_datetime(
            res.created) < time_before_update
        assert time_before_third_update < to_aware_datetime(
            res.last_edited) < time_before_third_get
        assert to_aware_datetime(res.created) < to_aware_datetime(
            res.last_edited)
        assert res.last_editor_user_id == user.id
        assert res.creator_user_id == user.id
        assert res.owner_user_id == user.id
        assert not res.owner_community_id
        assert not res.owner_group_id
        assert res.editor_user_ids == [user.id]
        assert res.can_edit
        assert res.can_moderate

        time_before_fourth_update = now()
        api.UpdatePage(
            pages_pb2.UpdatePageReq(
                page_id=page_id,
                location=pages_pb2.Coordinate(
                    lat=3,
                    lng=1.222,
                ),
            ))

        time_before_fourth_get = now()
        res = api.GetPage(pages_pb2.GetPageReq(page_id=page_id))
        assert res.title == "test title"
        assert res.content == "test content"
        assert res.address == "test address"
        assert res.location.lat == 3
        assert res.location.lng == 1.222
        assert res.slug == "test-title"
        assert time_before_create < to_aware_datetime(
            res.created) < time_before_update
        assert time_before_fourth_update < to_aware_datetime(
            res.last_edited) < time_before_fourth_get
        assert to_aware_datetime(res.created) < to_aware_datetime(
            res.last_edited)
        assert res.last_editor_user_id == user.id
        assert res.creator_user_id == user.id
        assert res.owner_user_id == user.id
        assert not res.owner_community_id
        assert not res.owner_group_id
        assert res.editor_user_ids == [user.id]
        assert res.can_edit
        assert res.can_moderate