Completed
Push — main ( 7d4592...e74a90 )
by Jochen
03:11
created

item()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
"""
2
:Copyright: 2006-2020 Jochen Kupperschmidt
3
:License: Modified BSD, see LICENSE for details.
4
"""
5
6
import pytest
7
8
from byceps.announce.discord import news  # Load signal handlers.
9
from byceps.events.news import NewsItemPublished
10
from byceps.services.news import (
11
    channel_service as news_channel_service,
12
    service as news_service,
13
)
14
from byceps.services.webhooks import service as webhook_service
15
from byceps.signals import news as news_signals
16
17
from .helpers import assert_request, mocked_webhook_receiver, now
18
19
20
WEBHOOK_URL = 'https://webhoooks.test/news'
21
22
23
def test_published_news_item_announced(
24
    webhook_settings, admin_app, item, editor
25
):
26
    expected_content = (
27
        '[News] Die News "Zieh dir das rein!" wurde veröffentlicht. '
28
        + 'https://acme.example.com/news/zieh-dir-das-rein'
29
    )
30
31
    event = NewsItemPublished(
32
        occurred_at=now(),
33
        initiator_id=editor.id,
34
        initiator_screen_name=editor.screen_name,
35
        item_id=item.id,
36
        channel_id=item.channel.id,
37
        title=item.title,
38
        external_url=item.external_url,
39
    )
40
41
    with mocked_webhook_receiver(WEBHOOK_URL) as mock:
42
        news_signals.item_published.send(None, event=event)
43
44
    assert_request(mock, expected_content)
45
46
47
# helpers
48
49
50
@pytest.fixture(scope='module')
51
def webhook_settings(channel):
52
    scope = 'news'
53
    scope_id = str(channel.id)
54
    format = 'discord'
55
    text_prefix = '[News] '
56
    url = WEBHOOK_URL
57
    enabled = True
58
59
    webhook = webhook_service.create_outgoing_webhook(
60
        scope, scope_id, format, url, enabled, text_prefix=text_prefix
61
    )
62
63
    yield
64
65
    webhook_service.delete_outgoing_webhook(webhook.id)
66
67
68
@pytest.fixture(scope='module')
69
def channel(brand):
70
    channel_id = f'{brand.id}-test'
71
    url_prefix = 'https://acme.example.com/news/'
72
73
    channel = news_channel_service.create_channel(
74
        brand.id, channel_id, url_prefix
75
    )
76
77
    yield channel
78
79
    news_channel_service.delete_channel(channel_id)
80
81
82
@pytest.fixture(scope='module')
83
def editor(make_user):
84
    return make_user('RasendeReporterin')
85
86
87
@pytest.fixture(scope='module')
88
def item(channel, editor):
89
    slug = 'zieh-dir-das-rein'
90
    title = 'Zieh dir das rein!'
91
    body = 'any body'
92
93
    item = news_service.create_item(channel.id, slug, editor.id, title, body)
94
95
    yield item
96
97
    news_service.delete_item(item.id)
98