Ejemplo n.º 1
0
 def test_update_balance(self):
     exchange = Currency(EUR)
     rate = exchange.rate
     if not db.exists.currency(EUR):
         db.inserts.add_currency(EUR, rate)
     curr_id = db.gets.get_currency_id(EUR)
     passcode = get_rand_pass()
     email = get_email()
     credid = get_credid(email)
     balance = get_balance()
     email = get_email()
     name = get_name()
     db.inserts.add_client(name, email, curr_id)
     db.commit()
     cid = db.gets.get_client_id_byemail(email)
     db.inserts.register(cid, passcode, credid)
     db.commit()
     #add_bank_addount
     bid = db.inserts.add_bank_account(cid, balance, bank_name,
                                       branch_number, account_number,
                                       name_reference, curr_id)
     db.commit()
     db.updates.update_account(cid, 0)
     balance_cur = db.gets.get_balance_by_cid(cid)['balance']
     self.assertEqual(balance_cur, 0)
Ejemplo n.º 2
0
    def test_bid_cid_conversion(self):
        """ bid_cid conversion testing

        convert, and cross-reference from client id, to bank id
        """
        exchange = Currency(EUR)
        rate = exchange.rate
        if not db.exists.currency(EUR):
            db.inserts.add_currency(EUR, rate)
        curr_id = db.gets.get_currency_id(EUR)
        passcode = get_rand_pass()
        email = get_email()
        credid = get_credid(email)
        banalce = get_balance()
        email = get_email()
        name = get_name()
        db.inserts.add_client(name, email, curr_id)
        cid = db.gets.get_client_id_byemail(email)
        db.inserts.register(cid, passcode, credid)
        #add_bank_addount
        bid = db.inserts.add_bank_account(cid, balance, bank_name,
                                          branch_number, account_number,
                                          name_reference, curr_id)
        cid_eq = db.gets.get_client_id(bid)
        bid_eq = db.gets.get_banking_id(cid_eq)
        self.assertEqual(cid_eq, cid)
        self.assertEqual(bid_eq, bid)
Ejemplo n.º 3
0
def register_client():
    """register new client with email/name + password, expected json body would have keys ["name", "email", "passcode"]

  """
    #TODO this can through exception, need to handle it
    req = request.get_json(force=True)
    #TODO i think the required is to authenticate by email/name
    name = req.get('name', None)
    passcode = req.get('passcode', None)
    email = req.get('email', None)
    cur_pref = req.get('cur_pref', EUR)
    if not req or name == None or email == None or passcode == None:
        logger.critical("url is incomplete")
        print('incomplete')
        abort(401, 'incomplete post payload')
    cred_id = get_credid(email)
    logger.info("registering trader for client: {}".format(name))
    bid = 0
    db.init()
    lock = 2
    try:
        db.lock_advisory(lock)
        email_exists = db.exists.account_byemail(email)
        if email_exists:
            print('email exists')
            abort(400)
            logger.debug("account {}/{} + {} already exists".\
                         format(name, email, passcode))
            raise Exception("account already exists!")
        if not db.exists.currency(cur_pref):
            currency = Currency(cur_pref)
            if not currency.valid():
                print('invalid currency')
                raise Exception("currency isn't supported!")
            db.inserts.add_currency(cur_pref, currency.rate)
        cur_pref_id = db.gets.get_currency_id(cur_pref)
        cid = db.inserts.add_client(req['name'], req['email'], cur_pref_id)
        logger.debug("client added")
        db.inserts.register(cid, passcode, cred_id)
        db.commit(lock)
    except psycopg2.DatabaseError as error:
        db.rollback(lock)
        emsg = "registering failed, error: ".format(str(error))
        logger.critical(emsg)
        abort(500, emsg)
    except:
        db.rollback(lock)
        emsg = "registering failed"
        logger.critical(emsg)
        abort(403, emsg)
    finally:
        db.close()
        res = {'cred_id': cred_id}
    return jsonify(res), 201
Ejemplo n.º 4
0
 def test_credid2cid(self):
     exchange = Currency(EUR)
     rate = exchange.rate
     if not db.exists.currency(EUR):
         db.inserts.add_currency(EUR, rate)
     curr_id = db.gets.get_currency_id(EUR)
     passcode = get_rand_pass()
     email = get_email()
     credid = get_credid(email)
     # add new client
     db.inserts.add_client(name, email, curr_id)
     cid = db.gets.get_client_id_byemail(email)
     # register client's credentials
     db.inserts.register(cid, passcode, credid)
     # credid2cid conversion
     cid_eq = db.gets.credid2cid(credid)
     self.assertEqual(cid, cid_eq)
     credid_eq = db.gets.cid2credid(cid)
     self.assertEqual(credid, credid_eq)
Ejemplo n.º 5
0
 def test_credential_exists(self):
     exchange = Currency(EUR)
     rate = exchange.rate
     if not db.exists.currency(EUR):
         db.inserts.add_currency(EUR, rate)
     curr_id = db.gets.get_currency_id(EUR)
     name = get_name()
     email = get_email()
     passcode = get_rand_pass()
     credid = get_credid(email)
     db.inserts.add_client(name, email, curr_id)
     db.commit()
     cid = db.gets.get_client_id_byemail(email)
     db.commit()
     self.assertFalse(db.exists.credential_exists(0))
     db.inserts.register(cid, passcode, credid)
     cid = db.gets.get_client_id_byname(name, passcode)
     db.commit()
     self.assertTrue(db.exists.credential_exists(cid))
