__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_keyword.py) contains the functional tests for the `keyword` blueprint.
"""
import json
from tests.functional.helper_functions import get_token


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