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 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
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
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 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
def run(args): """This function is called to handle requests for information about particular accounts """ 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 = login_to_service_account() account = Accounts(authorisation.user_uid()).get_account(account_name, bucket=bucket) # validate the authorisation for this account authorisation.verify(resource=account.uid()) balance_status = account.balance_status(bucket=bucket) status = 0 message = "Success" return_value = create_return_value(status, message) if account: return_value["description"] = account.description() return_value["overdraft_limit"] = str(account.get_overdraft_limit()) if balance_status: for key in balance_status.keys(): return_value[key] = str(balance_status[key]) return return_value
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 request to cash cheques. This will verify that the cheque is valid and will then create the debit/credit note pair for the transation. It will return the CreditNote to the caller so they can see that the funds have been reserved, and can receipt the transaction once goods/services have been delivered. Args: args (dict): information for payment for service Returns: dict: contains status, status message and credit note if valid """ credit_notes = [] try: cheque = args["cheque"] except: raise ValueError("You must supply a cheque to be cashed!") try: cheque = Cheque.from_data(cheque) except Exception as e: from Acquire.Service import exception_to_string raise TypeError("Unable to interpret the cheque.\n\nCAUSE: %s" % exception_to_string(e)) try: spend = args["spend"] except: spend = None if spend is not None: try: spend = string_to_decimal(spend) except Exception as e: from Acquire.Service import exception_to_string raise TypeError("Unable to interpret the spend.\n\nCause: %s" % exception_to_string(e)) try: resource = str(args["resource"]) except: raise ValueError( "You must supply a string representing the resource that will " "be paid for using this cheque") try: account_uid = str(args["account_uid"]) except: raise ValueError("You must supply the UID of the account to which the " "cheque will be cashed") try: receipt_by = args["receipt_by"] except: raise ValueError( "You must supply the datetime by which you promise to " "receipt this transaction") try: receipt_by = string_to_datetime(receipt_by) except Exception as e: from Acquire.Service import exception_to_string raise TypeError( "Unable to interpret the receipt_by date.\n\nCAUSE: %s" % exception_to_string(e)) # now read the cheque - this will only succeed if the cheque # is valid, has been signed, has been sent from the right # service, and was authorised by the user, the cheque # has not expired and we are the # service which holds the account from which funds are drawn info = cheque.read(resource=resource, spend=spend, receipt_by=receipt_by) try: description = str(args["description"]) except: description = info["resource"] authorisation = info["authorisation"] auth_resource = info["auth_resource"] user_guid = authorisation.user_guid() # the cheque is valid bucket = get_service_account_bucket() try: debit_account = Account(uid=info["account_uid"], bucket=bucket) except Exception as e: from Acquire.Service import exception_to_string raise PaymentError("Cannot find the account associated with the cheque" "\n\nCAUSE: %s" % exception_to_string(e)) try: credit_account = Account(uid=account_uid, bucket=bucket) except Exception as e: from Acquire.Service import exception_to_string raise PaymentError( "Cannot find the account to which funds will be creditted:" "\n\nCAUSE: %s" % exception_to_string(e)) # validate that this account is in a group that can be authorised # by the user (this should eventually go as the ACLs now allow users # to authorised payments from many accounts) accounts = Accounts(user_guid=user_guid) if not accounts.contains(account=debit_account, bucket=bucket): raise PermissionError( "The user with UID '%s' cannot authorise transactions from " "the account '%s' as they do not own this account." % (user_guid, str(debit_account))) transaction = Transaction(value=info["spend"], description=description) # we have enough information to perform the transaction # - this is provisional as the service must receipt everything transaction_records = Ledger.perform(transactions=transaction, debit_account=debit_account, credit_account=credit_account, authorisation=authorisation, authorisation_resource=auth_resource, is_provisional=True, receipt_by=receipt_by, bucket=bucket) # extract all of the credit notes to return to the user, # and also to record so that we can check if they have not # been receipted in time... credit_notes = [] for record in transaction_records: credit_notes.append(record.credit_note()) credit_notes = list_to_string(credit_notes) receipt_key = "accounting/cashed_cheque/%s" % info["uid"] mutex = Mutex(receipt_key, bucket=bucket) try: receipted = ObjectStore.get_object_from_json(bucket, receipt_key) except: receipted = None if receipted is not None: # we have tried to cash this cheque twice! mutex.unlock() Ledger.refund(transaction_records, bucket=bucket) else: info = {"status": "needs_receipt", "creditnotes": credit_notes} ObjectStore.set_object_from_json(bucket, receipt_key, info) mutex.unlock() return {"credit_notes": credit_notes}
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
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
def run(args): """This function is called to handle requests to perform transactions between accounts Args: args (dict): data for account transfers Returns: dict: contains status, status message and transaction records if any are available """ transaction_records = None try: debit_account_uid = str(args["debit_account_uid"]) except: debit_account_uid = None try: credit_account_uid = str(args["credit_account_uid"]) except: credit_account_uid = None try: authorisation = Authorisation.from_data(args["authorisation"]) except: authorisation = None try: transaction = Transaction.from_data(args["transaction"]) except: transaction = None try: is_provisional = bool(args["is_provisional"]) except: is_provisional = None if debit_account_uid is None: raise TransactionError("You must supply the account UID " "for the debit account") if credit_account_uid is None: raise TransactionError("You must supply the account UID " "for the credit account") if debit_account_uid == credit_account_uid: raise TransactionError( "You cannot perform a transaction where the debit and credit " "accounts are the same!") if transaction is None or transaction.is_null(): raise TransactionError("You must supply a valid transaction to " "perform!") if is_provisional is None: raise TransactionError("You must say whether or not the " "transaction is provisional using " "is_provisional") if authorisation is None: raise PermissionError("You must supply a valid authorisation " "to perform transactions between accounts") authorisation.assert_once() user_guid = authorisation.user_guid() # load the account from which the transaction will be performed bucket = get_service_account_bucket() debit_account = Account(uid=debit_account_uid, bucket=bucket) # validate that this account is in a group that can be authorised # by the user - This should eventually go as this is all # handled by the ACLs if not Accounts(user_guid).contains(account=debit_account, bucket=bucket): raise PermissionError( "The user with GUID '%s' cannot authorise transactions from " "the account '%s' as they do not own this account." % (user_guid, str(debit_account))) # now load the two accounts involved in the transaction credit_account = Account(uid=credit_account_uid, bucket=bucket) # we have enough information to perform the transaction transaction_records = Ledger.perform(transactions=transaction, debit_account=debit_account, credit_account=credit_account, authorisation=authorisation, is_provisional=is_provisional, 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 return return_value