"""
:Copyright: 2006-2019 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

import pytest

from byceps.services.tourney.models.match_comment import MatchComment
from byceps.services.tourney import match_service
from byceps.services.user import command_service as user_command_service

from tests.helpers import create_user


def test_create_comment(api_client, api_client_authz_header, player, match):
    response = request_comment_creation(
        api_client, api_client_authz_header, match.id, player.id
    )

    assert response.status_code == 201
    assert get_comment_count_for_match(match.id) == 1


def test_create_comment_on_nonexistent_match(
    api_client, api_client_authz_header, player
):
    unknown_match_id = '00000000-0000-0000-0000-000000000000'

    response = request_comment_creation(
        api_client, api_client_authz_header, unknown_match_id, player.id
    )

    assert response.status_code == 400


def test_create_comment_by_suspended_user(
    api_client, api_client_authz_header, cheater, match
):
    response = request_comment_creation(
        api_client, api_client_authz_header, match.id, cheater.id
    )

    assert response.status_code == 400
    assert get_comment_count_for_match(match.id) == 0


def test_create_comment_by_unknown_user(
    api_client, api_client_authz_header, match
):
    unknown_user_id = '00000000-0000-0000-0000-000000000000'

    response = request_comment_creation(
        api_client, api_client_authz_header, match.id, unknown_user_id
    )

    assert response.status_code == 400
    assert get_comment_count_for_match(match.id) == 0


# helpers


@pytest.fixture(scope='module')
def player(user):
    return user


@pytest.fixture(scope='module')
def cheater(app):
    user = create_user('Cheater!')

    user_command_service.suspend_account(
        user.id, user.id, reason='I cheat, therefore I lame.'
    )

    return user


@pytest.fixture
def match(app):
    return match_service.create_match()


def request_comment_creation(
    api_client, api_client_authz_header, match_id, creator_id
):
    url = f'/api/tourney/match_comments'

    headers = [api_client_authz_header]
    json_data = {
        'match_id': str(match_id),
        'creator_id': creator_id,
        'body': 'gg',
    }

    return api_client.post(url, headers=headers, json=json_data)


def get_comment_count_for_match(match_id):
    return MatchComment.query.for_match(match_id).count()

@@ -166,16 +166,22 @@
  import 
+(%0A    
 MatchCom
@@ -184,16 +184,37 @@
 hComment
+ as DbMatchComment,%0A)
 %0Afrom by
@@ -242,29 +242,84 @@
  import 
-match_service
+(%0A    match_comment_service as comment_service,%0A    match_service,%0A)
 %0Afrom by
@@ -709,16 +709,471 @@
 ) == 1%0A%0A
+    comment_id = get_comment_id_from_location_header(response)%0A    comment = get_comment(comment_id)%0A    assert comment.match_id == match.id%0A    assert comment.created_at is not None%0A    assert comment.created_by.id == player.id%0A    assert comment.body_html == 'gg'%0A    assert comment.last_edited_at is None%0A    assert comment.last_edited_by is None%0A    assert not comment.hidden%0A    assert comment.hidden_at is None%0A    assert comment.hidden_by is None%0A%0A
 %0Adef tes
@@ -1485,16 +1485,78 @@
  == 400%0A
+    assert get_comment_count_for_match(unknown_match_id) == 0%0A
 %0A%0Adef te
@@ -2952,24 +2952,257 @@
 son_data)%0A%0A%0A
+def get_comment_id_from_location_header(response):%0A    location_header = response.headers%5B'Location'%5D%0A    return location_header.rpartition('/')%5B-1%5D%0A%0A%0Adef get_comment(comment_id):%0A    return comment_service.get_comment(comment_id)%0A%0A%0A
 def get_comm
@@ -3228,24 +3228,24 @@
 (match_id):%0A
-
     return M
@@ -3243,16 +3243,18 @@
  return 
+Db
 MatchCom

Test tourney match comment attributes after creation