Passed
Push — master ( 69ad40...f1cd88 )
by Jochen
02:35
created

match()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
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
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
13
from tests.api.helpers import assemble_authorization_header
14
from tests.conftest import database_recreated
15
from tests.helpers import (
16
    create_email_config,
17
    create_site,
18
    create_user,
19
    http_client,
20
    login_user,
21
)
22
23
24
def test_create_comment_on_existent_match(app, site, player, match):
25
    response = request_comment_creation(app, match.id, user_id=player.id)
26
27
    assert response.status_code == 201
28
29
    assert get_comment_count_for_match(match.id) == 1
30
31
32
def test_create_comment_on_existent_match_as_anonymous_user(app, site, match):
33
    response = request_comment_creation(app, match.id)
34
35
    assert response.status_code == 403
36
37
    assert get_comment_count_for_match(match.id) == 0
38
39
40
def test_create_comment_on_nonexistent_match(app, site, player):
41
    unknown_match_id = '00000000-0000-0000-0000-000000000000'
42
43
    response = request_comment_creation(
44
        app, unknown_match_id, user_id=player.id
45
    )
46
47
    assert response.status_code == 404
48
49
50
# helpers
51
52
53
@pytest.fixture(scope='module')
54
def app(db, party_app):
55
    with party_app.app_context():
56
        with database_recreated(db):
57
            yield party_app
58
59
60
@pytest.fixture(scope='module')
61
def site():
62
    create_email_config()
63
    create_site()
64
65
66
@pytest.fixture(scope='module')
67
def player(app):
68
    player = create_user()
69
70
    login_user(player.id)
71
72
    return player
73
74
75
@pytest.fixture
76
def match(app):
77
    return match_service.create_match()
78
79
80
def request_comment_creation(app, match_id, *, user_id=None):
81
    url = f'/api/tourney/matches/{match_id}/comments'
82
83
    headers = [assemble_authorization_header('just-say-PLEASE')]
84
85
    form_data = {'body': 'gg'}
86
87
    with http_client(app, user_id=user_id) as client:
88
        return client.post(url, headers=headers, data=form_data)
89
90
91
def get_comment_count_for_match(match_id):
92
    return MatchComment.query.for_match(match_id).count()
93