"""
Functional tests for the managing sites.

:copyright: Copyright 2013-2016, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""

import http

from pytest import mark
import pytest_bdd as bdd
from pytest_bdd import parsers
import requests


# TODO: Figure out how to generate a JWT using the correct hash.
USER_ROLE_JWT_MAPPING = {
    'admin': 'password',
    'editor': 'some_other_login',
}


@mark.skip(reason='Test scenarios need work')
@bdd.scenario('site_management.feature', 'Admin user can create a new site')
def test_site_creation_by_admin():
    pass


@mark.skip(reason='Test scenarios need work')
@bdd.scenario('site_management.feature', 'Editor user can not create a new site')
def test_site_creation_permissions_for_editor():
    pass


@mark.skip(reason='Test scenarios need work')
@bdd.scenario('site_management.feature', 'Any user can get an existing site')
def test_site_fetch():
    pass


@mark.skip(reason='Test scenarios need work')
@bdd.scenario('site_management.feature', 'Admin user can modify a site')
def test_site_modification_by_admin():
    pass


@mark.skip(reason='Test scenarios need work')
@bdd.scenario('site_management.feature', 'Editor user can not modify a site')
def test_site_modifications_permissions_for_editor():
    pass


@mark.skip(reason='Test scenarios need work')
@bdd.scenario('site_management.feature', 'Admin user can modify a site\'s menu')
def test_site_menu_modification_by_admin():
    pass


@mark.skip(reason='Test scenarios need work')
@bdd.scenario('site_management.feature', 'Editor user can not modify a site\'s menu')
def test_site_menu_modifications_permissions_for_editor():
    pass


@mark.skip(reason='Test scenarios need work')
@bdd.scenario('site_management.feature', 'Admin user can delete a site')
def test_site_deletion_by_admin():
    pass


@mark.skip(reason='Test scenarios need work')
@bdd.scenario('site_management.feature', 'Editor user can not delete a site')
def test_site_deletion_permissions_for_editor():
    pass


@bdd.given(parsers.parse('I am an {user_role} user'))
def user_credentials(user_role):
    return USER_ROLE_JWT_MAPPING[user_role]


@bdd.when('I create a site')
def posted_response_from_auth_endpoint(user_credentials, api_base_uri):
    # TODO: Needs work
    response = requests.post(f'{api_base_uri}/auth', json={
        'username': user_credentials.username,
        'password': user_credentials.password,
    })
    user_credentials.response['status'] = response.status_code
    user_credentials.response['json'] = response.json()


@bdd.then('I get an unauthorized response')
def assert_unauthorized_response(user_credentials):
    # TODO: Needs work
    assert user_credentials.response['status'] == http.HTTPStatus.UNAUTHORIZED
    assert user_credentials.response['json']['status_code'] == http.HTTPStatus.UNAUTHORIZED
    assert user_credentials.response['json']['error'] == 'Bad Request'
    assert user_credentials.response['json']['description'] == 'Invalid credentials'
