Ejemplo n.º 1
0
def test_create_response_install():
    """
    GIVEN install type request, authorization info and uri
    WHEN create_response is called with the type, uri and authorization info
    THEN an install response is returned.
    """
    uri = "uri 1"
    sub = "sub 1"
    auth_info = types.CredentialsAuthInfo(
        sub=sub, secret_key_hash=b"secret key 1", salt=b"salt 1"
    )
    request_type = types.TRequestType.INSTALL
    public_key = "public key 1"
    secret_key = "secret key 1"
    authorization = types.TAuthorization(public_key=public_key, secret_key=secret_key)

    returned_response = library.create_response(
        authorization=authorization,
        request_type=request_type,
        uri=uri,
        auth_info=auth_info,
    )

    assert returned_response.type == request_type
    assert uri in returned_response.value
    assert sub in returned_response.value
Ejemplo n.º 2
0
def test_create_response_list(_clean_specs_table):
    """
    GIVEN database with single spec and list type request, authorization info and uri
    WHEN create_response is called with the type, uri and authorization info
    THEN a list response is returned.
    """
    spec_id = "spec 1"
    version = "version 1"
    uri = f"/{spec_id}/"
    sub = "sub 1"
    auth_info = types.CredentialsAuthInfo(
        sub=sub, secret_key_hash=b"secret key 1", salt=b"salt 1"
    )
    request_type = types.TRequestType.LIST
    package_database.get().create_update_spec(
        sub=sub, name=spec_id, version=version, model_count=1
    )
    public_key = "public key 1"
    secret_key = "secret key 1"
    authorization = types.TAuthorization(public_key=public_key, secret_key=secret_key)

    returned_response = library.create_response(
        authorization=authorization,
        request_type=request_type,
        uri=uri,
        auth_info=auth_info,
    )

    assert returned_response.type == request_type
    assert "<body>" in returned_response.value
    assert spec_id in returned_response.value
    assert version in returned_response.value
    assert public_key in returned_response.value
    assert secret_key in returned_response.value
Ejemplo n.º 3
0
def test_get_user_error(_clean_credentials_table):
    """
    GIVEN empty database
    WHEN get_user is called
    THEN UnauthorizedError is raised.
    """
    authorization = types.TAuthorization(
        public_key="public key 1", secret_key="secret key 1"
    )

    with pytest.raises(exceptions.UnauthorizedError):
        library.get_user(authorization=authorization)
Ejemplo n.º 4
0
def test_authorize_user_error():
    """
    GIVEN invalid authorization
    WHEN authorize_user is called
    THEN UnauthorizedError is raised.
    """
    authorization = types.TAuthorization(
        public_key="public key 1", secret_key="secret key 1"
    )
    auth_info = types.CredentialsAuthInfo(
        sub="sub 1", secret_key_hash=b"invalid", salt=b"salt 1"
    )

    with pytest.raises(exceptions.UnauthorizedError):
        library.authorize_user(authorization=authorization, auth_info=auth_info)
Ejemplo n.º 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
Ejemplo n.º 6
0
def test_create_list_response_value_error(_clean_specs_table):
    """
    GIVEN empty database and uri
    WHEN create_list_response_value is called with the uri
    THEN NotFoundError is raised.
    """
    uri = "/spec 1/"
    auth_info = types.CredentialsAuthInfo(
        sub="sub 1", secret_key_hash=b"secret key 1", salt=b"salt 1"
    )
    authorization = types.TAuthorization(
        public_key="public key 1", secret_key="secret key 1"
    )

    with pytest.raises(exceptions.NotFoundError):
        library.create_list_response_value(
            authorization=authorization, uri=uri, auth_info=auth_info
        )
Ejemplo n.º 7
0
def test_authorize_user():
    """
    GIVEN valid authorization
    WHEN authorize_user 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
    )
    authorization = types.TAuthorization(
        public_key="public key 1", secret_key=secret_key
    )
    auth_info = types.CredentialsAuthInfo(
        sub="sub 1", secret_key_hash=secret_key_hash, salt=salt
    )

    library.authorize_user(authorization=authorization, auth_info=auth_info)
Ejemplo n.º 8
0
def test_create_list_response_value(_clean_specs_table, monkeypatch):
    """
    GIVEN database with single then multiple specs and uri that points to the spec
    WHEN create_list_response_value is called with the uri
    THEN a HTML response with links to the packages are returned.
    """
    mock_time = mock.MagicMock()
    monkeypatch.setattr(time, "time", mock_time)
    mock_time.return_value = 1000000
    spec_id = "spec-id-1"
    version_1 = "version 1"
    uri = f"/{spec_id}/"
    sub = "sub 1"
    auth_info = types.CredentialsAuthInfo(
        sub=sub, secret_key_hash=b"secret key 1", salt=b"salt 1"
    )
    public_key = "public key 1"
    secret_key = "secret key 1"
    authorization = types.TAuthorization(public_key=public_key, secret_key=secret_key)
    package_database.get().create_update_spec(
        sub=sub, name=spec_id, version=version_1, model_count=1
    )

    returned_value = library.create_list_response_value(
        authorization=authorization, uri=uri, auth_info=auth_info
    )

    package_name = spec_id.replace("-", "_")
    assert "<body>" in returned_value
    assert (
        '<a href="https://'
        f"{public_key}:{secret_key}"
        "@index.package.openalchemy.io/"
        f'{spec_id}/{package_name}-{version_1}.tar.gz">'
        f"{package_name}-{version_1}.tar.gz</a><br>"
    ) in returned_value
    assert "</body>" in returned_value

    mock_time.return_value = 2000000
    version_2 = "version 2"
    package_database.get().create_update_spec(
        sub=sub, name=spec_id, version=version_2, model_count=1
    )

    returned_value = library.create_list_response_value(
        authorization=authorization, uri=uri, auth_info=auth_info
    )

    assert (
        '<a href="https://'
        f"{public_key}:{secret_key}"
        "@index.package.openalchemy.io/"
        f'{spec_id}/{package_name}-{version_1}.tar.gz">'
        f"{package_name}-{version_1}.tar.gz</a><br>"
    ) in returned_value
    assert (
        '<a href="https://'
        f"{public_key}:{secret_key}"
        "@index.package.openalchemy.io/"
        f'{spec_id}/{package_name}-{version_2}.tar.gz">'
        f"{package_name}-{version_2}.tar.gz</a><br>"
    ) in returned_value