LOGIN and LOGOUT ircbot tests

Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
Brian S. Stephan 2024-05-18 08:26:55 -05:00
parent e9b985bd79
commit 0ad687669e
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
2 changed files with 94 additions and 1 deletions

View File

@ -108,7 +108,25 @@ class IdleRPG(Plugin):
self.bot.reply(None, f"{character}, is now online from nickname {nick}. "
f"Next level at {character.next_level_str()}.",
explicit_target=character.game.channel.name)
return self.bot.reply(event, f"{character} has been successfully logged in.")
return self.bot.reply(event, f"{character}, has been successfully logged in.")
def handle_logout(self, connection, event, match):
"""Log out a character when requested."""
hostmask = event.source
try:
character = Character.objects.get(enabled=True, hostmask=hostmask)
except Character.DoesNotExist:
return self.bot.reply(event, "You do not have a character in a running game.")
if character.status != Character.CHARACTER_STATUS_LOGGED_IN:
return self.bot.reply(event, f"Cannot log out {character.name}, either they already are, "
"or they are in a state that cannot be modified.")
character.log_out()
penalty = character.penalize(20, "logging out")
character.time_penalized_logout += penalty
character.save()
return self.bot.reply(event, f"{character}, has been successfully logged out.")
def handle_remove(self, connection, event, match):
"""Handle the disabling of a character."""

View File

@ -257,6 +257,81 @@ class IrcPluginTest(TestCase):
self.plugin.seen_hostmasks.remove('bss!bss@test_login')
def test_login(self):
"""Check that the LOGIN command actually sets the character as logged in."""
mock_event = mock.MagicMock()
mock_event.source = 'bss!bss@bss_login'
mock_event.recursing = False
game = Game.objects.get(pk=2)
test_char = Character.objects.register('test_login', game, 'test', 'bss!bss@bss_login', 'tester')
test_char.log_out()
test_char.next_level = datetime.datetime.fromisoformat('2024-05-17 17:00:00-00:00')
test_char.last_login = test_char.next_level
test_char.save()
self.plugin.seen_hostmasks.add('bss!bss@bss_login')
# manipulate login so it doesn't add time to next_level and so we have a consistent
# value to test against below
with mock.patch('django.utils.timezone.now', return_value=test_char.next_level):
match = re.match(IdleRPG.LOGIN_COMMAND_PATTERN, 'LOGIN test_login test')
self.plugin.handle_login(self.mock_connection, mock_event, match)
# refetch
refetch = Character.objects.get(name='test_login')
# should have been penalized
assert refetch.status == Character.CHARACTER_STATUS_LOGGED_IN
# the ordering of this surprises me... keep an eye on it
self.mock_bot.reply.assert_has_calls([
mock.call(None, "test_login, the level 0 tester, is now online from nickname bss. "
"Next level at 2024-05-17 17:00:00 UTC.", explicit_target='#level_test'),
mock.call(mock_event, "test_login, the level 0 tester, has been successfully logged in."),
])
def test_logout_character_not_found(self):
"""Test the LOGOUT command sent to the bot."""
mock_event = mock.MagicMock()
mock_event.source = 'bss!bss@test_login'
mock_event.recursing = False
match = re.match(IdleRPG.LOGOUT_COMMAND_PATTERN, 'LOGOUT')
self.plugin.handle_logout(self.mock_connection, mock_event, match)
self.mock_bot.reply.assert_called_once_with(mock_event, "You do not have a character in a running game.")
def test_logout_character_not_logged_in(self):
"""Test the LOGOUT command sent to the bot."""
mock_event = mock.MagicMock()
mock_event.source = 'bss2!bss@bss'
mock_event.recursing = False
match = re.match(IdleRPG.LOGOUT_COMMAND_PATTERN, 'LOGOUT')
self.plugin.handle_logout(self.mock_connection, mock_event, match)
self.mock_bot.reply.assert_called_once_with(
mock_event,
"Cannot log out bss2, either they already are, or they are in a state that cannot be modified."
)
def test_logout(self):
"""Check that the LOGOUT command actually sets the character offline."""
mock_event = mock.MagicMock()
mock_event.source = 'bss!bss@bss_logout'
mock_event.recursing = False
game = Game.objects.get(pk=2)
test_char = Character.objects.register('test_logout', game, 'test', 'bss!bss@bss_logout', 'tester')
match = re.match(IdleRPG.LOGOUT_COMMAND_PATTERN, 'LOGOUT')
self.plugin.handle_logout(self.mock_connection, mock_event, match)
# refetch
refetch = Character.objects.get(name='test_logout')
# should have been penalized
assert refetch.next_level > test_char.next_level
assert refetch.status == Character.CHARACTER_STATUS_OFFLINE
self.mock_bot.reply.assert_called_once_with(
mock_event, "test_logout, the level 0 tester, has been successfully logged out."
)
def test_remove(self):
"""Test the remove command."""
mock_event = mock.MagicMock()