Debian Codegraph @

tests/test_plugin_base_formatting.py

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