- Improved directory structure
- Added functional and unit tests
This commit is contained in:
2022-03-30 10:46:54 +02:00
parent e27e2175b5
commit 21d2bc334c
33 changed files with 1782 additions and 238 deletions

View File

View File

@@ -0,0 +1,14 @@
"""
This file (test_helper_functions.py) contains the unit tests for the helper_functions.py file.
"""
import datetime
import jwt
from app.auth import *
def test_verify_token():
"""
Test verify_token function
"""
assert verify_token(None) is False

View File

@@ -0,0 +1,67 @@
"""
This file (test_helper_functions.py) contains the unit tests for the helper_functions.py file.
"""
import datetime
import jwt
from app.helper_functions import *
def test_hash_password():
"""
Test hash_password function
"""
assert hash_password("password") != "password"
def test_check_password():
"""
Test check_password function
"""
hashed = hash_password("password")
assert check_password(hashed, "password".encode("utf-8")) is True
assert check_password(hashed, "password1".encode("utf-8")) is False
def test_get_email_from_token_data(test_client, init_database):
"""
Test get_email_from_token_data function
"""
# Invalid token
# request_ctx = test_client.test_request_context(headers={'Authorization': 'Bearer XXXXX'})
# request_ctx.push()
# assert get_email_from_token_data("SECRET_KEY") is None
#
# # empty token
# request_ctx = test_client.test_request_context(headers={'Authorization': 'Bearer'})
# request_ctx.push()
# assert get_email_from_token_data("SECRET_KEY") is None
#
# # valid token
# exp = datetime.datetime.utcnow() + datetime.timedelta(days=365)
# token = jwt.encode({'email': "user@example.com", 'exp': exp}, "SECRET_KEY", "HS256")
# request_ctx = test_client.test_request_context(headers={'Authorization': 'Bearer ' + token})
# request_ctx.push()
# assert get_email_from_token_data("SECRET_KEY") == "user@example.com"
#
# # expired token
# exp = datetime.datetime.utcnow() + datetime.timedelta(days=-1)
# token = jwt.encode({'email': "user@example.com", 'exp': exp}, "SECRET_KEY", "HS256")
# request_ctx = test_client.test_request_context(headers={'Authorization': 'Bearer ' + token})
# request_ctx.push()
# assert get_email_from_token_data("SECRET_KEY") is None
#
# # bot token (includes ":") but user doesn't exist
# exp = datetime.datetime.utcnow() + datetime.timedelta(days=365)
# token = jwt.encode({'email': "bot@example.com", 'exp': exp}, "SECRET_KEY", "HS256")
# request_ctx = test_client.test_request_context(headers={'Authorization': 'Bearer ' + token + ':12345'})
# request_ctx.push()
# assert get_email_from_token_data("SECRET_KEY") is None
# # bot token (includes ":") user exists
# exp = datetime.datetime.utcnow() + datetime.timedelta(days=365)
# token = jwt.encode({'email': "bot@example.com", 'exp': exp}, "SECRET_KEY", "HS256")
# request_ctx = test_client.test_request_context(headers={'Authorization': 'Bearer ' + token + ':12345'})
# request_ctx.push()
# assert get_email_from_token_data("SECRET_KEY") is None

View File

