예제 #1
0
def test_account(bucket):
    name = "test account"
    description = "This is a test account"

    push_is_running_service()

    try:
        account = Account(name, description, bucket=bucket)

        assert (account.name() == name)
        assert (account.description() == description)
        assert (not account.is_null())

        uid = account.uid()
        assert (uid is not None)

        account2 = Account(uid=uid, bucket=bucket)

        assert (account2.name() == name)
        assert (account2.description() == description)

        assert (account.balance() == Balance())
    except:
        pop_is_running_service()
        raise

    pop_is_running_service()
예제 #2
0
def bucket(tmpdir_factory):
    try:
        return get_service_account_bucket()
    except:
        d = tmpdir_factory.mktemp("objstore")
        push_is_running_service()
        return get_service_account_bucket(str(d))
예제 #3
0
def bucket(tmpdir_factory):
    d = tmpdir_factory.mktemp("simple_objstore")
    push_is_running_service()
    bucket = get_service_account_bucket(str(d))

    while is_running_service():
        pop_is_running_service()

    return bucket
예제 #4
0
def _base_handler(additional_functions=None, ctx=None, data=None, loop=None):
    """This function routes calls to sub-functions, thereby allowing
       a single function to stay hot for longer. If you want
       to add additional functions then add them via the
       'additional_functions' argument. This should accept 'function'
       and 'args', returning some output if the function is found,
       or 'None' if the function is not available

       Args:
        additional_functions (function): function to be routed
        ctx: currently unused
        data (str): to be passed as arguments to other functions
        TODO - expand this
        loop: currently unused

        Returns:
            dict: JSON serialisable dict

       """

    # Make sure we set the flag to say that this code is running
    # as part of a service
    from Acquire.Service import push_is_running_service, \
        pop_is_running_service, unpack_arguments, \
        get_service_private_key, pack_return_value, \
        create_return_value

    push_is_running_service()

    result = None

    try:
        (function, args, keys) = unpack_arguments(data,
                                                  get_service_private_key)
    except Exception as e:
        function = None
        args = None
        result = e
        keys = None

    if result is None:
        try:
            result = _handle(function=function,
                             additional_functions=additional_functions,
                             args=args)
        except Exception as e:
            result = e

    result = create_return_value(payload=result)

    try:
        result = pack_return_value(payload=result, key=keys)
    except Exception as e:
        result = pack_return_value(payload=create_return_value(e))

    pop_is_running_service()
    return result
예제 #5
0
def test_service_object(tmpdir_factory):
    bucket = tmpdir_factory.mktemp("test_service")
    push_testing_objstore(bucket)
    push_is_running_service()

    try:
        service = Service.create(service_type="identity",
                                 service_url="identity")

        assert(service.uid() is not None)
        assert(service.uid().startswith("STAGE1"))

        service.create_stage2(service_uid="Z9-Z8", response=service.uid())

        assert(service.is_identity_service())
        assert(not service.should_refresh_keys())
        assert(service.is_unlocked())
        assert(not service.is_locked())

        passphrase = PrivateKey.random_passphrase()

        data = service.to_data(passphrase)

        service2 = IdentityService.from_data(data, passphrase)

        assert(service2.uid() == service.uid())
        assert(service2.is_unlocked())
        assert(not service2.is_locked())
        assert(service2.is_identity_service())
        assert(service.canonical_url() == service2.canonical_url())
        assert(not service2.should_refresh_keys())

        keys = service.dump_keys()

        keys = service.load_keys(keys)

        assert(keys[service.private_key().fingerprint()] ==
               service.private_key())
        assert(keys[service.private_certificate().fingerprint()] ==
               service.private_certificate())

        service.refresh_keys()

        assert(service.last_key_update() > service2.last_key_update())
        assert(service.last_certificate().public_key() ==
               service2.public_certificate())
        assert(service.last_key() == service2.private_key())
    except:
        pop_is_running_service()
        pop_testing_objstore()
        raise

    pop_is_running_service()
    pop_testing_objstore()
예제 #6
0
def account1(bucket):
    push_is_running_service()
    accounts = Accounts(user_guid=account1_user)
    account = Account(name="Testing Account",
                      description="This is the test account",
                      group_name=accounts.name(),
                      bucket=bucket)
    uid = account.uid()
    assert (uid is not None)
    assert (account.balance() == Balance())

    account.set_overdraft_limit(account1_overdraft_limit)
    assert (account.get_overdraft_limit() == account1_overdraft_limit)
    pop_is_running_service()

    return account
