Tests
- Improved directory structure - Added functional and unit tests
This commit is contained in:
108
api/tests/conftest.py
Normal file
108
api/tests/conftest.py
Normal file
@@ -0,0 +1,108 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
import pytest
|
||||
from app import create_app, db
|
||||
from app.models import User, Transaction, Keyword, Share
|
||||
|
||||
from app.helper_functions import hash_password
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def new_user():
|
||||
user = User(
|
||||
email="user@example.com",
|
||||
username="user",
|
||||
password=hash_password("password"),
|
||||
admin=False
|
||||
)
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def new_transaction():
|
||||
transaction = Transaction(
|
||||
email="user@example.com",
|
||||
symbol="DTEGY",
|
||||
time="2022-03-29T10:00:00.000Z",
|
||||
count=10,
|
||||
price=9.99
|
||||
)
|
||||
return transaction
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def new_keyword():
|
||||
keyword = Keyword(
|
||||
email="user@example.com",
|
||||
keyword="Elon Musk",
|
||||
)
|
||||
return keyword
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def new_share():
|
||||
share = Share(
|
||||
email="user@example.com",
|
||||
symbol="DTEGY",
|
||||
)
|
||||
return share
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def test_client():
|
||||
flask_app = create_app('config/flask_test.cfg')
|
||||
|
||||
# Create a test client using the Flask application configured for testing
|
||||
with flask_app.test_client() as testing_client:
|
||||
# Establish an application context
|
||||
with flask_app.app_context():
|
||||
yield testing_client # this is where the testing happens!
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def init_database(test_client):
|
||||
# Create the database and the database table
|
||||
db.create_all()
|
||||
|
||||
# Insert user data
|
||||
user1 = User(
|
||||
email="user1@example.com",
|
||||
username="user1",
|
||||
password=hash_password("password"),
|
||||
telegram_user_id="12345678",
|
||||
admin=False
|
||||
)
|
||||
user2 = User(
|
||||
email="user2@example.com",
|
||||
username="user2",
|
||||
password=hash_password("password"),
|
||||
telegram_user_id="87654321",
|
||||
admin=False
|
||||
)
|
||||
admin = User(
|
||||
email="admin1@example.com",
|
||||
username="admin1",
|
||||
password=hash_password("admin1"),
|
||||
telegram_user_id="00000000",
|
||||
admin=True
|
||||
)
|
||||
bot = User(
|
||||
email="bot1@example.com",
|
||||
username="bot1",
|
||||
password=hash_password("bot1"),
|
||||
telegram_user_id="00000000",
|
||||
admin=False
|
||||
)
|
||||
db.session.add(user1)
|
||||
db.session.add(user2)
|
||||
db.session.add(admin)
|
||||
db.session.add(bot)
|
||||
|
||||
# Commit the changes for the users
|
||||
db.session.commit()
|
||||
|
||||
yield # this is where the testing happens!
|
||||
|
||||
db.session.commit()
|
||||
db.drop_all()
|
0
api/tests/functional/__init__.py
Normal file
0
api/tests/functional/__init__.py
Normal file
191
api/tests/functional/test_keyword.py
Normal file
191
api/tests/functional/test_keyword.py
Normal file
@@ -0,0 +1,191 @@
|
||||
"""
|
||||
This file (test_keyword.py) contains the functional tests for the `keyword` blueprint.
|
||||
"""
|
||||
import json
|
||||
|
||||
|
||||
def test_add_keyword_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/keyword'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.post('/api/keyword')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_add_keyword_user1_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
"""
|
||||
response = test_client.post('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully added keyword' in response.data
|
||||
|
||||
|
||||
def test_add_keyword_user1_logged_in_but_keyword_exist(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Add keyword two times
|
||||
"""
|
||||
test_client.post('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
response = test_client.post('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 500
|
||||
assert b'Keyword already exist for this user' in response.data
|
||||
|
||||
|
||||
def test_add_keyword_user1_logged_in_but_keyword_missing(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword is missing in post data
|
||||
"""
|
||||
response = test_client.post('/api/keyword', data=json.dumps(dict()),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_add_keyword_user1_logged_in_but_keyword_empty(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword is empty in post data
|
||||
"""
|
||||
response = test_client.post('/api/keyword', data=json.dumps(dict(keyword="")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_get_keyword_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/keyword'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.get('/api/keywords')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_get_keyword_user1_logged_in_empty_response(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Empty response
|
||||
"""
|
||||
response = test_client.get('/api/keywords', headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))})
|
||||
assert response.status_code == 200
|
||||
assert b'"data":[' in response.data
|
||||
|
||||
|
||||
def test_get_keyword_user1_logged_in_response_data(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Create some keywords for user1
|
||||
"""
|
||||
test_client.post('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
response = test_client.get('/api/keywords', headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))})
|
||||
assert response.status_code == 200
|
||||
assert b'"data":[' in response.data
|
||||
|
||||
|
||||
def test_delete_keyword_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/keyword'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.delete('/api/keyword')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_delete_keyword_user1_logged_in_but_empty_keyword(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword empty in in delete data
|
||||
"""
|
||||
response = test_client.delete('/api/keyword', data=json.dumps(dict(keyword="")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_delete_keyword_user1_logged_in_but_missing_keyword(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword missing in in delete data
|
||||
"""
|
||||
response = test_client.delete('/api/keyword', data=json.dumps(dict()),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_delete_keyword_user1_logged_in_keyword_exists(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword exists
|
||||
"""
|
||||
test_client.post('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
response = test_client.delete('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully removed keyword' in response.data
|
||||
|
||||
|
||||
def test_delete_keyword_user1_logged_in_keyword_not_exists(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword doesn't exists
|
||||
"""
|
||||
response = test_client.delete('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 500
|
||||
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 ""
|
59
api/tests/functional/test_portfolio.py
Normal file
59
api/tests/functional/test_portfolio.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""
|
||||
This file (test_portfolio.py) contains the functional tests for the `portfolio` blueprint.
|
||||
"""
|
||||
import json
|
||||
|
||||
|
||||
def test_get_portfolio_not_logged_in_empty_response(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/portfolio'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.get('/api/portfolio')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_get_portfolio_user1_logged_in_empty_response(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/portfolio'
|
||||
|
||||
User1 is logged in
|
||||
Empty response
|
||||
"""
|
||||
response = test_client.get('/api/portfolio',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'"data":[]' in response.data
|
||||
assert b'Successfully loaded symbols' in response.data
|
||||
|
||||
|
||||
def test_get_portfolio_user1_logged_in_response_data(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/portfolio'
|
||||
|
||||
User1 is logged in
|
||||
Create transaction data
|
||||
"""
|
||||
test_client.post('/api/transaction', data=json.dumps(dict(count=5, price=9.99, symbol="DTEGY", time="2022-03-29T10:00:00.000Z")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
|
||||
response = test_client.get('/api/portfolio',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'"data":[]' not 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 ""
|
191
api/tests/functional/test_share.py
Normal file
191
api/tests/functional/test_share.py
Normal file
@@ -0,0 +1,191 @@
|
||||
"""
|
||||
This file (test_share.py) contains the functional tests for the `share` blueprint.
|
||||
"""
|
||||
import json
|
||||
|
||||
|
||||
def test_add_share_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/share'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.post('/api/share')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_add_share_user1_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/share'
|
||||
|
||||
User1 is logged in
|
||||
"""
|
||||
response = test_client.post('/api/share', data=json.dumps(dict(symbol="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully added symbol' in response.data
|
||||
|
||||
|
||||
def test_add_share_user1_logged_in_but_symbol_exist(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/share'
|
||||
|
||||
User1 is logged in
|
||||
Add symbol two times
|
||||
"""
|
||||
test_client.post('/api/share', data=json.dumps(dict(symbol="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
response = test_client.post('/api/share', data=json.dumps(dict(symbol="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 500
|
||||
assert b'Symbol already exist for this user' in response.data
|
||||
|
||||
|
||||
def test_add_share_user1_logged_in_but_symbol_missing(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/share'
|
||||
|
||||
User1 is logged in
|
||||
Symbol is missing in post data
|
||||
"""
|
||||
response = test_client.post('/api/share', data=json.dumps(dict()),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_add_share_user1_logged_in_but_symbol_empty(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/share'
|
||||
|
||||
User1 is logged in
|
||||
Symbol is empty in post data
|
||||
"""
|
||||
response = test_client.post('/api/share', data=json.dumps(dict(symbol="")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_get_share_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/share'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.get('/api/shares')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_get_share_user1_logged_in_empty_response(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/share'
|
||||
|
||||
User1 is logged in
|
||||
Empty response
|
||||
"""
|
||||
response = test_client.get('/api/shares', headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))})
|
||||
assert response.status_code == 200
|
||||
assert b'"data":[' in response.data
|
||||
|
||||
|
||||
def test_get_share_user1_logged_in_response_data(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/share'
|
||||
|
||||
User1 is logged in
|
||||
Create some symbols for user1
|
||||
"""
|
||||
test_client.post('/api/share', data=json.dumps(dict(symbol="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
response = test_client.get('/api/shares', headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))})
|
||||
assert response.status_code == 200
|
||||
assert b'"data":[' in response.data
|
||||
|
||||
|
||||
def test_delete_share_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/share'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.delete('/api/share')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_delete_share_user1_logged_in_but_empty_symbol(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/share'
|
||||
|
||||
User1 is logged in
|
||||
Symbol empty in in delete data
|
||||
"""
|
||||
response = test_client.delete('/api/share', data=json.dumps(dict(symbol="")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_delete_share_user1_logged_in_but_missing_symbol(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/share'
|
||||
|
||||
User1 is logged in
|
||||
Symbol missing in in delete data
|
||||
"""
|
||||
response = test_client.delete('/api/share', data=json.dumps(dict()),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_delete_share_user1_logged_in_symbol_exists(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/share'
|
||||
|
||||
User1 is logged in
|
||||
Symbol exists
|
||||
"""
|
||||
test_client.post('/api/share', data=json.dumps(dict(symbol="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
response = test_client.delete('/api/share', data=json.dumps(dict(symbol="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully removed symbol' in response.data
|
||||
|
||||
|
||||
def test_delete_share_user1_logged_in_symbol_not_exists(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/share'
|
||||
|
||||
User1 is logged in
|
||||
Symbol doesn't exists
|
||||
"""
|
||||
response = test_client.delete('/api/share', data=json.dumps(dict(symbol="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 500
|
||||
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 ""
|
66
api/tests/functional/test_telegram.py
Normal file
66
api/tests/functional/test_telegram.py
Normal file
@@ -0,0 +1,66 @@
|
||||
"""
|
||||
This file (test_telegram.py) contains the functional tests for the `telegram` blueprint.
|
||||
"""
|
||||
import json
|
||||
|
||||
|
||||
def test_add_telegram_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/telegram'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.post('/api/telegram')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_add_telegram_user1_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/telegram'
|
||||
|
||||
User1 is logged in
|
||||
"""
|
||||
response = test_client.post('/api/telegram', data=json.dumps(dict(telegram_user_id="12345678")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully connected telegram user' in response.data
|
||||
|
||||
|
||||
def test_add_telegram_user1_logged_in_user_data_missing(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/telegram'
|
||||
|
||||
User1 is logged in
|
||||
telegram_user_id is missing
|
||||
"""
|
||||
response = test_client.post('/api/telegram', data=json.dumps(dict()),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_add_telegram_user1_logged_in_user_data_empty(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/telegram'
|
||||
|
||||
User1 is logged in
|
||||
telegram_user_id is empty
|
||||
"""
|
||||
response = test_client.post('/api/telegram', data=json.dumps(dict(telegram_user_id="")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
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 ""
|
103
api/tests/functional/test_transaction.py
Normal file
103
api/tests/functional/test_transaction.py
Normal file
@@ -0,0 +1,103 @@
|
||||
"""
|
||||
This file (test_transaction.py) contains the functional tests for the `transaction` blueprint.
|
||||
"""
|
||||
import json
|
||||
|
||||
|
||||
def test_add_transaction_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/transaction'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.get('/api/portfolio')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_add_transaction_user1_logged_in_missing_data(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/transaction'
|
||||
|
||||
User1 is logged in
|
||||
Data missing
|
||||
"""
|
||||
# symbol missing
|
||||
response = test_client.post('/api/transaction', data=json.dumps(dict(time="2022-03-29T10:00:00.000Z", count=10, price=9.99)),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
# time missing
|
||||
response = test_client.post('/api/transaction', data=json.dumps(dict(symbol="DTEGY", count=10, price=9.99)),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
# count missing
|
||||
response = test_client.post('/api/transaction', data=json.dumps(dict(symbol="DTEGY", time="2022-03-29T10:00:00.000Z", price=9.99)),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
# price missing
|
||||
response = test_client.post('/api/transaction', data=json.dumps(dict(symbol="DTEGY", time="2022-03-29T10:00:00.000Z", count=10)),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_get_transaction_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/transaction'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.get('/api/transactions')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_get_transaction_user1_logged_in_empty_response(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/transaction'
|
||||
|
||||
User1 is logged in
|
||||
"""
|
||||
response = test_client.get('/api/transactions',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully loaded transactions' in response.data
|
||||
|
||||
|
||||
def test_get_transaction_user1_logged_in_response_data(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/transaction'
|
||||
|
||||
User1 is logged in
|
||||
Create transaction
|
||||
"""
|
||||
test_client.post('/api/transaction', data=json.dumps(dict(count=5, price=9.99, symbol="DTEGY", time="2022-03-29T10:00:00.000Z")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
|
||||
response = test_client.get('/api/transactions',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
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 ""
|
513
api/tests/functional/test_user.py
Normal file
513
api/tests/functional/test_user.py
Normal file
@@ -0,0 +1,513 @@
|
||||
"""
|
||||
This file (test_user.py) contains the functional tests for the `users` blueprint.
|
||||
"""
|
||||
import json
|
||||
|
||||
|
||||
def test_login_with_valid_data(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/login'
|
||||
|
||||
Valid data
|
||||
"""
|
||||
response = test_client.post('/api/user/login', data=json.dumps(dict(email="user1@example.com", password="password")), content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully logged in' in response.data
|
||||
|
||||
|
||||
def test_login_with_wrong_password(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/login'
|
||||
|
||||
Wrong password
|
||||
"""
|
||||
response = test_client.post('/api/user/login', data=json.dumps(dict(email="user2@example.com", password="password2")), content_type='application/json')
|
||||
assert response.status_code == 500
|
||||
assert b'Unable to login' in response.data
|
||||
|
||||
|
||||
def test_login_user_not_exist(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/login'
|
||||
|
||||
User doesn't exist
|
||||
"""
|
||||
response = test_client.post('/api/user/login', data=json.dumps(dict(email="notexistinguser@example.com", password="password")), content_type='application/json')
|
||||
assert response.status_code == 500
|
||||
assert b'Unable to login' in response.data
|
||||
|
||||
|
||||
def test_login_email_missing(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/login'
|
||||
|
||||
Email missing
|
||||
"""
|
||||
response = test_client.post('/api/user/login', data=json.dumps(dict(password="password")), content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_login_password_missing(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/login'
|
||||
|
||||
Password missing
|
||||
"""
|
||||
response = test_client.post('/api/user/login', data=json.dumps(dict(email="user1@example.com")), content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_register_valid_data(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/register'
|
||||
|
||||
Valid data
|
||||
"""
|
||||
response = test_client.post('/api/user/register', data=json.dumps(dict(email="user3@example.com", password="password", username="user3")), content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully registered user' in response.data
|
||||
|
||||
|
||||
def test_register_user_exists_already(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/register'
|
||||
|
||||
User exists already
|
||||
"""
|
||||
test_client.post('/api/user/register', data=json.dumps(dict(email="user3@example.com", password="password", username="user3")), content_type='application/json')
|
||||
response = test_client.post('/api/user/register', data=json.dumps(dict(email="user3@example.com", password="password", username="user3")), content_type='application/json')
|
||||
assert response.status_code == 500
|
||||
assert b'Email already exist' in response.data
|
||||
|
||||
|
||||
def test_register_email_missing(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/register'
|
||||
|
||||
Email missing
|
||||
"""
|
||||
response = test_client.post('/api/user/register', data=json.dumps(dict(password="password", username="user3")), content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_register_email_empty(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/register'
|
||||
|
||||
Email empty
|
||||
"""
|
||||
response = test_client.post('/api/user/register', data=json.dumps(dict(password="password", username="user3", email="")), content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'Not a valid email address' in response.data
|
||||
|
||||
|
||||
def test_register_password_missing(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/register'
|
||||
|
||||
Password missing
|
||||
"""
|
||||
response = test_client.post('/api/user/register', data=json.dumps(dict(email="user3@example.com", username="user3")), content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_register_password_empty(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/register'
|
||||
|
||||
password empty
|
||||
"""
|
||||
response = test_client.post('/api/user/register', data=json.dumps(dict(email="user3@example.com", username="user3", password="")), content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_register_username_missing(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/register'
|
||||
|
||||
Username missing
|
||||
"""
|
||||
response = test_client.post('/api/user/register', data=json.dumps(dict(password="password", email="user3@example.com")), content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_register_username_empty(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/user/register'
|
||||
|
||||
Username empty
|
||||
"""
|
||||
response = test_client.post('/api/user/register', data=json.dumps(dict(password="password", username="", email="user3@example.com")), content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_delete_user_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/user'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
test_client.post('/api/user/register', data=json.dumps(dict(email="user3@example.com", password="password", username="user3")), content_type='application/json')
|
||||
response = test_client.delete('/api/user', data=json.dumps(dict(email="user3@example.com")), content_type='application/json')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_delete_user_same_user_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/user'
|
||||
|
||||
User3 is logged in
|
||||
"""
|
||||
test_client.post('/api/user/register', data=json.dumps(dict(email="user3@example.com", password="password", username="user3")), content_type='application/json')
|
||||
response = test_client.delete('/api/user',
|
||||
data=json.dumps(dict(email="user3@example.com")),
|
||||
content_type='application/json',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user3@example.com", "password"))})
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully removed user' in response.data
|
||||
|
||||
|
||||
def test_delete_user_different_user_logged_in_no_admin(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/user'
|
||||
|
||||
Different user is logged in -> no admin
|
||||
"""
|
||||
test_client.post('/api/user/register', data=json.dumps(dict(email="user3@example.com", password="password", username="user3")), content_type='application/json')
|
||||
response = test_client.delete('/api/user',
|
||||
data=json.dumps(dict(email="user3@example.com")),
|
||||
content_type='application/json',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))})
|
||||
assert response.status_code == 401
|
||||
assert b'Only admin users can access this' in response.data
|
||||
|
||||
|
||||
def test_delete_user_different_user_logged_in_admin(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/user'
|
||||
|
||||
Different user is logged in -> admin
|
||||
"""
|
||||
test_client.post('/api/user/register', data=json.dumps(dict(email="user3@example.com", password="password", username="user3")), content_type='application/json')
|
||||
response = test_client.delete('/api/user',
|
||||
data=json.dumps(dict(email="user3@example.com")),
|
||||
content_type='application/json',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "admin1@example.com", "admin1"))})
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully removed user' in response.data
|
||||
|
||||
|
||||
def test_delete_user_email_missing(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/user'
|
||||
|
||||
Email missing
|
||||
"""
|
||||
test_client.post('/api/user/register', data=json.dumps(dict(email="user3@example.com", password="password", username="user3")), content_type='application/json')
|
||||
response = test_client.delete('/api/user',
|
||||
data=json.dumps(dict()),
|
||||
content_type='application/json',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user3@example.com", "password"))})
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_delete_user_email_empty(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/user'
|
||||
|
||||
Email empty
|
||||
"""
|
||||
test_client.post('/api/user/register', data=json.dumps(dict(email="user3@example.com", password="password", username="user3")), content_type='application/json')
|
||||
response = test_client.delete('/api/user',
|
||||
data=json.dumps(dict(email="")),
|
||||
content_type='application/json',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user3@example.com", "password"))})
|
||||
assert response.status_code == 400
|
||||
assert b'Not a valid email address' in response.data
|
||||
|
||||
|
||||
def test_get_current_user_user_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/user'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.get('/api/user')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_get_current_user_user1_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/user'
|
||||
|
||||
User1 is logged in
|
||||
"""
|
||||
response = test_client.get('/api/user', headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))})
|
||||
assert response.status_code == 200
|
||||
assert b'user1' in response.data
|
||||
assert b'user2' not in response.data
|
||||
|
||||
|
||||
def test_get_current_user_bot_logged_in_user_exists(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/user'
|
||||
|
||||
Bot1 is logged in and requests user 12345678
|
||||
"""
|
||||
response = test_client.get('/api/user', headers={"Authorization": "Bearer {}".format(get_token(test_client, "bot1@example.com", "bot1") + ":12345678")})
|
||||
assert response.status_code == 200
|
||||
assert b'user1' in response.data
|
||||
assert b'bot' not in response.data
|
||||
|
||||
|
||||
def test_get_current_user_bot_logged_in_user_not_exists(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/user'
|
||||
|
||||
Bot1 is logged in and requests user 1234 (not existing)
|
||||
"""
|
||||
response = test_client.get('/api/user', headers={"Authorization": "Bearer {}".format(get_token(test_client, "bot1@example.com", "bot1") + ":1234")})
|
||||
assert response.status_code == 401
|
||||
assert b'Unable to login' in response.data
|
||||
|
||||
|
||||
def test_get_current_user_user1_logged_in_but_no_bot(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/user'
|
||||
|
||||
User1 is logged in and requests user 1234 (not existing)
|
||||
Fails because user1 is not a bot
|
||||
"""
|
||||
response = test_client.get('/api/user', headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password") + ":12345678")})
|
||||
assert response.status_code == 401
|
||||
assert b'Unable to login' in response.data
|
||||
|
||||
|
||||
def test_get_current_user_invalid_token(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/user'
|
||||
|
||||
Invalid Bearer token
|
||||
"""
|
||||
response = test_client.get('/api/user', headers={"Authorization": "Bearer {}".format("invalidtoken:12345678")})
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_update_user_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.put('/api/user')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_update_user1_logged_in_password_username(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user'
|
||||
|
||||
User1 is logged in
|
||||
Change Username and Password
|
||||
"""
|
||||
test_client.put('/api/user', data=json.dumps(dict(username="user4", password="password")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
response = test_client.get('/api/user',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'user4' in response.data
|
||||
|
||||
|
||||
def test_update_user1_logged_in_password(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user'
|
||||
|
||||
User1 is logged in
|
||||
Change Password
|
||||
"""
|
||||
response = test_client.put('/api/user', data=json.dumps(dict(password="password123")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))})
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully updated user' in response.data
|
||||
|
||||
|
||||
def test_update_user1_logged_in_username(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user'
|
||||
|
||||
User1 is logged in
|
||||
Change Username
|
||||
"""
|
||||
response = test_client.put('/api/user', data=json.dumps(dict(username="user1")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))})
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully updated user' in response.data
|
||||
|
||||
|
||||
def test_set_admin_user_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user/setAdmin'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.put('/api/user/setAdmin', data=json.dumps(dict(email="user1@example.com", admin=True)), content_type='application/json')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_set_admin_user1_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user/setAdmin'
|
||||
|
||||
User1 is logged in (no admin)
|
||||
"""
|
||||
response = test_client.put('/api/user/setAdmin', data=json.dumps(dict(email="user1@example.com", admin=True)),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 401
|
||||
assert b'Only admin users can access this' in response.data
|
||||
|
||||
|
||||
def test_set_admin_admin1_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user/setAdmin'
|
||||
|
||||
Admin1 is logged in (admin)
|
||||
"""
|
||||
response = test_client.put('/api/user/setAdmin', data=json.dumps(dict(email="user1@example.com", admin=True)),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "admin1@example.com", "admin1"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully updated users admin rights' in response.data
|
||||
|
||||
|
||||
def test_set_admin_admin1_logged_in_user_not_exist(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user/setAdmin'
|
||||
|
||||
Admin1 is logged in (admin)
|
||||
notexistinguser@example.com does not exist
|
||||
"""
|
||||
response = test_client.put('/api/user/setAdmin', data=json.dumps(dict(email="notexistinguser@example.com", admin=True)),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "admin1@example.com", "admin1"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 500
|
||||
assert b'Unable to update user' in response.data
|
||||
|
||||
|
||||
def test_set_admin_admin1_logged_in_email_missing(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user/setAdmin'
|
||||
|
||||
Admin1 is logged in (admin)
|
||||
email missing
|
||||
"""
|
||||
response = test_client.put('/api/user/setAdmin', data=json.dumps(dict(admin=True)),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "admin1@example.com", "admin1"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_set_admin_admin1_logged_in_email_empty(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user/setAdmin'
|
||||
|
||||
Admin1 is logged in (admin)
|
||||
email missing
|
||||
"""
|
||||
response = test_client.put('/api/user/setAdmin', data=json.dumps(dict(email="", admin=True)),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "admin1@example.com", "admin1"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'Not a valid email address.' in response.data
|
||||
|
||||
|
||||
def test_set_admin_admin1_logged_in_admin_missing(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user/setAdmin'
|
||||
|
||||
Admin1 is logged in (admin)
|
||||
admin data missing
|
||||
"""
|
||||
response = test_client.put('/api/user/setAdmin', data=json.dumps(dict(email="user1@example.com")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "admin1@example.com", "admin1"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_set_admin_admin1_logged_in_admin_empty(test_client, init_database):
|
||||
"""
|
||||
Test PUT '/api/user/setAdmin'
|
||||
|
||||
Admin1 is logged in (admin)
|
||||
admin data missing
|
||||
"""
|
||||
response = test_client.put('/api/user/setAdmin', data=json.dumps(dict(email="user1@example.com", admin=None)),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "admin1@example.com", "admin1"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'Field may not be null' in response.data
|
||||
|
||||
|
||||
def test_get_users_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/users'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.get('/api/users')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_get_users_user1_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/users'
|
||||
|
||||
User1 is logged in (not admin)
|
||||
"""
|
||||
response = test_client.get('/api/users',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
|
||||
assert response.status_code == 401
|
||||
assert b'Only admin users can access this' in response.data
|
||||
|
||||
|
||||
def test_get_users_admin1_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/users'
|
||||
|
||||
Admin1 is logged in (admin)
|
||||
"""
|
||||
response = test_client.get('/api/users',
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "admin1@example.com", "admin1"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
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 ""
|
0
api/tests/pytest.ini
Normal file
0
api/tests/pytest.ini
Normal file
0
api/tests/unit/__init__.py
Normal file
0
api/tests/unit/__init__.py
Normal file
14
api/tests/unit/test_auth.py
Normal file
14
api/tests/unit/test_auth.py
Normal 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
|
67
api/tests/unit/test_helper_functions.py
Normal file
67
api/tests/unit/test_helper_functions.py
Normal 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
|
116
api/tests/unit/test_models.py
Normal file
116
api/tests/unit/test_models.py
Normal 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'
|
40
api/tests/unit/test_transaction.py
Normal file
40
api/tests/unit/test_transaction.py
Normal 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
|
40
api/tests/unit/test_user.py
Normal file
40
api/tests/unit/test_user.py
Normal 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
|
Reference in New Issue
Block a user