__author__ = "Florian Kaiser"
__copyright__ = "Copyright 2022, Project Aktienbot"
__credits__ = ["Florian Kaiser", "Florian Kellermann", "Linus Eickhof", "Kevin Pauer"]
__license__ = "GPL 3.0"
__version__ = "1.0.0"

"""
This file (test_transaction.py) contains the functional tests for the `transaction` blueprint.
"""
import json

from tests.functional.helper_functions import get_token


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
