Tests
- Improved directory structure - Added functional and unit tests
This commit is contained in:
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 ""
|
Reference in New Issue
Block a user