Exemple #1
0
def test_performance():
    token = ImageToken(referrer=referrer)
    t0 = time.time()
    for i in range(1000):
        ImageToken.decode(token.encode(secret='test'), secret='test')
    cost = int((time.time() - t0) * 1000)
    LOG.info('ImageToken 1000 encode + decode cost {}ms'.format(cost))
Exemple #2
0
def story_get_by_offset(
        request,
        feed_unionid: T.feed_unionid.object,
        offset: T.int.min(0).optional,
        detail: StoryDetailSchema,
        set_readed: T.bool.default(False),
) -> StorySchema:
    """Story detail"""
    check_unionid(request, feed_unionid)
    try:
        story = UnionStory.get_by_feed_offset(feed_unionid,
                                              offset,
                                              detail=detail)
    except StoryNotFoundError:
        return Response({"message": "does not exist"}, status=400)
    if set_readed:
        try:
            UnionFeed.set_story_offset(feed_unionid, offset + 1)
        except FeedStoryOffsetError as ex:
            return Response({'message': str(ex)}, status=400)
        except ConcurrentUpdateError as ex:
            LOG.error(f'ConcurrentUpdateError: story set_readed {ex}',
                      exc_info=ex)
    image_token = ImageToken(
        referrer=story.link,
        feed=feed_unionid.feed_id,
        offset=offset,
    ).encode(secret=CONFIG.image_token_secret)
    ret = story.to_dict()
    ret.update(image_token=image_token)
    return ret
Exemple #3
0
def test_encode_decode():
    token = ImageToken(referrer=referrer)
    assert repr(token)
    got = ImageToken.decode(token.encode(secret='test'), secret='test')
    assert got.referrer == referrer
    assert got.feed is None
    assert got.offset is None
    assert got.timestamp == token.timestamp

    token = ImageToken(referrer=referrer, feed=123, offset=0)
    assert repr(token)
    got = ImageToken.decode(token.encode(secret='test'), secret='test')
    assert got.referrer == referrer
    assert got.feed == 123
    assert got.offset == 0
    assert got.timestamp == token.timestamp
Exemple #4
0
def test_decode_expires():
    timestamp = 1600000000
    token = ImageToken(timestamp=timestamp).encode(secret='test')

    def clock():
        return 1600001000

    got = ImageToken.decode(token, secret='test', expires=1001, clock=clock)
    assert got.timestamp == timestamp
    with pytest.raises(ImageTokenExpiredError):
        ImageToken.decode(token, secret='test', expires=999, clock=clock)
Exemple #5
0
def story_get_by_offset(
    request,
    feed_unionid: T.feed_unionid.object,
    offset: T.int.min(0).optional,
    detail: StoryDetailSchema,
) -> StorySchema:
    """Story detail"""
    check_unionid(request, feed_unionid)
    try:
        story = UnionStory.get_by_feed_offset(feed_unionid, offset, detail=detail)
    except StoryNotFoundError:
        return Response({"message": "does not exist"}, status=400)
    image_token = ImageToken(referrer=story.link)\
        .encode(secret=CONFIG.image_token_secret)
    ret = story.to_dict()
    ret.update(image_token=image_token)
    return ret
Exemple #6
0
def test_decode_signature():
    token = ImageToken().encode(secret='test')
    with pytest.raises(ImageTokenDecodeError):
        ImageToken.decode(token, secret='xxx')
Exemple #7
0
def test_encode_decode():
    token = ImageToken(referrer=referrer)
    assert repr(token)
    got = ImageToken.decode(token.encode(secret='test'), secret='test')
    assert got.referrer == referrer
    assert got.timestamp == token.timestamp