~homeworkprod/byceps

82ee7b5b0671004c0be07024e0e39085981c8f28 — Jochen Kupperschmidt 2 years ago c8301bc
Provide specific fixtures for API tests
M config/test_admin.py => config/test_admin.py +0 -2
@@ 16,5 16,3 @@ MAIL_DEFAULT_SENDER = 'BYCEPS <noreply@example.com>'
MAIL_SUPPRESS_SEND = True

JOBS_ASYNC = False

API_TOKEN = 'just-say-PLEASE'

M config/test_party.py => config/test_party.py +0 -2
@@ 28,5 28,3 @@ MAIL_DEFAULT_SENDER = 'BYCEPS <noreply@example.com>'
MAIL_SUPPRESS_SEND = True

JOBS_ASYNC = False

API_TOKEN = 'just-say-PLEASE'

M tests/api/attendance/test_create_archived_attendance.py => tests/api/attendance/test_create_archived_attendance.py +5 -17
@@ 3,37 3,25 @@
:License: Modified BSD, see LICENSE for details.
"""

import pytest

from byceps.services.ticketing import attendance_service

from tests.api.helpers import assemble_authorization_header
from tests.helpers import create_brand, create_party


def test_create_archived_attendance(admin_app_with_db, party, normal_user):
    user = normal_user

def test_create_archived_attendance(
    api_client, api_client_authz_header, party, user
):
    before = attendance_service.get_attended_parties(user.id)
    assert before == []

    url = f'/api/attendances/archived_attendances'
    headers = [assemble_authorization_header('just-say-PLEASE')]
    headers = [api_client_authz_header]
    form_data = {
        'user_id': str(user.id),
        'party_id': str(party.id),
    }

    with admin_app_with_db.test_client() as client:
        response = client.post(url, headers=headers, data=form_data)
    response = api_client.post(url, headers=headers, data=form_data)
    assert response.status_code == 204

    actual = attendance_service.get_attended_parties(user.id)
    actual_ids = [party.id for party in actual]
    assert actual_ids == [party.id]


@pytest.fixture
def party():
    brand = create_brand()
    return create_party(brand.id)

A tests/api/conftest.py => tests/api/conftest.py +60 -0
@@ 0,0 1,60 @@
"""
:Copyright: 2006-2019 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.

API-specific fixtures
"""

import pytest

from tests.base import CONFIG_FILENAME_TEST_ADMIN, create_app
from tests.helpers import create_brand, create_party, create_user

from ..conftest import database_recreated

from .helpers import assemble_authorization_header


API_TOKEN = 'just-say-PLEASE!'


@pytest.fixture(scope='session')
def api_app_without_db(db):
    app = create_app(CONFIG_FILENAME_TEST_ADMIN, {'API_TOKEN': API_TOKEN})
    with app.app_context():
        yield app


@pytest.fixture(scope='module')
def app(api_app_without_db, db):
    app = api_app_without_db
    with database_recreated(db):
        yield app


@pytest.fixture(scope='module')
def party(app):
    brand = create_brand()
    return create_party(brand.id)


@pytest.fixture(scope='module')
def admin(app):
    return create_user('Admin')


@pytest.fixture(scope='module')
def user(app):
    return create_user('User')


@pytest.fixture(scope='module')
def api_client(app):
    """Provide a test HTTP client against the API."""
    return app.test_client()


@pytest.fixture(scope='module')
def api_client_authz_header():
    """Provide a test HTTP client against the API."""
    return assemble_authorization_header(API_TOKEN)

M tests/api/tourney/match/test_comment_create.py => tests/api/tourney/match/test_comment_create.py +29 -24
@@ 11,37 11,49 @@ from byceps.services.tourney.models.match import MatchComment
from byceps.services.tourney import match_service
from byceps.services.user import command_service as user_command_service

from tests.api.helpers import assemble_authorization_header
from tests.conftest import database_recreated
from tests.helpers import create_user, http_client


def test_create_comment(app, player, match):
    response = request_comment_creation(app, match.id, player.id)
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(app, player):
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(app, unknown_match_id, player.id)
    response = request_comment_creation(
        api_client, api_client_authz_header, unknown_match_id, player.id
    )

    assert response.status_code == 404


def test_create_comment_by_suspended_user(app, cheater, match):
    response = request_comment_creation(app, match.id, cheater.id)
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(app, match):
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(app, match.id, unknown_user_id)
    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


@@ 51,15 63,8 @@ def test_create_comment_by_unknown_user(app, match):


@pytest.fixture(scope='module')
def app(db, admin_app):
    with admin_app.app_context():
        with database_recreated(db):
            yield admin_app


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


@pytest.fixture(scope='module')


@@ 78,15 83,15 @@ def match(app):
    return match_service.create_match()


def request_comment_creation(app, match_id, creator_id):
def request_comment_creation(
    api_client, api_client_authz_header, match_id, creator_id
):
    url = f'/api/tourney/matches/{match_id}/comments'

    headers = [assemble_authorization_header('just-say-PLEASE')]

    headers = [api_client_authz_header]
    form_data = {'creator_id': creator_id, 'body': 'gg'}

    with http_client(app) as client:
        return client.post(url, headers=headers, data=form_data)
    return api_client.post(url, headers=headers, data=form_data)


def get_comment_count_for_match(match_id):

M tests/api/user/test_user_profile.py => tests/api/user/test_user_profile.py +13 -33
@@ 3,39 3,19 @@
:License: Modified BSD, see LICENSE for details.
"""

