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))
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)
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
async def image_proxy_view_v2(request, token: T.str, url: T.url): try: token = ImageToken.decode(token, secret=CONFIG.image_token_secret, expires=CONFIG.image_token_expires) except ImageTokenDecodeError as ex: return json_response({'message': str(ex)}, status=400) response = await image_proxy(request, url, token.referrer) return response
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
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
def test_decode_signature(): token = ImageToken().encode(secret='test') with pytest.raises(ImageTokenDecodeError): ImageToken.decode(token, secret='xxx')
def test_decode_error(): with pytest.raises(ImageTokenDecodeError): ImageToken.decode('aaa', secret='test') with pytest.raises(ImageTokenDecodeError): ImageToken.decode('aaa.bbb', secret='test')
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