def test_fails_expired(client_with_secrets): client, secret_key, bearer_token = client_with_secrets key = random_bytes(32).hex() now = datetime.utcnow() created = now - timedelta(minutes=12) expiry = now - timedelta(minutes=2) request = media_pb2.UploadRequest( key=key, type=media_pb2.UploadRequest.UploadType.IMAGE, created=Timestamp_from_datetime(created), expiry=Timestamp_from_datetime(expiry), max_width=2000, max_height=1600, ) upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): with open(DATADIR / "1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel.jpg")}) assert rv.status_code == 400
def InitiateMediaUpload(self, request, context): key = random_hex() created = now() expiry = created + timedelta(minutes=20) with session_scope() as session: upload = InitiatedUpload(key=key, created=created, expiry=expiry, initiator_user_id=context.user_id) session.add(upload) session.commit() req = media_pb2.UploadRequest( key=upload.key, type=media_pb2.UploadRequest.UploadType.IMAGE, created=Timestamp_from_datetime(upload.created), expiry=Timestamp_from_datetime(upload.expiry), max_width=2000, max_height=1600, ).SerializeToString() data = urlsafe_b64encode(req).decode("utf8") sig = urlsafe_b64encode(generate_hash_signature(req, config["MEDIA_SERVER_SECRET_KEY"])).decode("utf8") path = "upload?" + urlencode({"data": data, "sig": sig}) return api_pb2.InitiateMediaUploadRes( upload_url=urls.media_upload_url(path), expiry=Timestamp_from_datetime(expiry), )
def create_upload_request(): key = random_bytes(32).hex() now = datetime.utcnow() expiry = now + timedelta(minutes=20) return key, media_pb2.UploadRequest( key=key, type=media_pb2.UploadRequest.UploadType.IMAGE, created=Timestamp_from_datetime(now), expiry=Timestamp_from_datetime(expiry), max_width=2000, max_height=1600, )