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 import MatchComment |
9
|
|
|
from byceps.services.tourney import match_service |
10
|
|
|
from byceps.services.user import command_service as user_command_service |
11
|
|
|
|
12
|
|
|
from tests.helpers import create_user |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
def test_create_comment(api_client, api_client_authz_header, player, match): |
16
|
|
|
response = request_comment_creation( |
17
|
|
|
api_client, api_client_authz_header, match.id, player.id |
18
|
|
|
) |
19
|
|
|
|
20
|
|
|
assert response.status_code == 201 |
21
|
|
|
assert get_comment_count_for_match(match.id) == 1 |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def test_create_comment_on_nonexistent_match( |
25
|
|
|
api_client, api_client_authz_header, player |
26
|
|
|
): |
27
|
|
|
unknown_match_id = '00000000-0000-0000-0000-000000000000' |
28
|
|
|
|
29
|
|
|
response = request_comment_creation( |
30
|
|
|
api_client, api_client_authz_header, unknown_match_id, player.id |
31
|
|
|
) |
32
|
|
|
|
33
|
|
|
assert response.status_code == 400 |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def test_create_comment_by_suspended_user( |
37
|
|
|
api_client, api_client_authz_header, cheater, match |
38
|
|
|
): |
39
|
|
|
response = request_comment_creation( |
40
|
|
|
api_client, api_client_authz_header, match.id, cheater.id |
41
|
|
|
) |
42
|
|
|
|
43
|
|
|
assert response.status_code == 400 |
44
|
|
|
assert get_comment_count_for_match(match.id) == 0 |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def test_create_comment_by_unknown_user( |
48
|
|
|
api_client, api_client_authz_header, match |
49
|
|
|
): |
50
|
|
|
unknown_user_id = '00000000-0000-0000-0000-000000000000' |
51
|
|
|
|
52
|
|
|
response = request_comment_creation( |
53
|
|
|
api_client, api_client_authz_header, match.id, unknown_user_id |
54
|
|
|
) |
55
|
|
|
|
56
|
|
|
assert response.status_code == 400 |
57
|
|
|
assert get_comment_count_for_match(match.id) == 0 |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
# helpers |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
@pytest.fixture(scope='module') |
64
|
|
|
def player(user): |
65
|
|
|
return user |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
@pytest.fixture(scope='module') |
69
|
|
|
def cheater(app): |
70
|
|
|
user = create_user('Cheater!') |
71
|
|
|
|
72
|
|
|
user_command_service.suspend_account( |
73
|
|
|
user.id, user.id, reason='I cheat, therefore I lame.' |
74
|
|
|
) |
75
|
|
|
|
76
|
|
|
return user |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
@pytest.fixture |
80
|
|
|
def match(app): |
81
|
|
|
return match_service.create_match() |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def request_comment_creation( |
85
|
|
|
api_client, api_client_authz_header, match_id, creator_id |
86
|
|
|
): |
87
|
|
|
url = f'/api/tourney/match_comments' |
88
|
|
|
|
89
|
|
|
headers = [api_client_authz_header] |
90
|
|
|
json_data = { |
91
|
|
|
'match_id': str(match_id), |
92
|
|
|
'creator_id': creator_id, |
93
|
|
|
'body': 'gg', |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return api_client.post(url, headers=headers, json=json_data) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def get_comment_count_for_match(match_id): |
100
|
|
|
return MatchComment.query.for_match(match_id).count() |
101
|
|
|
|