tests.integration.announce.irc.test_board   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 468
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 321
dl 0
loc 468
rs 9.68
c 0
b 0
f 0
wmc 34

21 Functions

Rating   Name   Duplication   Size   Complexity  
A creator() 0 3 1
A another_category() 0 5 1
A category() 0 5 1
A test_announce_topic_locked() 0 27 2
A test_announce_topic_unhidden() 0 27 2
A test_announce_topic_hidden() 0 27 2
A test_announce_topic_created() 0 27 2
A test_announce_posting_hidden() 0 29 2
A test_announce_topic_moved() 0 33 2
A test_announce_topic_unlocked() 0 27 2
A moderator() 0 3 1
A _create_topic() 0 10 2
A _create_posting() 0 8 1
A test_announce_posting_created_on_muted_topic() 0 21 2
A test_announce_posting_unhidden() 0 29 2
A topic() 0 7 1
A test_announce_topic_pinned() 0 27 2
A test_announce_topic_unpinned() 0 27 2
A posting() 0 5 1
A _create_category() 0 7 1
A test_announce_posting_created() 0 30 2
1
"""
2
:Copyright: 2006-2021 Jochen Kupperschmidt
3
:License: Revised BSD (see `LICENSE` file for details)
4
"""
5
6
import pytest
7
8
import byceps.announce.connections  # Connect signal handlers.
9
from byceps.events.board import (
10
    BoardPostingCreated,
11
    BoardPostingHidden,
12
    BoardPostingUnhidden,
13
    BoardTopicCreated,
14
    BoardTopicHidden,
15
    BoardTopicLocked,
16
    BoardTopicMoved,
17
    BoardTopicPinned,
18
    BoardTopicUnhidden,
19
    BoardTopicUnlocked,
20
    BoardTopicUnpinned,
21
)
22
from byceps.services.board import (
23
    category_command_service,
24
    posting_command_service,
25
    topic_command_service,
26
)
27
from byceps.signals import board as board_signals
28
29
from .helpers import (
30
    assert_request_data,
31
    assert_submitted_data,
32
    CHANNEL_ORGA_LOG,
33
    CHANNEL_PUBLIC,
34
    get_submitted_json,
35
    mocked_irc_bot,
36
    now,
37
)
38
39
40
def test_announce_topic_created(app, board, topic, creator):
41
    expected_channel1 = CHANNEL_PUBLIC
42
    expected_channel2 = CHANNEL_ORGA_LOG
43
    expected_link = f'http://example.com/board/topics/{topic.id}'
44
    expected_text = (
45
        'TheShadow999 hat im "ACME Entertainment Convention"-Forum '
46
        f'das Thema "Brötchen zum Frühstück" erstellt: {expected_link}'
47
    )
48
49
    event = BoardTopicCreated(
50
        occurred_at=topic.created_at,
51
        initiator_id=creator.id,
52
        initiator_screen_name=creator.screen_name,
53
        board_id=board.id,
54
        topic_id=topic.id,
55
        topic_creator_id=creator.id,
56
        topic_creator_screen_name=creator.screen_name,
57
        topic_title=topic.title,
58
        url=expected_link,
59
    )
60
61
    with mocked_irc_bot() as mock:
62
        board_signals.topic_created.send(None, event=event)
63
64
    actual1, actual2 = get_submitted_json(mock, 2)
65
    assert_request_data(actual1, expected_channel1, expected_text)
66
    assert_request_data(actual2, expected_channel2, expected_text)
67
68
69
def test_announce_topic_hidden(app, board, topic, creator, moderator):
70
    expected_channel = CHANNEL_ORGA_LOG
71
    expected_link = f'http://example.com/board/topics/{topic.id}'
72
    expected_text = (
73
        'ElBosso hat im "ACME Entertainment Convention"-Forum das Thema '
74
        '"Brötchen zum Frühstück" von TheShadow999 '
75
        f'versteckt: {expected_link}'
76
    )
77
78
    event = BoardTopicHidden(
79
        occurred_at=now(),
80
        initiator_id=moderator.id,
81
        initiator_screen_name=moderator.screen_name,
82
        board_id=board.id,
83
        topic_id=topic.id,
84
        topic_creator_id=creator.id,
85
        topic_creator_screen_name=creator.screen_name,
86
        topic_title=topic.title,
87
        moderator_id=moderator.id,
88
        moderator_screen_name=moderator.screen_name,
89
        url=expected_link,
90
    )
91
92
    with mocked_irc_bot() as mock:
93
        board_signals.topic_hidden.send(None, event=event)
94
95
    assert_submitted_data(mock, expected_channel, expected_text)
