def test_accounts(bucket):
    for group in [None, "chris", "ƒ˚®ø©∆∂µ"]:
        accounts = Accounts(group=group)

        account_names = [
            "new account", "chris's checking account", "å∫ç∂´® account"
        ]

        created_accounts = {}

        for name in account_names:
            account = accounts.create_account(name,
                                              description="Account: %s" % name,
                                              bucket=bucket)

            assert (name == account.name())

            created_accounts[name] = account

        names = accounts.list_accounts()

        for name in account_names:
            assert (name in names)

        for name in account_names:
            account = accounts.get_account(name, bucket=bucket)
            assert (name == account.name())

            assert (account == created_accounts[name])
Beispiel #2
0
def run(args):
    """This function is called to handle requests for information about
       particular accounts

       Args:
            args (dict): data for account query

        Returns:
            dict: contains status, status message and details regarding
                the account including balance (if available), overdraft
                limit and a description of the account
    """

    status = 0
    message = None

    account = None
    balance_status = None

    try:
        account_name = str(args["account_name"])
    except:
        account_name = None

    try:
        authorisation = Authorisation.from_data(args["authorisation"])
    except:
        authorisation = None

    if account_name is None:
        raise AccountError("You must supply the account_name")

    if authorisation is None:
        raise AccountError("You must supply a valid authorisation")

    # load the account
    bucket = get_service_account_bucket()
    accounts = Accounts(user_guid=authorisation.user_guid())
    account = accounts.get_account(account_name, bucket=bucket)

    # validate the authorisation for this account
    authorisation.verify(resource="get_info %s" % account.uid())

    balance = account.balance()

    return_value = {}

    return_value["description"] = account.description()
    return_value["overdraft_limit"] = str(account.get_overdraft_limit())
    return_value["balance"] = balance.to_data()

    return return_value
Beispiel #3
0
def test_accounts(bucket):
    for user_guid in [None, "chris@something", "ƒ˚®ø©∆∂µ@¨^ø¨^ø"]:
        if user_guid is None:
            accounts = Accounts(group=user_guid,
                                aclrules=ACLRules.owner(user_guid=None))
        else:
            accounts = Accounts(user_guid=user_guid)

        account_names = [
            "new account", "chris's checking account", "å∫ç∂´® account"
        ]

        created_accounts = {}

        testing_key = get_private_key("testing")

        for name in account_names:
            authorisation = Authorisation(resource="create_account %s" % name,
                                          testing_key=testing_key,
                                          testing_user_guid=user_guid)

            account = accounts.create_account(name,
                                              description="Account: %s" % name,
                                              bucket=bucket,
                                              authorisation=authorisation)

            assert (name == account.name())

            created_accounts[name] = account

        names = accounts.list_accounts()

        for name in account_names:
            assert (name in names)

        for name in account_names:
            account = accounts.get_account(name, bucket=bucket)
            assert (name == account.name())

            assert (account == created_accounts[name])
def run(args):
    """This function is called to handle requests for the UIDs of accounts
    
        Args:
            args (dict): data regarding account to be queried

        Returns:
            dict: contains status, status message and account UIDs
    
    """

    status = 0
    message = None

    account_uids = None

    try:
        account_name = str(args["account_name"])
    except:
        account_name = None

    try:
        authorisation = Authorisation.from_data(args["authorisation"])
    except:
        authorisation = None

    try:
        user_guid = str(args["user_guid"])
    except:
        user_guid = None

    is_authorised = False

    if authorisation is not None:
        if not isinstance(authorisation, Authorisation):
            raise TypeError("All authorisations must be of type "
                            "Authorisation")

        if user_guid:
            if user_guid == authorisation.user_guid():
                authorisation.verify(resource="get_account_uids")
                is_authorised = True
        else:
            authorisation.verify(resource="get_account_uids")
            user_guid = authorisation.user_guid()
            is_authorised = True

    if user_guid is None:
        raise ValueError("You must supply either an Authorisation or the "
                         "user_guid")

    # try to create a 'main' account for this user
    account_uids = {}
    accounts = Accounts(user_guid=user_guid)

    if account_name is None:
        if not is_authorised:
            raise PermissionError(
                "You cannot list general information about a user's "
                "accounts unless you have authenticated as the user!")

        bucket = get_service_account_bucket()
        account_names = accounts.list_accounts(bucket=bucket)

        for account_name in account_names:
            account = accounts.get_account(account_name, bucket=bucket)
            account_uids[account.uid()] = account.name()

    else:
        if not is_authorised:
            try:
                account = accounts.get_account(account_name)
            except:
                # don't leak any information
                raise ListAccountsError(
                    "No account called '%s' for user '%s'" %
                    (account_name, user_guid))
        else:
            # allow the user to see the real exception if this
            # account doesn't exist
            account = accounts.get_account(account_name)

        account_uids[account.uid()] = account.name()

    return_value = {}

    if account_uids:
        return_value["account_uids"] = account_uids

    return return_value