示例#1
0
def squrl_get(bucket, stubber):
    stubber.add_response(
        "head_object", {}, expected_params={"Bucket": ANY, "Key": ANY}
    )
    stubber.activate()

    return Squrl(bucket, client=stubber.client)
示例#2
0
def handler(event, context, **kwargs):
    """
    Handler.

    Handle the lambda function event and return a response with
    a body containing the url and the key, if it exists.

    response body: '{"url": <string>, "key": <string>}'
    """
    squrlifier = kwargs.get("squrl", Squrl(getenv("S3_BUCKET")))
    registry = kwargs.get(
        "registry",
        {
            "GET": squrlifier.get,
            "POST": squrlifier.create,
            "PUT": squrlifier.create
        },
    )
    method, body = ApiHandler.parse_event(event)

    if method not in registry.keys():
        error = ValueError(f"Unsupported method: {method}")

        return ApiHandler.get_response(error=error)

    else:
        url = unquote_plus(body["url"])
        key = registry[method](url)

        return ApiHandler.get_response(response={"url": url, "key": key})
示例#3
0
def test_key_exists(stubber):
    expected_params = {"Bucket": ANY, "Key": ANY}

    stubber["stub"].add_response("head_object", {},
                                 expected_params=expected_params)
    stubber["stub"].activate()

    assert Squrl(stubber["client"], "test-bucket").key_exists("test-key")
示例#4
0
def test_get_method_key_exists(stubber, parameters):
    bucket = parameters["bucket"]
    url = parameters["url"]

    stubber.add_response("head_object", {},
                         expected_params=parameters["head_object"])
    stubber.activate()

    assert Squrl(bucket, client=stubber.client).get(url)
示例#5
0
def test_key_exists(stubber, parameters):
    bucket = parameters["bucket"]
    key = parameters["key"]

    stubber.add_response("head_object", {},
                         expected_params=parameters["head_object"])
    stubber.activate()

    assert Squrl(bucket, client=stubber.client).key_exists(key)
示例#6
0
def test_key_does_not_exist(stubber):
    expected_params = {"Bucket": ANY, "Key": ANY}

    stubber["stub"].add_client_error("head_object",
                                     expected_params=expected_params,
                                     service_error_code="404")
    stubber["stub"].activate()

    assert not Squrl(stubber["client"], "test-bucket").key_exists("test-key")
示例#7
0
def test_create_method(stubber, parameters):
    bucket = parameters["bucket"]
    url = parameters["url"]

    stubber.add_response("put_object", {},
                         expected_params=parameters["put_object"])
    stubber.activate()

    assert Squrl(bucket, client=stubber.client).create(url)
示例#8
0
def test_get_method_key_does_not_exist(stubber, parameters):
    bucket = parameters["bucket"]
    url = parameters["url"]

    stubber.add_client_error("head_object",
                             expected_params=parameters["head_object"],
                             service_error_code="404")
    stubber.activate()

    assert not Squrl(bucket, client=stubber.client).get(url)
示例#9
0
def test_key_does_not_exist(stubber, parameters):
    bucket = parameters["bucket"]
    key = parameters["key"]

    stubber.add_client_error("head_object",
                             expected_params=parameters["head_object"],
                             service_error_code="404")
    stubber.activate()

    assert not Squrl(bucket, client=stubber.client).key_exists(key)
示例#10
0
def test_get_method_key_exists(stubber):
    expected_params = {"Bucket": ANY, "Key": ANY}

    stubber["stub"].add_response("head_object", {},
                                 expected_params=expected_params)
    stubber["stub"].activate()

    squrl = Squrl(stubber["stub"].client, "test-bucket")

    assert squrl.registry["GET"]("test-url")
示例#11
0
def test_key_error(stubber, parameters):
    bucket = parameters["bucket"]
    key = parameters["key"]

    stubber.add_client_error("head_object",
                             expected_params=parameters["head_object"],
                             service_error_code="500")
    stubber.activate()

    with pytest.raises(ClientError):
        Squrl(bucket, client=stubber.client).key_exists(key)
示例#12
0
def test_get_method_key_does_not_exist(stubber):
    expected_params = {"Bucket": ANY, "Key": ANY}

    stubber["stub"].add_client_error("head_object",
                                     expected_params=expected_params,
                                     service_error_code="404")
    stubber["stub"].activate()

    squrl = Squrl(stubber["stub"].client, "test-bucket")

    assert not squrl.registry["GET"]("test-url")
示例#13
0
def test_get_response_error():
    error = ValueError("test-error")
    expected_response = {
        "statusCode": "400",
        "body": str(error),
        "headers": {
            "Content-Type": "application/json",
        },
    }
    actual_response = Squrl.get_response(error=error)

    assert expected_response == actual_response
示例#14
0
def test_get_response_ok():
    response = "test-body"
    expected_response = {
        "statusCode": "200",
        "body": json.dumps(response),
        "headers": {
            "Content-Type": "application/json",
        },
    }
    actual_response = Squrl.get_response(response=response)

    assert expected_response == actual_response
示例#15
0
def squrl_create(bucket, stubber, url):
    stubber.add_response(
        "put_object", {}, expected_params={
            "Bucket": ANY,
            "Key": ANY,
            "WebsiteRedirectLocation": url,
            "Expires": ANY,
            "ContentType": ANY
        }
    )
    stubber.activate()

    return Squrl(bucket, client=stubber.client)
示例#16
0
def test_create_key(stubber):
    url = "test-url"
    content_type = "text/plain"
    expected_params = {
        "Bucket": ANY,
        "Key": ANY,
        "WebsiteRedirectLocation": url,
        "Expires": ANY,
        "ContentType": content_type
    }

    stubber["stub"].add_response("put_object", {},
                                 expected_params=expected_params)
    stubber["stub"].activate()

    Squrl(stubber["client"], "test-bucket").create_key("test-url")
示例#17
0
def test_post_method(stubber):
    expected_params = {
        "Bucket": ANY,
        "Key": ANY,
        "WebsiteRedirectLocation": ANY,
        "Expires": ANY,
        "ContentType": ANY
    }

    stubber["stub"].add_response("put_object", {},
                                 expected_params=expected_params)
    stubber["stub"].activate()

    squrl = Squrl(stubber["stub"].client, "test-bucket")

    assert squrl.registry["POST"]("test-url")
示例#18
0
def parameters():
    bucket = "test-bucket"
    url = "test-url"
    key = Squrl.get_key(url)

    return {
        "bucket": bucket,
        "url": url,
        "key": key,
        "head_object": {
            "Bucket": bucket,
            "Key": key
        },
        "put_object": {
            "Bucket": bucket,
            "Key": key,
            "WebsiteRedirectLocation": url,
            "Expires": ANY,
            "ContentType": ANY
        }
    }
示例#19
0
def test_get_key(parameters):
    key = Squrl.get_key("test-url")

    assert len(key) == Squrl.key_length + 2
    assert key.startswith("u/")
示例#20
0
def test_get_key():
    key = Squrl.get_key("test-url")

    assert len(key) == 9
    assert key.startswith("u/")
示例#21
0
def test_get_expiration(parameters):
    expiration = Squrl.get_expiration()

    assert expiration > datetime.now()