Ejemplo n.º 6
0
 def test_add_bank_account(self):
     exchange = Currency(EUR)
     rate = exchange.rate
     if not db.exists.currency(EUR):
         db.inserts.add_currency(EUR, rate)
     curr_id = db.gets.get_currency_id(EUR)
     passcode = get_rand_pass()
     email = get_email()
     credid = get_credid(email)
     banalce = get_balance()
     email = get_email()
     name = get_name()
     db.inserts.add_client(name, email, curr_id)
     cid = db.gets.get_client_id_byemail(email)
     db.inserts.register(cid, passcode, credid)
     #add_bank_addount
     bid = db.inserts.add_bank_account(cid, balance, bank_name,
                                       branch_number, account_number,
                                       name_reference, curr_id)
     self.assertTrue(db.exists.bank_account_bycid(cid))
Ejemplo n.º 7
0
    def test_transaction(self):
        """ create two clients, client_1, client_2

        procedure:
        - client_1 sends 10k to client_2
        - client_1 sends 20k to client_2
        - client_2 sends 5k to client_1
        - transaction sum 35k sent/received
        """
        exchange = Currency(EUR)
        rate = exchange.rate
        if not db.exists.currency(EUR):
            db.inserts.add_currency(EUR, rate)
        curr_id = db.gets.get_currency_id(EUR)
        c1_name = get_name()
        c1_email = get_email()
        c1_passcode = get_rand_pass()
        c1_credid = get_credid(c1_email)
        c1_bank_name = get_bank_name()
        c1_branch_number = get_branch_number()
        c1_account_number = get_account_number()
        c1_name_reference = get_name_reference()
        c1_balance = get_balance()
        db.inserts.add_client(c1_name, c1_email, curr_id)
        db.commit()
        c1_cid = db.gets.get_client_id_byemail(c1_email)
        db.inserts.register(c1_cid, c1_passcode, c1_credid)
        db.commit()
        db.inserts.add_bank_account(c1_cid, c1_balance, c1_bank_name,
                                    c1_branch_number, c1_account_number,
                                    c1_name_reference, curr_id)
        db.commit()
        #
        c2_name = get_name()
        c2_email = get_email()
        c2_passcode = get_rand_pass()
        c2_credid = get_credid(c2_email)
        c2_bank_name = get_bank_name()
        c2_branch_number = get_branch_number()
        c2_account_number = get_account_number()
        c2_name_reference = get_name_reference()
        c2_balance = get_balance()
        db.inserts.add_client(c2_name, c2_email, curr_id)
        db.commit()
        c2_cid = db.gets.get_client_id_byemail(c2_email)
        db.inserts.register(c2_cid, c2_passcode, c2_credid)
        db.commit()
        db.inserts.add_bank_account(c2_cid, c2_balance, c2_bank_name,
                                    c2_branch_number, c2_account_number,
                                    c2_name_reference, curr_id)
        db.commit()
        #
        ################
        # transactions
        ################
        costs = [10000, 20000, 5000]
        trx_st_0 = datetime.datetime.now().strftime(TIMESTAMP_FORMAT)
        db.inserts.insert_trx(c2_credid, c1_credid, costs[0], curr_id,
                              'transaction1')
        db.commit()
        trx_st_1 = datetime.datetime.now().strftime(TIMESTAMP_FORMAT)
        db.inserts.insert_trx(c2_credid, c1_credid, costs[1], curr_id,
                              'transaction2')
        db.commit()
        trx_st_2 = datetime.datetime.now().strftime(TIMESTAMP_FORMAT)
        db.inserts.insert_trx(c1_credid, c2_credid, costs[2], curr_id,
                              'transaction3')
        db.commit()
        trx_st_3 = datetime.datetime.now().strftime(TIMESTAMP_FORMAT)
        ##################
        # validation test
        ##################
        #epoch 1
        c1_trx_sum_0 = db.gets.get_transactions_sum(c1_credid, trx_st_0)
        db.commit()
        self.assertEqual(sum(costs), c1_trx_sum_0)
        #epoch 2
        c1_trx_sum_1 = db.gets.get_transactions_sum(c1_credid, trx_st_1)
        db.commit()
        self.assertEqual(sum(costs[1:]), c1_trx_sum_1)
        #epoch 3
        c1_trx_sum_2 = db.gets.get_transactions_sum(c1_credid, trx_st_2)
        db.commit()
        self.assertEqual(sum(costs[2:]), c1_trx_sum_2)
Ejemplo n.º 8
0
def get_name():
    return faker.name()


def get_email():
    return faker.email()


def get_balance():
    return 333 * random.random()


email = get_email()
name = get_name()
credid = get_credid(email)
balance = get_balance()
ADD_CURRENCY_LOCK = 1
lock = ADD_CURRENCY_LOCK


def get_rand_pass(L=9):
    passcode=''.join(random.choice(string.ascii_uppercase+\
                                 string.ascii_lowercase+\
                                 string.digits)\
                     for _ in range(L))
    return passcode


class ServerDatabaseTest(unittest.TestCase):
    def test_currency(self):