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])
def run(args):
    """This function is called to handle creating accounts for users"""

    status = 0
    message = None

    account_uid = None

    try:
        account_name = args["account_name"]
    except:
        account_name = None

    try:
        description = args["description"]
    except:
        description = None

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

    if account_name is None or description is None \
            or authorisation is None:
        raise CreateAccountError("You must supply both an account name "
                                 "and a description to create an account")

    if not isinstance(authorisation, Authorisation):
        raise TypeError("The passed authorisation must be of type "
                        "Authorisation")

    authorisation.verify()

    # try to create a 'main' account for this user
    accounts = Accounts(authorisation.user_uid())
    account = accounts.create_account(name=account_name,
                                      description=description)

    account_uid = account.uid()

    status = 0
    message = "Success"

    return_value = create_return_value(status, message)

    if account_uid:
        return_value["account_uid"] = account_uid

    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])
Beispiel #4
0
def run(args):
    """This function is called to handle requests from a user to deposit
       more funds into their account. This will add this deposit as a
       debt for the user. Once the debt exceeds a certain value, then the
       backend-payment system will charge the user's real account to
       recover the funds

       Args:
            args (dict): data for deposit of funds into the account

        Returns:
            dict: contains status, status message and details regarding
                the deposit into the account and invoice data
    """

    transaction_records = None
    invoice_value = None
    invoice_user = None

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

    transaction = Transaction.from_data(args["transaction"])

    if authorisation is None:
        raise PermissionError("You must supply a valid authorisation "
                              "to deposit funds into your account")

    if transaction is None or transaction.is_null():
        raise ValueError("You must supply a valid transaction that "
                         "represents the deposit")

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

    if transaction.value() > 0:
        user_guid = authorisation.user_guid()

        # load the account from which the transaction will be performed
        bucket = get_service_account_bucket()
        accounts = Accounts(user_guid=user_guid)

        # deposits are made by transferring funds from the user's
        # 'billing' account to their named account (or 'deposits' account)
        deposit_account = accounts.create_account(account_name,
                                                  "Deposit account",
                                                  bucket=bucket)

        billing_account = accounts.create_account("billing",
                                                  "Billing account",
                                                  overdraft_limit=150,
                                                  bucket=bucket)

        billing_balance = billing_account.balance() - transaction.value()

        if billing_balance.balance() < -50.0:
            # there are sufficient funds that need to be transferred that
            # it is worth really charging the user
            invoice_user = user_guid
            invoice_value = billing_balance

        # we have enough information to perform the transaction
        transaction_records = Ledger.perform(transactions=transaction,
                                             debit_account=billing_account,
                                             credit_account=deposit_account,
                                             authorisation=authorisation,
                                             is_provisional=False,
                                             bucket=bucket)

    return_value = {}

    if transaction_records:
        try:
            transaction_records[0]
        except:
            transaction_records = [transaction_records]

        for i in range(0, len(transaction_records)):
            transaction_records[i] = transaction_records[i].to_data()

        return_value["transaction_records"] = transaction_records

    if invoice_user:
        return_value["invoice_user"] = invoice_user
        return_value["invoice_value"] = str(invoice_value)

    return return_value