1
|
|
|
""" |
2
|
|
|
:Copyright: 2006-2019 Jochen Kupperschmidt |
3
|
|
|
:License: Modified BSD, see LICENSE for details. |
4
|
|
|
""" |
5
|
|
|
|
6
|
|
|
from base64 import b64encode |
7
|
|
|
|
8
|
|
|
import pytest |
9
|
|
|
|
10
|
|
|
from byceps.services.tourney.models.match import MatchComment |
11
|
|
|
from byceps.services.tourney import match_service |
12
|
|
|
from byceps.services.user import command_service as user_command_service |
13
|
|
|
|
14
|
|
|
from tests.api.helpers import assemble_authorization_header |
15
|
|
|
from tests.conftest import database_recreated |
16
|
|
|
from tests.helpers import ( |
17
|
|
|
create_email_config, |
18
|
|
|
create_site, |
19
|
|
|
create_user, |
20
|
|
|
http_client, |
21
|
|
|
login_user, |
22
|
|
|
) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def test_create_comment(app, site, player, match): |
26
|
|
|
response = request_comment_creation(app, match.id, player.id) |
27
|
|
|
|
28
|
|
|
assert response.status_code == 201 |
29
|
|
|
assert get_comment_count_for_match(match.id) == 1 |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def test_create_comment_on_nonexistent_match(app, site, player): |
33
|
|
|
unknown_match_id = '00000000-0000-0000-0000-000000000000' |
34
|
|
|
|
35
|
|
|
response = request_comment_creation(app, unknown_match_id, player.id) |
36
|
|
|
|
37
|
|
|
assert response.status_code == 404 |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def test_create_comment_by_suspended_user(app, site, cheater, match): |
41
|
|
|
response = request_comment_creation(app, match.id, cheater.id) |
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(app, site, match): |
48
|
|
|
unknown_user_id = '00000000-0000-0000-0000-000000000000' |
49
|
|
|
|
50
|
|
|
response = request_comment_creation(app, match.id, unknown_user_id) |
51
|
|
|
|
52
|
|
|
assert response.status_code == 400 |
53
|
|
|
assert get_comment_count_for_match(match.id) == 0 |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
# helpers |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
@pytest.fixture(scope='module') |
60
|
|
|
def app(db, party_app): |
61
|
|
|
with party_app.app_context(): |
62
|
|
|
with database_recreated(db): |
63
|
|
|
yield party_app |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
@pytest.fixture(scope='module') |
67
|
|
|
def site(): |
68
|
|
|
create_email_config() |
69
|
|
|
create_site() |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
@pytest.fixture(scope='module') |
73
|
|
|
def player(app): |
74
|
|
|
player = create_user() |
75
|
|
|
|
76
|
|
|
login_user(player.id) |
77
|
|
|
|
78
|
|
|
return player |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
@pytest.fixture(scope='module') |
82
|
|
|
def cheater(app): |
83
|
|
|
user = create_user('Cheater!') |
84
|
|
|
|
85
|
|
|
user_command_service.suspend_account( |
86
|
|
|
user.id, user.id, reason='I cheat, therefore I lame.' |
87
|
|
|
) |
88
|
|
|
|
89
|
|
|
return user |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
@pytest.fixture |
93
|
|
|
def match(app): |
94
|
|
|
return match_service.create_match() |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
def request_comment_creation(app, match_id, creator_id): |
98
|
|
|
url = f'/api/tourney/matches/{match_id}/comments' |
99
|
|
|
|
100
|
|
|
headers = [assemble_authorization_header('just-say-PLEASE')] |
101
|
|
|
|
102
|
|
|
form_data = {'creator_id': creator_id, 'body': 'gg'} |
103
|
|
|
|
104
|
|
|
with http_client(app) as client: |
105
|
|
|
return client.post(url, headers=headers, data=form_data) |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
def get_comment_count_for_match(match_id): |
109
|
|
|
return MatchComment.query.for_match(match_id).count() |
110
|
|
|
|