def test_execute__silence_awake_server(self, awake_patch): time = datetime.datetime.now() server_json = { 'server_id': 1, 'awake': True, 'timeout_duration_seconds': 1800, 'prefix': '!vt', 'banned_words': [{ 'rowid': 1, 'server_id': 1, 'banned_word': 'vore', 'infracted_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'calledout_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'record': { 'record_seconds': 2400, 'infraction_count': 0 } }] } server = DiscordServer(server_json, time, None) self.command.execute(server, None, None, self.author) awake_patch.assert_called_with(False) self.assertTrue(awake_patch.called)
def test_execute__multiple_words(self): time = datetime.datetime.now() server_json = { 'server_id' : 1, 'awake' : True, 'timeout_duration_seconds': 1800, 'prefix': '!vt', 'banned_words': [{ 'rowid': 1, 'server_id': 1, 'banned_word': 'vore', 'infracted_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'calledout_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'record': { 'record_seconds': 2400, 'infraction_count': 0 } }, { 'rowid': 2, 'server_id': 1, 'banned_word': 'test', 'infracted_at': (time - datetime.timedelta(minutes=40)).strftime("%Y-%m-%d %H:%M:%S"), 'calledout_at': (time - datetime.timedelta(minutes=40)).strftime("%Y-%m-%d %H:%M:%S"), 'record': { 'record_seconds': 2400, 'infraction_count': 0 } }] } server = DiscordServer(server_json, time, None) self.assertEqual( self.command.execute(server, time, "!vt", None), "1, 'vore': 20 minutes and 0 seconds.\n" + "2, 'test': 40 minutes and 0 seconds.")
def test_execute__one_word_record_is_current(self): time = datetime.datetime.now() server_json = { 'server_id': 1, 'awake': True, 'timeout_duration_seconds': 1800, 'prefix': '!vt', 'banned_words': [{ 'rowid': 1, 'server_id': 1, 'banned_word': 'vore', 'infracted_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'calledout_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'record': { 'record_seconds': 600, 'infraction_count': 20 } }] } server = DiscordServer(server_json, time, None) self.assertEqual( self.command.execute(server, time, "!vtr", None), "The server has said 'vore' 20 times.\nThe server's currently on its longest streak without saying 'vore': 20 minutes and 0 seconds." )
def test_execute__change_ban_invalid(self, word_patch): time = datetime.datetime.now() server_json = { 'server_id': 1, 'awake': True, 'timeout_duration_seconds': 1800, 'prefix': '!vt', 'banned_words': [{ 'rowid': 1, 'server_id': 1, 'banned_word': 'vore', 'infracted_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'calledout_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'record': { 'record_seconds': 2400, 'infraction_count': 0 } }] } message = Mock( **{ 'server': Mock(**{'id': 1}), 'content': "!vtban 1", 'author': Mock(**{ 'id': 2, 'mention': "@test", 'bot': False }), }) message2 = Mock( **{ 'server': Mock(**{'id': 1}), 'content': "!vtban asdf asdf", 'author': Mock(**{ 'id': 2, 'mention': "@test", 'bot': False }), }) server = DiscordServer(server_json, time, None) self.command.execute(server, time, message.content, message.author) self.command.execute(server, time, message2.content, message.author) self.assertFalse(word_patch.called)
def fetch_server_from_api(server_id, current_time, session): response = session.get(API_BASE_URL + 'v1/servers/' + str(server_id)) if (not response.ok): response = session.post( API_BASE_URL + 'v1/servers', json = {'server_id': server_id}) if (response.ok): jData = json.loads(response.content) return DiscordServer(jData, current_time, session)
def test_execute__change_no_time_invalid(self, prefix_patch): message = Mock( **{ 'server': Mock(**{'id': 1}), 'content': "!vtprefix", 'author': Mock(**{ 'id': 2, 'mention': "@test", 'bot': False }), }) server = DiscordServer(self.server_json, self.time, None) self.command.execute(server, self.time, message.content, message.author) self.assertFalse(prefix_patch.called)
def test_execute__change_ban_failed(self, word_patch): time = datetime.datetime.now() server_json = { 'server_id': 1, 'awake': True, 'timeout_duration_seconds': 1800, 'prefix': '!vt', 'banned_words': [{ 'rowid': 1, 'server_id': 1, 'banned_word': 'vore', 'infracted_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'calledout_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'record': { 'record_seconds': 2400, 'infraction_count': 0 } }] } message = Mock( **{ 'server': Mock(**{'id': 1}), 'content': "!vtban 1 Vore", 'author': Mock(**{ 'id': 2, 'mention': "@test", 'bot': False }), }) server = DiscordServer(server_json, time, None) result = self.command.execute(server, time, message.content, message.author) word_patch.assert_called_with('Vore') self.assertEqual( result, "Sorry, I couldn't swap out for this word. It might be confusable for an existing ban?" )
def test_execute__remove_ban_failed(self, word_patch): time = datetime.datetime.now() server_json = { 'server_id': 1, 'awake': True, 'timeout_duration_seconds': 1800, 'prefix': '!vt', 'banned_words': [{ 'rowid': 1, 'server_id': 1, 'banned_word': 'vore', 'infracted_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'calledout_at': (time - datetime.timedelta(minutes=20)).strftime("%Y-%m-%d %H:%M:%S"), 'record': { 'record_seconds': 2400, 'infraction_count': 0 } }] } message = Mock( **{ 'server': Mock(**{'id': 1}), 'content': "!vtunban vore", 'author': Mock(**{ 'id': 2, 'mention': "@test", 'bot': False }), }) server = DiscordServer(server_json, time, None) result = self.command.execute(server, time, message.content, message.author) self.assertEqual( result, "Sorry, I couldn't unban that word. You may have tried to unban your only banned word." )
def test_execute__change_prefix_too_long(self, prefix_patch): prefix_patch.return_value = False message = Mock( **{ 'server': Mock(**{'id': 1}), 'content': "!vtprefix asdfasdfasdf", 'author': Mock(**{ 'id': 2, 'mention': "@test", 'bot': False }), }) server = DiscordServer(self.server_json, self.time, None) retval = self.command.execute(server, self.time, message.content, message.author) self.assertEqual( retval, "Sorry, I don't understand that formatting. I was expecting a new prefix between 1 and 10 characters long." )
def test_execute__change_full_time_valid(self, prefix_patch): message = Mock( **{ 'server': Mock(**{'id': 1}), 'content': "!vtprefix !testin", 'author': Mock(**{ 'id': 2, 'mention': "@test", 'bot': False }), }) server = DiscordServer(self.server_json, self.time, None) retval = self.command.execute(server, self.time, message.content, message.author) prefix_patch.assert_called_with({'prefix': '!testin'}) self.assertEqual( retval, "Cool, from now on you'll need to start a message with '!testin' for me to treat it as a command." ) self.assertTrue(prefix_patch.called)