import pytest

from tests.base import AbstractAppTestCase
from tests.conftest import database_recreated
from tests.helpers import (
    create_email_config,
    create_site,
    create_user,
    http_client,
)
from tests.helpers import create_user


CONTENT_TYPE_JSON = 'application/json'


@pytest.fixture(scope='module')
def client(party_app, db):
    app = party_app
    with app.app_context():
        with database_recreated(db):
            create_email_config()
            create_site()

            yield app.test_client()


def test_with_existent_user(client):
def test_with_existent_user(api_client):
    screen_name = 'Gemüsefrau'

    user = create_user(screen_name)
    user_id = str(user.id)

    response = send_request(client, user_id)
    response = send_request(api_client, user_id)

    assert response.status_code == 200
    assert response.content_type == CONTENT_TYPE_JSON


@@ 47,12 27,12 @@ def test_with_existent_user(client):
    assert response_data['avatar_url'] is None


def test_with_not_uninitialized_user(client):
def test_with_not_uninitialized_user(api_client):
    screen_name = 'UninitializedUser'

    user = create_user(screen_name, initialized=False)

    response = send_request(client, str(user.id))
    response = send_request(api_client, str(user.id))

    assert response.status_code == 404
    assert response.content_type == CONTENT_TYPE_JSON


@@ 60,14 40,14 @@ def test_with_not_uninitialized_user(client):
    assert response.json == {}


def test_with_suspended_user(client, db):
def test_with_suspended_user(api_client, db):
    screen_name = 'SuspendedUser'

    user = create_user(screen_name)
    user.suspended = True
    db.session.commit()

    response = send_request(client, str(user.id))
    response = send_request(api_client, str(user.id))

    assert response.status_code == 404
    assert response.content_type == CONTENT_TYPE_JSON


@@ 75,14 55,14 @@ def test_with_suspended_user(client, db):
    assert response.json == {}


def test_with_deleted_user(client, db):
def test_with_deleted_user(api_client, db):
    screen_name = 'DeletedUser'

    user = create_user(screen_name)
    user.deleted = True
    db.session.commit()

    response = send_request(client, str(user.id))
    response = send_request(api_client, str(user.id))

    assert response.status_code == 404
    assert response.content_type == CONTENT_TYPE_JSON


@@ 90,10 70,10 @@ def test_with_deleted_user(client, db):
    assert response.json == {}


def test_with_nonexistent_user(client):
def test_with_nonexistent_user(api_client):
    unknown_user_id = '00000000-0000-0000-0000-000000000000'

    response = send_request(client, unknown_user_id)
    response = send_request(api_client, unknown_user_id)

    assert response.status_code == 404
    assert response.content_type == CONTENT_TYPE_JSON


@@ 101,5 81,5 @@ def test_with_nonexistent_user(client):
    assert response.json == {}


def send_request(client, user_id):
    return client.get(f'/api/users/{user_id}/profile')
def send_request(api_client, user_id):
    return api_client.get(f'/api/users/{user_id}/profile')

M tests/api/user_badge/test_award_badge.py => tests/api/user_badge/test_award_badge.py +6 -9
@@ 9,10 9,8 @@ from byceps.services.user_badge import (
)
from byceps.services.user_badge.transfer.models import QuantifiedBadgeAwarding

from tests.api.helpers import assemble_authorization_header


def test_award_badge(admin_app_with_db, normal_user, admin_user):
def test_award_badge(api_client, api_client_authz_header, user, admin):
    badge = badge_command_service.create_badge(
        'supporter', 'Supporter', 'supporter.svg'
    )


@@ 21,16 19,15 @@ def test_award_badge(admin_app_with_db, normal_user, admin_user):
    assert before == set()

    url = f'/api/user_badges/awardings'
    headers = [assemble_authorization_header('just-say-PLEASE')]
    headers = [api_client_authz_header]
    form_data = {
        'badge_id': str(badge.id),
        'user_id': str(normal_user.id),
        'initiator_id': str(admin_user.id),
        'user_id': str(user.id),
        'initiator_id': str(admin.id),
    }

    with admin_app_with_db.test_client() as client:
        response = client.post(url, headers=headers, data=form_data)
    response = api_client.post(url, headers=headers, data=form_data)
    assert response.status_code == 204

    actual = badge_service.get_awardings_of_badge(badge.id)
    assert actual == {QuantifiedBadgeAwarding(badge.id, normal_user.id, 1)}
    assert actual == {QuantifiedBadgeAwarding(badge.id, user.id, 1)}