__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_share.py) contains the functional tests for the `share` blueprint.
"""
import json

from tests.functional.helper_functions import get_token


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
