def setUp(self): self.transactions = [ Transaction(345, -1000), Transaction(345, 1500), Transaction(347, -2500) ] self.accounts = [Account(345, 14428), Account(347, 15000)]
class TestAccount(unittest.TestCase): def setUp(self): self.transactions = [Transaction(345, -1000), Transaction(345, 1500)] self.account = Account(345, 14428) def test_init_error_number_value_empty(self): with self.assertRaises(ValueError): Account('', '14428') def test_init_error_balance_value_empty(self): with self.assertRaises(ValueError): Account('345', '') def test_calculate_balance(self): self.account.calculate_balance(self.transactions) self.assertEqual(self.account.balance, 14928) def test_calculate_balance_negative_balance(self): transactions = [Transaction(345, -1000), Transaction(345, -1600)] account = Account(345, 2500) account.calculate_balance(transactions) self.assertEqual(account.balance, -600) def test_create_accounts_list(self): rows = File.read_csv(f'{CSV_FOLDER}/accounts.csv') accounts = Account.create_accounts_list(rows) self.assertEqual(len(accounts), 2)
def create_token(): """This function logs in and generates the access token.""" json_values = request.json values_required = {"username", "password"} response = Response(json.dumps(json_error(ResponsesREST.INVALID_INPUT.value)), status=ResponsesREST.INVALID_INPUT.value, mimetype="application/json") if all(key in json_values for key in values_required): if validator_login.is_valid(json_values): account_login = Account() account_login.username = json_values["username"] account_login.password = encode_password(json_values["password"]) account_login.member_ate_status = AccountStatus.ACTIVE.value result = account_login.login() if result in (ResponsesREST.SERVER_ERROR.value, ResponsesREST.NOT_FOUND.value, ResponsesREST.INVALID_REQUEST.value): response = Response(json.dumps(json_error(result)), status=result, mimetype="application/json") else: account_login.member_ate_type = result.member_ate_type account_login.id_member_ate = result.id_member_ate account_login.id_city = result.id_city token = Auth.generate_token(account_login) session.permanent = True session["token"] = token response = Response(json.dumps({"token": token, "memberATEType": account_login.member_ate_type, "idMemberATE": account_login.id_member_ate, "idCity": account_login.id_city}), status=ResponsesREST.CREATED.value, mimetype="application/json") return response
def login(): if signed_in(): return redirect(url_for('account')) if request.method == 'GET': return render_template('login.html', messages=None) else: username, password = request.form['username'], request.form['password'] username = username.strip().lower() rows = db.execute_query( "SELECT * FROM Account WHERE username = %(username)s;", {'username': username} ) try: row = rows.pop(0) stored_hash = row[2] if pw.matches(password, stored_hash): account = Account(row[0], row[1], row[2], row[3]) set_signed_in(account.username, account.email, account.password, account.admin) return redirect(url_for('account')) else: error = "Incorrect username or password." return render_template('login.html', messages={'error': error}, username=username) except Exception as e: error = "Incorrect username or password." return render_template('login.html', messages={'error': error}, username=username)
def find_all() -> List['Thread']: rows = db.execute_query(""" SELECT Thread.*, Account.username, Account.display_name, COUNT(Response.id) as response_count, MAX(Response.created) as last_active FROM Thread INNER JOIN Account ON Account.id = Thread.author_id LEFT JOIN Response ON Response.thread_id = Thread.id GROUP BY Thread.id, Account.username, Account.display_name ORDER BY last_active DESC; """) result = [] for row in rows: account = Account(row['author_id'], row['username'], row['display_name']) tags = Tag.find_by_thread_id(row['id']) thread = Thread(row['id'], account, tags, row['title'], row['created'], row['last_active'], row['response_count']) result.append(thread) return result
def find_by_id(uid: int) -> 'Thread': rows = db.execute_query( """ SELECT Thread.*, Account.username, Account.display_name FROM Thread INNER JOIN Account ON Account.id = Thread.author_id AND Thread.id = %(id)s; """, {'id': uid}) try: row = rows.pop(0) account = Account(row['author_id'], row['username'], row['display_name']) tags = Tag.find_by_thread_id(row['id']) responses = Response.find_by_thread_id(row['id']) thread = Thread(row['id'], account, tags, row['title'], row['created'], responses=responses) return thread except: return None
def validate_account(): """This function validates an account so that you can log in.""" json_values = request.json values_required = {"username", "password", "code"} response = Response(json.dumps(json_error(ResponsesREST.INVALID_INPUT.value)), status=ResponsesREST.INVALID_INPUT.value, mimetype="application/json") if all(key in json_values for key in values_required): if validator_login_validator.is_valid(json_values): account_login = Account() account_login.username = json_values["username"] account_login.password = encode_password(json_values["password"]) result = account_login.validate_account(json_values["code"]) if result == ResponsesREST.SUCCESSFUL.value: response = Response(status=result) else: response = Response(json.dumps(json_error(result)), status=result, mimetype="application/json") return response
def update_token(): """This function changes an employee's token so that he can sign in as a client.""" json_values = request.json values_required = {"username", "password"} response = Response(json.dumps(json_error(ResponsesREST.INVALID_INPUT.value)), status=ResponsesREST.INVALID_INPUT.value, mimetype="application/json") if all(key in json_values for key in values_required): if validator_login.is_valid(json_values): account_login = Account() account_login.username = json_values["username"] account_login.password = encode_password(json_values["password"]) token = Auth.generate_token(account_login) session.permanent = True session["token"] = token response = Response(json.dumps({"token": token}), status=ResponsesREST.SUCCESSFUL.value, mimetype="application/json") return response
def add_employee_account(id_account): """This function adds the type of user employed to an already created account.""" response = Response(json.dumps( json_error(ResponsesREST.INVALID_INPUT.value)), status=ResponsesREST.INVALID_INPUT.value, mimetype="application/json") if validator_id.is_valid({"id": id_account}): account_status = Account() account_status.id_member_ate = id_account account_status.member_ate_type = AccountRole.CLIENT_EMPLOYEE.value result = account_status.add_employee_account() if result == ResponsesREST.SUCCESSFUL.value: response = Response(status=result) else: response = Response(json.dumps(json_error(result)), status=result, mimetype="application/json") return response
def validate_change_password(): """This function changes the password of an account based on the confirmation code.""" json_values = request.json values_required = {"email", "password", "code"} response = Response(json.dumps(json_error(ResponsesREST.INVALID_INPUT.value)), status=ResponsesREST.INVALID_INPUT.value, mimetype="application/json") if all(key in json_values for key in values_required): if validator_login_password.is_valid(json_values): account_login = Account() account_login.email = json_values["email"] account_login.password = encode_password(json_values["password"]) result = account_login.validate_change_password(json_values["code"]) if result == ResponsesREST.SUCCESSFUL.value: response = Response(status=result) else: response = Response(json.dumps(json_error(result)), status=result, mimetype="application/json") return response
def account(): # account = Account(session['username'], session['password'], session['admin']) if signed_in() else None account = None messages = None if all(key in session for key in ('username', 'email', 'password', 'admin')): account = Account(session['username'], session['email'], session['password'], session['admin']) if 'fresh' in session: messages = {'welcome': "Welcome!"} return render_template('account.html', account=account, messages=messages)
def send_message_to_account(): """This function updates the verification code and sends it to the email.""" json_values = request.json values = {"email"} response = Response(json.dumps( json_error(ResponsesREST.INVALID_INPUT.value)), status=ResponsesREST.INVALID_INPUT.value, mimetype="application/json") if all(key in json_values for key in values): if validator_email.is_valid(json_values): account = Account() account.email = json_values["email"] result = account.change_code(create_code()) if result == ResponsesREST.SUCCESSFUL.value: response = Response(json.dumps({"email": account.email}), status=result, mimetype="application/json") else: response = Response(json.dumps(json_error(result)), status=result, mimetype="application/json") return response
def find_by_thread_id(thread_id: int) -> List['Response']: rows = db.execute_query( """ SELECT Response.*, Account.username, Account.display_name FROM Response INNER JOIN Account ON Account.id = Response.author_id AND Response.thread_id = %(thread_id)s ORDER BY created ASC; """, {'thread_id': thread_id}) result = [] for row in rows: account = Account(row['author_id'], row['username'], row['display_name']) response = Response(row['id'], account, row['content'], row['created']) result.append(response) return result
def test_init_error_number_value_empty(self): with self.assertRaises(ValueError): Account('', '14428')
def test_create_transaction(self): account = Account(345, 14428) transactions = Transaction.find_transaction(self.transactions, account) self.assertEqual(len(transactions), 2)
def test_calculate_balance_negative_balance(self): transactions = [Transaction(345, -1000), Transaction(345, -1600)] account = Account(345, 2500) account.calculate_balance(transactions) self.assertEqual(account.balance, -600)
def test_init_error_balance_value_empty(self): with self.assertRaises(ValueError): Account('345', '')
def test_create_account(): acc = Account("abc", "123") assert acc.username == "abc"
def test_create_accounts_list(self): rows = File.read_csv(f'{CSV_FOLDER}/accounts.csv') accounts = Account.create_accounts_list(rows) self.assertEqual(len(accounts), 2)
def update(uid: int, display_name: str): account = Account(uid, None, display_name) result = account.update() return result
def signup(username: str, password: str, repeated_password: str): result = Account.create(username, password, repeated_password) return result
def signin(username: str, password: str): result = Account.authenticate(username, password) return result
def view_for_edit_profile(uid: int, messages: Dict[str, str] = None): account = Account.find_by_id(uid) return render_template('edit_profile.html', title="Profile", messages=messages, account=account)
from src.models.account import Account from src.models.profile import Profile from src.models.transaction import Transaction p = Profile.create(name="test") a = Account.create(name="test_account", profile=p) t = Transaction.create( name="test_account", value=0.0, category="yeet", description="supreme", account=a, ) print(p) print(t) print(a)