96
97
98
def test_announce_topic_unhidden(app, board, topic, creator, moderator):
99
    expected_channel = CHANNEL_ORGA_LOG
100
    expected_link = f'http://example.com/board/topics/{topic.id}'
101
    expected_text = (
102
        'ElBosso hat im "ACME Entertainment Convention"-Forum das Thema '
103
        '"Brötchen zum Frühstück" von TheShadow999 '
104
        f'wieder sichtbar gemacht: {expected_link}'
105
    )
106
107
    event = BoardTopicUnhidden(
108
        occurred_at=now(),
109
        initiator_id=moderator.id,
110
        initiator_screen_name=moderator.screen_name,
111
        board_id=board.id,
112
        topic_id=topic.id,
113
        topic_creator_id=creator.id,
114
        topic_creator_screen_name=creator.screen_name,
115
        topic_title=topic.title,
116
        moderator_id=moderator.id,
117
        moderator_screen_name=moderator.screen_name,
118
        url=expected_link,
119
    )
120
121
    with mocked_irc_bot() as mock:
122
        board_signals.topic_unhidden.send(None, event=event)
123
124
    assert_submitted_data(mock, expected_channel, expected_text)
125
126
127
def test_announce_topic_locked(app, board, topic, creator, moderator):
128
    expected_channel = CHANNEL_ORGA_LOG
129
    expected_link = f'http://example.com/board/topics/{topic.id}'
130
    expected_text = (
131
        'ElBosso hat im "ACME Entertainment Convention"-Forum das Thema '
132
        '"Brötchen zum Frühstück" von TheShadow999 '
133
        f'geschlossen: {expected_link}'
134
    )
135
136
    event = BoardTopicLocked(
137
        occurred_at=now(),
138
        initiator_id=moderator.id,
139
        initiator_screen_name=moderator.screen_name,
140
        board_id=board.id,
141
        topic_id=topic.id,
142
        topic_creator_id=creator.id,
143
        topic_creator_screen_name=creator.screen_name,
144
        topic_title=topic.title,
145
        moderator_id=moderator.id,
146
        moderator_screen_name=moderator.screen_name,
147
        url=expected_link,
148
    )
149
150
    with mocked_irc_bot() as mock:
151
        board_signals.topic_locked.send(None, event=event)
152
153
    assert_submitted_data(mock, expected_channel, expected_text)
154
155
156
def test_announce_topic_unlocked(app, board, topic, creator, moderator):
157
    expected_channel = CHANNEL_ORGA_LOG
158
    expected_link = f'http://example.com/board/topics/{topic.id}'
159
    expected_text = (
160
        'ElBosso hat im "ACME Entertainment Convention"-Forum '
161
        'das Thema "Brötchen zum Frühstück" von TheShadow999 '
162
        f'wieder geöffnet: {expected_link}'
163
    )
164
165
    event = BoardTopicUnlocked(
166
        occurred_at=now(),
167
        initiator_id=moderator.id,
168
        initiator_screen_name=moderator.screen_name,
169
        board_id=board.id,
170
        topic_id=topic.id,
171
        topic_creator_id=creator.id,
172
        topic_creator_screen_name=creator.screen_name,
173
        topic_title=topic.title,
174
        moderator_id=moderator.id,
175
        moderator_screen_name=moderator.screen_name,
176
        url=expected_link,
177
    )
178
179
    with mocked_irc_bot() as mock:
180
        board_signals.topic_unlocked.send(None, event=event)
181
182
    assert_submitted_data(mock, expected_channel, expected_text)
183
184
185
def test_announce_topic_pinned(app, board, topic, creator, moderator):
186
    expected_channel = CHANNEL_ORGA_LOG
187
    expected_link = f'http://example.com/board/topics/{topic.id}'
188
    expected_text = (
189
        'ElBosso hat im "ACME Entertainment Convention"-Forum '
190
        'das Thema "Brötchen zum Frühstück" von TheShadow999 '
191
        f'angepinnt: {expected_link}'
192
    )
193
194
    event = BoardTopicPinned(
195
        occurred_at=now(),
196
        initiator_id=moderator.id,
197
        initiator_screen_name=moderator.screen_name,
198
        board_id=board.id,
199
        topic_id=topic.id,
200
        topic_creator_id=creator.id,
201
        topic_creator_screen_name=creator.screen_name,
202
        topic_title=topic.title,
203
        moderator_id=moderator.id,
204
        moderator_screen_name=moderator.screen_name,
205
        url=expected_link,
206
    )
207
208
    with mocked_irc_bot() as mock:
209
        board_signals.topic_pinned.send(None, event=event)
210
211
    assert_submitted_data(mock, expected_channel, expected_text)
