1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
"""
:Copyright: 2014-2025 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from collections.abc import Iterator
import os
from pathlib import Path
from tempfile import TemporaryDirectory
from wsgiref.types import WSGIApplication

from flask import Flask
from moneyed import EUR
import pytest

from byceps.app_dispatcher import create_dispatcher_app
from byceps.application import (
    create_admin_app as _create_admin_app,
    create_site_app as _create_site_app,
)
from byceps.byceps_app import BycepsApp
from byceps.config.converter import assemble_database_uri
from byceps.config.models import (
    AdminAppConfig,
    ApiAppConfig,
    AppsConfig,
    BycepsConfig,
    DatabaseConfig,
    DevelopmentConfig,
    JobsConfig,
    MetricsConfig,
    PaymentGatewaysConfig,
    RedisConfig,
    SiteAppConfig,
    SmtpConfig,
)
from byceps.database import db
from byceps.services.authz import authz_service
from byceps.services.authz.models import PermissionID, Role, RoleID
from byceps.services.board import board_service
from byceps.services.board.models import Board, BoardID
from byceps.services.brand import brand_service
from byceps.services.brand.models import Brand, BrandID
from byceps.services.email import email_config_service
from byceps.services.email.models import EmailConfig
from byceps.services.news import news_channel_service
from byceps.services.news.models import NewsChannel, NewsChannelID
from byceps.services.party.models import Party, PartyID
from byceps.services.shop.order import order_sequence_service
from byceps.services.shop.order.models.number import (
    OrderNumberSequence,
    OrderNumberSequenceID,
)
from byceps.services.shop.order.models.order import Orderer
from byceps.services.shop.payment import payment_gateway_service
from byceps.services.shop.payment.models import PaymentGateway
from byceps.services.shop.product.models import Product
from byceps.services.shop.shop import shop_service
from byceps.services.shop.shop.models import Shop, ShopID
from byceps.services.shop.storefront import storefront_service
from byceps.services.shop.storefront.models import (
    Storefront,
    StorefrontID,
)
from byceps.services.site.models import Site, SiteID
from byceps.services.ticketing import ticket_category_service
from byceps.services.ticketing.models.ticket import TicketCategory
from byceps.services.user.models.user import User, UserID

from tests.helpers import (
    create_party,
    create_role_with_permissions_assigned,
    create_site,
    create_user,
    generate_token,
    http_client,
)
from tests.helpers.shop import create_product, create_orderer

from .database import populate_database, set_up_database, tear_down_database


@pytest.fixture(scope='session')
def database_config():
    host = os.environ.get('POSTGRES_HOST', '127.0.0.1')
    port = int(os.environ.get('POSTGRES_PORT', '5432'))
    username = os.environ.get('POSTGRES_USER', 'byceps_test')
    password = os.environ.get('POSTGRES_PASSWORD', 'test')
    database = os.environ.get('POSTGRES_DB', 'byceps_test')

    return DatabaseConfig(
        host=host,
        port=port,
        username=username,
        password=password,
        database=database,
    )


@pytest.fixture(scope='session')
def redis_config():
    host = os.environ.get('REDIS_HOST', '127.0.0.1')
    port = int(os.environ.get('REDIS_PORT', '6379'))
    database = 0

    return RedisConfig(
        url=f'redis://{host}:{port}/{database}',
    )


def build_byceps_config(
    data_path: Path,
    apps_config: AppsConfig,
    database_config: DatabaseConfig,
    redis_config: RedisConfig,
    *,
    metrics_enabled: bool = False,
    style_guide_enabled: bool = False,
) -> BycepsConfig:
    return BycepsConfig(
        data_path=data_path,
        locale='de',
        propagate_exceptions=None,
        testing=True,
        timezone='Europe/Berlin',
        secret_key='secret-key-for-testing-ONLY',
        apps=apps_config,
        database=database_config,
        development=DevelopmentConfig(
            style_guide_enabled=style_guide_enabled,
            toolbar_enabled=False,
        ),
        discord=None,
        invoiceninja=None,
        jobs=JobsConfig(
            asynchronous=False,
        ),
        metrics=MetricsConfig(
            enabled=metrics_enabled,
        ),
        payment_gateways=PaymentGatewaysConfig(
            paypal=None,
            stripe=None,
        ),
        redis=redis_config,
        smtp=SmtpConfig(
            host='127.0.0.1',
            port=25,
            starttls=False,
            use_ssl=False,
            username=None,
            password=None,
            suppress_send=True,
        ),
    )


@pytest.fixture(scope='session')
def database(database_config: DatabaseConfig):
    app = Flask('byceps')

    app.config['SQLALCHEMY_DATABASE_URI'] = assemble_database_uri(
        database_config
    )

    db.init_app(app)

    with app.app_context():
        # tear_down_database()
        set_up_database()
        populate_database()


@pytest.fixture(scope='session')
def apps(database, make_byceps_config) -> WSGIApplication:
    apps_config = AppsConfig(
        admin=None,
        api=ApiAppConfig(server_name='api.acmecon.test'),
        sites=[],
    )

    byceps_config = make_byceps_config(apps_config)

    return create_dispatcher_app(byceps_config)


@pytest.fixture(scope='session')
def make_admin_app(make_byceps_config):
    """Provide the admin web application."""

    def _wrapper(
        server_name: str,
        *,
        metrics_enabled: bool = False,
        style_guide_enabled: bool = False,
    ) -> BycepsApp:
        byceps_config = make_byceps_config(
            None,
            metrics_enabled=metrics_enabled,
            style_guide_enabled=style_guide_enabled,
        )

        app_config = AdminAppConfig(
            server_name=server_name,
        )

        return _create_admin_app(byceps_config, app_config)

    return _wrapper


@pytest.fixture(scope='session')
def admin_app(database, make_admin_app) -> Iterator[BycepsApp]:
    """Provide the admin web application."""
    server_name = 'admin.acmecon.test'
    app = make_admin_app(server_name)
    with app.app_context():
        yield app


@pytest.fixture(scope='session')
def api_app(apps, site: Site) -> BycepsApp:
    """Provide a API web application."""
    server_name = 'api.acmecon.test'
    app = apps.wsgi_app.get_application(server_name)
    with app.app_context():
        return app


@pytest.fixture(scope='session')
def make_site_app(admin_app, make_byceps_config):
    """Provide a site web application."""

    def _wrapper(
        server_name: str, site_id: SiteID, *, style_guide_enabled: bool = False
    ) -> BycepsApp:
        byceps_config = make_byceps_config(
            None, style_guide_enabled=style_guide_enabled
        )

        app_config = SiteAppConfig(
            server_name=server_name,
            site_id=site_id,
        )

        return _create_site_app(byceps_config, app_config)

    return _wrapper


@pytest.fixture(scope='session')
def site_app(database, make_site_app, site: Site) -> BycepsApp:
    """Provide a site web application."""
    server_name = 'www.acmecon.test'
    app = make_site_app(server_name, site.id)
    with app.app_context():
        return app


@pytest.fixture(scope='session')
def make_byceps_config(
    data_path: Path, database_config: DatabaseConfig, redis_config: RedisConfig
):
    def _wrapper(
        apps_config: AppsConfig | None = None,
        *,
        metrics_enabled: bool = False,
        style_guide_enabled: bool = False,
    ) -> BycepsConfig:
        if apps_config is None:
            apps_config = AppsConfig(admin=None, api=None, sites=[])

        return build_byceps_config(
            data_path,
            apps_config,
            database_config,
            redis_config,
            metrics_enabled=metrics_enabled,
            style_guide_enabled=style_guide_enabled,
        )

    return _wrapper


@pytest.fixture(scope='session')
def data_path() -> Path:
    with TemporaryDirectory() as d:
        return Path(d)


@pytest.fixture(scope='package')
def make_client():
    """Provide a test HTTP client against the application."""

    def _wrapper(app: BycepsApp, *, user_id: UserID | None = None):
        with http_client(app, user_id=user_id) as client:
            return client

    return _wrapper


@pytest.fixture(scope='session')
def make_role(admin_app: BycepsApp):
    def _wrapper() -> Role:
        role_id = RoleID(generate_token())
        return authz_service.create_role(role_id, role_id).unwrap()

    return _wrapper


@pytest.fixture(scope='session')
def make_user(admin_app: BycepsApp):
    def _wrapper(*args, **kwargs) -> User:
        return create_user(*args, **kwargs)

    return _wrapper


@pytest.fixture(scope='session')
def make_admin(make_user):
    def _wrapper(permission_id_strs: set[str], *args, **kwargs) -> User:
        admin = make_user(*args, **kwargs)

        # Create role.
        role_id = RoleID(f'admin_{generate_token()}')
        permission_ids = [PermissionID(p_id) for p_id in permission_id_strs]
        create_role_with_permissions_assigned(role_id, permission_ids)

        # Assign role to user.
        authz_service.assign_role_to_user(role_id, admin)

        return admin

    return _wrapper


@pytest.fixture(scope='session')
def admin_user(make_admin) -> User:
    permission_ids = {'admin.access'}
    return make_admin(permission_ids, screen_name='Admin')


@pytest.fixture(scope='session')
def user(make_user) -> User:
    return make_user('User')


@pytest.fixture(scope='session')
def uninitialized_user(make_user) -> User:
    return make_user('UninitializedUser', initialized=False)


@pytest.fixture(scope='session')
def suspended_user(make_user) -> User:
    return make_user('SuspendedUser', suspended=True)


@pytest.fixture(scope='session')
def deleted_user(make_user) -> User:
    return make_user('DeletedUser', deleted=True)


@pytest.fixture(scope='session')
def make_email_config(admin_app: BycepsApp):
    def _wrapper(
        brand_id: BrandID,
        *,
        sender_address: str | None = None,
        sender_name: str | None = None,
        contact_address: str | None = None,
    ) -> EmailConfig:
        if sender_address is None:
            sender_address = f'{generate_token()}@domain.example'

        email_config_service.set_config(
            brand_id,
            sender_address,
            sender_name=sender_name,
            contact_address=contact_address,
        )

        return email_config_service.get_config(brand_id)

    return _wrapper


@pytest.fixture(scope='session')
def email_config(make_email_config, brand: Brand) -> EmailConfig:
    return make_email_config(
        brand.id,
        sender_address='noreply@acmecon.test',
        sender_name='ACME Entertainment Convention',
        contact_address='help@acmecon.test',
    )


@pytest.fixture(scope='session')
def site(email_config: EmailConfig, party: Party, board: Board) -> Site:
    return create_site(
        SiteID('acmecon-2014-website'),
        party.brand_id,
        title='ACMECon 2014 website',
        server_name='www.acmecon.test',
        party_id=party.id,
        board_id=board.id,
    )


@pytest.fixture(scope='session')
def make_brand(admin_app: BycepsApp):
    def _wrapper(
        brand_id: BrandID | None = None, title: str | None = None
    ) -> Brand:
        if brand_id is None:
            brand_id = BrandID(generate_token())

        if title is None:
            title = brand_id

        return brand_service.create_brand(brand_id, title)

    return _wrapper


@pytest.fixture(scope='session')
def brand(make_brand) -> Brand:
    return make_brand('acmecon', 'ACME Entertainment Convention')


@pytest.fixture(scope='session')
def make_party(admin_app: BycepsApp):
    def _wrapper(*args, **kwargs) -> Party:
        return create_party(*args, **kwargs)

    return _wrapper


@pytest.fixture(scope='session')
def party(make_party, brand: Brand) -> Party:
    return make_party(brand, title='ACMECon 2014')


@pytest.fixture(scope='session')
def make_ticket_category(admin_app: BycepsApp):
    def _wrapper(party_id: PartyID, title: str) -> TicketCategory:
        return ticket_category_service.create_category(party_id, title)

    return _wrapper


@pytest.fixture(scope='session')
def board(brand: Brand) -> Board:
    board_id = BoardID(generate_token())
    return board_service.create_board(brand, board_id)


@pytest.fixture()
def make_news_channel():
    def _wrapper(
        brand: Brand,
        channel_id: NewsChannelID | None = None,
        *,
        announcement_site_id: SiteID | None = None,
    ) -> NewsChannel:
        if channel_id is None:
            channel_id = NewsChannelID(generate_token())

        return news_channel_service.create_channel(
            brand, channel_id, announcement_site_id=announcement_site_id
        )

    return _wrapper


@pytest.fixture(scope='session')
def make_payment_gateway(admin_app: BycepsApp):
    def _wrapper(
        *,
        payment_gateway_id: str | None = None,
        name: str | None = None,
        enabled: bool = True,
    ) -> PaymentGateway:
        if payment_gateway_id is None:
            payment_gateway_id = generate_token()

        if name is None:
            name = payment_gateway_id

        return payment_gateway_service.create_payment_gateway(
            payment_gateway_id, name, enabled
        )

    return _wrapper


@pytest.fixture(scope='session')
def make_shop(admin_app: BycepsApp):
    def _wrapper(brand: Brand) -> Shop:
        return shop_service.create_shop(brand, EUR)

    return _wrapper


@pytest.fixture(scope='session')
def make_order_number_sequence():
    def _wrapper(
        shop_id: ShopID,
        *,
        prefix: str | None = None,
        value: int = 0,
    ) -> OrderNumberSequence:
        if prefix is None:
            prefix = f'{generate_token()}-O'

        return order_sequence_service.create_order_number_sequence(
            shop_id, prefix, value=value
        ).unwrap()

    return _wrapper


@pytest.fixture(scope='session')
def make_storefront():
    def _wrapper(
        shop_id: ShopID, order_number_sequence_id: OrderNumberSequenceID
    ) -> Storefront:
        storefront_id = StorefrontID(generate_token())
        return storefront_service.create_storefront(
            storefront_id, shop_id, order_number_sequence_id, closed=False
        )

    return _wrapper


@pytest.fixture(scope='session')
def make_product():
    def _wrapper(shop_id: ShopID, **kwargs) -> Product:
        return create_product(shop_id, **kwargs)

    return _wrapper


@pytest.fixture(scope='session')
def make_orderer():
    def _wrapper(user: User) -> Orderer:
        return create_orderer(user)

    return _wrapper