def test_output_created_with_path_and_pattern(self): self.http_server.serve_content("<IMAGE CONTENT>") downloader = FileDownloader() config = ImageDependingOnMessageContentCreator(downloader) options = { "images": [{ "path": "%s/test-filename-1.png" % self.http_server.url, "if-matches": "test pattern1" }] } try: config = config.create(options, self._EMPTY_SECRET_STORE) self.assertIsInstance(config, ImageDependingOnMessageContent) image_filters = config.filtered_images self.assertEqual(1, len(image_filters), "Single image has been configured") image_and_filter = image_filters[0] downloaded_files = downloader.get_downloaded_files() self.assertEqual(1, len(downloaded_files), "Single image has been downloaded") download_path = downloaded_files[0] self.assertEqual( download_path, image_and_filter["path"], "Image path in handler matches path to downloaded file") self.assertEqual("test pattern1", str(image_and_filter["filter"].pattern), "Image filter's pattern matches configuration") finally: self.cleanup_downloaded_files(downloader, "test-filename-1.png")
def test_exception_raised_when_image_missing_path_in_options(self): config = ImageDependingOnMessageContentCreator() options = {"images": [{}]} with pytest.raises(MissingRequiredOptionException) as exception: config.create(options, {}) self.assertEqual("Expected 'path' option to exist", exception.value.message)
def test_exception_raised_when_no_image_list_and_default_image_in_options( self): config = ImageDependingOnMessageContentCreator() with pytest.raises(MissingRequiredOptionException) as exception: config.create(self._EMPTY_OPTIONS, self._EMPTY_SECRET_STORE) self.assertEqual( "Expected 'images' list and/or default-image to exist", exception.value.message)
def test_exception_raised_when_image_in_list_missing_pattern_and_contains( self): config = ImageDependingOnMessageContentCreator() options = {"images": [{"path": "test"}]} with pytest.raises(MissingRequiredOptionException) as exception: config.create(options, {}) self.assertEqual( "Expected either 'if-contains' or 'if-matches' option to exist", exception.value.message)
def test_notification_encodes_image_path_in_output(self): file_contents = str(uuid.uuid4()) self.http_server.serve_content(file_contents) downloader = FileDownloader() config = ImageDependingOnMessageContentCreator(downloader) options = { "default-image": "%s/default image.png" % self.http_server.url } try: notification = config.create(options, self._EMPTY_SECRET_STORE) image_output = notification.create([]) self.assertIsNotNone(image_output.image_path) with open(image_output.image_path, 'r') as f: self.assertEquals(file_contents, f.read()) finally: self.cleanup_downloaded_files(downloader, "default20image.png")
def test_output_created_with_default_image(self): self.http_server.serve_content("<IMAGE CONTENT>") downloader = FileDownloader() config = ImageDependingOnMessageContentCreator(downloader) options = { "default-image": "%s/default-image.png" % self.http_server.url } try: notification = config.create(options, self._EMPTY_SECRET_STORE) downloaded_files = downloader.get_downloaded_files() self.assertEqual(1, len(downloaded_files)) download_path = downloaded_files[0] self.assertEqual( download_path, notification.default_image, "Image path in handler matches path to downloaded file") finally: self.cleanup_downloaded_files(downloader, "default-image.png")