Refactoring
This commit is contained in:
parent
4b7dfa8afd
commit
1f2487f1c1
@ -1,6 +1,3 @@
|
|||||||
import json
|
|
||||||
import os
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from app import create_app, db
|
from app import create_app, db
|
||||||
from app.models import User, Transaction, Keyword, Share
|
from app.models import User, Transaction, Keyword, Share
|
||||||
|
11
api/tests/functional/helper_functions.py
Normal file
11
api/tests/functional/helper_functions.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def get_token(test_client, email, password):
|
||||||
|
response = test_client.post('/api/user/login', data=json.dumps(dict(email=email, password=password)), content_type='application/json')
|
||||||
|
|
||||||
|
if "data" in json.loads(response.data):
|
||||||
|
if "token" in json.loads(response.data)["data"]:
|
||||||
|
return json.loads(response.data)["data"]["token"]
|
||||||
|
|
||||||
|
return ""
|
@ -2,6 +2,7 @@
|
|||||||
This file (test_keyword.py) contains the functional tests for the `keyword` blueprint.
|
This file (test_keyword.py) contains the functional tests for the `keyword` blueprint.
|
||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
|
from tests.functional.helper_functions import get_token
|
||||||
|
|
||||||
|
|
||||||
def test_add_keyword_not_logged_in(test_client, init_database):
|
def test_add_keyword_not_logged_in(test_client, init_database):
|
||||||
@ -179,13 +180,3 @@ def test_delete_keyword_user1_logged_in_keyword_not_exists(test_client, init_dat
|
|||||||
content_type='application/json')
|
content_type='application/json')
|
||||||
assert response.status_code == 500
|
assert response.status_code == 500
|
||||||
assert b'Keyword doesn\'t exist for this user' in response.data
|
assert b'Keyword doesn\'t exist for this user' in response.data
|
||||||
|
|
||||||
|
|
||||||
def get_token(test_client, email, password):
|
|
||||||
response = test_client.post('/api/user/login', data=json.dumps(dict(email=email, password=password)), content_type='application/json')
|
|
||||||
|
|
||||||
if "data" in json.loads(response.data):
|
|
||||||
if "token" in json.loads(response.data)["data"]:
|
|
||||||
return json.loads(response.data)["data"]["token"]
|
|
||||||
|
|
||||||
return ""
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
This file (test_portfolio.py) contains the functional tests for the `portfolio` blueprint.
|
This file (test_portfolio.py) contains the functional tests for the `portfolio` blueprint.
|
||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
|
from tests.functional.helper_functions import get_token
|
||||||
|
|
||||||
|
|
||||||
def test_get_portfolio_not_logged_in_empty_response(test_client, init_database):
|
def test_get_portfolio_not_logged_in_empty_response(test_client, init_database):
|
||||||
@ -47,13 +48,3 @@ def test_get_portfolio_user1_logged_in_response_data(test_client, init_database)
|
|||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert b'"data":[]' not in response.data
|
assert b'"data":[]' not in response.data
|
||||||
assert b'"data":[' in response.data
|
assert b'"data":[' in response.data
|
||||||
|
|
||||||
|
|
||||||
def get_token(test_client, email, password):
|
|
||||||
response = test_client.post('/api/user/login', data=json.dumps(dict(email=email, password=password)), content_type='application/json')
|
|
||||||
|
|
||||||
if "data" in json.loads(response.data):
|
|
||||||
if "token" in json.loads(response.data)["data"]:
|
|
||||||
return json.loads(response.data)["data"]["token"]
|
|
||||||
|
|
||||||
return ""
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
This file (test_share.py) contains the functional tests for the `share` blueprint.
|
This file (test_share.py) contains the functional tests for the `share` blueprint.
|
||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
|
from tests.functional.helper_functions import get_token
|
||||||
|
|
||||||
|
|
||||||
def test_add_share_not_logged_in(test_client, init_database):
|
def test_add_share_not_logged_in(test_client, init_database):
|
||||||
@ -179,13 +180,3 @@ def test_delete_share_user1_logged_in_symbol_not_exists(test_client, init_databa
|
|||||||
content_type='application/json')
|
content_type='application/json')
|
||||||
assert response.status_code == 500
|
assert response.status_code == 500
|
||||||
assert b'Symbol doesn\'t exist for this user' in response.data
|
assert b'Symbol doesn\'t exist for this user' in response.data
|
||||||
|
|
||||||
|
|
||||||
def get_token(test_client, email, password):
|
|
||||||
response = test_client.post('/api/user/login', data=json.dumps(dict(email=email, password=password)), content_type='application/json')
|
|
||||||
|
|
||||||
if "data" in json.loads(response.data):
|
|
||||||
if "token" in json.loads(response.data)["data"]:
|
|
||||||
return json.loads(response.data)["data"]["token"]
|
|
||||||
|
|
||||||
return ""
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
This file (test_telegram.py) contains the functional tests for the `telegram` blueprint.
|
This file (test_telegram.py) contains the functional tests for the `telegram` blueprint.
|
||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
|
from tests.functional.helper_functions import get_token
|
||||||
|
|
||||||
|
|
||||||
def test_add_telegram_not_logged_in(test_client, init_database):
|
def test_add_telegram_not_logged_in(test_client, init_database):
|
||||||
@ -54,13 +55,3 @@ def test_add_telegram_user1_logged_in_user_data_empty(test_client, init_database
|
|||||||
content_type='application/json')
|
content_type='application/json')
|
||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
assert b'missing' in response.data
|
assert b'missing' in response.data
|
||||||
|
|
||||||
|
|
||||||
def get_token(test_client, email, password):
|
|
||||||
response = test_client.post('/api/user/login', data=json.dumps(dict(email=email, password=password)), content_type='application/json')
|
|
||||||
|
|
||||||
if "data" in json.loads(response.data):
|
|
||||||
if "token" in json.loads(response.data)["data"]:
|
|
||||||
return json.loads(response.data)["data"]["token"]
|
|
||||||
|
|
||||||
return ""
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
This file (test_transaction.py) contains the functional tests for the `transaction` blueprint.
|
This file (test_transaction.py) contains the functional tests for the `transaction` blueprint.
|
||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
|
from tests.functional.helper_functions import get_token
|
||||||
|
|
||||||
|
|
||||||
def test_add_transaction_not_logged_in(test_client, init_database):
|
def test_add_transaction_not_logged_in(test_client, init_database):
|
||||||
@ -91,13 +92,3 @@ def test_get_transaction_user1_logged_in_response_data(test_client, init_databas
|
|||||||
content_type='application/json')
|
content_type='application/json')
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert b'Successfully loaded transactions' in response.data
|
assert b'Successfully loaded transactions' in response.data
|
||||||
|
|
||||||
|
|
||||||
def get_token(test_client, email, password):
|
|
||||||
response = test_client.post('/api/user/login', data=json.dumps(dict(email=email, password=password)), content_type='application/json')
|
|
||||||
|
|
||||||
if "data" in json.loads(response.data):
|
|
||||||
if "token" in json.loads(response.data)["data"]:
|
|
||||||
return json.loads(response.data)["data"]["token"]
|
|
||||||
|
|
||||||
return ""
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
This file (test_user.py) contains the functional tests for the `users` blueprint.
|
This file (test_user.py) contains the functional tests for the `users` blueprint.
|
||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
|
from tests.functional.helper_functions import get_token
|
||||||
|
|
||||||
|
|
||||||
def test_login_with_valid_data(test_client, init_database):
|
def test_login_with_valid_data(test_client, init_database):
|
||||||
@ -501,13 +502,3 @@ def test_get_users_admin1_logged_in(test_client, init_database):
|
|||||||
content_type='application/json')
|
content_type='application/json')
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert b'Successfully received all users' in response.data
|
assert b'Successfully received all users' in response.data
|
||||||
|
|
||||||
|
|
||||||
def get_token(test_client, email, password):
|
|
||||||
response = test_client.post('/api/user/login', data=json.dumps(dict(email=email, password=password)), content_type='application/json')
|
|
||||||
|
|
||||||
if "data" in json.loads(response.data):
|
|
||||||
if "token" in json.loads(response.data)["data"]:
|
|
||||||
return json.loads(response.data)["data"]["token"]
|
|
||||||
|
|
||||||
return ""
|
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
This file (test_helper_functions.py) contains the unit tests for the helper_functions.py file.
|
This file (test_helper_functions.py) contains the unit tests for the helper_functions.py file.
|
||||||
"""
|
"""
|
||||||
import datetime
|
|
||||||
|
|
||||||
import jwt
|
|
||||||
from app.auth import *
|
from app.auth import *
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
This file (test_helper_functions.py) contains the unit tests for the helper_functions.py file.
|
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 *
|
from app.helper_functions import *
|
||||||
|
|
||||||
|
|
||||||
@ -21,47 +18,3 @@ def test_check_password():
|
|||||||
hashed = hash_password("password")
|
hashed = hash_password("password")
|
||||||
assert check_password(hashed, "password".encode("utf-8")) is True
|
assert check_password(hashed, "password".encode("utf-8")) is True
|
||||||
assert check_password(hashed, "password1".encode("utf-8")) is False
|
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
|
|
Loading…
Reference in New Issue
Block a user