67 lines
2.6 KiB
Python
67 lines
2.6 KiB
Python
"""
|
|
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 |