Example #1
0
def test_main_install(_clean_credentials_table):
    """
    GIVEN install event
    WHEN main is called with the event
    THEN a install response is returned.
    """
    secret_key = "secret key 1"
    salt = b"salt 1"
    secret_key_hash = package_security.calculate_secret_key_hash(
        secret_key=secret_key, salt=salt)
    credentials = factory.CredentialsFactory(secret_key_hash=secret_key_hash,
                                             salt=salt)
    credentials.save()
    token = base64.b64encode(
        f"{credentials.public_key}:{secret_key}".encode()).decode()
    authorization_value = f"Basic {token}"
    spec_id = "spec 1"
    version = "version 1"
    uri = f"/{spec_id}/{spec_id}-{version}.tar.gz"
    request = {
        "headers": {
            "authorization": [{
                "key": "Authorization",
                "value": authorization_value
            }]
        },
        "uri": uri,
    }
    event = {"Records": [{"cf": {"request": copy.deepcopy(request)}}]}

    returned_response = app.main(event, None)

    assert returned_response == {**request, "uri": f"/{credentials.sub}{uri}"}
Example #2
0
def test_process_invalid_secret_key_error(_clean_credentials_table):
    """
    GIVEN invalid authorization value
    WHEN process is called
    THEN UnauthorizedError is raised.
    """
    credentials = factory.CredentialsFactory()
    credentials.save()
    token = base64.b64encode(f"{credentials.public_key}:invalid".encode()).decode()
    authorization_value = f"Basic {token}"

    with pytest.raises(exceptions.UnauthorizedError):
        library.process(uri="", authorization_value=authorization_value)
Example #3
0
def test_item_to_info():
    """
    GIVEN properties for credentials
    WHEN Credentials are constructed and item_to_info is called
    THEN the expected info is returned.
    """
    item = factory.CredentialsFactory()

    spec_info = models.Credentials.item_to_info(item)

    assert spec_info["id"] == item.id
    assert spec_info["public_key"] == item.public_key
    assert spec_info["salt"] == item.salt
Example #4
0
def test_main_list(_clean_credentials_table, _clean_specs_table):
    """
    GIVEN list event and database with the spec
    WHEN main is called with the event
    THEN a list response is returned.
    """
    secret_key = "secret key 1"
    salt = b"salt 1"
    secret_key_hash = package_security.calculate_secret_key_hash(
        secret_key=secret_key, salt=salt)
    credentials = factory.CredentialsFactory(secret_key_hash=secret_key_hash,
                                             salt=salt)
    credentials.save()
    token = base64.b64encode(
        f"{credentials.public_key}:{secret_key}".encode()).decode()
    authorization_value = f"Basic {token}"
    spec_id = "spec 1"
    version = "version 1"
    uri = f"/{spec_id}/"
    package_database.get().create_update_spec(sub=credentials.sub,
                                              name=spec_id,
                                              version=version,
                                              model_count=1)
    request = {
        "headers": {
            "authorization": [{
                "key": "Authorization",
                "value": authorization_value
            }]
        },
        "uri": uri,
    }
    event = {"Records": [{"cf": {"request": copy.deepcopy(request)}}]}

    returned_response = app.main(event, None)

    assert returned_response["status"] == "200"
    assert returned_response["statusDescription"] == "OK"
    assert returned_response["headers"]["cache-control"][0][
        "value"] == "max-age=0"
    assert returned_response["headers"]["content-type"][0][
        "value"] == "text/html"
    assert spec_id in returned_response["body"]
    assert version in returned_response["body"]
    assert credentials.public_key in returned_response["body"]
    assert secret_key in returned_response["body"]
Example #5
0
def test_get_user(_clean_credentials_table):
    """
    GIVEN database with credentials
    WHEN get_user is called
    THEN then user is returned.
    """
    credentials = factory.CredentialsFactory()
    credentials.save()
    authorization = types.TAuthorization(
        public_key=credentials.public_key, secret_key="secret key 1"
    )

    returned_user = library.get_user(authorization=authorization)

    assert returned_user.sub == credentials.sub
    assert returned_user.salt == credentials.salt
    assert returned_user.secret_key_hash == credentials.secret_key_hash
Example #6
0
def test_process(_clean_credentials_table):
    """
    GIVEN valid authorization value
    WHEN process is called
    THEN UnauthorizedError is not raised.
    """
    secret_key = "secret key 1"
    salt = b"salt 1"
    secret_key_hash = package_security.calculate_secret_key_hash(
        secret_key=secret_key, salt=salt
    )
    spec_id = "spec 1"
    version = "version 1"
    uri = f"/{spec_id}/{spec_id}-{version}.tar.gz"
    credentials = factory.CredentialsFactory(secret_key_hash=secret_key_hash, salt=salt)
    credentials.save()
    token = base64.b64encode(f"{credentials.public_key}:{secret_key}".encode()).decode()
    authorization_value = f"Basic {token}"

    library.process(uri=uri, authorization_value=authorization_value)
Example #7
0
def test_main_list_not_found(_clean_credentials_table, _clean_specs_table):
    """
    GIVEN list event and empty database
    WHEN main is called with the event
    THEN 404 is returned.
    """
    secret_key = "secret key 1"
    salt = b"salt 1"
    secret_key_hash = package_security.calculate_secret_key_hash(
        secret_key=secret_key, salt=salt)
    credentials = factory.CredentialsFactory(secret_key_hash=secret_key_hash,
                                             salt=salt)
    credentials.save()
    token = base64.b64encode(
        f"{credentials.public_key}:{secret_key}".encode()).decode()
    authorization_value = f"Basic {token}"
    spec_id = "spec 1"
    uri = f"/{spec_id}/"
    request = {
        "headers": {
            "authorization": [{
                "key": "Authorization",
                "value": authorization_value
            }]
        },
        "uri": uri,
    }
    event = {"Records": [{"cf": {"request": copy.deepcopy(request)}}]}

    returned_response = app.main(event, None)

    assert returned_response["status"] == "404"
    assert returned_response["statusDescription"] == "NOT FOUND"
    assert returned_response["headers"]["cache-control"][0][
        "value"] == "max-age=0"
    assert returned_response["headers"]["content-type"][0][
        "value"] == "text/plain"
    assert spec_id in returned_response["body"]
Example #8
0
"""Tests for the models."""

import pytest
from open_alchemy.package_database import factory, models, types

LIST_TESTS = [
    pytest.param([], "sub 1", [], id="empty"),
    pytest.param(
        [factory.CredentialsFactory(sub="sub 1")],
        "sub 2",
        [],
        id="single sub miss",
    ),
    pytest.param(
        [factory.CredentialsFactory(sub="sub 1")],
        "sub 1",
        [0],
        id="single hit",
    ),
    pytest.param(
        [
            factory.CredentialsFactory(sub="sub 1"),
            factory.CredentialsFactory(sub="sub 2"),
        ],
        "sub 3",
        [],
        id="multiple miss",
    ),
    pytest.param(
        [
            factory.CredentialsFactory(sub="sub 1"),