""" 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 ""