def test_get_identifier_from_token_should_return_none_if_exchange_does_not_exist(
            self, mock_get_table, an_exchange, get_dynamodb_table):
        # Given
        mock_get_table.return_value = get_dynamodb_table

        # When
        identifier_from_upload_token = exchange_repository.get_identifier_from_token(
            upload_token="fake_upload_token")
        identifier_from_download_token = exchange_repository.get_identifier_from_token(
            download_token="fake_download_token")

        # Then
        assert identifier_from_upload_token is None
        assert identifier_from_download_token is None
    def test_get_identifier_from_token_should_return_identifier_associated_with_given_token(
            self, mock_get_table, an_exchange, get_dynamodb_table):
        # Given
        mock_get_table.return_value = get_dynamodb_table
        exchange_repository.save(an_exchange)

        # When
        identifier_from_upload_token = exchange_repository.get_identifier_from_token(
            upload_token=an_exchange.upload_token)
        identifier_from_download_token = exchange_repository.get_identifier_from_token(
            download_token=an_exchange.download_token)

        # Then
        assert identifier_from_upload_token == an_exchange.identifier
        assert identifier_from_download_token == an_exchange.identifier
def handle(event: dict, context: dict) -> Optional[dict]:
    s3_client = boto3.client('s3')
    upload_token = event_parser.extract_from_headers(
        config.AUTHORIZATION_HEADER, event)
    logger.info(f'Extracted upload_token from headers: {upload_token}')
    identifier = exchange_repository.get_identifier_from_token(
        upload_token=upload_token)
    logger.info(f'Queried identifier from repository: {identifier}')
    filename = event['queryStringParameters']['filename']
    logger.info(f'Extracted filename from query string parameters: {filename}')

    if _check_if_a_file_exists(identifier):
        return generate_response('A file already exists for this identifier',
                                 HTTPStatus.CONFLICT)
    else:
        try:
            response = s3_client.generate_presigned_post(BUCKET_NAME,
                                                         _generate_key(
                                                             identifier,
                                                             filename),
                                                         Fields=None,
                                                         Conditions=None,
                                                         ExpiresIn=600)
        except ClientError as e:
            logging.error(e)
            return generate_response('Internal error',
                                     HTTPStatus.INTERNAL_SERVER_ERROR)

        exchange_repository.revoke_upload(identifier)
        return generate_response(json.dumps(response), HTTPStatus.OK)
Esempio n. 4
0
def handle(event: dict, context: dict) -> Optional[dict]:
    s3_client = boto3.client('s3')
    download_token = event_parser.extract_from_headers(
        config.AUTHORIZATION_HEADER, event)
    logger.info(f'Extracted download_token from headers: {download_token}')
    identifier = exchange_repository.get_identifier_from_token(
        download_token=download_token)
    logger.info(f'Queried identifier from repository: {identifier}')
    key = file_repository.get_file(identifier)
    logger.info(f'Extracted file key from storage: {key}')

    if key:
        try:
            logger.info("Generating presigned url")
            response = s3_client.generate_presigned_url('get_object',
                                                        Params={
                                                            'Bucket':
                                                            BUCKET_NAME,
                                                            'Key': key
                                                        })
        except ClientError as e:
            logging.error(e)
            return generate_response("Internal Error",
                                     HTTPStatus.INTERNAL_SERVER_ERROR)
        return generate_response(response, HTTPStatus.OK)
    else:
        return generate_response("No file associated to this token",
                                 HTTPStatus.NOT_FOUND)
    def test_get_identifier_from_token_should_be_none_safe(
            self, mock_get_table, get_dynamodb_table):
        # Given
        mock_get_table.return_value = get_dynamodb_table

        # When
        identifier_from_upload_token = exchange_repository.get_identifier_from_token(
        )

        # Then
        assert identifier_from_upload_token is None