Skip to content

Commit 7a8faf1

Browse files
authored
Merge branch 'main' into dependabot/pip/sqlparse-0.5.4
2 parents a0d3d20 + e4a6d7f commit 7a8faf1

2 files changed

Lines changed: 106 additions & 6 deletions

File tree

src/cogs/admin/test_announcements.py

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,29 @@
66
from discord.app_commands import Choice
77

88
from src.cogs.admin.announcements import Announcements
9-
from src.ui.modals.announcement import Announcement
9+
from src.ui.modals.announcement import Announcement, _format_announcement
10+
11+
12+
class TestAnnouncementFormatting(unittest.TestCase):
13+
def test_format_announcement_with_title_uses_single_newline(self):
14+
announcement = _format_announcement("my title", "test")
15+
16+
self.assertEqual(announcement, "# my title\ntest")
17+
18+
def test_format_announcement_with_title_strips_leading_line_breaks_from_content(self):
19+
announcement = _format_announcement("my title", "\n\ntest")
20+
21+
self.assertEqual(announcement, "# my title\ntest")
22+
23+
def test_format_announcement_without_title_has_no_leading_newline(self):
24+
announcement = _format_announcement("", "test")
25+
26+
self.assertEqual(announcement, "test")
27+
28+
def test_format_announcement_with_whitespace_title_treats_it_as_missing(self):
29+
announcement = _format_announcement(" ", "test")
30+
31+
self.assertEqual(announcement, "test")
1032

1133

1234
class TestAnnouncements(IsolatedAsyncioTestCase):
@@ -49,5 +71,73 @@ async def test_announce_command(self, mock_wait):
4971
self.assertIsInstance(sent_modal, Announcement)
5072
mock_wait.assert_awaited_once()
5173

74+
async def test_on_submit_regular_submission_uses_single_newline(self):
75+
mock_channel = MagicMock()
76+
mock_channel.send = AsyncMock()
77+
78+
mock_interaction = MagicMock(spec=Interaction)
79+
mock_interaction.response = MagicMock()
80+
mock_interaction.response.is_done = MagicMock(return_value=False)
81+
mock_interaction.response.send_message = AsyncMock()
82+
mock_interaction.followup = MagicMock()
83+
mock_interaction.followup.send = AsyncMock()
84+
85+
modal = Announcement(None, mock_channel, "regular")
86+
modal.announcement_title = MagicMock(value="my title")
87+
modal.announcement = MagicMock(value="test")
88+
89+
await modal.on_submit(mock_interaction)
90+
91+
mock_channel.send.assert_awaited_once_with(
92+
content="# my title\ntest",
93+
file=None
94+
)
95+
96+
async def test_on_submit_regular_submission_drops_leading_line_breaks_in_content(self):
97+
mock_channel = MagicMock()
98+
mock_channel.send = AsyncMock()
99+
100+
mock_interaction = MagicMock(spec=Interaction)
101+
mock_interaction.response = MagicMock()
102+
mock_interaction.response.is_done = MagicMock(return_value=False)
103+
mock_interaction.response.send_message = AsyncMock()
104+
mock_interaction.followup = MagicMock()
105+
mock_interaction.followup.send = AsyncMock()
106+
107+
modal = Announcement(None, mock_channel, "regular")
108+
modal.announcement_title = MagicMock(value="my title")
109+
modal.announcement = MagicMock(value="\n\ntest")
110+
111+
await modal.on_submit(mock_interaction)
112+
113+
mock_channel.send.assert_awaited_once_with(
114+
content="# my title\ntest",
115+
file=None
116+
)
117+
118+
async def test_on_submit_embed_submission_skips_leading_newline_without_title(self):
119+
mock_channel = MagicMock()
120+
mock_channel.send = AsyncMock()
121+
122+
mock_interaction = MagicMock(spec=Interaction)
123+
mock_interaction.response = MagicMock()
124+
mock_interaction.response.is_done = MagicMock(return_value=False)
125+
mock_interaction.response.send_message = AsyncMock()
126+
mock_interaction.followup = MagicMock()
127+
mock_interaction.followup.send = AsyncMock()
128+
129+
modal = Announcement(None, mock_channel, "embed")
130+
modal.announcement_title = MagicMock(value=" ")
131+
modal.announcement = MagicMock(value="test")
132+
133+
await modal.on_submit(mock_interaction)
134+
135+
mock_channel.send.assert_awaited_once()
136+
self.assertEqual(
137+
mock_channel.send.await_args.kwargs["embed"].description,
138+
"test"
139+
)
140+
self.assertIsNone(mock_channel.send.await_args.kwargs["file"])
141+
52142
if __name__ == "__main__":
53143
unittest.main()

src/ui/modals/announcement.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ def _unique(iterable: list) -> list:
2727
return r
2828

2929

30+
def _format_announcement(title: str, content: str) -> str:
31+
"""Build the outgoing announcement body."""
32+
title = title.strip()
33+
34+
if title:
35+
content = content.lstrip("\r\n")
36+
return f"# {title}\n{content}"
37+
38+
return content
39+
40+
3041
class Announcement(Modal, title='Announcement'):
3142
announcement_title = TextInput(
3243
label='Title',
@@ -57,15 +68,14 @@ def __init__(
5768

5869
async def on_submit(self, interaction: Interaction) -> None:
5970
photo = None
60-
announcement_title = ""
6171

6272
if self.attachment: # If the user has uploaded an attachment
6373
photo = await self.attachment.to_file()
6474

65-
if self.announcement_title.value:
66-
announcement_title = f"**{self.announcement_title.value}**"
67-
68-
announcement = announcement_title + f'\n\n{self.announcement.value}'
75+
announcement = _format_announcement(
76+
self.announcement_title.value,
77+
self.announcement.value
78+
)
6979

7080
if self.mention:
7181
selection_view = AnnouncementView()

0 commit comments

Comments
 (0)