Skip to content

Commit

Permalink
Added truncation to the formatted message sent to Discord when syncin…
Browse files Browse the repository at this point in the history
…g. Added a test case to confirm.
  • Loading branch information
Paul Philion committed Feb 28, 2024
1 parent 6e03d4e commit 2566a86
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
7 changes: 5 additions & 2 deletions netbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

log = logging.getLogger(__name__)


MAX_MESSAGE_LEN = 2000

class NetbotException(Exception):
"""netbot exception"""

Expand Down Expand Up @@ -89,8 +92,8 @@ async def gather_discord_notes(self, thread: discord.Thread, sync_rec:synctime.S
def format_discord_note(self, note):
"""Format a note for Discord"""
age = synctime.age_str(synctime.parse_str(note.created_on))
return f"> **{note.user.name}** *{age} ago*\n> {note.notes}\n\n"
#TODO Move format table
message = f"> **{note.user.name}** *{age} ago*\n> {note.notes}"[:MAX_MESSAGE_LEN]
return message


def gather_redmine_notes(self, ticket, sync_rec:synctime.SyncRecord):
Expand Down
35 changes: 34 additions & 1 deletion test_netbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import unittest
import logging
import discord

import discord
from dotenv import load_dotenv

import netbot
Expand Down Expand Up @@ -70,6 +70,34 @@ async def test_synchronize_ticket(self):
self.redmine.remove_ticket(ticket.id)


async def test_sync_ticket_long_message(self):
# create a new ticket, identified by the tag, with a note
ticket = self.create_test_ticket()

long_message = test_utils.randstr(3000) # random string 3000 chars long
self.redmine.append_message(ticket.id, self.user.login, long_message)

# create mock message and thread
message = unittest.mock.AsyncMock(discord.Message)
message.content = f"This is a new note about ticket #{ticket.id} for test {self.tag}"
message.author = unittest.mock.AsyncMock(discord.Member)
message.author.name = self.discord_user

thread = unittest.mock.AsyncMock(discord.Thread)
thread.name = f"Ticket #{ticket.id}"

# synchronize!
await self.bot.synchronize_ticket(ticket, thread)

# assert method send called on mock thread, with the correct values
log.debug(f"### call args: {thread.send.call_args}")
self.assertIn(self.tag, thread.send.call_args.args[0])
self.assertLessEqual(len(thread.send.call_args.args[0]), netbot.MAX_MESSAGE_LEN, "Message sent to Discord is too long")

# clean up
self.redmine.remove_ticket(ticket.id)


async def test_on_application_command_error(self):
ctx = self.build_context()
error = discord.DiscordException("this is exception " + self.tag)
Expand All @@ -79,4 +107,9 @@ async def test_on_application_command_error(self):


if __name__ == '__main__':
# when running this main, turn on DEBUG
logging.basicConfig(level=logging.DEBUG, format="{asctime} {levelname:<8s} {name:<16} {message}", style='{')
logging.getLogger("urllib3.connectionpool").setLevel(logging.INFO)
logging.getLogger("asyncio").setLevel(logging.ERROR)

unittest.main()
7 changes: 7 additions & 0 deletions test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"""Utilities to help testing"""

import time
import string
import random
import logging
import unittest
from unittest import mock
Expand Down Expand Up @@ -37,6 +39,11 @@ def tagstr() -> str:
"""convert the current timestamp in seconds to a base36 str"""
return dumps(int(time.time()))


def randstr(length:int=12) -> str:
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))


def create_test_user(redmine:Client, tag:str):
# create new test user name: [email protected], login test-12345
first = "test-" + tag
Expand Down

0 comments on commit 2566a86

Please sign in to comment.