Passed
Push — main ( a0d2b9...52c0ea )
by Jochen
04:32
created

item_without_url()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
"""
2
:Copyright: 2006-2022 Jochen Kupperschmidt
3
:License: Revised BSD (see `LICENSE` file for details)
4
"""
5
6
from flask import Flask
7
import pytest
8
9
import byceps.announce.connections  # Connect signal handlers.
10
from byceps.services.brand.transfer.models import Brand
11
from byceps.services.news import service as news_service
12
from byceps.services.news.transfer.models import BodyFormat, Channel, Item
13
from byceps.services.site.transfer.models import Site
14
from byceps.services.webhooks import service as webhook_service
15
from byceps.signals import news as news_signals
16
17
from tests.integration.services.news.conftest import make_channel
18
19
from .helpers import assert_request, mocked_webhook_receiver
20
21
22
WEBHOOK_URL = 'https://webhoooks.test/news'
23
24
25
def test_published_news_item_announced_with_url(
26
    admin_app: Flask, item_with_url: Item
27
) -> None:
28
    expected_content = (
29
        '[News] Die News "Zieh dir das rein!" wurde veröffentlicht. '
30
        + 'https://www.acmecon.test/news/zieh-dir-das-rein'
31
    )
32
33
    create_webhooks(item_with_url.channel)
34
35
    event = news_service.publish_item(item_with_url.id)
36
37
    with mocked_webhook_receiver(WEBHOOK_URL) as mock:
38
        news_signals.item_published.send(None, event=event)
39
40
    assert_request(mock, expected_content)
41
42
43
def test_published_news_item_announced_without_url(
44
    admin_app: Flask, item_without_url: Item
45
) -> None:
46
    expected_content = (
47
        '[News] Die News "Zieh dir auch das rein!" wurde veröffentlicht.'
48
    )
49
50
    create_webhooks(item_without_url.channel)
51
52
    event = news_service.publish_item(item_without_url.id)
53
54
    with mocked_webhook_receiver(WEBHOOK_URL) as mock:
55
        news_signals.item_published.send(None, event=event)
56
57
    assert_request(mock, expected_content)
58
59
60
# helpers
61
62
63
@pytest.fixture
64
def channel_with_site(brand: Brand, site: Site, make_channel) -> Channel:
65
    return make_channel(brand.id, announcement_site_id=site.id)
66
67
68
@pytest.fixture
69
def channel_without_site(brand: Brand, make_channel) -> Channel:
70
    return make_channel(brand.id)
71
72
73
def create_webhooks(channel: Channel) -> None:
74
    news_channel_ids = [str(channel.id), 'totally-different-id']
75
    format = 'discord'
76
    text_prefix = '[News] '
77
    url = WEBHOOK_URL
78
    enabled = True
79
80
    for news_channel_id in news_channel_ids:
81
        webhook_service.create_outgoing_webhook(
82
            # event_types
83
            {'news-item-published'},
84
            # event_filters
85
            {'news-item-published': {'channel_id': [news_channel_id]}},
86
            format,
87
            url,
88
            enabled,
89
            text_prefix=text_prefix,
90
        )
91
92
93 View Code Duplication
@pytest.fixture
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
94
def make_item(make_user):
95
    def _wrapper(channel: Channel, slug: str, title: str) -> Item:
96
        editor = make_user()
97
        body = 'any body'
98
        body_format = BodyFormat.html
99
100
        return news_service.create_item(
101
            channel.id, slug, editor.id, title, body, body_format
102
        )
103
104
    return _wrapper
105
106
107
@pytest.fixture
108
def item_with_url(make_item, channel_with_site: Channel) -> Item:
109
    slug = 'zieh-dir-das-rein'
110
    title = 'Zieh dir das rein!'
111
112
    return make_item(channel_with_site, slug, title)
113
114
115
@pytest.fixture
116
def item_without_url(make_item, channel_without_site: Channel) -> Item:
117
    slug = 'zieh-dir-auch-das-rein'
118
    title = 'Zieh dir auch das rein!'
119
120
    return make_item(channel_without_site, slug, title)
121