Passed
Push — master ( 307301...a898f8 )
by Jochen
02:51
created

get_comment()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
"""
2
:Copyright: 2006-2019 Jochen Kupperschmidt
3
:License: Modified BSD, see LICENSE for details.
4
"""
5
6
import pytest
7
8
from byceps.services.tourney.models.match_comment import (
9
    MatchComment as DbMatchComment,
10
)
11
from byceps.services.tourney import (
12
    match_comment_service as comment_service,
13
    match_service,
14
)
15
from byceps.services.user import command_service as user_command_service
16
17
from tests.helpers import create_user
18
19
20
def test_create_comment(api_client, api_client_authz_header, player, match):
21
    response = request_comment_creation(
22
        api_client, api_client_authz_header, match.id, player.id
23
    )
24
25
    assert response.status_code == 201
26
    assert get_comment_count_for_match(match.id) == 1
27
28
    comment_id = get_comment_id_from_location_header(response)
29
    comment = get_comment(comment_id)
30
    assert comment.match_id == match.id
31
    assert comment.created_at is not None
32
    assert comment.created_by.id == player.id
33
    assert comment.body_text == 'gg [i]lol[/i]'
34
    assert comment.body_html == 'gg <em>lol</em>'
35
    assert comment.last_edited_at is None
36
    assert comment.last_edited_by is None
37
    assert not comment.hidden
38
    assert comment.hidden_at is None
39
    assert comment.hidden_by is None
40
41
42
def test_create_comment_on_nonexistent_match(
43
    api_client, api_client_authz_header, player
44
):
45
    unknown_match_id = '00000000-0000-0000-0000-000000000000'
46
47
    response = request_comment_creation(
48
        api_client, api_client_authz_header, unknown_match_id, player.id
49
    )
50
51
    assert response.status_code == 400
52
    assert get_comment_count_for_match(unknown_match_id) == 0
53
54
55
def test_create_comment_by_suspended_user(
56
    api_client, api_client_authz_header, cheater, match
57
):
58
    response = request_comment_creation(
59
        api_client, api_client_authz_header, match.id, cheater.id
60
    )
61
62
    assert response.status_code == 400
63
    assert get_comment_count_for_match(match.id) == 0
64
65
66
def test_create_comment_by_unknown_user(
67
    api_client, api_client_authz_header, match
68
):
69
    unknown_user_id = '00000000-0000-0000-0000-000000000000'
70
71
    response = request_comment_creation(
72
        api_client, api_client_authz_header, match.id, unknown_user_id
73
    )
74
75
    assert response.status_code == 400
76
    assert get_comment_count_for_match(match.id) == 0
77
78
79
# helpers
80
81
82
@pytest.fixture(scope='module')
83
def player(user):
84
    return user
85
86
87
@pytest.fixture(scope='module')
88
def cheater(app):
89
    user = create_user('Cheater!')
90
91
    user_command_service.suspend_account(
92
        user.id, user.id, reason='I cheat, therefore I lame.'
93
    )
94
95
    return user
96
97
98
@pytest.fixture
99
def match(app):
100
    return match_service.create_match()
101
102
103
def request_comment_creation(
104
    api_client, api_client_authz_header, match_id, creator_id
105
):
106
    url = f'/api/tourney/match_comments'
107
108
    headers = [api_client_authz_header]
109
    json_data = {
110
        'match_id': str(match_id),
111
        'creator_id': creator_id,
112
        'body': 'gg [i]lol[/i]',
113
    }
114
115
    return api_client.post(url, headers=headers, json=json_data)
116
117
118
def get_comment_id_from_location_header(response):
119
    location_header = response.headers['Location']
120
    return location_header.rpartition('/')[-1]
121
122
123
def get_comment(comment_id):
124
    return comment_service.get_comment(comment_id)
125
126
127
def get_comment_count_for_match(match_id):
128
    return DbMatchComment.query.for_match(match_id).count()
129