1
|
|
|
""" |
2
|
|
|
:Copyright: 2006-2020 Jochen Kupperschmidt |
3
|
|
|
:License: Modified BSD, see LICENSE for details. |
4
|
|
|
""" |
5
|
|
|
|
6
|
|
|
from contextlib import contextmanager |
7
|
|
|
|
8
|
|
|
import pytest |
9
|
|
|
from requests_mock import Mocker |
10
|
|
|
|
11
|
|
|
from byceps.announce.discord import board # Load signal handlers. |
12
|
|
|
from byceps.events.board import BoardPostingCreated, BoardTopicCreated |
13
|
|
|
from byceps.services.board import ( |
14
|
|
|
category_command_service, |
15
|
|
|
posting_command_service, |
16
|
|
|
topic_command_service, |
17
|
|
|
) |
18
|
|
|
from byceps.services.brand import ( |
19
|
|
|
service as brand_service, |
20
|
|
|
settings_service as brand_settings_service, |
21
|
|
|
) |
22
|
|
|
from byceps.signals import board as board_signals |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
WEBHOOK_URL = 'https://webhoooks.test/board' |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def test_announce_topic_created(brand_settings, admin_app, topic, creator): |
29
|
|
|
expected_url = f'https://website.test/board/topics/{topic.id}' |
30
|
|
|
expected_content = ( |
31
|
|
|
'[Forum] RocketRandy hat das Thema ' |
32
|
|
|
'"Cannot connect to the party network :(" erstellt: ' |
33
|
|
|
f'<{expected_url}>' |
34
|
|
|
) |
35
|
|
|
|
36
|
|
|
event = BoardTopicCreated( |
37
|
|
|
occurred_at=topic.created_at, |
38
|
|
|
initiator_id=creator.id, |
39
|
|
|
initiator_screen_name=creator.screen_name, |
40
|
|
|
topic_id=topic.id, |
41
|
|
|
topic_creator_id=creator.id, |
42
|
|
|
topic_creator_screen_name=creator.screen_name, |
43
|
|
|
topic_title=topic.title, |
44
|
|
|
url=expected_url, |
45
|
|
|
) |
46
|
|
|
|
47
|
|
|
with mocked_webhook_receiver() as mock: |
48
|
|
|
board_signals.topic_created.send(None, event=event) |
49
|
|
|
|
50
|
|
|
assert_request(mock, expected_content) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def test_announce_posting_created(brand_settings, admin_app, posting, creator): |
54
|
|
|
expected_url = f'https://website.test/board/postings/{posting.id}' |
55
|
|
|
expected_content = ( |
56
|
|
|
'[Forum] RocketRandy hat auf das Thema ' |
57
|
|
|
'"Cannot connect to the party network :(" geantwortet: ' |
58
|
|
|
f'<{expected_url}>' |
59
|
|
|
) |
60
|
|
|
|
61
|
|
|
event = BoardPostingCreated( |
62
|
|
|
occurred_at=posting.created_at, |
63
|
|
|
initiator_id=creator.id, |
64
|
|
|
initiator_screen_name=creator.screen_name, |
65
|
|
|
posting_creator_id=creator.id, |
66
|
|
|
posting_creator_screen_name=creator.screen_name, |
67
|
|
|
posting_id=posting.id, |
68
|
|
|
topic_id=posting.topic.id, |
69
|
|
|
topic_title=posting.topic.title, |
70
|
|
|
topic_muted=posting.topic.muted, |
71
|
|
|
url=expected_url, |
72
|
|
|
) |
73
|
|
|
|
74
|
|
|
with mocked_webhook_receiver() as mock: |
75
|
|
|
board_signals.posting_created.send(None, event=event) |
76
|
|
|
|
77
|
|
|
assert_request(mock, expected_content) |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
# helpers |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
@pytest.fixture(scope='module') |
84
|
|
|
def brand_settings(): |
85
|
|
|
brand_id = 'YOUR-BRAND-HERE' |
86
|
|
|
brand = brand_service.create_brand(brand_id, brand_id) |
87
|
|
|
|
88
|
|
|
name_enabled = 'announce_discord_enabled' |
89
|
|
|
name_webhook_url = 'announce_discord_webhook_url' |
90
|
|
|
|
91
|
|
|
brand_settings_service.create_setting(brand.id, name_enabled, 'true') |
92
|
|
|
brand_settings_service.create_setting( |
93
|
|
|
brand.id, name_webhook_url, WEBHOOK_URL |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
yield |
97
|
|
|
|
98
|
|
|
for name in name_enabled, name_webhook_url: |
99
|
|
|
brand_settings_service.remove_setting(brand.id, name) |
100
|
|
|
|
101
|
|
|
brand_service.delete_brand(brand_id) |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
@pytest.fixture(scope='module') |
105
|
|
|
def creator(make_user): |
106
|
|
|
return make_user('RocketRandy') |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
@pytest.fixture(scope='module') |
110
|
|
|
def category(board): |
111
|
|
|
slug = 'support' |
112
|
|
|
title = 'Support' |
113
|
|
|
description = f'How can I help you, dear Sir/Madam?' |
114
|
|
|
|
115
|
|
|
category = category_command_service.create_category( |
116
|
|
|
board.id, slug, title, description |
117
|
|
|
) |
118
|
|
|
|
119
|
|
|
yield category |
120
|
|
|
|
121
|
|
|
category_command_service.delete_category(category.id) |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
@pytest.fixture(scope='module') |
125
|
|
|
def topic(category, creator): |
126
|
|
|
title = 'Cannot connect to the party network :(' |
127
|
|
|
body = 'I think I did not receive an IP address via DHCP. BUT WHY?!' |
128
|
|
|
|
129
|
|
|
topic, _ = topic_command_service.create_topic( |
130
|
|
|
category.id, creator.id, title, body |
131
|
|
|
) |
132
|
|
|
|
133
|
|
|
yield topic |
134
|
|
|
|
135
|
|
|
topic_command_service.delete_topic(topic.id) |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
@pytest.fixture(scope='module') |
139
|
|
|
def posting(topic, creator): |
140
|
|
|
posting, _ = posting_command_service.create_posting( |
141
|
|
|
topic.id, creator.id, 'This is nice and all, but check out my website!' |
142
|
|
|
) |
143
|
|
|
|
144
|
|
|
yield posting |
145
|
|
|
|
146
|
|
|
posting_command_service.delete_posting(posting.id) |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
@contextmanager |
150
|
|
|
def mocked_webhook_receiver(): |
151
|
|
|
with Mocker() as mock: |
152
|
|
|
mock.post(WEBHOOK_URL) |
153
|
|
|
yield mock |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
def assert_request(mock, expected_content: str) -> None: |
157
|
|
|
assert mock.called |
158
|
|
|
|
159
|
|
|
history = mock.request_history |
160
|
|
|
assert len(history) == 1 |
161
|
|
|
|
162
|
|
|
actual = mock.last_request.json() |
163
|
|
|
|
164
|
|
|
assert actual == {'content': expected_content} |
165
|
|
|
|