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

tests.integration.announce.discord.test_board.assert_request()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 2
dl 0
loc 9
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 board  # Load signal handlers.
9
from byceps.events.board import BoardPostingCreated, BoardTopicCreated
10
from byceps.services.board import (
11
    category_command_service,
12
    posting_command_service,
13
    topic_command_service,
14
)
15
from byceps.services.webhooks import service as webhook_service
16
from byceps.signals import board as board_signals
17
18
from .helpers import assert_request, mocked_webhook_receiver
19
20
21
WEBHOOK_URL = 'https://webhoooks.test/board'
22
23
24
def test_announce_topic_created(
25
    webhook_settings, admin_app, board, topic, creator
26
):
27
    expected_url = f'https://website.test/board/topics/{topic.id}'
28
    expected_content = (
29
        '[Forum] RocketRandy hat das Thema '
30
        '"Cannot connect to the party network :(" erstellt: '
31
        f'<{expected_url}>'
32
    )
33
34
    event = BoardTopicCreated(
35
        occurred_at=topic.created_at,
36
        initiator_id=creator.id,
37
        initiator_screen_name=creator.screen_name,
38
        board_id=board.id,
39
        topic_id=topic.id,
40
        topic_creator_id=creator.id,
41
        topic_creator_screen_name=creator.screen_name,
42
        topic_title=topic.title,
43
        url=expected_url,
44
    )
45
46
    with mocked_webhook_receiver(WEBHOOK_URL) as mock:
47
        board_signals.topic_created.send(None, event=event)
48
49
    assert_request(mock, expected_content)
50
51
52
def test_announce_posting_created(
53
    webhook_settings, admin_app, board, posting, creator
54
):
55
    expected_url = f'https://website.test/board/postings/{posting.id}'
56
    expected_content = (
57
        '[Forum] RocketRandy hat auf das Thema '
58
        '"Cannot connect to the party network :(" geantwortet: '
59
        f'<{expected_url}>'
60
    )
61
62
    event = BoardPostingCreated(
63
        occurred_at=posting.created_at,
64
        initiator_id=creator.id,
65
        initiator_screen_name=creator.screen_name,
66
        board_id=board.id,
67
        posting_creator_id=creator.id,
68
        posting_creator_screen_name=creator.screen_name,
69
        posting_id=posting.id,
70
        topic_id=posting.topic.id,
71
        topic_title=posting.topic.title,
72
        topic_muted=posting.topic.muted,
73
        url=expected_url,
74
    )
75
76
    with mocked_webhook_receiver(WEBHOOK_URL) as mock:
77
        board_signals.posting_created.send(None, event=event)
78
79
    assert_request(mock, expected_content)
80
81
82
# helpers
83
84
85
@pytest.fixture(scope='module')
86
def webhook_settings(board):
87
    scope = 'board'
88
    scope_id = str(board.id)
89
    format = 'discord'
90
    text_prefix = '[Forum] '
91
    url = WEBHOOK_URL
92
    enabled = True
93
94
    webhook = webhook_service.create_outgoing_webhook(
95
        scope, scope_id, format, url, enabled, text_prefix=text_prefix
96
    )
97
98
    yield
99
100
    webhook_service.delete_outgoing_webhook(webhook.id)
101
102
103
@pytest.fixture(scope='module')
104
def creator(make_user):
105
    return make_user('RocketRandy')
106
107
108
@pytest.fixture(scope='module')
109
def category(board):
110
    slug = 'support'
111
    title = 'Support'
112
    description = f'How can I help you, dear Sir/Madam?'
113
114
    category = category_command_service.create_category(
115
        board.id, slug, title, description
116
    )
117
118
    yield category
119
120
    category_command_service.delete_category(category.id)
121
122
123
@pytest.fixture(scope='module')
124
def topic(category, creator):
125
    title = 'Cannot connect to the party network :('
126
    body = 'I think I did not receive an IP address via DHCP. BUT WHY?!'
127
128
    topic, _ = topic_command_service.create_topic(
129
        category.id, creator.id, title, body
130
    )
131
132
    yield topic
133
134
    topic_command_service.delete_topic(topic.id)
135
136
137
@pytest.fixture(scope='module')
138
def posting(topic, creator):
139
    posting, _ = posting_command_service.create_posting(
140
        topic.id, creator.id, 'This is nice and all, but check out my website!'
141
    )
142
143
    yield posting
144
145
    posting_command_service.delete_posting(posting.id)
146