212
213
214
def test_announce_topic_unpinned(app, board, topic, creator, moderator):
215
    expected_channel = CHANNEL_ORGA_LOG
216
    expected_link = f'http://example.com/board/topics/{topic.id}'
217
    expected_text = (
218
        'ElBosso hat im "ACME Entertainment Convention"-Forum '
219
        'das Thema "Brötchen zum Frühstück" von TheShadow999 '
220
        f'wieder gelöst: {expected_link}'
221
    )
222
223
    event = BoardTopicUnpinned(
224
        occurred_at=now(),
225
        initiator_id=moderator.id,
226
        initiator_screen_name=moderator.screen_name,
227
        board_id=board.id,
228
        topic_id=topic.id,
229
        topic_creator_id=creator.id,
230
        topic_creator_screen_name=creator.screen_name,
231
        topic_title=topic.title,
232
        moderator_id=moderator.id,
233
        moderator_screen_name=moderator.screen_name,
234
        url=expected_link,
235
    )
236
237
    with mocked_irc_bot() as mock:
238
        board_signals.topic_unpinned.send(None, event=event)
239
240
    assert_submitted_data(mock, expected_channel, expected_text)
241
242
243
def test_announce_topic_moved(
244
    app, board, category, another_category, topic, creator, moderator
245
):
246
    expected_channel = CHANNEL_ORGA_LOG
247
    expected_link = f'http://example.com/board/topics/{topic.id}'
248
    expected_text = (
249
        'ElBosso hat im "ACME Entertainment Convention"-Forum '
250
        'das Thema "Brötchen zum Frühstück" von TheShadow999 '
251
        f'aus "Kategorie 1" in "Kategorie 2" verschoben: {expected_link}'
252
    )
253
254
    event = BoardTopicMoved(
255
        occurred_at=now(),
256
        initiator_id=moderator.id,
257
        initiator_screen_name=moderator.screen_name,
258
        board_id=board.id,
259
        topic_id=topic.id,
260
        topic_creator_id=creator.id,
261
        topic_creator_screen_name=creator.screen_name,
262
        topic_title=topic.title,
263
        old_category_id=category.id,
264
        old_category_title=category.title,
265
        new_category_id=another_category.id,
266
        new_category_title=another_category.title,
267
        moderator_id=moderator.id,
268
        moderator_screen_name=moderator.screen_name,
269
        url=expected_link,
270
    )
271
272
    with mocked_irc_bot() as mock:
273
        board_signals.topic_moved.send(None, event=event)
274
275
    assert_submitted_data(mock, expected_channel, expected_text)
276
277
278
def test_announce_posting_created(app, board, posting, creator):
279
    expected_channel1 = CHANNEL_PUBLIC
280
    expected_channel2 = CHANNEL_ORGA_LOG
281
    expected_link = f'http://example.com/board/postings/{posting.id}'
282
    expected_text = (
283
        'TheShadow999 hat im "ACME Entertainment Convention"-Forum '
284
        'auf das Thema "Brötchen zum Frühstück" '
285
        f'geantwortet: {expected_link}'
286
    )
287
288
    event = BoardPostingCreated(
289
        occurred_at=posting.created_at,
290
        initiator_id=creator.id,
291
        initiator_screen_name=creator.screen_name,
292
        board_id=board.id,
293
        posting_creator_id=creator.id,
294
        posting_creator_screen_name=creator.screen_name,
295
        posting_id=posting.id,
296
        topic_id=posting.topic.id,
297
        topic_title=posting.topic.title,
298
        topic_muted=posting.topic.muted,
299
        url=expected_link,
300
    )
301
302
    with mocked_irc_bot() as mock:
303
        board_signals.posting_created.send(None, event=event)
304
305
    actual1, actual2 = get_submitted_json(mock, 2)
306
    assert_request_data(actual1, expected_channel1, expected_text)
307
    assert_request_data(actual2, expected_channel2, expected_text)
308
309
310
def test_announce_posting_created_on_muted_topic(app, board, posting, creator):
311
    expected_link = f'http://example.com/board/postings/{posting.id}'
312
313
    event = BoardPostingCreated(
314
        occurred_at=posting.created_at,
315
        initiator_id=creator.id,
316
        initiator_screen_name=creator.screen_name,
317
        board_id=board.id,
318
        posting_creator_id=creator.id,
319
        posting_creator_screen_name=creator.screen_name,
320
        posting_id=posting.id,
321
        topic_id=posting.topic.id,
322
        topic_title=posting.topic.title,
323
        topic_muted=True,
324
        url=expected_link,
325
    )
326
327
    with mocked_irc_bot() as mock:
328
        board_signals.posting_created.send(None, event=event)
329
330
    assert not mock.called
331
332
333
def test_announce_posting_hidden(app, board, posting, creator, moderator):
334
    expected_channel = CHANNEL_ORGA_LOG
335
    expected_link = f'http://example.com/board/postings/{posting.id}'
336
    expected_text = (
337
        'ElBosso hat im "ACME Entertainment Convention"-Forum '
338
        'eine Antwort von TheShadow999 '
339
        'im Thema "Brötchen zum Frühstück" '
340
        f'versteckt: {expected_link}'
341
    )
342
343
    event = BoardPostingHidden(
344
        occurred_at=now(),
345
        initiator_id=moderator.id,
346
        initiator_screen_name=moderator.screen_name,
347
        board_id=board.id,
348
        posting_id=posting.id,
349
        posting_creator_id=creator.id,
350
        posting_creator_screen_name=creator.screen_name,
351
        topic_id=posting.topic.id,
352
        topic_title=posting.topic.title,
353
        moderator_id=moderator.id,
354
        moderator_screen_name=moderator.screen_name,
355
        url=expected_link,
356
    )
357
358
    with mocked_irc_bot() as mock:
359
        board_signals.posting_hidden.send(None, event=event)
360
361
    assert_submitted_data(mock, expected_channel, expected_text)
362
363
364
def test_announce_posting_unhidden(app, board, posting, creator, moderator):
365
    expected_channel = CHANNEL_ORGA_LOG
366
    expected_link = f'http://example.com/board/postings/{posting.id}'
367
    expected_text = (
368
        'ElBosso hat im "ACME Entertainment Convention"-Forum '
369
        'eine Antwort von TheShadow999 '
370
        'im Thema "Brötchen zum Frühstück" '
371
        f'wieder sichtbar gemacht: {expected_link}'
372
    )
373
374
    event = BoardPostingUnhidden(
375
        occurred_at=now(),
376
        initiator_id=moderator.id,
377
        initiator_screen_name=moderator.screen_name,
378
        board_id=board.id,
379
        posting_id=posting.id,
380
        posting_creator_id=creator.id,
381
        posting_creator_screen_name=creator.screen_name,
382
        topic_id=posting.topic.id,
383
        topic_title=posting.topic.title,
384
        moderator_id=moderator.id,
385
        moderator_screen_name=moderator.screen_name,
386
        url=expected_link,
387
    )
388
389
    with mocked_irc_bot() as mock:
390
        board_signals.posting_unhidden.send(None, event=event)
391
392
    assert_submitted_data(mock, expected_channel, expected_text)
393
394
395
# helpers
396
397
398
@pytest.fixture(scope='module')
399
def creator(make_user):
400
    return make_user('TheShadow999')
401
402
403
@pytest.fixture(scope='module')
404
def moderator(make_user):
405
    return make_user('ElBosso')
406
407
408
@pytest.fixture(scope='module')
409
def category(board):
410
    category = _create_category(board.id, number=1)
411
    yield category
412
    category_command_service.delete_category(category.id)
413
414
415
@pytest.fixture(scope='module')
416
def another_category(board):
417
    category = _create_category(board.id, number=2)
418
    yield category
419
    category_command_service.delete_category(category.id)
420
421
422
@pytest.fixture(scope='module')
423
def topic(category, creator):
424
    topic = _create_topic(
425
        category.id, creator.id, number=192, title='Brötchen zum Frühstück'
426
    )
427
    yield topic
428
    topic_command_service.delete_topic(topic.id)
429
430
431
@pytest.fixture(scope='module')
432
def posting(topic, creator):
433
    posting = _create_posting(topic.id, creator.id)
434
    yield posting
435
    posting_command_service.delete_posting(posting.id)
436
437
438
def _create_category(board_id, *, number=1):
439
    slug = f'category-{number}'
440
    title = f'Kategorie {number}'
441
    description = f'Hier geht es um Kategorie {number}'
442
443
    return category_command_service.create_category(
444
        board_id, slug, title, description
445
    )
446
447
448
def _create_topic(category_id, creator_id, *, number=1, title=None):
449
    if title is None:
450
        title = f'Thema {number}'
451
    body = f'Inhalt von Thema {number}'
452
453
    topic, _ = topic_command_service.create_topic(
454
        category_id, creator_id, title, body
455
    )
456
457
    return topic
458
459
460
def _create_posting(topic_id, creator_id, *, number=1):
461
    body = f'Inhalt von Beitrag {number}.'
462
463
    posting, event = posting_command_service.create_posting(
464
        topic_id, creator_id, body
465
    )
466
467
    return posting
468