Debian Codegraph @

tests/test_plugin_base_formatting.py

raw
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2026, Chris Caron <[email protected]>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 import json
29
30 # Disable logging for a cleaner testing output
31 import logging
32 from random import choice
33 from string import ascii_uppercase as str_alpha, digits as str_num
34 from timeit import default_timer
35 from unittest import mock
36
37 import pytest
38 import requests
39
40 from apprise import (
41 Apprise,
42 AppriseAsset,
43 NotifyBase,
44 NotifyFormat,
45 OverflowMode,
46 )
47 from apprise.exception import AppriseImproperlyConfigured
48
49 logging.disable(logging.CRITICAL)
50
51
52 def assert_body(
53 source: str,
54 offset: int,
55 chunk_body: str,
56 ) -> int:
57 """
58 Assert that `chunk_body` comes from `source` starting at `offset`,
59 accounting for leading/trailing vertical whitespace that _apply_overflow
60 trims away. Returns the updated offset.
61 """
62 segment = source[offset : offset + len(chunk_body)]
63 ws_diff = len(segment) - len(segment.lstrip("\r\n\x0b\x0c").rstrip())
64
65 assert (
66 source[offset : offset + len(chunk_body) + ws_diff]
67 .lstrip("\r\n\x0b\x0c")
68 .rstrip()
69 == chunk_body
70 )
71
72 return offset + len(chunk_body) + ws_diff
73
74
75 def test_notify_overflow_truncate_with_amalgamation():
76 """
77 API: Overflow With Amalgamation Truncate Functionality Testing
78
79 """
80 #
81 # A little preparation
82 #
83
84 # Number of characters per line
85 row = 24
86
87 # Some variables we use to control the data we work with
88 body_len = 1024
89 title_len = 1024
90
91 # Create a large body and title with random data
92 body = "".join(choice(str_alpha + str_num + " ") for _ in range(body_len))
93 body = "\r\n".join([body[i : i + row] for i in range(0, len(body), row)])
94
95 # the new lines add a large amount to our body; lets force the content
96 # back to being 1024 characters.
97 body = body[0:1024]
98
99 # Create our title using random data
100 title = "".join(choice(str_alpha + str_num) for _ in range(title_len))
101
102 #
103 # First Test: Truncated Title
104 #
105 class TestNotification(NotifyBase):
106 # Test title max length
107 title_maxlen = 10
108
109 # With amalgamation
110 overflow_amalgamate_title = True
111
112 def __init__(self, *args, **kwargs):
113 super().__init__(**kwargs)
114
115 def notify(self, *args, **kwargs):
116 # Pretend everything is okay
117 return True
118
119 # We should throw an exception because our specified overflow is wrong.
120 with pytest.raises(AppriseImproperlyConfigured):
121 # Load our object
122 obj = TestNotification(overflow="invalid")
123
124 # Load our object
125 obj = TestNotification(overflow=OverflowMode.TRUNCATE)
126 assert obj is not None
127
128 # Verify that we break the title to a max length of our title_max
129 # and that the body remains untouched
130 chunks = obj._apply_overflow(body="", title=title, overflow=None)
131 chunks = obj._apply_overflow(body="", title="", overflow=None)
132 chunks = obj._apply_overflow(body=body, title="", overflow=None)
133 chunks = obj._apply_overflow(body=body, title=title, overflow=None)
134 chunks = obj._apply_overflow(
135 body=body, title=title, overflow=OverflowMode.SPLIT
136 )
137 assert len(chunks) == 1
138 assert body.lstrip("\r\n\x0b\x0c").rstrip() == chunks[0].get("body")
139 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
140
141 #
142 # Next Test: Line Count Control
143 #
144
145 class TestNotification(NotifyBase):
146 # Test title max length
147 title_maxlen = 5
148
149 # Maximum number of lines
150 body_max_line_count = 5
151
152 # With amalgamation
153 overflow_amalgamate_title = True
154
155 def __init__(self, *args, **kwargs):
156 super().__init__(**kwargs)
157
158 def notify(self, *args, **kwargs):
159 # Pretend everything is okay
160 return True
161
162 # Load our object
163 obj = TestNotification(overflow=OverflowMode.TRUNCATE)
164 assert obj is not None
165
166 # Verify that we break the title to a max length of our title_max
167 # and that the body remains untouched
168 chunks = obj._apply_overflow(body="", title=title)
169 chunks = obj._apply_overflow(body="", title="")
170 chunks = obj._apply_overflow(body=body, title="")
171 chunks = obj._apply_overflow(body=body, title=title)
172 assert len(chunks) == 1
173 assert len(chunks[0].get("body").split("\n")) == obj.body_max_line_count
174 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
175
176 #
177 # Next Test: Truncated body
178 #
179
180 class TestNotification(NotifyBase):
181 # Test title max length
182 title_maxlen = title_len
183
184 # Enforce a body length of just 10
185 body_maxlen = 10
186
187 # With amalgamation
188 overflow_amalgamate_title = True
189
190 def __init__(self, *args, **kwargs):
191 super().__init__(**kwargs)
192
193 def notify(self, *args, **kwargs):
194 # Pretend everything is okay
195 return True
196
197 # Load our object
198 obj = TestNotification(overflow=OverflowMode.TRUNCATE)
199 assert obj is not None
200
201 # Verify that we break the title to a max length of our title_max
202 # and that the body remains untouched
203 chunks = obj._apply_overflow(body="", title=title)
204 chunks = obj._apply_overflow(body="", title="")
205 chunks = obj._apply_overflow(body=body, title="")
206 chunks = obj._apply_overflow(body=body, title=title)
207 assert len(chunks) == 1
208 # Body is lost as the title prevails over everything
209 assert chunks[0].get("body") == ""
210 # Title is not longer then the maximum size the body can be due to
211 # amalgamationflag:
212 assert title[: obj.body_maxlen] == chunks[0].get("title")
213
214 class TestNotification(NotifyBase):
215 # Test title max length
216 title_maxlen = title_len
217
218 # Enforce a body length of just 10
219 body_maxlen = 10
220
221 # With amalgamation
222 overflow_amalgamate_title = True
223
224 def __init__(self, *args, **kwargs):
225 super().__init__(**kwargs)
226
227 def notify(self, *args, **kwargs):
228 # Pretend everything is okay
229 return True
230
231 # Load our object
232 obj = TestNotification(overflow=OverflowMode.TRUNCATE)
233 assert obj is not None
234
235 # Verify that we break the title to a max length of our title_max
236 # and that the body remains untouched
237 chunks = obj._apply_overflow(body="", title=title)
238 chunks = obj._apply_overflow(body="", title="")
239 chunks = obj._apply_overflow(body=body, title="")
240 chunks = obj._apply_overflow(body=body, title=title)
241 assert len(chunks) == 1
242
243 assert chunks[0].get("body") == ""
244 # body_maxlen prevails due to it being smaller and amalgamation flag set
245 assert title[0 : obj.body_maxlen] == chunks[0].get("title")
246
247 #
248 # Next Test: Append title to body + Truncated body
249 #
250
251 class TestNotification(NotifyBase):
252 # Enforce no title
253 title_maxlen = 0
254
255 # Enforce a body length of just 100
256 body_maxlen = 100
257
258 # With amalgamation
259 overflow_amalgamate_title = True
260
261 def __init__(self, *args, **kwargs):
262 super().__init__(**kwargs)
263
264 def notify(self, *args, **kwargs):
265 # Pretend everything is okay
266 return True
267
268 # Load our object
269 obj = TestNotification(overflow=OverflowMode.TRUNCATE)
270 assert obj is not None
271
272 # Verify that we break the title to a max length of our title_max
273 # and that the body remains untouched
274
275 obj.notify_format = NotifyFormat.HTML
276 chunks = obj._apply_overflow(body="", title=title)
277 chunks = obj._apply_overflow(body="", title="")
278 chunks = obj._apply_overflow(body=body, title="")
279 chunks = obj._apply_overflow(body=body, title=title)
280 assert len(chunks) == 1
281
282 obj.notify_format = NotifyFormat.MARKDOWN
283 chunks = obj._apply_overflow(body="", title=title)
284 chunks = obj._apply_overflow(body="", title="")
285 chunks = obj._apply_overflow(body=body, title="")
286 chunks = obj._apply_overflow(body=body, title=title)
287 assert len(chunks) == 1
288
289 obj.notify_format = NotifyFormat.TEXT
290 chunks = obj._apply_overflow(body="", title=title)
291 chunks = obj._apply_overflow(body="", title="")
292 chunks = obj._apply_overflow(body=body, title="")
293 chunks = obj._apply_overflow(body=body, title=title)
294 assert len(chunks) == 1
295
296 # The below line should be read carefully... We're actually testing to see
297 # that our title is matched against our body. Behind the scenes, the title
298 # was appended to the body. The body was then truncated to the maxlen.
299 # The thing is, since the title is so large, all of the body was lost
300 # and a good chunk of the title was too. The message sent will just be a
301 # small portion of the title
302 assert len(chunks[0].get("body")) == obj.body_maxlen
303 assert title[0 : obj.body_maxlen] == chunks[0].get("body")
304
305
306 def test_notify_overflow_truncate_no_amalgamation():
307 """
308 API: Overflow No Amalgamation Truncate Functionality Testing
309
310 """
311 #
312 # A little preparation
313 #
314
315 # Number of characters per line
316 row = 24
317
318 # Some variables we use to control the data we work with
319 body_len = 1024
320 title_len = 1024
321
322 # Create a large body and title with random data
323 body = "".join(choice(str_alpha + str_num + " ") for _ in range(body_len))
324 body = "\r\n".join([body[i : i + row] for i in range(0, len(body), row)])
325
326 # the new lines add a large amount to our body; lets force the content
327 # back to being 1024 characters.
328 body = body[0:1024]
329
330 # Create our title using random data
331 title = "".join(choice(str_alpha + str_num) for _ in range(title_len))
332
333 #
334 # First Test: Truncated Title
335 #
336 class TestNotification(NotifyBase):
337 # Test title max length
338 title_maxlen = 10
339
340 # No amalgamation
341 overflow_amalgamate_title = False
342
343 def __init__(self, *args, **kwargs):
344 super().__init__(**kwargs)
345
346 def notify(self, *args, **kwargs):
347 # Pretend everything is okay
348 return True
349
350 # We should throw an exception because our specified overflow is wrong.
351 with pytest.raises(AppriseImproperlyConfigured):
352 # Load our object
353 obj = TestNotification(overflow="invalid")
354
355 # Load our object
356 obj = TestNotification(overflow=OverflowMode.TRUNCATE)
357 assert obj is not None
358
359 # Verify that we break the title to a max length of our title_max
360 # and that the body remains untouched
361 chunks = obj._apply_overflow(body="", title=title, overflow=None)
362 chunks = obj._apply_overflow(body="", title="", overflow=None)
363 chunks = obj._apply_overflow(body=body, title="", overflow=None)
364 chunks = obj._apply_overflow(body=body, title=title, overflow=None)
365 chunks = obj._apply_overflow(
366 body=body, title=title, overflow=OverflowMode.SPLIT
367 )
368 assert len(chunks) == 1
369 assert body.lstrip("\r\n\x0b\x0c").rstrip() == chunks[0].get("body")
370 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
371
372 #
373 # Next Test: Line Count Control
374 #
375
376 class TestNotification(NotifyBase):
377 # Test title max length
378 title_maxlen = 5
379
380 # Maximum number of lines
381 body_max_line_count = 5
382
383 # No amalgamation
384 overflow_amalgamate_title = False
385
386 def __init__(self, *args, **kwargs):
387 super().__init__(**kwargs)
388
389 def notify(self, *args, **kwargs):
390 # Pretend everything is okay
391 return True
392
393 # Load our object
394 obj = TestNotification(overflow=OverflowMode.TRUNCATE)
395 assert obj is not None
396
397 # Verify that we break the title to a max length of our title_max
398 # and that the body remains untouched
399 chunks = obj._apply_overflow(body="", title=title)
400 chunks = obj._apply_overflow(body="", title="")
401 chunks = obj._apply_overflow(body=body, title="")
402 chunks = obj._apply_overflow(body=body, title=title)
403 assert len(chunks) == 1
404 assert len(chunks[0].get("body").split("\n")) == obj.body_max_line_count
405 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
406
407 #
408 # Next Test: Truncated body
409 #
410
411 class TestNotification(NotifyBase):
412 # Test title max length
413 title_maxlen = title_len
414
415 # Enforce a body length of just 10
416 body_maxlen = 10
417
418 # No amalgamation
419 overflow_amalgamate_title = False
420
421 def __init__(self, *args, **kwargs):
422 super().__init__(**kwargs)
423
424 def notify(self, *args, **kwargs):
425 # Pretend everything is okay
426 return True
427
428 # Load our object
429 obj = TestNotification(overflow=OverflowMode.TRUNCATE)
430 assert obj is not None
431
432 # Verify that we break the title to a max length of our title_max
433 # and that the body remains untouched
434 chunks = obj._apply_overflow(body="", title=title)
435 chunks = obj._apply_overflow(body="", title="")
436 chunks = obj._apply_overflow(body=body, title="")
437 chunks = obj._apply_overflow(body=body, title=title)
438 assert len(chunks) == 1
439 assert body[0 : obj.body_maxlen].lstrip("\r\n\x0b\x0c").rstrip() == chunks[
440 0
441 ].get("body")
442 assert title == chunks[0].get("title")
443
444 class TestNotification(NotifyBase):
445 # Test title max length
446 title_maxlen = title_len
447
448 # Enforce a body length of just 10
449 body_maxlen = 10
450
451 # No amalgamation
452 overflow_amalgamate_title = False
453
454 def __init__(self, *args, **kwargs):
455 super().__init__(**kwargs)
456
457 def notify(self, *args, **kwargs):
458 # Pretend everything is okay
459 return True
460
461 # Load our object
462 obj = TestNotification(overflow=OverflowMode.TRUNCATE)
463 assert obj is not None
464
465 # Verify that we break the title to a max length of our title_max
466 # and that the body remains untouched
467 chunks = obj._apply_overflow(body="", title=title)
468 chunks = obj._apply_overflow(body="", title="")
469 chunks = obj._apply_overflow(body=body, title="")
470 chunks = obj._apply_overflow(body=body, title=title)
471 assert len(chunks) == 1
472 # No amalgamation set so our body aligns in size (no -2 like previous
473 # test)
474 assert body[0 : obj.body_maxlen].lstrip("\r\n\x0b\x0c").rstrip() == chunks[
475 0
476 ].get("body")
477 assert title == chunks[0].get("title")
478
479 #
480 # Next Test: Append title to body + Truncated body
481 #
482
483 class TestNotification(NotifyBase):
484 # Enforce no title
485 title_maxlen = 0
486
487 # Enforce a body length of just 100
488 body_maxlen = 100
489
490 # No amalgamation
491 overflow_amalgamate_title = False
492
493 def __init__(self, *args, **kwargs):
494 super().__init__(**kwargs)
495
496 def notify(self, *args, **kwargs):
497 # Pretend everything is okay
498 return True
499
500 # Load our object
501 obj = TestNotification(overflow=OverflowMode.TRUNCATE)
502 assert obj is not None
503
504 # Verify that we break the title to a max length of our title_max
505 # and that the body remains untouched
506
507 obj.notify_format = NotifyFormat.HTML
508 chunks = obj._apply_overflow(body="", title=title)
509 chunks = obj._apply_overflow(body="", title="")
510 chunks = obj._apply_overflow(body=body, title="")
511 chunks = obj._apply_overflow(body=body, title=title)
512 assert len(chunks) == 1
513
514 obj.notify_format = NotifyFormat.MARKDOWN
515 chunks = obj._apply_overflow(body="", title=title)
516 chunks = obj._apply_overflow(body="", title="")
517 chunks = obj._apply_overflow(body=body, title="")
518 chunks = obj._apply_overflow(body=body, title=title)
519 assert len(chunks) == 1
520
521 obj.notify_format = NotifyFormat.TEXT
522 chunks = obj._apply_overflow(body="", title=title)
523 chunks = obj._apply_overflow(body="", title="")
524 chunks = obj._apply_overflow(body=body, title="")
525 chunks = obj._apply_overflow(body=body, title=title)
526 assert len(chunks) == 1
527
528 # The below line should be read carefully... We're actually testing to see
529 # that our title is matched against our body. Behind the scenes, the title
530 # was appended to the body. The body was then truncated to the maxlen.
531 # The thing is, since the title is so large, all of the body was lost
532 # and a good chunk of the title was too. The message sent will just be a
533 # small portion of the title
534 assert len(chunks[0].get("body")) == obj.body_maxlen
535 assert title[0 : obj.body_maxlen] == chunks[0].get("body")
536
537
538 def test_notify_overflow_split_with_amalgamation():
539 """
540 API: Overflow With Amalgamation Splits Functionality Testing
541
542 """
543
544 #
545 # A little preparation
546 #
547
548 # Number of characters per line
549 row = 24
550
551 # Some variables we use to control the data we work with
552 body_len = 1024
553 title_len = 1024
554
555 # Create a large body and title with random data
556 body = "".join(choice(str_alpha + str_num) for _ in range(body_len))
557 body = "\r\n".join([body[i : i + row] for i in range(0, len(body), row)])
558
559 # the new lines add a large amount to our body; lets force the content
560 # back to being 1024 characters.
561 body = body[0:1024]
562
563 # Create our title using random data
564 title = "".join(choice(str_alpha + str_num) for _ in range(title_len))
565
566 #
567 # First Test: Truncated Title
568 #
569 class TestNotification(NotifyBase):
570 # Test title max length
571 title_maxlen = 10
572
573 # With amalgamation
574 overflow_amalgamate_title = True
575
576 def __init__(self, *args, **kwargs):
577 super().__init__(**kwargs)
578
579 def notify(self, *args, **kwargs):
580 # Pretend everything is okay
581 return True
582
583 # Load our object
584 obj = TestNotification(overflow=OverflowMode.SPLIT)
585 assert obj is not None
586
587 # Verify that we break the title to a max length of our title_max
588 # and that the body remains untouched
589 chunks = obj._apply_overflow(body="", title=title)
590 chunks = obj._apply_overflow(body="", title="")
591 chunks = obj._apply_overflow(body=body, title="")
592 chunks = obj._apply_overflow(body=body, title=title)
593 assert len(chunks) == 1
594 assert body.lstrip("\r\n\x0b\x0c").rstrip() == chunks[0].get("body")
595 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
596
597 #
598 # Next Test: Line Count Control
599 #
600
601 class TestNotification(NotifyBase):
602 # Test title max length
603 title_maxlen = 5
604
605 # Maximum number of lines
606 body_max_line_count = 5
607
608 # With amalgamation
609 overflow_amalgamate_title = True
610
611 def __init__(self, *args, **kwargs):
612 super().__init__(**kwargs)
613
614 def notify(self, *args, **kwargs):
615 # Pretend everything is okay
616 return True
617
618 # Load our object
619 obj = TestNotification(overflow=OverflowMode.SPLIT)
620 assert obj is not None
621
622 # Verify that we break the title to a max length of our title_max
623 # and that the body remains untouched
624 chunks = obj._apply_overflow(body="", title=title)
625 chunks = obj._apply_overflow(body="", title="")
626 chunks = obj._apply_overflow(body=body, title="")
627 chunks = obj._apply_overflow(body=body, title=title)
628 assert len(chunks) == 1
629 assert len(chunks[0].get("body").split("\n")) == obj.body_max_line_count
630 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
631
632 #
633 # Next Test: Split body
634 #
635
636 class TestNotification(NotifyBase):
637 # Test title max length
638 title_maxlen = title_len
639
640 # Enforce a body length. Make sure it's an int.
641 body_maxlen = int(body_len / 4)
642
643 # With amalgamation
644 overflow_amalgamate_title = True
645
646 def __init__(self, *args, **kwargs):
647 super().__init__(**kwargs)
648
649 def notify(self, *args, **kwargs):
650 # Pretend everything is okay
651 return True
652
653 # Load our object
654 obj = TestNotification(overflow=OverflowMode.SPLIT)
655 assert obj is not None
656
657 # Verify that we break the title to a max length of our title_max
658 # and that the body remains untouched
659 chunks = obj._apply_overflow(body="", title=title)
660 chunks = obj._apply_overflow(body="", title="")
661 chunks = obj._apply_overflow(body=body, title="")
662 chunks = obj._apply_overflow(body=body, title=title)
663
664 offset = 0
665 for idx, chunk in enumerate(chunks, start=1):
666 # Verification
667 assert len(chunk.get("title")) <= obj.title_maxlen
668 assert len(chunk.get("body")) <= obj.body_maxlen
669
670 # No counter is displayed because our title is so enormous
671 # We switch to a display title on first message only
672 if idx > 1:
673 # Empty (no title displayed on following entries
674 assert chunk.get("title") == ""
675 else:
676 # The length of the body prevails our title due to it being
677 # so much smaller then our title length
678 assert len(chunk.get("title")) == obj.body_maxlen
679 assert title[: obj.body_maxlen] == chunk.get("title")
680
681 # Our body is only broken up; not lost
682 offset = assert_body(
683 body,
684 offset,
685 chunk.get("body"),
686 )
687
688 # Another edge case where the title just isn't that long leaving
689 # a lot of space for the [xx/xx] entries (no truncation needed)
690 chunks = obj._apply_overflow(body="", title=title)
691 chunks = obj._apply_overflow(body="", title="")
692 chunks = obj._apply_overflow(body=body, title="")
693 chunks = obj._apply_overflow(body=body, title=title[:20])
694 c_len = len(" [X/X]")
695
696 offset = 0
697 for idx, chunk in enumerate(chunks, start=1):
698 # Verification
699 assert len(chunk.get("title")) <= obj.title_maxlen
700 assert len(chunk.get("body")) <= obj.body_maxlen
701
702 # Our title has a counter added to it
703 assert title[:20] == chunk.get("title")[:-c_len]
704 assert chunk.get("title")[-c_len:] == f" [{idx:01}/{len(chunks):01}]"
705
706 # Our body is only broken up; not lost
707 offset = assert_body(
708 body,
709 offset,
710 chunk.get("body"),
711 )
712
713 #
714 # Test forcing overflow_display_title_once
715 #
716 class TestNotification(NotifyBase):
717 # Test title max length
718 title_maxlen = title_len
719
720 # Enforce a body length. Make sure it's an int.
721 body_maxlen = int(body_len / 4)
722
723 # With amalgamation
724 overflow_amalgamate_title = True
725
726 # Only display title once
727 overflow_display_title_once = True
728
729 def __init__(self, *args, **kwargs):
730 super().__init__(**kwargs)
731
732 def notify(self, *args, **kwargs):
733 # Pretend everything is okay
734 return True
735
736 # Load our object
737 obj = TestNotification(overflow=OverflowMode.SPLIT)
738 assert obj is not None
739
740 chunks = obj._apply_overflow(body="", title=title)
741 chunks = obj._apply_overflow(body="", title="")
742 chunks = obj._apply_overflow(body=body, title="")
743 chunks = obj._apply_overflow(body=body, title=title)
744
745 offset = 0
746 for idx, chunk in enumerate(chunks, start=1):
747 # Verification
748 assert len(chunk.get("title")) <= obj.title_maxlen
749 assert len(chunk.get("body")) <= obj.body_maxlen
750
751 # Our title has no counter added to it
752 if idx > 1:
753 # Empty (no title displayed on following entries
754 assert chunk.get("title") == ""
755 else:
756 # The body length prevails due to our amalgamation flag
757 assert len(chunk.get("title")) == obj.body_maxlen
758 assert title[: obj.body_maxlen] == chunk.get("title")
759
760 # Our body is only broken up; not lost
761 offset = assert_body(
762 body,
763 offset,
764 chunk.get("body"),
765 )
766
767 # Test larger messages
768 # and that the body remains untouched
769 class TestNotification(NotifyBase):
770 # Test title max length
771 title_maxlen = 150
772
773 # Enforce a body length. Make sure it's an int.
774 body_maxlen = 400
775
776 # With amalgamation
777 overflow_amalgamate_title = True
778
779 def __init__(self, *args, **kwargs):
780 super().__init__(**kwargs)
781
782 def notify(self, *args, **kwargs):
783 # Pretend everything is okay
784 return True
785
786 # Load our object
787 obj = TestNotification(overflow=OverflowMode.SPLIT)
788 assert obj is not None
789
790 new_body = body * 500
791 chunks = obj._apply_overflow(body="", title=title)
792 chunks = obj._apply_overflow(body="", title="")
793 chunks = obj._apply_overflow(body=new_body, title="")
794 chunks = obj._apply_overflow(body=new_body, title=title)
795 c_len = len(" [XXXX/XXXX]")
796
797 offset = 0
798 for idx, chunk in enumerate(chunks, start=1):
799 # Verification
800 assert len(chunk.get("title")) <= obj.title_maxlen
801 assert len(chunk.get("body")) <= obj.body_maxlen
802
803 # Our title has a counter added to it
804 assert (
805 title[: obj.title_maxlen][:-c_len] == chunk.get("title")[:-c_len]
806 )
807 assert chunk.get("title")[-c_len:] == f" [{idx:04}/{len(chunks):04}]"
808
809 # Our body is only broken up; not lost
810 offset = assert_body(
811 new_body,
812 offset,
813 chunk.get("body"),
814 )
815
816 # Body chunk is beyond 4 digits, so [XXXX/XXXX] is turned off
817 new_body = body * 2500
818 chunks = obj._apply_overflow(body="", title=title)
819 chunks = obj._apply_overflow(body="", title="")
820 chunks = obj._apply_overflow(body=new_body, title="")
821 chunks = obj._apply_overflow(body=new_body, title=title)
822
823 offset = 0
824 for _idx, chunk in enumerate(chunks, start=1):
825 # Verification
826 assert len(chunk.get("title")) <= obj.title_maxlen
827 assert len(chunk.get("body")) <= obj.body_maxlen
828
829 # Our title has no counter added to it
830 assert title[: obj.title_maxlen] == chunk.get("title")
831
832 # Our body is only broken up; not lost
833 offset = assert_body(
834 new_body,
835 offset,
836 chunk.get("body"),
837 )
838
839 # Test larger messages
840 # and that the body remains untouched
841 class TestNotification(NotifyBase):
842 # Test title max length
843 title_maxlen = 150
844
845 # Enforce a body length. Make sure it's an int.
846 body_maxlen = 150
847
848 # With amalgamation
849 overflow_amalgamate_title = True
850
851 def __init__(self, *args, **kwargs):
852 super().__init__(**kwargs)
853
854 def notify(self, *args, **kwargs):
855 # Pretend everything is okay
856 return True
857
858 # Load our object
859 obj = TestNotification(overflow=OverflowMode.SPLIT)
860 assert obj is not None
861
862 new_body = body * 5
863 chunks = obj._apply_overflow(body="", title=title)
864 chunks = obj._apply_overflow(body="", title="")
865 chunks = obj._apply_overflow(body=new_body, title="")
866 chunks = obj._apply_overflow(body=new_body, title=title)
867
868 offset = 0
869 for idx, chunk in enumerate(chunks, start=1):
870 # Verification
871 assert len(chunk.get("title")) <= obj.title_maxlen
872 assert len(chunk.get("body")) <= obj.body_maxlen
873
874 # Our title has no counter added to it
875 if idx > 1:
876 # Empty (no title displayed on following entries
877 assert chunk.get("title") == ""
878 else:
879 # Because 150 is what we set the title limit to
880 assert len(chunk.get("title")) == obj.title_maxlen
881 assert title[: obj.title_maxlen] == chunk.get("title")
882
883 # Our body is only broken up; not lost
884 offset = assert_body(
885 new_body,
886 offset,
887 chunk.get("body"),
888 )
889
890 #
891 # Next Test: Append title to body + split body
892 #
893
894 class TestNotification(NotifyBase):
895 # Enforce no title
896 title_maxlen = 0
897
898 # Enforce a body length based on the title. Make sure it's an int.
899 body_maxlen = int(title_len / 4)
900
901 # With amalgamation
902 overflow_amalgamate_title = True
903
904 def __init__(self, *args, **kwargs):
905 super().__init__(**kwargs)
906
907 def notify(self, *args, **kwargs):
908 # Pretend everything is okay
909 return True
910
911 # Load our object
912 obj = TestNotification(overflow=OverflowMode.SPLIT)
913 assert obj is not None
914
915 # Verify that we break the title to a max length of our title_max
916 # and that the body remains untouched
917 chunks = obj._apply_overflow(body="", title=title)
918 chunks = obj._apply_overflow(body="", title="")
919 chunks = obj._apply_overflow(body=body, title="")
920 chunks = obj._apply_overflow(body=body, title=title)
921
922 # Our final product is that our title has been appended to our body to
923 # create one great big body. As a result we'll get quite a few lines back
924 # now.
925 offset = 0
926
927 # Our body will look like this in small chunks at the end of the day
928 bulk = title + "\r\n" + body
929
930 # Due to the new line added to the end
931 assert len(chunks) == (
932 # wrap division in int() so Python 3 doesn't convert it to a float on
933 # us
934 int(len(bulk) / obj.body_maxlen)
935 + (1 if len(bulk) % obj.body_maxlen else 0)
936 )
937
938 for chunk in chunks:
939 # Verification
940 assert len(chunk.get("title")) == 0
941 assert len(chunk.get("body")) <= obj.body_maxlen
942
943 # Our title is empty every time
944 assert chunk.get("title") == ""
945
946 # Our body is only broken up; not lost
947 offset = assert_body(
948 bulk,
949 offset,
950 chunk.get("body"),
951 )
952
953 #
954 # Test case where our title_len is shorter then the value
955 # that would otherwise trigger the [XX/XX] elements
956 #
957
958 class TestNotification(NotifyBase):
959 # Set a small title length
960 title_maxlen = 100
961
962 # Enforce a body length. Make sure it's an int.
963 body_maxlen = int(body_len / 4)
964
965 # With amalgamation
966 overflow_amalgamate_title = True
967
968 def __init__(self, *args, **kwargs):
969 super().__init__(**kwargs)
970
971 def notify(self, *args, **kwargs):
972 # Pretend everything is okay
973 return True
974
975 # Load our object
976 obj = TestNotification(overflow=OverflowMode.SPLIT)
977 assert obj is not None
978
979 # Verify that we break the title to a max length of our title_max
980 # and that the body remains untouched
981 chunks = obj._apply_overflow(body="", title=title)
982 chunks = obj._apply_overflow(body="", title="")
983 chunks = obj._apply_overflow(body=body, title="")
984 chunks = obj._apply_overflow(body=body, title=title)
985
986 offset = 0
987 for _idx, chunk in enumerate(chunks, start=1):
988 # Verification
989 assert len(chunk.get("title")) <= obj.title_maxlen
990 assert len(chunk.get("body")) <= obj.body_maxlen
991
992 # Our title is truncated and no counter added
993 assert title[: obj.title_maxlen] == chunk.get("title")
994
995 # Our body is only broken up; not lost
996 offset = assert_body(
997 body,
998 offset,
999 chunk.get("body"),
1000 )
1001
1002 #
1003 # Scenario where the title length is larger than the body
1004 #
1005
1006 class TestNotification(NotifyBase):
1007 # Set a small title length
1008 title_maxlen = 100
1009
1010 # Enforce a body length. Make sure it's an int.
1011 body_maxlen = 50
1012
1013 # With amalgamation
1014 overflow_amalgamate_title = True
1015
1016 def __init__(self, *args, **kwargs):
1017 super().__init__(**kwargs)
1018
1019 def notify(self, *args, **kwargs):
1020 # Pretend everything is okay
1021 return True
1022
1023 # Load our object
1024 obj = TestNotification(overflow=OverflowMode.SPLIT)
1025 assert obj is not None
1026
1027 # Verify that we break the title to a max length of our title_max
1028 # and that the body remains untouched
1029 chunks = obj._apply_overflow(body="", title=title)
1030 chunks = obj._apply_overflow(body="", title="")
1031 chunks = obj._apply_overflow(body=body, title="")
1032 chunks = obj._apply_overflow(body=body, title=title)
1033
1034 offset = 0
1035 for idx, chunk in enumerate(chunks, start=1):
1036 # Verification
1037 assert len(chunk.get("title")) <= obj.title_maxlen
1038 assert len(chunk.get("body")) <= obj.body_maxlen
1039
1040 # Our title has no counter added to it due to it's length
1041 if idx > 1:
1042 # Empty (no title displayed on following entries
1043 assert chunk.get("title") == ""
1044 else:
1045 # Because 150 is what we set the title limit to 50 due
1046 # to amalamation. The lowest value always prevails
1047 assert len(chunk.get("title")) == obj.body_maxlen
1048 assert title[: obj.body_maxlen] == chunk.get("title")
1049
1050 # Our body is only broken up; not lost
1051 offset = assert_body(
1052 body,
1053 offset,
1054 chunk.get("body"),
1055 )
1056
1057
1058 def test_notify_overflow_split_with_amalgamation_force_title_always():
1059 """
1060 API: Overflow With Amalgamation (title alaways Split Functionality Testing
1061
1062 """
1063
1064 #
1065 # A little preparation
1066 #
1067
1068 # Number of characters per line
1069 row = 24
1070
1071 # Some variables we use to control the data we work with
1072 body_len = 1024
1073 title_len = 1024
1074
1075 # Create a large body and title with random data
1076 body = "".join(choice(str_alpha + str_num) for _ in range(body_len))
1077 body = "\r\n".join([body[i : i + row] for i in range(0, len(body), row)])
1078
1079 # the new lines add a large amount to our body; lets force the content
1080 # back to being 1024 characters.
1081 body = body[0:1024]
1082
1083 # Create our title using random data
1084 title = "".join(choice(str_alpha + str_num) for _ in range(title_len))
1085
1086 #
1087 # First Test: Truncated Title
1088 #
1089 class TestNotification(NotifyBase):
1090 # Test title max length
1091 title_maxlen = 10
1092
1093 # With amalgamation
1094 overflow_amalgamate_title = True
1095
1096 # Force title to be displayed always
1097 overflow_display_title_once = False
1098
1099 def __init__(self, *args, **kwargs):
1100 super().__init__(**kwargs)
1101
1102 def notify(self, *args, **kwargs):
1103 # Pretend everything is okay
1104 return True
1105
1106 # Load our object
1107 obj = TestNotification(overflow=OverflowMode.SPLIT)
1108 assert obj is not None
1109
1110 # Verify that we break the title to a max length of our title_max
1111 # and that the body remains untouched
1112 chunks = obj._apply_overflow(body="", title=title)
1113 chunks = obj._apply_overflow(body="", title="")
1114 chunks = obj._apply_overflow(body=body, title="")
1115 chunks = obj._apply_overflow(body=body, title=title)
1116 assert len(chunks) == 1
1117 assert body.lstrip("\r\n\x0b\x0c").rstrip() == chunks[0].get("body")
1118 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
1119
1120 #
1121 # Next Test: Line Count Control
1122 #
1123
1124 class TestNotification(NotifyBase):
1125 # Test title max length
1126 title_maxlen = 5
1127
1128 # Maximum number of lines
1129 body_max_line_count = 5
1130
1131 # With amalgamation
1132 overflow_amalgamate_title = True
1133
1134 # Force title to be displayed always
1135 overflow_display_title_once = False
1136
1137 def __init__(self, *args, **kwargs):
1138 super().__init__(**kwargs)
1139
1140 def notify(self, *args, **kwargs):
1141 # Pretend everything is okay
1142 return True
1143
1144 # Load our object
1145 obj = TestNotification(overflow=OverflowMode.SPLIT)
1146 assert obj is not None
1147
1148 # Verify that we break the title to a max length of our title_max
1149 # and that the body remains untouched
1150 chunks = obj._apply_overflow(body="", title=title)
1151 chunks = obj._apply_overflow(body="", title="")
1152 chunks = obj._apply_overflow(body=body, title="")
1153 chunks = obj._apply_overflow(body=body, title=title)
1154 assert len(chunks) == 1
1155 assert len(chunks[0].get("body").split("\n")) == obj.body_max_line_count
1156 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
1157
1158 #
1159 # Next Test: Split body
1160 #
1161
1162 class TestNotification(NotifyBase):
1163 # Test title max length
1164 title_maxlen = title_len
1165
1166 # Enforce a body length. Make sure it's an int.
1167 body_maxlen = int(body_len / 4)
1168
1169 # With amalgamation
1170 overflow_amalgamate_title = True
1171
1172 # Force title to be displayed always
1173 overflow_display_title_once = False
1174
1175 def __init__(self, *args, **kwargs):
1176 super().__init__(**kwargs)
1177
1178 def notify(self, *args, **kwargs):
1179 # Pretend everything is okay
1180 return True
1181
1182 # Load our object
1183 obj = TestNotification(overflow=OverflowMode.SPLIT)
1184 assert obj is not None
1185
1186 # Verify that we break the title to a max length of our title_max
1187 # and that the body remains untouched
1188 chunks = obj._apply_overflow(body="", title=title)
1189 chunks = obj._apply_overflow(body="", title="")
1190 chunks = obj._apply_overflow(body=body, title="")
1191 chunks = obj._apply_overflow(body=body, title=title)
1192
1193 offset = 0
1194 for idx, chunk in enumerate(chunks, start=1):
1195 # Verification
1196 assert len(chunk.get("title")) <= obj.title_maxlen
1197 assert len(chunk.get("body")) <= obj.body_maxlen
1198
1199 # No counter is displayed because our title is so enormous
1200 # We switch to a display title on first message only
1201 if idx > 1:
1202 # Empty (no title displayed on following entries
1203 assert chunk.get("title") == ""
1204 else:
1205 # The length of the body prevails our title due to it being
1206 # so much smaller then our title length
1207 assert len(chunk.get("title")) == obj.body_maxlen
1208 assert title[: obj.body_maxlen] == chunk.get("title")
1209
1210 # Our body is only broken up; not lost
1211 offset = assert_body(
1212 body,
1213 offset,
1214 chunk.get("body"),
1215 )
1216
1217 # Another edge case where the title just isn't that long leaving
1218 # a lot of space for the [xx/xx] entries (no truncation needed)
1219 chunks = obj._apply_overflow(body="", title=title)
1220 chunks = obj._apply_overflow(body="", title="")
1221 chunks = obj._apply_overflow(body=body, title="")
1222 chunks = obj._apply_overflow(body=body, title=title[:20])
1223 offset = 0
1224 c_len = len(" [X/X]")
1225 assert len(chunks) == 5
1226 for idx, chunk in enumerate(chunks, start=1):
1227 # Verification
1228 assert len(chunk.get("title")) <= obj.title_maxlen
1229 assert len(chunk.get("body")) <= obj.body_maxlen
1230
1231 # Our title has a counter added to it
1232 assert title[:20] == chunk.get("title")[:-c_len]
1233 assert chunk.get("title")[-c_len:] == f" [{idx:01}/{len(chunks):01}]"
1234
1235 # Our body is only broken up; not lost
1236 offset = assert_body(
1237 body,
1238 offset,
1239 chunk.get("body"),
1240 )
1241
1242 #
1243 # Test forcing overflow_display_title_once
1244 #
1245 class TestNotification(NotifyBase):
1246 # Test title max length
1247 title_maxlen = title_len
1248
1249 # Enforce a body length. Make sure it's an int.
1250 body_maxlen = int(body_len / 4)
1251
1252 # With amalgamation
1253 overflow_amalgamate_title = True
1254
1255 # Force title to be displayed always
1256 overflow_display_title_once = False
1257
1258 def __init__(self, *args, **kwargs):
1259 super().__init__(**kwargs)
1260
1261 def notify(self, *args, **kwargs):
1262 # Pretend everything is okay
1263 return True
1264
1265 # Load our object
1266 obj = TestNotification(overflow=OverflowMode.SPLIT)
1267 assert obj is not None
1268
1269 chunks = obj._apply_overflow(body="", title=title)
1270 chunks = obj._apply_overflow(body="", title="")
1271 chunks = obj._apply_overflow(body=body, title="")
1272 chunks = obj._apply_overflow(body=body, title=title)
1273
1274 offset = 0
1275 for idx, chunk in enumerate(chunks, start=1):
1276 # Verification
1277 assert len(chunk.get("title")) <= obj.title_maxlen
1278 assert len(chunk.get("body")) <= obj.body_maxlen
1279
1280 # Our title has no counter added to it
1281 if idx > 1:
1282 # Empty (no title displayed on following entries
1283 assert chunk.get("title") == ""
1284 else:
1285 # The body length prevails due to our amalgamation flag
1286 assert len(chunk.get("title")) == obj.body_maxlen
1287 assert title[: obj.body_maxlen] == chunk.get("title")
1288
1289 # Our body is only broken up; not lost
1290 offset = assert_body(
1291 body,
1292 offset,
1293 chunk.get("body"),
1294 )
1295
1296 # Test larger messages
1297 # and that the body remains untouched
1298 class TestNotification(NotifyBase):
1299 # Test title max length
1300 title_maxlen = 150
1301
1302 # Enforce a body length. Make sure it's an int.
1303 body_maxlen = 400
1304
1305 # With amalgamation
1306 overflow_amalgamate_title = True
1307
1308 # Force title to be displayed always
1309 overflow_display_title_once = False
1310
1311 def __init__(self, *args, **kwargs):
1312 super().__init__(**kwargs)
1313
1314 def notify(self, *args, **kwargs):
1315 # Pretend everything is okay
1316 return True
1317
1318 # Load our object
1319 obj = TestNotification(overflow=OverflowMode.SPLIT)
1320 assert obj is not None
1321
1322 new_body = body * 500
1323 chunks = obj._apply_overflow(body="", title=title)
1324 chunks = obj._apply_overflow(body="", title="")
1325 chunks = obj._apply_overflow(body=new_body, title="")
1326 chunks = obj._apply_overflow(body=new_body, title=title)
1327 c_len = len(" [XXXX/XXXX]")
1328
1329 offset = 0
1330 for idx, chunk in enumerate(chunks, start=1):
1331 # Verification
1332 assert len(chunk.get("title")) <= obj.title_maxlen
1333 assert len(chunk.get("body")) <= obj.body_maxlen
1334
1335 # Our title has a counter added to it
1336 assert (
1337 title[: obj.title_maxlen][:-c_len] == chunk.get("title")[:-c_len]
1338 )
1339 assert chunk.get("title")[-c_len:] == f" [{idx:04}/{len(chunks):04}]"
1340
1341 # Our body is only broken up; not lost
1342 offset = assert_body(
1343 new_body,
1344 offset,
1345 chunk.get("body"),
1346 )
1347
1348 # Body chunk is beyond 4 digits, so [XXXX/XXXX] is turned off
1349 new_body = body * 2500
1350 chunks = obj._apply_overflow(body="", title=title)
1351 chunks = obj._apply_overflow(body="", title="")
1352 chunks = obj._apply_overflow(body=new_body, title="")
1353 chunks = obj._apply_overflow(body=new_body, title=title)
1354
1355 offset = 0
1356 for _idx, chunk in enumerate(chunks, start=1):
1357 # Verification
1358 assert len(chunk.get("title")) <= obj.title_maxlen
1359 assert len(chunk.get("body")) <= obj.body_maxlen
1360
1361 # Our title has no counter added to it
1362 assert title[: obj.title_maxlen] == chunk.get("title")
1363
1364 # Our body is only broken up; not lost
1365 offset = assert_body(
1366 new_body,
1367 offset,
1368 chunk.get("body"),
1369 )
1370
1371 # Test larger messages
1372 # and that the body remains untouched
1373 class TestNotification(NotifyBase):
1374 # Test title max length
1375 title_maxlen = 150
1376
1377 # Enforce a body length. Make sure it's an int.
1378 body_maxlen = 150
1379
1380 # With amalgamation
1381 overflow_amalgamate_title = True
1382
1383 # Force title to be displayed always
1384 overflow_display_title_once = False
1385
1386 def __init__(self, *args, **kwargs):
1387 super().__init__(**kwargs)
1388
1389 def notify(self, *args, **kwargs):
1390 # Pretend everything is okay
1391 return True
1392
1393 # Load our object
1394 obj = TestNotification(overflow=OverflowMode.SPLIT)
1395 assert obj is not None
1396
1397 new_body = body * 5
1398 chunks = obj._apply_overflow(body="", title=title)
1399 chunks = obj._apply_overflow(body="", title="")
1400 chunks = obj._apply_overflow(body=new_body, title="")
1401 chunks = obj._apply_overflow(body=new_body, title=title)
1402
1403 # overflow_display_title_once whle set to False is still ignored
1404 # because our title_maxlen > body_maxlen and a full title was
1405 # provided
1406 offset = 0
1407 for idx, chunk in enumerate(chunks, start=1):
1408 # Verification
1409 assert len(chunk.get("title")) <= obj.title_maxlen
1410 assert len(chunk.get("body")) <= obj.body_maxlen
1411
1412 # Our title has no counter added to it
1413 if idx > 1:
1414 # Empty (no title displayed on following entries
1415 assert chunk.get("title") == ""
1416 else:
1417 # Because 150 is what we set the title limit to
1418 assert len(chunk.get("title")) == obj.title_maxlen
1419 assert title[: obj.title_maxlen] == chunk.get("title")
1420
1421 # Our body is only broken up; not lost
1422 offset = assert_body(
1423 new_body,
1424 offset,
1425 chunk.get("body"),
1426 )
1427
1428 chunks = obj._apply_overflow(body=new_body, title=title)
1429 offset = 0
1430 # overflow_display_title_once whle set to False is still ignored
1431 # because our title_maxlen > body_maxlen and a full title was
1432 # provided
1433 for idx, chunk in enumerate(chunks, start=1):
1434 # Verification
1435 assert len(chunk.get("title")) <= obj.title_maxlen
1436 assert len(chunk.get("body")) <= obj.body_maxlen
1437
1438 # Our title has no counter added to it
1439 if idx > 1:
1440 # Empty (no title displayed on following entries
1441 assert chunk.get("title") == ""
1442 else:
1443 # Because 150 is what we set the title limit to
1444 assert len(chunk.get("title")) == obj.title_maxlen
1445 assert title[: obj.title_maxlen] == chunk.get("title")
1446
1447 # Our body is only broken up; not lost
1448 offset = assert_body(
1449 new_body,
1450 offset,
1451 chunk.get("body"),
1452 )
1453
1454 #
1455 # Run again but with a smaller title
1456 #
1457 chunks = obj._apply_overflow(body=new_body, title=title[:30])
1458 # overflow_display_title_once whle set to False is still ignored
1459 # because our body_maxlen (after title has been calculated with it)
1460 # is less then the overflow_display_count_threshold; hence a message
1461 # must be a certain minimum size in order to kick in
1462 offset = 0
1463 for _idx, chunk in enumerate(chunks, start=1):
1464 # Verification
1465 assert len(chunk.get("title")) <= obj.title_maxlen
1466 assert len(chunk.get("body")) <= obj.body_maxlen
1467
1468 # Our title doesn't change
1469 assert len(chunk.get("title")) == 30
1470 assert title[:30] == chunk.get("title")
1471
1472 # Our body is only broken up; not lost
1473 offset = assert_body(
1474 new_body,
1475 offset,
1476 chunk.get("body"),
1477 )
1478
1479 #
1480 # Next Test: Append title to body + split body
1481 #
1482
1483 class TestNotification(NotifyBase):
1484 # Enforce no title
1485 title_maxlen = 0
1486
1487 # Enforce a body length based on the title. Make sure it's an int.
1488 body_maxlen = int(title_len / 4)
1489
1490 # With amalgamation
1491 overflow_amalgamate_title = True
1492
1493 # Force title to be displayed always
1494 overflow_display_title_once = False
1495
1496 def __init__(self, *args, **kwargs):
1497 super().__init__(**kwargs)
1498
1499 def notify(self, *args, **kwargs):
1500 # Pretend everything is okay
1501 return True
1502
1503 # Load our object
1504 obj = TestNotification(overflow=OverflowMode.SPLIT)
1505 assert obj is not None
1506
1507 # Verify that we break the title to a max length of our title_max
1508 # and that the body remains untouched
1509 chunks = obj._apply_overflow(body="", title=title)
1510 chunks = obj._apply_overflow(body="", title="")
1511 chunks = obj._apply_overflow(body=body, title="")
1512 chunks = obj._apply_overflow(body=body, title=title)
1513
1514 # Our final product is that our title has been appended to our body to
1515 # create one great big body. As a result we'll get quite a few lines back
1516 # now.
1517 offset = 0
1518
1519 # Our body will look like this in small chunks at the end of the day
1520 bulk = title + "\r\n" + body
1521
1522 # Due to the new line added to the end
1523 assert len(chunks) == (
1524 # wrap division in int() so Python 3 doesn't convert it to a float on
1525 # us
1526 int(len(bulk) / obj.body_maxlen)
1527 + (1 if len(bulk) % obj.body_maxlen else 0)
1528 )
1529
1530 for chunk in chunks:
1531 # Verification
1532 assert len(chunk.get("title")) == 0
1533 assert len(chunk.get("body")) <= obj.body_maxlen
1534
1535 # Our title is empty every time
1536 assert chunk.get("title") == ""
1537
1538 # Our body is only broken up; not lost
1539 offset = assert_body(
1540 bulk,
1541 offset,
1542 chunk.get("body"),
1543 )
1544
1545 #
1546 # Test case where our title_len is shorter then the value
1547 # that would otherwise trigger the [XX/XX] elements
1548 #
1549
1550 class TestNotification(NotifyBase):
1551 # Set a small title length
1552 title_maxlen = 100
1553
1554 # Enforce a body length. Make sure it's an int.
1555 body_maxlen = int(body_len / 4)
1556
1557 # With amalgamation
1558 overflow_amalgamate_title = True
1559
1560 # Force title to be displayed always
1561 overflow_display_title_once = False
1562
1563 def __init__(self, *args, **kwargs):
1564 super().__init__(**kwargs)
1565
1566 def notify(self, *args, **kwargs):
1567 # Pretend everything is okay
1568 return True
1569
1570 # Load our object
1571 obj = TestNotification(overflow=OverflowMode.SPLIT)
1572 assert obj is not None
1573
1574 # Verify that we break the title to a max length of our title_max
1575 # and that the body remains untouched
1576 chunks = obj._apply_overflow(body="", title=title)
1577 chunks = obj._apply_overflow(body="", title="")
1578 chunks = obj._apply_overflow(body=body, title="")
1579 chunks = obj._apply_overflow(body=body, title=title)
1580
1581 offset = 0
1582 for _idx, chunk in enumerate(chunks, start=1):
1583 # Verification
1584 assert len(chunk.get("title")) <= obj.title_maxlen
1585 assert len(chunk.get("body")) <= obj.body_maxlen
1586
1587 # Our title is truncated and no counter added
1588 assert title[: obj.title_maxlen] == chunk.get("title")
1589
1590 # Our body is only broken up; not lost
1591 offset = assert_body(
1592 body,
1593 offset,
1594 chunk.get("body"),
1595 )
1596
1597 #
1598 # Scenario where the title length is larger than the body
1599 #
1600
1601 class TestNotification(NotifyBase):
1602 # Set a small title length
1603 title_maxlen = 100
1604
1605 # Enforce a body length. Make sure it's an int.
1606 body_maxlen = 50
1607
1608 # With amalgamation
1609 overflow_amalgamate_title = True
1610
1611 # Force title to be displayed always
1612 overflow_display_title_once = False
1613
1614 def __init__(self, *args, **kwargs):
1615 super().__init__(**kwargs)
1616
1617 def notify(self, *args, **kwargs):
1618 # Pretend everything is okay
1619 return True
1620
1621 # Load our object
1622 obj = TestNotification(overflow=OverflowMode.SPLIT)
1623 assert obj is not None
1624
1625 # Verify that we break the title to a max length of our title_max
1626 # and that the body remains untouched
1627 chunks = obj._apply_overflow(body="", title=title)
1628 chunks = obj._apply_overflow(body="", title="")
1629 chunks = obj._apply_overflow(body=body, title="")
1630 chunks = obj._apply_overflow(body=body, title=title)
1631
1632 offset = 0
1633
1634 # overflow_display_title_once whle set to False is still ignored
1635 # because our title_maxlen > body_maxlen and a full title was
1636 # provided
1637 for idx, chunk in enumerate(chunks, start=1):
1638 # Verification
1639 assert len(chunk.get("title")) <= obj.title_maxlen
1640 assert len(chunk.get("body")) <= obj.body_maxlen
1641
1642 # Our title has no counter added to it due to it's length
1643 if idx > 1:
1644 # Empty (no title displayed on following entries
1645 assert chunk.get("title") == ""
1646 else:
1647 # Because 150 is what we set the title limit to 50 due
1648 # to amalamation. The lowest value always prevails
1649 assert len(chunk.get("title")) == obj.body_maxlen
1650 assert title[: obj.body_maxlen] == chunk.get("title")
1651
1652 # Our body is only broken up; not lost
1653 offset = assert_body(
1654 body,
1655 offset,
1656 chunk.get("body"),
1657 )
1658
1659
1660 def test_notify_overflow_split_with_amalgamation_force_title_once():
1661 """
1662 API: Overflow With Amalgamation (title once) Split Functionality Testing
1663
1664 """
1665
1666 #
1667 # A little preparation
1668 #
1669
1670 # Number of characters per line
1671 row = 24
1672
1673 # Some variables we use to control the data we work with
1674 body_len = 1024
1675 title_len = 1024
1676
1677 # Create a large body and title with random data
1678 body = "".join(choice(str_alpha + str_num) for _ in range(body_len))
1679 body = "\r\n".join([body[i : i + row] for i in range(0, len(body), row)])
1680
1681 # the new lines add a large amount to our body; lets force the content
1682 # back to being 1024 characters.
1683 body = body[0:1024]
1684
1685 # Create our title using random data
1686 title = "".join(choice(str_alpha + str_num) for _ in range(title_len))
1687
1688 #
1689 # First Test: Truncated Title
1690 #
1691 class TestNotification(NotifyBase):
1692 # Test title max length
1693 title_maxlen = 10
1694
1695 # With amalgamation
1696 overflow_amalgamate_title = True
1697
1698 # Force title displayed once
1699 overflow_display_title_once = True
1700
1701 def __init__(self, *args, **kwargs):
1702 super().__init__(**kwargs)
1703
1704 def notify(self, *args, **kwargs):
1705 # Pretend everything is okay
1706 return True
1707
1708 # Load our object
1709 obj = TestNotification(overflow=OverflowMode.SPLIT)
1710 assert obj is not None
1711
1712 # Verify that we break the title to a max length of our title_max
1713 # and that the body remains untouched
1714 chunks = obj._apply_overflow(body="", title=title)
1715 chunks = obj._apply_overflow(body="", title="")
1716 chunks = obj._apply_overflow(body=body, title="")
1717 chunks = obj._apply_overflow(body=body, title=title)
1718 assert len(chunks) == 1
1719 assert body.lstrip("\r\n\x0b\x0c").rstrip() == chunks[0].get("body")
1720 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
1721
1722 #
1723 # Next Test: Line Count Control
1724 #
1725
1726 class TestNotification(NotifyBase):
1727 # Test title max length
1728 title_maxlen = 5
1729
1730 # Maximum number of lines
1731 body_max_line_count = 5
1732
1733 # With amalgamation
1734 overflow_amalgamate_title = True
1735
1736 # Force title displayed once
1737 overflow_display_title_once = True
1738
1739 def __init__(self, *args, **kwargs):
1740 super().__init__(**kwargs)
1741
1742 def notify(self, *args, **kwargs):
1743 # Pretend everything is okay
1744 return True
1745
1746 # Load our object
1747 obj = TestNotification(overflow=OverflowMode.SPLIT)
1748 assert obj is not None
1749
1750 # Verify that we break the title to a max length of our title_max
1751 # and that the body remains untouched
1752 chunks = obj._apply_overflow(body="", title=title)
1753 chunks = obj._apply_overflow(body="", title="")
1754 chunks = obj._apply_overflow(body=body, title="")
1755 chunks = obj._apply_overflow(body=body, title=title)
1756 assert len(chunks) == 1
1757 assert len(chunks[0].get("body").split("\n")) == obj.body_max_line_count
1758 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
1759
1760 #
1761 # Next Test: Split body
1762 #
1763
1764 class TestNotification(NotifyBase):
1765 # Test title max length
1766 title_maxlen = title_len
1767
1768 # Enforce a body length. Make sure it's an int.
1769 body_maxlen = int(body_len / 4)
1770
1771 # With amalgamation
1772 overflow_amalgamate_title = True
1773
1774 # Force title displayed once
1775 overflow_display_title_once = True
1776
1777 def __init__(self, *args, **kwargs):
1778 super().__init__(**kwargs)
1779
1780 def notify(self, *args, **kwargs):
1781 # Pretend everything is okay
1782 return True
1783
1784 # Load our object
1785 obj = TestNotification(overflow=OverflowMode.SPLIT)
1786 assert obj is not None
1787
1788 # Verify that we break the title to a max length of our title_max
1789 # and that the body remains untouched
1790 chunks = obj._apply_overflow(body="", title=title)
1791 chunks = obj._apply_overflow(body="", title="")
1792 chunks = obj._apply_overflow(body=body, title="")
1793 chunks = obj._apply_overflow(body=body, title=title)
1794
1795 offset = 0
1796 for idx, chunk in enumerate(chunks, start=1):
1797 # Verification
1798 assert len(chunk.get("title")) <= obj.title_maxlen
1799 assert len(chunk.get("body")) <= obj.body_maxlen
1800
1801 # No counter is displayed because our title is so enormous
1802 # We switch to a display title on first message only
1803 if idx > 1:
1804 # Empty (no title displayed on following entries
1805 assert chunk.get("title") == ""
1806 else:
1807 # The length of the body prevails our title due to it being
1808 # so much smaller then our title length
1809 assert len(chunk.get("title")) == obj.body_maxlen
1810 assert title[: obj.body_maxlen] == chunk.get("title")
1811
1812 # Our body is only broken up; not lost
1813 offset = assert_body(
1814 body,
1815 offset,
1816 chunk.get("body"),
1817 )
1818
1819 # Another edge case where the title just isn't that long leaving
1820 # a lot of space for the [xx/xx] entries (no truncation needed)
1821 chunks = obj._apply_overflow(body="", title=title)
1822 chunks = obj._apply_overflow(body="", title="")
1823 chunks = obj._apply_overflow(body=body, title="")
1824 chunks = obj._apply_overflow(body=body, title=title[:20])
1825 offset = 0
1826 assert len(chunks) == 5
1827 for idx, chunk in enumerate(chunks, start=1):
1828 # Verification
1829 assert len(chunk.get("title")) <= obj.title_maxlen
1830 assert len(chunk.get("body")) <= obj.body_maxlen
1831
1832 # Our title has no counter added to it
1833 if idx > 1:
1834 # Empty (no title displayed on following entries
1835 assert chunk.get("title") == ""
1836
1837 else:
1838 # The body length prevails due to our amalgamation flag
1839 assert len(chunk.get("title")) == 20
1840 assert title[:20] == chunk.get("title")
1841
1842 # Our body is only broken up; not lost
1843 offset = assert_body(
1844 body,
1845 offset,
1846 chunk.get("body"),
1847 )
1848
1849 #
1850 # Test forcing overflow_display_title_once
1851 #
1852 class TestNotification(NotifyBase):
1853 # Test title max length
1854 title_maxlen = title_len
1855
1856 # Enforce a body length. Make sure it's an int.
1857 body_maxlen = int(body_len / 4)
1858
1859 # With amalgamation
1860 overflow_amalgamate_title = True
1861
1862 # Only display title once
1863 overflow_display_title_once = True
1864
1865 def __init__(self, *args, **kwargs):
1866 super().__init__(**kwargs)
1867
1868 def notify(self, *args, **kwargs):
1869 # Pretend everything is okay
1870 return True
1871
1872 # Load our object
1873 obj = TestNotification(overflow=OverflowMode.SPLIT)
1874 assert obj is not None
1875
1876 chunks = obj._apply_overflow(body="", title=title)
1877 chunks = obj._apply_overflow(body="", title="")
1878 chunks = obj._apply_overflow(body=body, title="")
1879 chunks = obj._apply_overflow(body=body, title=title)
1880
1881 offset = 0
1882 for idx, chunk in enumerate(chunks, start=1):
1883 # Verification
1884 assert len(chunk.get("title")) <= obj.title_maxlen
1885 assert len(chunk.get("body")) <= obj.body_maxlen
1886
1887 # Our title has no counter added to it
1888 if idx > 1:
1889 # Empty (no title displayed on following entries
1890 assert chunk.get("title") == ""
1891 else:
1892 # The body length prevails due to our amalgamation flag
1893 assert len(chunk.get("title")) == obj.body_maxlen
1894 assert title[: obj.body_maxlen] == chunk.get("title")
1895
1896 # Our body is only broken up; not lost
1897 offset = assert_body(
1898 body,
1899 offset,
1900 chunk.get("body"),
1901 )
1902
1903 # Test larger messages
1904 # and that the body remains untouched
1905 class TestNotification(NotifyBase):
1906 # Test title max length
1907 title_maxlen = 150
1908
1909 # Enforce a body length. Make sure it's an int.
1910 body_maxlen = 400
1911
1912 # With amalgamation
1913 overflow_amalgamate_title = True
1914
1915 # Force title displayed once
1916 overflow_display_title_once = True
1917
1918 def __init__(self, *args, **kwargs):
1919 super().__init__(**kwargs)
1920
1921 def notify(self, *args, **kwargs):
1922 # Pretend everything is okay
1923 return True
1924
1925 # Load our object
1926 obj = TestNotification(overflow=OverflowMode.SPLIT)
1927 assert obj is not None
1928
1929 new_body = body * 500
1930 chunks = obj._apply_overflow(body="", title=title)
1931 chunks = obj._apply_overflow(body="", title="")
1932 chunks = obj._apply_overflow(body=new_body, title="")
1933 chunks = obj._apply_overflow(body=new_body, title=title)
1934
1935 offset = 0
1936 for idx, chunk in enumerate(chunks, start=1):
1937 # Verification
1938 assert len(chunk.get("title")) <= obj.title_maxlen
1939 assert len(chunk.get("body")) <= obj.body_maxlen
1940
1941 # Our title has no counter added to it
1942 if idx > 1:
1943 # Empty (no title displayed on following entries
1944 assert chunk.get("title") == ""
1945 else:
1946 # The body length prevails due to our amalgamation flag
1947 assert len(chunk.get("title")) == obj.title_maxlen
1948 assert title[: obj.title_maxlen] == chunk.get("title")
1949 # Our body is only broken up; not lost
1950 offset = assert_body(
1951 new_body,
1952 offset,
1953 chunk.get("body"),
1954 )
1955
1956 # Body chunk is beyond 4 digits, so [XXXX/XXXX] is turned off
1957 new_body = body * 2500
1958 chunks = obj._apply_overflow(body="", title=title)
1959 chunks = obj._apply_overflow(body="", title="")
1960 chunks = obj._apply_overflow(body=new_body, title="")
1961 chunks = obj._apply_overflow(body=new_body, title=title)
1962
1963 offset = 0
1964 for idx, chunk in enumerate(chunks, start=1):
1965 # Verification
1966 assert len(chunk.get("title")) <= obj.title_maxlen
1967 assert len(chunk.get("body")) <= obj.body_maxlen
1968
1969 # Our title has no counter added to it
1970 if idx > 1:
1971 # Empty (no title displayed on following entries
1972 assert chunk.get("title") == ""
1973 else:
1974 # The body length prevails due to our amalgamation flag
1975 assert len(chunk.get("title")) == obj.title_maxlen
1976 assert title[: obj.title_maxlen] == chunk.get("title")
1977
1978 # Our body is only broken up; not lost
1979 offset = assert_body(
1980 new_body,
1981 offset,
1982 chunk.get("body"),
1983 )
1984
1985 # Test larger messages
1986 # and that the body remains untouched
1987 class TestNotification(NotifyBase):
1988 # Test title max length
1989 title_maxlen = 150
1990
1991 # Enforce a body length. Make sure it's an int.
1992 body_maxlen = 150
1993
1994 # With amalgamation
1995 overflow_amalgamate_title = True
1996
1997 # Force title displayed once
1998 overflow_display_title_once = True
1999
2000 def __init__(self, *args, **kwargs):
2001 super().__init__(**kwargs)
2002
2003 def notify(self, *args, **kwargs):
2004 # Pretend everything is okay
2005 return True
2006
2007 # Load our object
2008 obj = TestNotification(overflow=OverflowMode.SPLIT)
2009 assert obj is not None
2010
2011 new_body = body * 5
2012 chunks = obj._apply_overflow(body="", title=title)
2013 chunks = obj._apply_overflow(body="", title="")
2014 chunks = obj._apply_overflow(body=new_body, title="")
2015 chunks = obj._apply_overflow(body=new_body, title=title)
2016
2017 offset = 0
2018 for idx, chunk in enumerate(chunks, start=1):
2019 # Verification
2020 assert len(chunk.get("title")) <= obj.title_maxlen
2021 assert len(chunk.get("body")) <= obj.body_maxlen
2022
2023 # Our title has no counter added to it
2024 if idx > 1:
2025 # Empty (no title displayed on following entries
2026 assert chunk.get("title") == ""
2027 else:
2028 # Because 150 is what we set the title limit to
2029 assert len(chunk.get("title")) == obj.title_maxlen
2030 assert title[: obj.title_maxlen] == chunk.get("title")
2031
2032 # Our body is only broken up; not lost
2033 offset = assert_body(
2034 new_body,
2035 offset,
2036 chunk.get("body"),
2037 )
2038
2039 #
2040 # Next Test: Append title to body + split body
2041 #
2042
2043 class TestNotification(NotifyBase):
2044 # Enforce no title
2045 title_maxlen = 0
2046
2047 # Enforce a body length based on the title. Make sure it's an int.
2048 body_maxlen = int(title_len / 4)
2049
2050 # With amalgamation
2051 overflow_amalgamate_title = True
2052
2053 # Force title displayed once
2054 overflow_display_title_once = True
2055
2056 def __init__(self, *args, **kwargs):
2057 super().__init__(**kwargs)
2058
2059 def notify(self, *args, **kwargs):
2060 # Pretend everything is okay
2061 return True
2062
2063 # Load our object
2064 obj = TestNotification(overflow=OverflowMode.SPLIT)
2065 assert obj is not None
2066
2067 # Verify that we break the title to a max length of our title_max
2068 # and that the body remains untouched
2069 chunks = obj._apply_overflow(body="", title=title)
2070 chunks = obj._apply_overflow(body="", title="")
2071 chunks = obj._apply_overflow(body=body, title="")
2072 chunks = obj._apply_overflow(body=body, title=title)
2073
2074 # Our final product is that our title has been appended to our body to
2075 # create one great big body. As a result we'll get quite a few lines back
2076 # now.
2077 offset = 0
2078
2079 # Our body will look like this in small chunks at the end of the day
2080 bulk = title + "\r\n" + body
2081
2082 # Due to the new line added to the end
2083 assert len(chunks) == (
2084 # wrap division in int() so Python 3 doesn't convert it to a float on
2085 # us
2086 int(len(bulk) / obj.body_maxlen)
2087 + (1 if len(bulk) % obj.body_maxlen else 0)
2088 )
2089
2090 for chunk in chunks:
2091 # Verification
2092 assert len(chunk.get("title")) == 0
2093 assert len(chunk.get("body")) <= obj.body_maxlen
2094
2095 # Our title is empty every time
2096 assert chunk.get("title") == ""
2097
2098 # Our body is only broken up; not lost
2099 offset = assert_body(
2100 bulk,
2101 offset,
2102 chunk.get("body"),
2103 )
2104
2105 #
2106 # Test case where our title_len is shorter then the value
2107 # that would otherwise trigger the [XX/XX] elements
2108 #
2109
2110 class TestNotification(NotifyBase):
2111 # Set a small title length
2112 title_maxlen = 100
2113
2114 # Enforce a body length. Make sure it's an int.
2115 body_maxlen = int(body_len / 4)
2116
2117 # With amalgamation
2118 overflow_amalgamate_title = True
2119
2120 # Force title displayed once
2121 overflow_display_title_once = True
2122
2123 def __init__(self, *args, **kwargs):
2124 super().__init__(**kwargs)
2125
2126 def notify(self, *args, **kwargs):
2127 # Pretend everything is okay
2128 return True
2129
2130 # Load our object
2131 obj = TestNotification(overflow=OverflowMode.SPLIT)
2132 assert obj is not None
2133
2134 # Verify that we break the title to a max length of our title_max
2135 # and that the body remains untouched
2136 chunks = obj._apply_overflow(body="", title=title)
2137 chunks = obj._apply_overflow(body="", title="")
2138 chunks = obj._apply_overflow(body=body, title="")
2139 chunks = obj._apply_overflow(body=body, title=title)
2140
2141 offset = 0
2142 for idx, chunk in enumerate(chunks, start=1):
2143 # Verification
2144 assert len(chunk.get("title")) <= obj.title_maxlen
2145 assert len(chunk.get("body")) <= obj.body_maxlen
2146
2147 # Our title has no counter added to it
2148 if idx > 1:
2149 # Empty (no title displayed on following entries
2150 assert chunk.get("title") == ""
2151 else:
2152 # The body length prevails due to our amalgamation flag
2153 assert len(chunk.get("title")) == obj.title_maxlen
2154 assert title[: obj.title_maxlen] == chunk.get("title")
2155
2156 # Our body is only broken up; not lost
2157 offset = assert_body(
2158 body,
2159 offset,
2160 chunk.get("body"),
2161 )
2162
2163 #
2164 # Scenario where the title length is larger than the body
2165 #
2166
2167 class TestNotification(NotifyBase):
2168 # Set a small title length
2169 title_maxlen = 100
2170
2171 # Enforce a body length. Make sure it's an int.
2172 body_maxlen = 50
2173
2174 # With amalgamation
2175 overflow_amalgamate_title = True
2176
2177 # Force title displayed once
2178 overflow_display_title_once = True
2179
2180 def __init__(self, *args, **kwargs):
2181 super().__init__(**kwargs)
2182
2183 def notify(self, *args, **kwargs):
2184 # Pretend everything is okay
2185 return True
2186
2187 # Load our object
2188 obj = TestNotification(overflow=OverflowMode.SPLIT)
2189 assert obj is not None
2190
2191 # Verify that we break the title to a max length of our title_max
2192 # and that the body remains untouched
2193 chunks = obj._apply_overflow(body="", title=title)
2194 chunks = obj._apply_overflow(body="", title="")
2195 chunks = obj._apply_overflow(body=body, title="")
2196 chunks = obj._apply_overflow(body=body, title=title)
2197
2198 offset = 0
2199 for idx, chunk in enumerate(chunks, start=1):
2200 # Verification
2201 assert len(chunk.get("title")) <= obj.title_maxlen
2202 assert len(chunk.get("body")) <= obj.body_maxlen
2203
2204 # Our title has no counter added to it due to it's length
2205 if idx > 1:
2206 # Empty (no title displayed on following entries
2207 assert chunk.get("title") == ""
2208 else:
2209 # Because 150 is what we set the title limit to 50 due
2210 # to amalamation. The lowest value always prevails
2211 assert len(chunk.get("title")) == obj.body_maxlen
2212 assert title[: obj.body_maxlen] == chunk.get("title")
2213
2214 # Our body is only broken up; not lost
2215 offset = assert_body(
2216 body,
2217 offset,
2218 chunk.get("body"),
2219 )
2220
2221
2222 def test_notify_overflow_split_no_amalgamation():
2223 """
2224 API: Overflow No Amalgamation Splits Functionality Testing
2225
2226 """
2227
2228 #
2229 # A little preparation
2230 #
2231
2232 # Number of characters per line
2233 row = 24
2234
2235 # Some variables we use to control the data we work with
2236 body_len = 1024
2237 title_len = 1024
2238
2239 # Create a large body and title with random data
2240 body = "".join(choice(str_alpha + str_num) for _ in range(body_len))
2241 body = "\r\n".join([body[i : i + row] for i in range(0, len(body), row)])
2242
2243 # the new lines add a large amount to our body; lets force the content
2244 # back to being 1024 characters.
2245 body = body[0:1024]
2246
2247 # Create our title using random data
2248 title = "".join(choice(str_alpha + str_num) for _ in range(title_len))
2249
2250 #
2251 # First Test: Truncated Title
2252 #
2253 class TestNotification(NotifyBase):
2254 # Test title max length
2255 title_maxlen = 10
2256
2257 # No amalgamation
2258 overflow_amalgamate_title = False
2259
2260 def __init__(self, *args, **kwargs):
2261 super().__init__(**kwargs)
2262
2263 def notify(self, *args, **kwargs):
2264 # Pretend everything is okay
2265 return True
2266
2267 # Load our object
2268 obj = TestNotification(overflow=OverflowMode.SPLIT)
2269 assert obj is not None
2270
2271 # Verify that we break the title to a max length of our title_max
2272 # and that the body remains untouched
2273 chunks = obj._apply_overflow(body="", title=title)
2274 chunks = obj._apply_overflow(body="", title="")
2275 chunks = obj._apply_overflow(body=body, title="")
2276 chunks = obj._apply_overflow(body=body, title=title)
2277 assert len(chunks) == 1
2278 assert body.lstrip("\r\n\x0b\x0c").rstrip() == chunks[0].get("body")
2279 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
2280
2281 #
2282 # Next Test: Line Count Control
2283 #
2284
2285 class TestNotification(NotifyBase):
2286 # Test title max length
2287 title_maxlen = 5
2288
2289 # Maximum number of lines
2290 body_max_line_count = 5
2291
2292 # No amalgamation
2293 overflow_amalgamate_title = False
2294
2295 def __init__(self, *args, **kwargs):
2296 super().__init__(**kwargs)
2297
2298 def notify(self, *args, **kwargs):
2299 # Pretend everything is okay
2300 return True
2301
2302 # Load our object
2303 obj = TestNotification(overflow=OverflowMode.SPLIT)
2304 assert obj is not None
2305
2306 # Verify that we break the title to a max length of our title_max
2307 # and that the body remains untouched
2308 chunks = obj._apply_overflow(body="", title=title)
2309 chunks = obj._apply_overflow(body="", title="")
2310 chunks = obj._apply_overflow(body=body, title="")
2311 chunks = obj._apply_overflow(body=body, title=title)
2312 assert len(chunks) == 1
2313 assert len(chunks[0].get("body").split("\n")) == obj.body_max_line_count
2314 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
2315
2316 #
2317 # Next Test: Split body
2318 #
2319
2320 class TestNotification(NotifyBase):
2321 # Test title max length
2322 title_maxlen = title_len
2323
2324 # Enforce a body length. Make sure it's an int.
2325 body_maxlen = int(body_len / 4)
2326
2327 # No amalgamation
2328 overflow_amalgamate_title = False
2329
2330 def __init__(self, *args, **kwargs):
2331 super().__init__(**kwargs)
2332
2333 def notify(self, *args, **kwargs):
2334 # Pretend everything is okay
2335 return True
2336
2337 # Load our object
2338 obj = TestNotification(overflow=OverflowMode.SPLIT)
2339 assert obj is not None
2340
2341 # Verify that we break the title to a max length of our title_max
2342 # and that the body remains untouched
2343 chunks = obj._apply_overflow(body="", title=title)
2344 chunks = obj._apply_overflow(body="", title="")
2345 chunks = obj._apply_overflow(body=body, title="")
2346 chunks = obj._apply_overflow(body=body, title=title)
2347 c_len = len(" [X/X]")
2348
2349 offset = 0
2350 for idx, chunk in enumerate(chunks, start=1):
2351 # Verification
2352 assert len(chunk.get("title")) <= obj.title_maxlen
2353 assert len(chunk.get("body")) <= obj.body_maxlen
2354
2355 # Our title has a counter added to it
2356 assert title[:-c_len] == chunk.get("title")[:-c_len]
2357 assert chunk.get("title")[-c_len:] == f" [{idx:01}/{len(chunks):01}]"
2358
2359 # Our body is only broken up; not lost
2360 offset = assert_body(
2361 body,
2362 offset,
2363 chunk.get("body"),
2364 )
2365
2366 # Another edge case where the title just isn't that long leaving
2367 # a lot of space for the [xx/xx] entries (no truncation needed)
2368 chunks = obj._apply_overflow(body="", title=title)
2369 chunks = obj._apply_overflow(body="", title="")
2370 chunks = obj._apply_overflow(body=body, title="")
2371 chunks = obj._apply_overflow(body=body, title=title[:20])
2372
2373 offset = 0
2374 for idx, chunk in enumerate(chunks, start=1):
2375 # Verification
2376 assert len(chunk.get("title")) <= obj.title_maxlen
2377 assert len(chunk.get("body")) <= obj.body_maxlen
2378
2379 # Our title has a counter added to it
2380 assert title[:20] == chunk.get("title")[:-c_len]
2381 assert chunk.get("title")[-c_len:] == f" [{idx:01}/{len(chunks):01}]"
2382
2383 # Our body is only broken up; not lost
2384 offset = assert_body(
2385 body,
2386 offset,
2387 chunk.get("body"),
2388 )
2389
2390 # Test larger messages
2391 # and that the body remains untouched
2392 class TestNotification(NotifyBase):
2393 # Test title max length
2394 title_maxlen = 150
2395
2396 # Enforce a body length. Make sure it's an int.
2397 body_maxlen = 400
2398
2399 # No amalgamation
2400 overflow_amalgamate_title = False
2401
2402 def __init__(self, *args, **kwargs):
2403 super().__init__(**kwargs)
2404
2405 def notify(self, *args, **kwargs):
2406 # Pretend everything is okay
2407 return True
2408
2409 # Load our object
2410 obj = TestNotification(overflow=OverflowMode.SPLIT)
2411 assert obj is not None
2412
2413 new_body = body * 500
2414 chunks = obj._apply_overflow(body="", title=title)
2415 chunks = obj._apply_overflow(body="", title="")
2416 chunks = obj._apply_overflow(body=new_body, title="")
2417 chunks = obj._apply_overflow(body=new_body, title=title)
2418 c_len = len(" [XXXX/XXXX]")
2419
2420 offset = 0
2421 for idx, chunk in enumerate(chunks, start=1):
2422 # Verification
2423 assert len(chunk.get("title")) <= obj.title_maxlen
2424 assert len(chunk.get("body")) <= obj.body_maxlen
2425 # Our title has a counter added to it
2426 assert (
2427 title[: obj.title_maxlen][:-c_len] == chunk.get("title")[:-c_len]
2428 )
2429 assert chunk.get("title")[-c_len:] == f" [{idx:04}/{len(chunks):04}]"
2430
2431 # Our body is only broken up; not lost
2432 offset = assert_body(
2433 new_body,
2434 offset,
2435 chunk.get("body"),
2436 )
2437
2438 # Body chunk is beyond 4 digits, so [XXXX/XXXX] is turned off
2439 new_body = body * 4500
2440 chunks = obj._apply_overflow(body="", title=title)
2441 chunks = obj._apply_overflow(body="", title="")
2442 chunks = obj._apply_overflow(body=new_body, title="")
2443 chunks = obj._apply_overflow(body=new_body, title=title)
2444
2445 offset = 0
2446 for _idx, chunk in enumerate(chunks, start=1):
2447 # Verification
2448 assert len(chunk.get("title")) <= obj.title_maxlen
2449 assert len(chunk.get("body")) <= obj.body_maxlen
2450
2451 # Our title has no counter added to it
2452 assert title[: obj.title_maxlen] == chunk.get("title")
2453
2454 # Our body is only broken up; not lost
2455 offset = assert_body(
2456 new_body,
2457 offset,
2458 chunk.get("body"),
2459 )
2460
2461 # Test larger messages
2462 # and that the body remains untouched
2463 class TestNotification(NotifyBase):
2464 # Test title max length
2465 title_maxlen = 150
2466
2467 # Enforce a body length. Make sure it's an int.
2468 body_maxlen = 150
2469
2470 # No amalgamation
2471 overflow_amalgamate_title = False
2472
2473 def __init__(self, *args, **kwargs):
2474 super().__init__(**kwargs)
2475
2476 def notify(self, *args, **kwargs):
2477 # Pretend everything is okay
2478 return True
2479
2480 # Load our object
2481 obj = TestNotification(overflow=OverflowMode.SPLIT)
2482 assert obj is not None
2483
2484 new_body = body * 5
2485 chunks = obj._apply_overflow(body="", title=title)
2486 chunks = obj._apply_overflow(body="", title="")
2487 chunks = obj._apply_overflow(body=new_body, title="")
2488 chunks = obj._apply_overflow(body=new_body, title=title)
2489 c_len = len(" [XX/XX]")
2490
2491 offset = 0
2492 for idx, chunk in enumerate(chunks, start=1):
2493 # Verification
2494 assert len(chunk.get("title")) <= obj.title_maxlen
2495 assert len(chunk.get("body")) <= obj.body_maxlen
2496
2497 # Our title has a counter added to it
2498 assert (
2499 title[: obj.title_maxlen][:-c_len] == chunk.get("title")[:-c_len]
2500 )
2501 assert chunk.get("title")[-c_len:] == f" [{idx:02}/{len(chunks):02}]"
2502
2503 # Our body is only broken up; not lost
2504 offset = assert_body(
2505 new_body,
2506 offset,
2507 chunk.get("body"),
2508 )
2509
2510 #
2511 # Next Test: Append title to body + split body
2512 #
2513
2514 class TestNotification(NotifyBase):
2515 # Enforce no title
2516 title_maxlen = 0
2517
2518 # Enforce a body length based on the title. Make sure it's an int.
2519 body_maxlen = int(title_len / 4)
2520
2521 # No Amalgamation
2522 overflow_amalgamate_title = False
2523
2524 def __init__(self, *args, **kwargs):
2525 super().__init__(**kwargs)
2526
2527 def notify(self, *args, **kwargs):
2528 # Pretend everything is okay
2529 return True
2530
2531 # Load our object
2532 obj = TestNotification(overflow=OverflowMode.SPLIT)
2533 assert obj is not None
2534
2535 # Verify that we break the title to a max length of our title_max
2536 # and that the body remains untouched
2537 chunks = obj._apply_overflow(body="", title=title)
2538 chunks = obj._apply_overflow(body="", title="")
2539 chunks = obj._apply_overflow(body=body, title="")
2540 chunks = obj._apply_overflow(body=body, title=title)
2541
2542 # Our final product is that our title has been appended to our body to
2543 # create one great big body. As a result we'll get quite a few lines back
2544 # now.
2545 offset = 0
2546
2547 # Our body will look like this in small chunks at the end of the day
2548 bulk = title + "\r\n" + body
2549
2550 # Due to the new line added to the end
2551 assert len(chunks) == (
2552 # wrap division in int() so Python 3 doesn't convert it to a float on
2553 # us
2554 int(len(bulk) / obj.body_maxlen)
2555 + (1 if len(bulk) % obj.body_maxlen else 0)
2556 )
2557
2558 for chunk in chunks:
2559 # Verification
2560 assert len(chunk.get("title")) <= obj.title_maxlen
2561 assert len(chunk.get("body")) <= obj.body_maxlen
2562
2563 # Our title is empty every time
2564 assert chunk.get("title") == ""
2565
2566 # Our body is only broken up; not lost
2567 offset = assert_body(
2568 bulk,
2569 offset,
2570 chunk.get("body"),
2571 )
2572
2573 #
2574 # Test case where our title_len is shorter then the value
2575 # that would otherwise trigger the [XX/XX] elements
2576 #
2577
2578 class TestNotification(NotifyBase):
2579 # Set a small title length
2580 title_maxlen = 100
2581
2582 # Enforce a body length. Make sure it's an int.
2583 body_maxlen = int(body_len / 4)
2584
2585 # No Amalgamation
2586 overflow_amalgamate_title = False
2587
2588 def __init__(self, *args, **kwargs):
2589 super().__init__(**kwargs)
2590
2591 def notify(self, *args, **kwargs):
2592 # Pretend everything is okay
2593 return True
2594
2595 # Load our object
2596 obj = TestNotification(overflow=OverflowMode.SPLIT)
2597 assert obj is not None
2598
2599 # Verify that we break the title to a max length of our title_max
2600 # and that the body remains untouched
2601 chunks = obj._apply_overflow(body="", title=title)
2602 chunks = obj._apply_overflow(body="", title="")
2603 chunks = obj._apply_overflow(body=body, title="")
2604 chunks = obj._apply_overflow(body=body, title=title)
2605
2606 offset = 0
2607 for _idx, chunk in enumerate(chunks, start=1):
2608 # Verification
2609 assert len(chunk.get("title")) <= obj.title_maxlen
2610 assert len(chunk.get("body")) <= obj.body_maxlen
2611
2612 # Our title is truncated and no counter added
2613 assert title[: obj.title_maxlen] == chunk.get("title")
2614
2615 # Our body is only broken up; not lost
2616 offset = assert_body(
2617 body,
2618 offset,
2619 chunk.get("body"),
2620 )
2621
2622 #
2623 # Scenario where the title length is larger than the body
2624 #
2625
2626 class TestNotification(NotifyBase):
2627 # Set a small title length
2628 title_maxlen = 100
2629
2630 # Enforce a body length. Make sure it's an int.
2631 body_maxlen = 50
2632
2633 # No Amalgamation
2634 overflow_amalgamate_title = False
2635
2636 def __init__(self, *args, **kwargs):
2637 super().__init__(**kwargs)
2638
2639 def notify(self, *args, **kwargs):
2640 # Pretend everything is okay
2641 return True
2642
2643 # Load our object
2644 obj = TestNotification(overflow=OverflowMode.SPLIT)
2645 assert obj is not None
2646
2647 # Verify that we break the title to a max length of our title_max
2648 # and that the body remains untouched
2649 chunks = obj._apply_overflow(body="", title=title)
2650 chunks = obj._apply_overflow(body="", title="")
2651 chunks = obj._apply_overflow(body=body, title="")
2652 chunks = obj._apply_overflow(body=body, title=title)
2653
2654 offset = 0
2655 for _idx, chunk in enumerate(chunks, start=1):
2656 # Verification
2657 assert len(chunk.get("title")) <= obj.title_maxlen
2658 assert len(chunk.get("body")) <= obj.body_maxlen
2659
2660 # Our title is truncated and no counter added
2661 assert title[: obj.title_maxlen] == chunk.get("title")
2662
2663 # Our body is only broken up; not lost
2664 offset = assert_body(
2665 body,
2666 offset,
2667 chunk.get("body"),
2668 )
2669
2670
2671 def test_notify_overflow_split_no_amalgamation_force_title_always():
2672 """
2673 API: Overflow No Amalgamation (title always) Split Functionality Testing
2674
2675 """
2676
2677 #
2678 # A little preparation
2679 #
2680
2681 # Number of characters per line
2682 row = 24
2683
2684 # Some variables we use to control the data we work with
2685 body_len = 1024
2686 title_len = 1024
2687
2688 # Create a large body and title with random data
2689 body = "".join(choice(str_alpha + str_num) for _ in range(body_len))
2690 body = "\r\n".join([body[i : i + row] for i in range(0, len(body), row)])
2691
2692 # the new lines add a large amount to our body; lets force the content
2693 # back to being 1024 characters.
2694 body = body[0:1024]
2695
2696 # Create our title using random data
2697 title = "".join(choice(str_alpha + str_num) for _ in range(title_len))
2698
2699 #
2700 # First Test: Truncated Title
2701 #
2702 class TestNotification(NotifyBase):
2703 # Test title max length
2704 title_maxlen = 10
2705
2706 # No amalgamation
2707 overflow_amalgamate_title = False
2708
2709 # Force title to be displayed always
2710 overflow_display_title_once = False
2711
2712 def __init__(self, *args, **kwargs):
2713 super().__init__(**kwargs)
2714
2715 def notify(self, *args, **kwargs):
2716 # Pretend everything is okay
2717 return True
2718
2719 # Load our object
2720 obj = TestNotification(overflow=OverflowMode.SPLIT)
2721 assert obj is not None
2722
2723 # Verify that we break the title to a max length of our title_max
2724 # and that the body remains untouched
2725 chunks = obj._apply_overflow(body="", title=title)
2726 chunks = obj._apply_overflow(body="", title="")
2727 chunks = obj._apply_overflow(body=body, title="")
2728 chunks = obj._apply_overflow(body=body, title=title)
2729 assert len(chunks) == 1
2730 assert body.lstrip("\r\n\x0b\x0c").rstrip() == chunks[0].get("body")
2731 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
2732
2733 #
2734 # Next Test: Line Count Control
2735 #
2736
2737 class TestNotification(NotifyBase):
2738 # Test title max length
2739 title_maxlen = 5
2740
2741 # Maximum number of lines
2742 body_max_line_count = 5
2743
2744 # No amalgamation
2745 overflow_amalgamate_title = False
2746
2747 # Force title to be displayed always
2748 overflow_display_title_once = False
2749
2750 def __init__(self, *args, **kwargs):
2751 super().__init__(**kwargs)
2752
2753 def notify(self, *args, **kwargs):
2754 # Pretend everything is okay
2755 return True
2756
2757 # Load our object
2758 obj = TestNotification(overflow=OverflowMode.SPLIT)
2759 assert obj is not None
2760
2761 # Verify that we break the title to a max length of our title_max
2762 # and that the body remains untouched
2763 chunks = obj._apply_overflow(body="", title=title)
2764 chunks = obj._apply_overflow(body="", title="")
2765 chunks = obj._apply_overflow(body=body, title="")
2766 chunks = obj._apply_overflow(body=body, title=title)
2767 assert len(chunks) == 1
2768 assert len(chunks[0].get("body").split("\n")) == obj.body_max_line_count
2769 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
2770
2771 #
2772 # Next Test: Split body
2773 #
2774
2775 class TestNotification(NotifyBase):
2776 # Test title max length
2777 title_maxlen = title_len
2778
2779 # Enforce a body length. Make sure it's an int.
2780 body_maxlen = int(body_len / 4)
2781
2782 # No amalgamation
2783 overflow_amalgamate_title = False
2784
2785 # Force title to be displayed always
2786 overflow_display_title_once = False
2787
2788 def __init__(self, *args, **kwargs):
2789 super().__init__(**kwargs)
2790
2791 def notify(self, *args, **kwargs):
2792 # Pretend everything is okay
2793 return True
2794
2795 # Load our object
2796 obj = TestNotification(overflow=OverflowMode.SPLIT)
2797 assert obj is not None
2798
2799 # Verify that we break the title to a max length of our title_max
2800 # and that the body remains untouched
2801 chunks = obj._apply_overflow(body="", title=title)
2802 chunks = obj._apply_overflow(body="", title="")
2803 chunks = obj._apply_overflow(body=body, title="")
2804 chunks = obj._apply_overflow(body=body, title=title)
2805 c_len = len(" [X/X]")
2806
2807 offset = 0
2808 for idx, chunk in enumerate(chunks, start=1):
2809 # Verification
2810 assert len(chunk.get("title")) <= obj.title_maxlen
2811 assert len(chunk.get("body")) <= obj.body_maxlen
2812
2813 # Our title has a counter added to it
2814 assert title[:-c_len] == chunk.get("title")[:-c_len]
2815 assert chunk.get("title")[-c_len:] == f" [{idx:01}/{len(chunks):01}]"
2816
2817 # Our body is only broken up; not lost
2818 offset = assert_body(
2819 body,
2820 offset,
2821 chunk.get("body"),
2822 )
2823
2824 # Another edge case where the title just isn't that long leaving
2825 # a lot of space for the [xx/xx] entries (no truncation needed)
2826 chunks = obj._apply_overflow(body="", title=title)
2827 chunks = obj._apply_overflow(body="", title="")
2828 chunks = obj._apply_overflow(body=body, title="")
2829 chunks = obj._apply_overflow(body=body, title=title[:20])
2830
2831 offset = 0
2832 for idx, chunk in enumerate(chunks, start=1):
2833 # Verification
2834 assert len(chunk.get("title")) <= obj.title_maxlen
2835 assert len(chunk.get("body")) <= obj.body_maxlen
2836
2837 # Our title has a counter added to it
2838 assert title[:20] == chunk.get("title")[:-c_len]
2839 assert chunk.get("title")[-c_len:] == f" [{idx:01}/{len(chunks):01}]"
2840
2841 # Our body is only broken up; not lost
2842 offset = assert_body(
2843 body,
2844 offset,
2845 chunk.get("body"),
2846 )
2847
2848 # Test larger messages
2849 # and that the body remains untouched
2850 class TestNotification(NotifyBase):
2851 # Test title max length
2852 title_maxlen = 150
2853
2854 # Enforce a body length. Make sure it's an int.
2855 body_maxlen = 400
2856
2857 # No amalgamation
2858 overflow_amalgamate_title = False
2859
2860 # Force title to be displayed always
2861 overflow_display_title_once = False
2862
2863 def __init__(self, *args, **kwargs):
2864 super().__init__(**kwargs)
2865
2866 def notify(self, *args, **kwargs):
2867 # Pretend everything is okay
2868 return True
2869
2870 # Load our object
2871 obj = TestNotification(overflow=OverflowMode.SPLIT)
2872 assert obj is not None
2873
2874 new_body = body * 500
2875 chunks = obj._apply_overflow(body="", title=title)
2876 chunks = obj._apply_overflow(body="", title="")
2877 chunks = obj._apply_overflow(body=new_body, title="")
2878 chunks = obj._apply_overflow(body=new_body, title=title)
2879 c_len = len(" [XXXX/XXXX]")
2880
2881 offset = 0
2882 for idx, chunk in enumerate(chunks, start=1):
2883 # Verification
2884 assert len(chunk.get("title")) <= obj.title_maxlen
2885 assert len(chunk.get("body")) <= obj.body_maxlen
2886 # Our title has a counter added to it
2887 assert (
2888 title[: obj.title_maxlen][:-c_len] == chunk.get("title")[:-c_len]
2889 )
2890 assert chunk.get("title")[-c_len:] == f" [{idx:04}/{len(chunks):04}]"
2891
2892 # Our body is only broken up; not lost
2893 offset = assert_body(
2894 new_body,
2895 offset,
2896 chunk.get("body"),
2897 )
2898
2899 # Body chunk is beyond 4 digits, so [XXXX/XXXX] is turned off
2900 new_body = body * 4500
2901 chunks = obj._apply_overflow(body="", title=title)
2902 chunks = obj._apply_overflow(body="", title="")
2903 chunks = obj._apply_overflow(body=new_body, title="")
2904 chunks = obj._apply_overflow(body=new_body, title=title)
2905
2906 offset = 0
2907 for _idx, chunk in enumerate(chunks, start=1):
2908 # Verification
2909 assert len(chunk.get("title")) <= obj.title_maxlen
2910 assert len(chunk.get("body")) <= obj.body_maxlen
2911
2912 # Our title has no counter added to it
2913 assert title[: obj.title_maxlen] == chunk.get("title")
2914
2915 # Our body is only broken up; not lost
2916 offset = assert_body(
2917 new_body,
2918 offset,
2919 chunk.get("body"),
2920 )
2921
2922 # Test larger messages
2923 # and that the body remains untouched
2924 class TestNotification(NotifyBase):
2925 # Test title max length
2926 title_maxlen = 150
2927
2928 # Enforce a body length. Make sure it's an int.
2929 body_maxlen = 150
2930
2931 # No amalgamation
2932 overflow_amalgamate_title = False
2933
2934 # Force title to be displayed always
2935 overflow_display_title_once = False
2936
2937 def __init__(self, *args, **kwargs):
2938 super().__init__(**kwargs)
2939
2940 def notify(self, *args, **kwargs):
2941 # Pretend everything is okay
2942 return True
2943
2944 # Load our object
2945 obj = TestNotification(overflow=OverflowMode.SPLIT)
2946 assert obj is not None
2947
2948 new_body = body * 5
2949 chunks = obj._apply_overflow(body="", title=title)
2950 chunks = obj._apply_overflow(body="", title="")
2951 chunks = obj._apply_overflow(body=new_body, title="")
2952 chunks = obj._apply_overflow(body=new_body, title=title)
2953 c_len = len(" [XX/XX]")
2954
2955 offset = 0
2956 for idx, chunk in enumerate(chunks, start=1):
2957 # Verification
2958 assert len(chunk.get("title")) <= obj.title_maxlen
2959 assert len(chunk.get("body")) <= obj.body_maxlen
2960
2961 # Our title has a counter added to it
2962 assert (
2963 title[: obj.title_maxlen][:-c_len] == chunk.get("title")[:-c_len]
2964 )
2965 assert chunk.get("title")[-c_len:] == f" [{idx:02}/{len(chunks):02}]"
2966
2967 # Our body is only broken up; not lost
2968 offset = assert_body(
2969 new_body,
2970 offset,
2971 chunk.get("body"),
2972 )
2973
2974 #
2975 # Next Test: Append title to body + split body
2976 #
2977
2978 class TestNotification(NotifyBase):
2979 # Enforce no title
2980 title_maxlen = 0
2981
2982 # Enforce a body length based on the title. Make sure it's an int.
2983 body_maxlen = int(title_len / 4)
2984
2985 # No Amalgamation
2986 overflow_amalgamate_title = False
2987
2988 # Force title to be displayed always
2989 overflow_display_title_once = False
2990
2991 def __init__(self, *args, **kwargs):
2992 super().__init__(**kwargs)
2993
2994 def notify(self, *args, **kwargs):
2995 # Pretend everything is okay
2996 return True
2997
2998 # Load our object
2999 obj = TestNotification(overflow=OverflowMode.SPLIT)
3000 assert obj is not None
3001
3002 # Verify that we break the title to a max length of our title_max
3003 # and that the body remains untouched
3004 chunks = obj._apply_overflow(body="", title=title)
3005 chunks = obj._apply_overflow(body="", title="")
3006 chunks = obj._apply_overflow(body=body, title="")
3007 chunks = obj._apply_overflow(body=body, title=title)
3008
3009 # Our final product is that our title has been appended to our body to
3010 # create one great big body. As a result we'll get quite a few lines back
3011 # now.
3012 offset = 0
3013
3014 # Our body will look like this in small chunks at the end of the day
3015 bulk = title + "\r\n" + body
3016
3017 # Due to the new line added to the end
3018 assert len(chunks) == (
3019 # wrap division in int() so Python 3 doesn't convert it to a float on
3020 # us
3021 int(len(bulk) / obj.body_maxlen)
3022 + (1 if len(bulk) % obj.body_maxlen else 0)
3023 )
3024
3025 for chunk in chunks:
3026 # Verification
3027 assert len(chunk.get("title")) <= obj.title_maxlen
3028 assert len(chunk.get("body")) <= obj.body_maxlen
3029
3030 # Our title is empty every time
3031 assert chunk.get("title") == ""
3032
3033 # Our body is only broken up; not lost
3034 offset = assert_body(
3035 bulk,
3036 offset,
3037 chunk.get("body"),
3038 )
3039
3040 #
3041 # Test case where our title_len is shorter then the value
3042 # that would otherwise trigger the [XX/XX] elements
3043 #
3044
3045 class TestNotification(NotifyBase):
3046 # Set a small title length
3047 title_maxlen = 100
3048
3049 # Enforce a body length. Make sure it's an int.
3050 body_maxlen = int(body_len / 4)
3051
3052 # No Amalgamation
3053 overflow_amalgamate_title = False
3054
3055 # Force title to be displayed always
3056 overflow_display_title_once = False
3057
3058 def __init__(self, *args, **kwargs):
3059 super().__init__(**kwargs)
3060
3061 def notify(self, *args, **kwargs):
3062 # Pretend everything is okay
3063 return True
3064
3065 # Load our object
3066 obj = TestNotification(overflow=OverflowMode.SPLIT)
3067 assert obj is not None
3068
3069 # Verify that we break the title to a max length of our title_max
3070 # and that the body remains untouched
3071 chunks = obj._apply_overflow(body="", title=title)
3072 chunks = obj._apply_overflow(body="", title="")
3073 chunks = obj._apply_overflow(body=body, title="")
3074 chunks = obj._apply_overflow(body=body, title=title)
3075
3076 offset = 0
3077 for _idx, chunk in enumerate(chunks, start=1):
3078 # Verification
3079 assert len(chunk.get("title")) <= obj.title_maxlen
3080 assert len(chunk.get("body")) <= obj.body_maxlen
3081
3082 # Our title is truncated and no counter added
3083 assert title[: obj.title_maxlen] == chunk.get("title")
3084
3085 # Our body is only broken up; not lost
3086 offset = assert_body(
3087 body,
3088 offset,
3089 chunk.get("body"),
3090 )
3091
3092 #
3093 # Scenario where the title length is larger than the body
3094 #
3095
3096 class TestNotification(NotifyBase):
3097 # Set a small title length
3098 title_maxlen = 100
3099
3100 # Enforce a body length. Make sure it's an int.
3101 body_maxlen = 50
3102
3103 # No Amalgamation
3104 overflow_amalgamate_title = False
3105
3106 # Force title to be displayed always
3107 overflow_display_title_once = False
3108
3109 def __init__(self, *args, **kwargs):
3110 super().__init__(**kwargs)
3111
3112 def notify(self, *args, **kwargs):
3113 # Pretend everything is okay
3114 return True
3115
3116 # Load our object
3117 obj = TestNotification(overflow=OverflowMode.SPLIT)
3118 assert obj is not None
3119
3120 # Verify that we break the title to a max length of our title_max
3121 # and that the body remains untouched
3122 chunks = obj._apply_overflow(body="", title=title)
3123 chunks = obj._apply_overflow(body="", title="")
3124 chunks = obj._apply_overflow(body=body, title="")
3125 chunks = obj._apply_overflow(body=body, title=title)
3126
3127 offset = 0
3128 for _idx, chunk in enumerate(chunks, start=1):
3129 # Verification
3130 assert len(chunk.get("title")) <= obj.title_maxlen
3131 assert len(chunk.get("body")) <= obj.body_maxlen
3132
3133 # Our title is truncated and no counter added
3134 assert title[: obj.title_maxlen] == chunk.get("title")
3135
3136 # Our body is only broken up; not lost
3137 offset = assert_body(
3138 body,
3139 offset,
3140 chunk.get("body"),
3141 )
3142
3143
3144 def test_notify_overflow_split_no_amalgamation_force_title_once():
3145 """
3146 API: Overflow No Amalgamation (title once) Split Functionality Testing
3147
3148 """
3149
3150 #
3151 # A little preparation
3152 #
3153
3154 # Number of characters per line
3155 row = 24
3156
3157 # Some variables we use to control the data we work with
3158 body_len = 1024
3159 title_len = 1024
3160
3161 # Create a large body and title with random data
3162 body = "".join(choice(str_alpha + str_num) for _ in range(body_len))
3163 body = "\r\n".join([body[i : i + row] for i in range(0, len(body), row)])
3164
3165 # the new lines add a large amount to our body; lets force the content
3166 # back to being 1024 characters.
3167 body = body[0:1024]
3168
3169 # Create our title using random data
3170 title = "".join(choice(str_alpha + str_num) for _ in range(title_len))
3171
3172 #
3173 # First Test: Truncated Title
3174 #
3175 class TestNotification(NotifyBase):
3176 # Test title max length
3177 title_maxlen = 10
3178
3179 # With amalgamation
3180 overflow_amalgamate_title = True
3181
3182 # Force title displayed once
3183 overflow_display_title_once = True
3184
3185 def __init__(self, *args, **kwargs):
3186 super().__init__(**kwargs)
3187
3188 def notify(self, *args, **kwargs):
3189 # Pretend everything is okay
3190 return True
3191
3192 # Load our object
3193 obj = TestNotification(overflow=OverflowMode.SPLIT)
3194 assert obj is not None
3195
3196 # Verify that we break the title to a max length of our title_max
3197 # and that the body remains untouched
3198 chunks = obj._apply_overflow(body="", title=title)
3199 chunks = obj._apply_overflow(body="", title="")
3200 chunks = obj._apply_overflow(body=body, title="")
3201 chunks = obj._apply_overflow(body=body, title=title)
3202 assert len(chunks) == 1
3203 assert body.lstrip("\r\n\x0b\x0c").rstrip() == chunks[0].get("body")
3204 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
3205
3206 #
3207 # Next Test: Line Count Control
3208 #
3209
3210 class TestNotification(NotifyBase):
3211 # Test title max length
3212 title_maxlen = 5
3213
3214 # Maximum number of lines
3215 body_max_line_count = 5
3216
3217 # With amalgamation
3218 overflow_amalgamate_title = True
3219
3220 # Force title displayed once
3221 overflow_display_title_once = True
3222
3223 def __init__(self, *args, **kwargs):
3224 super().__init__(**kwargs)
3225
3226 def notify(self, *args, **kwargs):
3227 # Pretend everything is okay
3228 return True
3229
3230 # Load our object
3231 obj = TestNotification(overflow=OverflowMode.SPLIT)
3232 assert obj is not None
3233
3234 # Verify that we break the title to a max length of our title_max
3235 # and that the body remains untouched
3236 chunks = obj._apply_overflow(body="", title=title)
3237 chunks = obj._apply_overflow(body="", title="")
3238 chunks = obj._apply_overflow(body=body, title="")
3239 chunks = obj._apply_overflow(body=body, title=title)
3240 assert len(chunks) == 1
3241 assert len(chunks[0].get("body").split("\n")) == obj.body_max_line_count
3242 assert title[0 : obj.title_maxlen] == chunks[0].get("title")
3243
3244 #
3245 # Next Test: Split body
3246 #
3247
3248 class TestNotification(NotifyBase):
3249 # Test title max length
3250 title_maxlen = title_len
3251
3252 # Enforce a body length. Make sure it's an int.
3253 body_maxlen = int(body_len / 4)
3254
3255 # With amalgamation
3256 overflow_amalgamate_title = True
3257
3258 # Force title displayed once
3259 overflow_display_title_once = True
3260
3261 def __init__(self, *args, **kwargs):
3262 super().__init__(**kwargs)
3263
3264 def notify(self, *args, **kwargs):
3265 # Pretend everything is okay
3266 return True
3267
3268 # Load our object
3269 obj = TestNotification(overflow=OverflowMode.SPLIT)
3270 assert obj is not None
3271
3272 # Verify that we break the title to a max length of our title_max
3273 # and that the body remains untouched
3274 chunks = obj._apply_overflow(body="", title=title)
3275 chunks = obj._apply_overflow(body="", title="")
3276 chunks = obj._apply_overflow(body=body, title="")
3277 chunks = obj._apply_overflow(body=body, title=title)
3278
3279 offset = 0
3280 for idx, chunk in enumerate(chunks, start=1):
3281 # Verification
3282 assert len(chunk.get("title")) <= obj.title_maxlen
3283 assert len(chunk.get("body")) <= obj.body_maxlen
3284
3285 # No counter is displayed because our title is so enormous
3286 # We switch to a display title on first message only
3287 if idx > 1:
3288 # Empty (no title displayed on following entries
3289 assert chunk.get("title") == ""
3290 else:
3291 # The length of the body prevails our title due to it being
3292 # so much smaller then our title length
3293 assert len(chunk.get("title")) == obj.body_maxlen
3294 assert title[: obj.body_maxlen] == chunk.get("title")
3295
3296 # Our body is only broken up; not lost
3297 offset = assert_body(
3298 body,
3299 offset,
3300 chunk.get("body"),
3301 )
3302
3303 # Another edge case where the title just isn't that long leaving
3304 # a lot of space for the [xx/xx] entries (no truncation needed)
3305 chunks = obj._apply_overflow(body="", title=title)
3306 chunks = obj._apply_overflow(body="", title="")
3307 chunks = obj._apply_overflow(body=body, title="")
3308 chunks = obj._apply_overflow(body=body, title=title[:20])
3309 offset = 0
3310 for idx, chunk in enumerate(chunks, start=1):
3311 # Verification
3312 assert len(chunk.get("title")) <= obj.title_maxlen
3313 assert len(chunk.get("body")) <= obj.body_maxlen
3314
3315 # No counter is displayed because our title is so enormous
3316 # We switch to a display title on first message only
3317 if idx > 1:
3318 # Empty (no title displayed on following entries
3319 assert chunk.get("title") == ""
3320 else:
3321 # The length of the body prevails our title due to it being
3322 # so much smaller then our title length
3323 assert len(chunk.get("title")) == 20
3324 assert title[:20] == chunk.get("title")
3325
3326 # Our body is only broken up; not lost
3327 offset = assert_body(
3328 body,
3329 offset,
3330 chunk.get("body"),
3331 )
3332
3333 #
3334 # Test forcing overflow_display_title_once
3335 #
3336 class TestNotification(NotifyBase):
3337 # Test title max length
3338 title_maxlen = title_len
3339
3340 # Enforce a body length. Make sure it's an int.
3341 body_maxlen = int(body_len / 4)
3342
3343 # With amalgamation
3344 overflow_amalgamate_title = True
3345
3346 # Force title displayed once
3347 overflow_display_title_once = True
3348
3349 def __init__(self, *args, **kwargs):
3350 super().__init__(**kwargs)
3351
3352 def notify(self, *args, **kwargs):
3353 # Pretend everything is okay
3354 return True
3355
3356 # Load our object
3357 obj = TestNotification(overflow=OverflowMode.SPLIT)
3358 assert obj is not None
3359
3360 chunks = obj._apply_overflow(body="", title=title)
3361 chunks = obj._apply_overflow(body="", title="")
3362 chunks = obj._apply_overflow(body=body, title="")
3363 chunks = obj._apply_overflow(body=body, title=title)
3364 offset = 0
3365 for idx, chunk in enumerate(chunks, start=1):
3366 # Verification
3367 assert len(chunk.get("title")) <= obj.title_maxlen
3368 assert len(chunk.get("body")) <= obj.body_maxlen
3369
3370 # Our title has no counter added to it
3371 if idx > 1:
3372 # Empty (no title displayed on following entries
3373 assert chunk.get("title") == ""
3374 else:
3375 # The body length prevails due to our amalgamation flag
3376 assert len(chunk.get("title")) == obj.body_maxlen
3377 assert title[: obj.body_maxlen] == chunk.get("title")
3378
3379 # Our body is only broken up; not lost
3380 offset = assert_body(
3381 body,
3382 offset,
3383 chunk.get("body"),
3384 )
3385
3386 # Test larger messages
3387 # and that the body remains untouched
3388 class TestNotification(NotifyBase):
3389 # Test title max length
3390 title_maxlen = 150
3391
3392 # Enforce a body length. Make sure it's an int.
3393 body_maxlen = 400
3394
3395 # With amalgamation
3396 overflow_amalgamate_title = True
3397
3398 # Force title displayed once
3399 overflow_display_title_once = True
3400
3401 def __init__(self, *args, **kwargs):
3402 super().__init__(**kwargs)
3403
3404 def notify(self, *args, **kwargs):
3405 # Pretend everything is okay
3406 return True
3407
3408 # Load our object
3409 obj = TestNotification(overflow=OverflowMode.SPLIT)
3410 assert obj is not None
3411
3412 new_body = body * 500
3413 chunks = obj._apply_overflow(body="", title=title)
3414 chunks = obj._apply_overflow(body="", title="")
3415 chunks = obj._apply_overflow(body=new_body, title="")
3416 chunks = obj._apply_overflow(body=new_body, title=title)
3417
3418 offset = 0
3419 for idx, chunk in enumerate(chunks, start=1):
3420 # Verification
3421 assert len(chunk.get("title")) <= obj.title_maxlen
3422 assert len(chunk.get("body")) <= obj.body_maxlen
3423
3424 # No counter is displayed because our title is so enormous
3425 # We switch to a display title on first message only
3426 if idx > 1:
3427 # Empty (no title displayed on following entries
3428 assert chunk.get("title") == ""
3429 else:
3430 # The length of the body prevails our title due to it being
3431 # so much smaller then our title length
3432 assert len(chunk.get("title")) == obj.title_maxlen
3433 assert title[: obj.title_maxlen] == chunk.get("title")
3434
3435 # Our body is only broken up; not lost
3436 offset = assert_body(
3437 new_body,
3438 offset,
3439 chunk.get("body"),
3440 )
3441
3442 # Body chunk is beyond 4 digits, so [XXXX/XXXX] is turned off
3443 new_body = body * 2500
3444 chunks = obj._apply_overflow(body="", title=title)
3445 chunks = obj._apply_overflow(body="", title="")
3446 chunks = obj._apply_overflow(body=new_body, title="")
3447 chunks = obj._apply_overflow(body=new_body, title=title)
3448
3449 offset = 0
3450 for idx, chunk in enumerate(chunks, start=1):
3451 # Verification
3452 assert len(chunk.get("title")) <= obj.title_maxlen
3453 assert len(chunk.get("body")) <= obj.body_maxlen
3454
3455 # No counter is displayed because our title is so enormous
3456 # We switch to a display title on first message only
3457 if idx > 1:
3458 # Empty (no title displayed on following entries
3459 assert chunk.get("title") == ""
3460 else:
3461 # The length of the body prevails our title due to it being
3462 # so much smaller then our title length
3463 assert len(chunk.get("title")) == obj.title_maxlen
3464 assert title[: obj.title_maxlen] == chunk.get("title")
3465
3466 # Our body is only broken up; not lost
3467 offset = assert_body(
3468 new_body,
3469 offset,
3470 chunk.get("body"),
3471 )
3472
3473 # Test larger messages
3474 # and that the body remains untouched
3475 class TestNotification(NotifyBase):
3476 # Test title max length
3477 title_maxlen = 150
3478
3479 # Enforce a body length. Make sure it's an int.
3480 body_maxlen = 150
3481
3482 # With amalgamation
3483 overflow_amalgamate_title = True
3484
3485 # Force title displayed once
3486 overflow_display_title_once = True
3487
3488 def __init__(self, *args, **kwargs):
3489 super().__init__(**kwargs)
3490
3491 def notify(self, *args, **kwargs):
3492 # Pretend everything is okay
3493 return True
3494
3495 # Load our object
3496 obj = TestNotification(overflow=OverflowMode.SPLIT)
3497 assert obj is not None
3498
3499 new_body = body * 5
3500 chunks = obj._apply_overflow(body="", title=title)
3501 chunks = obj._apply_overflow(body="", title="")
3502 chunks = obj._apply_overflow(body=new_body, title="")
3503 chunks = obj._apply_overflow(body=new_body, title=title)
3504 offset = 0
3505 for idx, chunk in enumerate(chunks, start=1):
3506 # Verification
3507 assert len(chunk.get("title")) <= obj.title_maxlen
3508 assert len(chunk.get("body")) <= obj.body_maxlen
3509
3510 # Our title has no counter added to it
3511 if idx > 1:
3512 # Empty (no title displayed on following entries
3513 assert chunk.get("title") == ""
3514 else:
3515 # Because 150 is what we set the title limit to
3516 assert len(chunk.get("title")) == obj.title_maxlen
3517 assert title[: obj.title_maxlen] == chunk.get("title")
3518
3519 # Our body is only broken up; not lost
3520 offset = assert_body(
3521 new_body,
3522 offset,
3523 chunk.get("body"),
3524 )
3525
3526 #
3527 # Next Test: Append title to body + split body
3528 #
3529
3530 class TestNotification(NotifyBase):
3531 # Enforce no title
3532 title_maxlen = 0
3533
3534 # Enforce a body length based on the title. Make sure it's an int.
3535 body_maxlen = int(title_len / 4)
3536
3537 # With amalgamation
3538 overflow_amalgamate_title = True
3539
3540 # Force title displayed once
3541 overflow_display_title_once = True
3542
3543 def __init__(self, *args, **kwargs):
3544 super().__init__(**kwargs)
3545
3546 def notify(self, *args, **kwargs):
3547 # Pretend everything is okay
3548 return True
3549
3550 # Load our object
3551 obj = TestNotification(overflow=OverflowMode.SPLIT)
3552 assert obj is not None
3553
3554 # Verify that we break the title to a max length of our title_max
3555 # and that the body remains untouched
3556 chunks = obj._apply_overflow(body="", title=title)
3557 chunks = obj._apply_overflow(body="", title="")
3558 chunks = obj._apply_overflow(body=body, title="")
3559 chunks = obj._apply_overflow(body=body, title=title)
3560
3561 # Our final product is that our title has been appended to our body to
3562 # create one great big body. As a result we'll get quite a few lines back
3563 # now.
3564 offset = 0
3565
3566 # Our body will look like this in small chunks at the end of the day
3567 bulk = title + "\r\n" + body
3568
3569 # Due to the new line added to the end
3570 assert len(chunks) == (
3571 # wrap division in int() so Python 3 doesn't convert it to a float on
3572 # us
3573 int(len(bulk) / obj.body_maxlen)
3574 + (1 if len(bulk) % obj.body_maxlen else 0)
3575 )
3576
3577 for chunk in chunks:
3578 # Verification
3579 assert len(chunk.get("title")) == 0
3580 assert len(chunk.get("body")) <= obj.body_maxlen
3581
3582 # Our title is empty every time
3583 assert chunk.get("title") == ""
3584
3585 # Our body is only broken up; not lost
3586 offset = assert_body(
3587 bulk,
3588 offset,
3589 chunk.get("body"),
3590 )
3591
3592 #
3593 # Test case where our title_len is shorter then the value
3594 # that would otherwise trigger the [XX/XX] elements
3595 #
3596
3597 class TestNotification(NotifyBase):
3598 # Set a small title length
3599 title_maxlen = 100
3600
3601 # Enforce a body length. Make sure it's an int.
3602 body_maxlen = int(body_len / 4)
3603
3604 # With amalgamation
3605 overflow_amalgamate_title = True
3606
3607 # Force title displayed once
3608 overflow_display_title_once = True
3609
3610 def __init__(self, *args, **kwargs):
3611 super().__init__(**kwargs)
3612
3613 def notify(self, *args, **kwargs):
3614 # Pretend everything is okay
3615 return True
3616
3617 # Load our object
3618 obj = TestNotification(overflow=OverflowMode.SPLIT)
3619 assert obj is not None
3620
3621 # Verify that we break the title to a max length of our title_max
3622 # and that the body remains untouched
3623 chunks = obj._apply_overflow(body="", title=title)
3624 chunks = obj._apply_overflow(body="", title="")
3625 chunks = obj._apply_overflow(body=body, title="")
3626 chunks = obj._apply_overflow(body=body, title=title)
3627
3628 offset = 0
3629 for idx, chunk in enumerate(chunks, start=1):
3630 # Verification
3631 assert len(chunk.get("title")) <= obj.title_maxlen
3632 assert len(chunk.get("body")) <= obj.body_maxlen
3633
3634 # No counter is displayed because our title is so enormous
3635 # We switch to a display title on first message only
3636 if idx > 1:
3637 # Empty (no title displayed on following entries
3638 assert chunk.get("title") == ""
3639 else:
3640 # The length of the body prevails our title due to it being
3641 # so much smaller then our title length
3642 assert len(chunk.get("title")) == obj.title_maxlen
3643 assert title[: obj.title_maxlen] == chunk.get("title")
3644
3645 # Our body is only broken up; not lost
3646 offset = assert_body(
3647 new_body,
3648 offset,
3649 chunk.get("body"),
3650 )
3651
3652 #
3653 # Scenario where the title length is larger than the body
3654 #
3655
3656 class TestNotification(NotifyBase):
3657 # Set a small title length
3658 title_maxlen = 100
3659
3660 # Enforce a body length. Make sure it's an int.
3661 body_maxlen = 50
3662
3663 # With amalgamation
3664 overflow_amalgamate_title = True
3665
3666 # Force title displayed once
3667 overflow_display_title_once = True
3668
3669 def __init__(self, *args, **kwargs):
3670 super().__init__(**kwargs)
3671
3672 def notify(self, *args, **kwargs):
3673 # Pretend everything is okay
3674 return True
3675
3676 # Load our object
3677 obj = TestNotification(overflow=OverflowMode.SPLIT)
3678 assert obj is not None
3679
3680 # Verify that we break the title to a max length of our title_max
3681 # and that the body remains untouched
3682 chunks = obj._apply_overflow(body="", title=title)
3683 chunks = obj._apply_overflow(body="", title="")
3684 chunks = obj._apply_overflow(body=body, title="")
3685 chunks = obj._apply_overflow(body=body, title=title)
3686
3687 offset = 0
3688 for idx, chunk in enumerate(chunks, start=1):
3689 # Verification
3690 assert len(chunk.get("title")) <= obj.title_maxlen
3691 assert len(chunk.get("body")) <= obj.body_maxlen
3692
3693 # Our title has no counter added to it due to it's length
3694 if idx > 1:
3695 # Empty (no title displayed on following entries
3696 assert chunk.get("title") == ""
3697 else:
3698 # Because 150 is what we set the title limit to 50 due
3699 # to amalamation. The lowest value always prevails
3700 assert len(chunk.get("title")) == obj.body_maxlen
3701 assert title[: obj.body_maxlen] == chunk.get("title")
3702
3703 # Our body is only broken up; not lost
3704 offset = assert_body(
3705 new_body,
3706 offset,
3707 chunk.get("body"),
3708 )
3709
3710
3711 def test_notify_dialect_overflow_repeat_title():
3712 """NotifyBase(): Preserve repeated titles after dialect splitting."""
3713
3714 class GrowingDialect(NotifyBase):
3715 # Small enough that a tripled chunk needs a further dialect split.
3716 body_maxlen = 8
3717 title_maxlen = 10
3718 overflow_amalgamate_title = False
3719
3720 def __init__(self, *args, **kwargs):
3721 super().__init__(**kwargs)
3722 # Record every piece that would have gone out over the wire.
3723 self.sent = []
3724
3725 def dialect_convert(self, body, body_format=None, *args, **kwargs):
3726 # Triple every character so a chunk that fit before conversion
3727 # overflows body_maxlen after conversion, forcing a re-split.
3728 return "".join(c * 3 for c in body)
3729
3730 def send(self, body, title="", notify_type=None, **kwargs):
3731 self.sent.append((body, title))
3732 return True
3733
3734 # Repeat mode keeps the title on every dialect-resplit piece.
3735 obj = GrowingDialect(overflow=OverflowMode.SPLIT)
3736 obj.overflow_display_title_once = False
3737 # Declaring the format enables dialect conversion.
3738 assert (
3739 obj.notify(
3740 body="abcdefghijklmnop",
3741 title="Alert",
3742 body_format=NotifyFormat.TEXT,
3743 )
3744 is True
3745 )
3746 # Tripling forces another split after the initial overflow split.
3747 assert len(obj.sent) > 2
3748 assert all(title == "Alert" for _, title in obj.sent)
3749
3750 # Show-once mode keeps the title only on the first physical piece.
3751 obj = GrowingDialect(overflow=OverflowMode.SPLIT)
3752 obj.overflow_display_title_once = True
3753 # Declaring the format enables dialect conversion.
3754 assert (
3755 obj.notify(
3756 body="abcdefghijklmnop",
3757 title="Alert",
3758 body_format=NotifyFormat.TEXT,
3759 )
3760 is True
3761 )
3762 assert len(obj.sent) > 2
3763 assert obj.sent[0][1] == "Alert"
3764 assert all(title == "" for _, title in obj.sent[1:])
3765
3766
3767 def test_notify_overflow_payload_max_size():
3768 """NotifyBase(): Apply the optional payload cap before overflow."""
3769
3770 class TestNotification(NotifyBase):
3771 def __init__(self, *args, **kwargs):
3772 super().__init__(**kwargs)
3773
3774 def notify(self, *args, **kwargs):
3775 return True
3776
3777 body = "x" * 100
3778
3779 # The default cap is disabled in every overflow mode.
3780 obj = TestNotification(overflow=OverflowMode.UPSTREAM)
3781 chunks = obj._apply_overflow(body=body, title="")
3782 assert chunks[0].get("body") == body
3783
3784 # Turn the cap on and set it smaller than the combined title + body.
3785 asset = AppriseAsset(payload_max_size=20)
3786 obj = TestNotification(asset=asset, overflow=OverflowMode.UPSTREAM)
3787 chunks = obj._apply_overflow(body=body, title="")
3788 assert len(chunks[0].get("body")) == 20
3789
3790 # The configured cap also applies in UPSTREAM mode.
3791 assert chunks[0].get("body") == body[:20]
3792
3793 # Keep a short title whole when the body minimum still fits.
3794 guarantee_asset = AppriseAsset(payload_max_size=40)
3795 title = "T" * 9
3796 obj = TestNotification(
3797 asset=guarantee_asset, overflow=OverflowMode.UPSTREAM
3798 )
3799 chunks = obj._apply_overflow(body=body, title=title)
3800 assert chunks[0].get("title") == title
3801 assert len(chunks[0].get("body")) == 31
3802
3803 # Otherwise, share the cap proportionally.
3804 title = "T" * 20
3805 obj = TestNotification(asset=asset, overflow=OverflowMode.UPSTREAM)
3806 chunks = obj._apply_overflow(body="B" * 18, title=title)
3807 assert len(chunks[0].get("title")) == 10
3808 assert len(chunks[0].get("body")) == 10
3809
3810 # Long title and body values share the cap proportionally.
3811 long_body = "x" * 950
3812 proportional_asset = AppriseAsset(payload_max_size=100)
3813 title = "T" * 50
3814 obj = TestNotification(
3815 asset=proportional_asset, overflow=OverflowMode.UPSTREAM
3816 )
3817 chunks = obj._apply_overflow(body=long_body, title=title)
3818 assert len(chunks[0].get("title")) == 5
3819 assert len(chunks[0].get("body")) == 95
3820
3821 # A proportional split keeps at least one title character when its
3822 # share would otherwise round down to zero.
3823 title = "T" * 11
3824 obj = TestNotification(
3825 asset=proportional_asset, overflow=OverflowMode.UPSTREAM
3826 )
3827 chunks = obj._apply_overflow(body="x" * 2200, title=title)
3828 assert chunks[0].get("title") == "T"
3829 assert len(chunks[0].get("body")) == 99
3830
3831 # A body that already fits within the cap is left untouched.
3832 small_asset = AppriseAsset(payload_max_size=1000)
3833 obj = TestNotification(asset=small_asset, overflow=OverflowMode.UPSTREAM)
3834 chunks = obj._apply_overflow(body=body, title="")
3835 assert chunks[0].get("body") == body
3836
3837
3838 def test_notify_markdown_general():
3839 """
3840 API: Markdown General Testing
3841
3842 """
3843
3844 #
3845 # A little preparation
3846 #
3847
3848 #
3849 # First Test: Truncated Title
3850 #
3851 class TestMarkdownNotification(NotifyBase):
3852 # Force our title to wrap
3853 title_maxlen = 0
3854
3855 # Default Notify Format
3856 notify_format = NotifyFormat.MARKDOWN
3857
3858 def __init__(self, *args, **kwargs):
3859 super().__init__(**kwargs)
3860
3861 def notify(self, *args, **kwargs):
3862 # Pretend everything is okay
3863 return True
3864
3865 # Load our object
3866 obj = TestMarkdownNotification()
3867 assert obj is not None
3868
3869 # A bad header
3870 title = " # "
3871 body = "**Test Body**"
3872
3873 chunks = obj._apply_overflow(body="", title=title)
3874 chunks = obj._apply_overflow(body="", title="")
3875 chunks = obj._apply_overflow(body=body, title="")
3876 chunks = obj._apply_overflow(body=body, title=title)
3877 assert len(chunks) == 1
3878 # whitspace is trimmed
3879 assert chunks[0].get("body") == "#\r\n**Test Body**"
3880 assert chunks[0].get("title") == ""
3881
3882 # If we know our input is text however, we perform manipulation
3883 chunks = obj._apply_overflow(body="", title=title)
3884 chunks = obj._apply_overflow(body="", title="")
3885 chunks = obj._apply_overflow(body=body, title="")
3886 chunks = obj._apply_overflow(
3887 body=body, title=title, body_format=NotifyFormat.TEXT
3888 )
3889 assert len(chunks) == 1
3890 # Our title get's stripped off since it's not of valid markdown
3891 assert body.lstrip("\r\n\x0b\x0c").rstrip() == chunks[0].get("body")
3892 assert chunks[0].get("title") == ""
3893
3894 # Declared Markdown turns a real title into a heading.
3895 real_title = "My Title"
3896 chunks = obj._apply_overflow(
3897 body=body,
3898 title=real_title,
3899 body_format=NotifyFormat.MARKDOWN,
3900 )
3901 assert len(chunks) == 1
3902 assert chunks[0].get("body") == f"# {real_title}\n{body}"
3903 assert chunks[0].get("title") == ""
3904
3905 # Undeclared input keeps the title literal.
3906 chunks = obj._apply_overflow(body=body, title=real_title)
3907 assert len(chunks) == 1
3908 assert chunks[0].get("body") == f"{real_title}\r\n{body}"
3909 assert chunks[0].get("title") == ""
3910
3911 # Repair a bold span left open by Markdown truncation.
3912 obj.body_maxlen = 6
3913 chunks = obj._apply_overflow(
3914 body="**bold text**",
3915 title="",
3916 overflow=OverflowMode.TRUNCATE,
3917 body_format=NotifyFormat.MARKDOWN,
3918 )
3919 assert len(chunks) == 1
3920 assert chunks[0].get("body") == "**bold**"
3921
3922 # Escape an unmatched opener when the discarded tail has no closer.
3923 obj.body_maxlen = 4
3924 chunks = obj._apply_overflow(
3925 body="*_a_ and a literal * elsewhere",
3926 title="",
3927 overflow=OverflowMode.TRUNCATE,
3928 body_format=NotifyFormat.MARKDOWN,
3929 )
3930 assert len(chunks) == 1
3931 assert chunks[0].get("body") == "\\*_a_"
3932
3933 # Truncating a large body must use bounded lookahead.
3934 obj.body_maxlen = 100
3935 huge_body = "hello world " * 200000
3936 start = default_timer()
3937 chunks = obj._apply_overflow(
3938 body=huge_body,
3939 title="",
3940 overflow=OverflowMode.TRUNCATE,
3941 body_format=NotifyFormat.MARKDOWN,
3942 )
3943 elapsed = default_timer() - start
3944 assert len(chunks) == 1
3945 assert len(chunks[0].get("body")) <= 100
3946 assert elapsed < 10.0
3947
3948
3949 @mock.patch("requests.request")
3950 def test_notify_emoji_general(mock_request):
3951 """
3952 API: Emoji General Testing
3953
3954 """
3955
3956 # Prepare our response
3957 response = requests.Request()
3958 response.status_code = requests.codes.ok
3959
3960 # Prepare Mock
3961 mock_request.return_value = response
3962
3963 # Set up our emojis
3964 title = ":smile:"
3965 body = ":grin:"
3966
3967 # general reference used below (using default values)
3968 asset = AppriseAsset()
3969
3970 #
3971 # Test default emoji asset value
3972 #
3973
3974 # Load our object
3975 ap_obj = Apprise(asset=asset)
3976 ap_obj.add("json://localhost")
3977 assert len(ap_obj) == 1
3978
3979 assert bool(ap_obj.notify(title=title, body=body)) is True
3980 assert mock_request.call_count == 1
3981
3982 details = mock_request.call_args_list[0]
3983 assert details[0][0] == "POST"
3984 dataset = json.loads(details[1]["data"])
3985
3986 # No changes
3987 assert dataset["title"] == title
3988 assert dataset["message"] == body
3989
3990 mock_request.reset_mock()
3991
3992 #
3993 # Test URL over-ride while default value set in asset
3994 #
3995
3996 # Load our object
3997 ap_obj = Apprise(asset=asset)
3998 ap_obj.add("json://localhost?emojis=no")
3999 assert len(ap_obj) == 1
4000
4001 assert bool(ap_obj.notify(title=title, body=body)) is True
4002 assert mock_request.call_count == 1
4003
4004 details = mock_request.call_args_list[0]
4005 assert details[0][0] == "POST"
4006 dataset = json.loads(details[1]["data"])
4007
4008 # No changes
4009 assert dataset["title"] == title
4010 assert dataset["message"] == body
4011
4012 mock_request.reset_mock()
4013
4014 #
4015 # Test URL over-ride while default value set in asset pt 2
4016 #
4017
4018 # Load our object
4019 ap_obj = Apprise(asset=asset)
4020 ap_obj.add("json://localhost?emojis=yes")
4021 assert len(ap_obj) == 1
4022
4023 assert bool(ap_obj.notify(title=title, body=body)) is True
4024 assert mock_request.call_count == 1
4025
4026 details = mock_request.call_args_list[0]
4027 assert details[0][0] == "POST"
4028 dataset = json.loads(details[1]["data"])
4029
4030 # Emoji's are displayed
4031 assert dataset["title"] == "😄"
4032 assert dataset["message"] == "😃"
4033
4034 mock_request.reset_mock()
4035
4036 #
4037 # Test URL over-ride while default value set in asset pt 2
4038 #
4039
4040 # Load our object
4041 ap_obj = Apprise(asset=asset)
4042 ap_obj.add("json://localhost?emojis=no")
4043 assert len(ap_obj) == 1
4044
4045 assert bool(ap_obj.notify(title=title, body=body)) is True
4046 assert mock_request.call_count == 1
4047
4048 details = mock_request.call_args_list[0]
4049 assert details[0][0] == "POST"
4050 dataset = json.loads(details[1]["data"])
4051
4052 # No changes
4053 assert dataset["title"] == title
4054 assert dataset["message"] == body
4055
4056 mock_request.reset_mock()
4057
4058 #
4059 # Test Default Emoji settings
4060 #
4061
4062 # Set our interpret emoji's flag
4063 asset = AppriseAsset(interpret_emojis=True)
4064
4065 # Re-create our object
4066 ap_obj = Apprise(asset=asset)
4067
4068 # Load our object
4069 ap_obj.add("json://localhost")
4070 assert len(ap_obj) == 1
4071
4072 assert bool(ap_obj.notify(title=title, body=body)) is True
4073 assert mock_request.call_count == 1
4074
4075 details = mock_request.call_args_list[0]
4076 assert details[0][0] == "POST"
4077 dataset = json.loads(details[1]["data"])
4078
4079 # emoji's are displayed
4080 assert dataset["title"] == "😄"
4081 assert dataset["message"] == "😃"
4082
4083 mock_request.reset_mock()
4084
4085 #
4086 # With Emoji's turned on by default, the user can optionally turn them
4087 # off.
4088 #
4089
4090 # Re-create our object
4091 ap_obj = Apprise(asset=asset)
4092
4093 ap_obj.add("json://localhost?emojis=no")
4094 assert len(ap_obj) == 1
4095
4096 assert bool(ap_obj.notify(title=title, body=body)) is True
4097 assert mock_request.call_count == 1
4098
4099 details = mock_request.call_args_list[0]
4100 assert details[0][0] == "POST"
4101 dataset = json.loads(details[1]["data"])
4102
4103 # No changes
4104 assert dataset["title"] == title
4105 assert dataset["message"] == body
4106
4107 mock_request.reset_mock()
4108
4109 #
4110 # With Emoji's turned on by default, there is no change when emojis
4111 # flag is set to on
4112 #
4113
4114 # Re-create our object
4115 ap_obj = Apprise(asset=asset)
4116
4117 ap_obj.add("json://localhost?emojis=yes")
4118 assert len(ap_obj) == 1
4119
4120 assert bool(ap_obj.notify(title=title, body=body)) is True
4121 assert mock_request.call_count == 1
4122
4123 details = mock_request.call_args_list[0]
4124 assert details[0][0] == "POST"
4125 dataset = json.loads(details[1]["data"])
4126
4127 # emoji's are displayed
4128 assert dataset["title"] == "😄"
4129 assert dataset["message"] == "😃"
4130
4131 mock_request.reset_mock()
4132
4133 #
4134 # Enforce the disabling of emojis
4135 #
4136
4137 # Set our interpret emoji's flag
4138 asset = AppriseAsset(interpret_emojis=False)
4139
4140 # Re-create our object
4141 ap_obj = Apprise(asset=asset)
4142
4143 # Load our object
4144 ap_obj.add("json://localhost")
4145 assert len(ap_obj) == 1
4146
4147 assert bool(ap_obj.notify(title=title, body=body)) is True
4148 assert mock_request.call_count == 1
4149
4150 details = mock_request.call_args_list[0]
4151 assert details[0][0] == "POST"
4152 dataset = json.loads(details[1]["data"])
4153
4154 # Disabled - no emojis
4155 assert dataset["title"] == title
4156 assert dataset["message"] == body
4157
4158 mock_request.reset_mock()
4159
4160 #
4161 # Enforce the disabling of emojis
4162 #
4163
4164 # Set our interpret emoji's flag
4165 asset = AppriseAsset(interpret_emojis=False)
4166
4167 # Re-create our object
4168 ap_obj = Apprise(asset=asset)
4169
4170 # Load our object
4171 ap_obj.add("json://localhost?emojis=yes")
4172 assert len(ap_obj) == 1
4173
4174 assert bool(ap_obj.notify(title=title, body=body)) is True
4175 assert mock_request.call_count == 1
4176
4177 details = mock_request.call_args_list[0]
4178 assert details[0][0] == "POST"
4179 dataset = json.loads(details[1]["data"])
4180
4181 # Disabled - no emojis
4182 assert dataset["title"] == title
4183 assert dataset["message"] == body
4184
4185 mock_request.reset_mock()