@@ -0,0 +1,116 @@
"""
This file (test_models.py) contains the unit tests for the models.py file.
"""
from app.models import User, Transaction, Keyword, Share
from app.helper_functions import hash_password
def test_new_user():
"""
GIVEN a User model
WHEN a new User is created
THEN check the email, password, username, telegram_user_id and admin fields are defined correctly
"""
user = User(
email="user@example.com",
username="user",
password=hash_password("password"),
admin=False
)
assert user.email == 'user@example.com'
assert user.password != 'password'
assert user.username == "user"
assert user.telegram_user_id is None
assert user.admin is False
assert user.as_dict() == {'email': 'user@example.com', 'username': 'user', 'telegram_user_id': None, 'admin': False}
def test_new_user_with_fixture(new_user):
"""
GIVEN a User model
WHEN a new User is created
THEN check the email and password_hashed fields are defined correctly
"""
assert new_user.email == 'user@example.com'
assert new_user.password != 'password'
def test_new_transaction():
"""
GIVEN a Transaction model
WHEN a new Transaction is created
THEN check the email, symbol, time count and price fields are defined correctly
"""
transaction = Transaction(
email="user@example.com",
symbol="DTEGY",
time="2022-03-29T10:00:00.000Z",
count=10,
price=9.99
)
assert transaction.email == 'user@example.com'
assert transaction.symbol == "DTEGY"
assert transaction.count == 10
assert transaction.price == 9.99
assert transaction.as_dict() == {'t_id': None, 'email': 'user@example.com', 'symbol': 'DTEGY', 'time': '2022-03-29T10:00:00.000Z', 'count': 10, 'price': 9.99}
def test_new_transaction_with_fixture(new_transaction):
"""
GIVEN a User model
WHEN a new User is created
THEN check the email and password_hashed fields are defined correctly
"""
assert new_transaction.email == 'user@example.com'
assert new_transaction.symbol == 'DTEGY'
def test_new_keyword():
"""
GIVEN a Transaction model
WHEN a new Transaction is created
THEN check the email, symbol, time count and price fields are defined correctly
"""
keyword = Keyword(
email="user@example.com",
keyword="Elon Musk"
)
assert keyword.email == 'user@example.com'
assert keyword.keyword == "Elon Musk"
assert keyword.as_dict() == {'s_id': None, 'email': 'user@example.com', 'keyword': 'Elon Musk'}
def test_new_keyword_with_fixture(new_keyword):
"""
GIVEN a User model
WHEN a new User is created
THEN check the email and password_hashed fields are defined correctly
"""
assert new_keyword.email == 'user@example.com'
assert new_keyword.keyword == 'Elon Musk'
def test_new_share():
"""
GIVEN a Transaction model
WHEN a new Transaction is created
THEN check the email, symbol, time count and price fields are defined correctly
"""
share = Share(
email="user@example.com",
symbol="DTEGY"
)
assert share.email == 'user@example.com'
assert share.symbol == "DTEGY"
assert share.as_dict() == {'a_id': None, 'email': 'user@example.com', 'symbol': 'DTEGY'}
def test_new_share_with_fixture(new_share):
"""
GIVEN a User model
WHEN a new User is created
THEN check the email and password_hashed fields are defined correctly
"""
assert new_share.email == 'user@example.com'
assert new_share.symbol == 'DTEGY'

View File

@@ -0,0 +1,40 @@
"""
This file (test_transaction.py) contains the unit tests for the blueprints/transaction.py file.
"""
from app.blueprints.transactions import *
def test_check_if_symbol_data_exists():
"""
Test check_if_symbol_data_exists function
"""
assert check_if_symbol_data_exists(dict(symbol='DTEGY')) is True
assert check_if_symbol_data_exists(dict(symbol='')) is False
assert check_if_symbol_data_exists(dict()) is False
def test_check_if_time_data_exists():
"""
Test check_if_time_data_exists function
"""
assert check_if_time_data_exists(dict(time='2022-03-29T10:00:00.000Z')) is True
assert check_if_time_data_exists(dict(time='')) is False
assert check_if_time_data_exists(dict()) is False
def test_check_if_count_data_exists():
"""
Test check_if_count_data_exists function
"""
assert check_if_count_data_exists(dict(count=10)) is True
assert check_if_count_data_exists(dict(count=None)) is False
assert check_if_count_data_exists(dict()) is False
def test_check_if_price_data_exists():
"""
Test check_if_price_data_exists function
"""
assert check_if_price_data_exists(dict(price=9.99)) is True
assert check_if_price_data_exists(dict(price=None)) is False
assert check_if_price_data_exists(dict()) is False

View File

@@ -0,0 +1,40 @@
"""
This file (test_helper_functions.py) contains the unit tests for the helper_functions.py file.
"""
from app.blueprints.user import *
def test_check_if_email_data_exists():
"""
Test check_if_email_data_exists function
"""
assert check_if_email_data_exists(dict(email='user@example.com')) is True
assert check_if_email_data_exists(dict(email='')) is False
assert check_if_email_data_exists(dict()) is False
def test_check_if_password_data_exists():
"""
Test check_if_password_data_exists function
"""
assert check_if_password_data_exists(dict(password='password')) is True
assert check_if_password_data_exists(dict(password='')) is False
assert check_if_password_data_exists(dict()) is False
def test_check_if_username_data_exists():
"""
Test check_if_username_data_exists function
"""
assert check_if_username_data_exists(dict(username='user')) is True
assert check_if_username_data_exists(dict(username='')) is False
assert check_if_username_data_exists(dict()) is False
def test_check_if_admin_data_exists():
"""
Test check_if_admin_data_exists function
"""
assert check_if_admin_data_exists(dict(admin=True)) is True
assert check_if_admin_data_exists(dict(admin=None)) is False
assert check_if_admin_data_exists(dict()) is False