def test_authorisation(bucket):
    push_is_running_service()

    try:
        key = get_private_key("testing")

        resource = uuid.uuid4()

        auth = Authorisation(resource=resource, testing_key=key)

        auth.assert_once()

        with pytest.raises(PermissionError):
            auth.assert_once()

        auth.verify(resource=resource)

        wrong_resource = uuid.uuid4()

        with pytest.raises(PermissionError):
            auth.verify(resource=wrong_resource)

        data = auth.to_data()

        new_auth = Authorisation.from_data(data)

        with pytest.raises(PermissionError):
            new_auth.verify(resource=resource)

        new_auth._testing_key = key

        new_auth.verify(resource=resource)

        with pytest.raises(PermissionError):
            new_auth.assert_once()

        with pytest.raises(PermissionError):
            new_auth.verify(resource=wrong_resource)
    except:
        pop_is_running_service()
        raise

    pop_is_running_service()
예제 #8
0
def account1(bucket):
    if not have_freezetime:
        return None

    with freeze_time(start_time) as _frozen_datetime:
        now = get_datetime_now()
        assert (start_time == now)
        push_is_running_service()
        accounts = Accounts(user_guid=account1_user)
        account = Account(name="Testing Account",
                          description="This is the test account",
                          group_name=accounts.name())
        uid = account.uid()
        assert (uid is not None)
        assert (account.balance() == Balance())

        account.set_overdraft_limit(account1_overdraft_limit)
        assert (account.get_overdraft_limit() == account1_overdraft_limit)
        pop_is_running_service()

    return account
예제 #9
0
def test_service(service_url, aaai_services):
    # get the public service from the default API frontend
    privkey = get_private_key("testing")
    response = call_function(service_url, response_key=privkey)
    service = Service.from_data(response["service_info"])

    # also read the service from the object store directly
    push_testing_objstore(aaai_services["_services"][service_url])
    push_is_running_service()
    private_service = get_this_service(need_private_access=True)
    pop_is_running_service()
    pop_testing_objstore()

    # create some test data that contain unicode characters for
    # testing encryption, signing and both encryption and signing
    data = {"hello": "'å∫ç∂ƒ©˙˚'", "key": privkey.public_key().to_data()}

    encrypted = service.encrypt_data(data)
    decrypted = private_service.decrypt_data(encrypted)

    assert(data == decrypted)

    signed = private_service.sign_data(data)
    verified = service.verify_data(signed)

    assert(data == verified)

    enc_sign = service.encrypt_data(private_service.sign_data(data))
    dec_ver = service.verify_data(private_service.decrypt_data(enc_sign))

    assert(data == dec_ver)

    service.call_function("admin/test")

    admin_user = aaai_services[service_url]["user"]
    auth = Authorisation(user=admin_user,
                         resource="dump_keys %s" % service.uid())

    service.call_function(
        function="dump_keys", args={"authorisation": auth.to_data()})
예제 #10
0
def test_mutex(bucket):
    push_is_running_service()

    try:
        m = Mutex("ObjectStore.test_mutex")

        assert (m.is_locked())
        m.unlock()
        assert (not m.is_locked())
        m.lock()
        assert (m.is_locked())
        m.lock()
        assert (m.is_locked())
        m.unlock()
        assert (m.is_locked())
        m.unlock()
        assert (not m.is_locked())

        m2 = Mutex("ObjectStore.test_mutex")
        assert (m2.is_locked())

        with pytest.raises(MutexTimeoutError):
            m.lock(timeout=0.25)

        assert (not m.is_locked())
        assert (m2.is_locked())

        m2.unlock()
        m.lock()

        assert (m.is_locked())
        assert (not m2.is_locked())

        m.lock(lease_time=0.25)

        time.sleep(0.3)

        with pytest.raises(MutexTimeoutError):
            m.assert_not_expired()

        assert (m.expired())

        with pytest.raises(MutexTimeoutError):
            m.unlock()

        assert (not m.is_locked())

        m.lock(lease_time=0.25)

        time.sleep(0.3)

        assert (m.expired())

        m2.lock()

        assert (m2.is_locked())

        m2.unlock()

        assert (not m2.is_locked())

        with pytest.raises(MutexTimeoutError):
            m.fully_unlock()

        assert (not m.is_locked())
    except:
        pop_is_running_service()
        raise

    pop_is_running_service()