Coverage for app/backend/src/tests/test_email_rendering.py: 100%

50 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-12-02 09:23 +0000

1""" 

2The text of every email block makes it into both the plaintext and HTML renderings, 

3so that tests of specific emails can check the plaintext rendering only. 

4""" 

5 

6from datetime import UTC 

7 

8from markupsafe import Markup 

9 

10from couchers.email.blocks import ( 

11 ActionBlock, 

12 ActionPairBlock, 

13 EmailBlock, 

14 EmailFooter, 

15 ParaBlock, 

16 QuoteBlock, 

17 UserBlock, 

18 UserInfo, 

19) 

20from couchers.email.rendering import render_html_body, render_plaintext_body 

21from couchers.i18n import LocalizationContext 

22 

23 

24def _render_block(block: EmailBlock) -> tuple[str, str]: 

25 """Renders an email consisting of a single block, returning its plaintext and HTML bodies.""" 

26 loc_context = LocalizationContext(locale="en", timezone=UTC) 

27 footer = EmailFooter(unsubscribe_info=None) 

28 plain = render_plaintext_body(blocks=[block], footer=footer, loc_context=loc_context) 

29 html = render_html_body(subject="Subject", preview=None, blocks=[block], footer=footer, loc_context=loc_context) 

30 return plain, html 

31 

32 

33def test_render_para_block(): 

34 plain, html = _render_block(ParaBlock(text="Plain paragraph")) 

35 assert "Plain paragraph" in plain 

36 assert "Plain paragraph" in html 

37 

38 plain, html = _render_block(ParaBlock(text=Markup("Paragraph with <b>bold</b> markup"))) 

39 assert "Paragraph with bold markup" in plain 

40 assert "Paragraph with <b>bold</b> markup" in html 

41 

42 

43def test_render_user_block(): 

44 plain, html = _render_block( 

45 UserBlock( 

46 info=UserInfo( 

47 name="Bob", 

48 age=30, 

49 city="Berlin, Germany", 

50 avatar_url="https://example.com/avatar.png", 

51 profile_url="https://example.com/user/bob", 

52 ), 

53 comment="User comment", 

54 ) 

55 ) 

56 assert "Bob" in plain 

57 assert "Bob" in html 

58 assert "Berlin, Germany" in plain 

59 assert "Berlin, Germany" in html 

60 assert "User comment" in plain 

61 assert "User comment" in html 

62 # Images are HTML-only 

63 assert "https://example.com/avatar.png" in html 

64 

65 

66def test_render_quote_block(): 

67 plain, html = _render_block(QuoteBlock(text="Quoted message", markdown=False)) 

68 assert "Quoted message" in plain 

69 assert "Quoted message" in html 

70 

71 plain, html = _render_block(QuoteBlock(text="Quoted **markdown** message", markdown=True)) 

72 assert "Quoted **markdown** message" in plain 

73 assert "Quoted <strong>markdown</strong> message" in html 

74 

75 

76def test_render_action_block(): 

77 plain, html = _render_block(ActionBlock(text="Single action", target_url="https://example.com/action")) 

78 assert "Single action" in plain 

79 assert "Single action" in html 

80 assert "https://example.com/action" in plain 

81 assert "https://example.com/action" in html 

82 

83 

84def test_render_action_pair_block(): 

85 plain, html = _render_block( 

86 ActionPairBlock( 

87 primary_text="Primary action", 

88 primary_target_url="https://example.com/primary", 

89 secondary_text="Secondary action", 

90 secondary_target_url="https://example.com/secondary", 

91 ) 

92 ) 

93 assert "Primary action" in plain 

94 assert "Primary action" in html 

95 assert "https://example.com/primary" in plain 

96 assert "https://example.com/primary" in html 

97 assert "Secondary action" in plain 

98 assert "Secondary action" in html 

99 assert "https://example.com/secondary" in plain 

100 assert "https://example.com/secondary" in html