def test_image_handler_update_works_when_no_image_filters(self): notification = ImageNotification() updater = ImageNotificationUpdater() updater.update(notification, None) self.assertIsNone(notification.get_image_path())
def test_get_image_returns_none_when_no_image_filters(self): notification = ImageNotification() updater = ImageNotificationUpdater() updater.update(notification, None) self.assertIsNone(notification.get_image_path())
def test_handler_created_with_default_image(self): self.http_server.serve_content("<IMAGE CONTENT>") config = { "name": "image-depending-on-message-content", "default-image": "%s/default-image.png" % self.http_server.url } downloader = FileDownloader() creator = ImageNotificationUpdaterConfig(downloader) self.assertTrue(creator.can_create(config)) try: updater = creator.create(config) notification = ImageNotification() updater.update(notification, None) downloaded_files = downloader.get_downloaded_files() self.assertEqual(1, len(downloaded_files)) download_path = downloaded_files[0] self.assertEqual( download_path, notification.get_image_path(), "Image path in handler matches path to downloaded file") finally: self.cleanup_downloaded_files(downloader, "default-image.png")
def test_get_image_returns_default_image_when_no_messages_match(self): notification = ImageNotification() updater = ImageNotificationUpdater() updater.add_image_filter("/tmp/default.png") updater.update(notification, None) self.assertEqual('/tmp/default.png', notification.get_image_path())
def test_text_centred_in_console(self, console_size, input_image, expected_ascii_terminal): expected_terminal = AsciiTerminal.extract_text(expected_ascii_terminal) image_notification = ImageNotification() image_notification.set_image_path(input_image) cmd = create_cmd(lambda: ConsoleDisplay(console_size).draw(image_notification)) result = CliRunner().invoke(cmd, catch_exceptions=False) self.assertEqual(expected_terminal, result.output)
def test_get_image_returns_previous_image_when_no_messages_match(self): notification = ImageNotification() updater = ImageNotificationUpdater() updater.add_image_filter("/tmp/happy.png", ContainsTextFilter("123")) updater.add_image_filter("/tmp/default.png") updater.update(notification, Message("123")) self.assertEqual('/tmp/happy.png', notification.get_image_path()) updater.update(notification, Message("Test")) self.assertEqual('/tmp/happy.png', notification.get_image_path())
def test_get_image_returns_correct_url_for_filtered_messages(self): notification = ImageNotification() updater = ImageNotificationUpdater() updater.add_image_filter("/tmp/happy.png", ContainsTextFilter("123")) updater.add_image_filter("/tmp/sad.png", MatchesRegexFilter("[4-6]+")) updater.update(notification, Message("123")) self.assertEqual('/tmp/happy.png', notification.get_image_path()) updater.update(notification, Message("456")) self.assertEqual('/tmp/sad.png', notification.get_image_path())
def test_handler_created_with_default_image_path_encoded(self): file_contents = str(uuid.uuid4()) self.http_server.serve_content(file_contents) config = { "name": "image-depending-on-message-content", "default-image": "%s/default image.png" % self.http_server.url } downloader = FileDownloader() creator = ImageNotificationUpdaterConfig(downloader) try: updater = creator.create(config) notification = ImageNotification() updater.update(notification, None) self.assertIsNotNone(notification.get_image_path()) with open(notification.get_image_path(), 'r') as f: self.assertEquals(file_contents, f.read()) finally: self.cleanup_downloaded_files(downloader, "default20image.png")