def test_disable_cleaning_on_save(self): logo = Image.objects.create( title="logo", file=get_test_image_file(colour='white'), ) poster = Image.objects.create( title="poster", file=get_test_image_file(colour='white'), ) self.assertCanCreate(self.home, StoryPage, nested_form_data({ 'title': "Wagtail spotting", 'publisher': "Torchbox", 'publisher_logo': logo.id, 'poster_image': poster.id, 'pages': streamfield([ ('page', { 'id': 'cover', 'html': """ <amp-story-page id="cover"> <amp-story-grid-layer template="vertical"> <h1>Wagtail spotting</h1> <script>alert("boo!")</script> </amp-story-grid-layer> </amp-story-page> """ }), ]) })) page = StoryPage.objects.get(title="Wagtail spotting") cover_html = page.pages[0].value['html'].source self.assertIn("<h1>Wagtail spotting</h1>", cover_html) self.assertIn('<script>alert("boo!")</script>', cover_html)
def test_create_with_local_images(self): logo = Image.objects.create( title="logo", file=get_test_image_file(colour='white'), ) poster = Image.objects.create( title="poster", file=get_test_image_file(colour='white'), ) story_page = StoryPage( title="Wagtail spotting", slug="wagtail-spotting", publisher="Torchbox", publisher_logo=logo, poster_image=poster, ) story_page.custom_css = """ #cover {background-color: #eee;} """ story_page.pages = self.page_data self.home.add_child(instance=story_page) self.assertTrue( story_page.publisher_logo_src.startswith( 'http://media.example.com/media/images/')) self.assertTrue( story_page.poster_portrait_src.startswith( 'http://media.example.com/media/images/'))
def test_process_files(slack_receiver, slack_image_message, mocker): message = slack_image_message["event"] files = slack_receiver.get_message_files(message=message) image = files[0] image_content = get_test_image_file( filename=slack_receiver.get_image_name(image=image), size=slack_receiver.get_image_dimensions(image=image), ) mocker.patch.object(slack_receiver, "get_image_content", return_value=image_content) live_post = blocks.construct_live_post_block( message_id="1234", created=datetime(1970, 1, 1, 12, 00), ) slack_receiver.process_files(live_post=live_post, files=files) post_content = live_post["content"] assert post_content[0].block_type == IMAGE post_value = post_content[0].value assert isinstance(post_value, Image) assert post_value.title == image["title"] assert (post_value.width, post_value.height) == slack_receiver.get_image_dimensions( image=image)
def test_create_message_with_image(self): self.assertEqual(Image.objects.count(), 0) response = self.client.post( "/webapp/api/messages/", { "channel": self.channel_name, "content": "Some content", "images": [get_test_image_file()], }, ) self.assertEqual(response.status_code, 201) self.assertEqual(Image.objects.count(), 1) image_model = Image.objects.first() image = image_model.image self.assertEqual( response.context["message"]["images"], [{ "id": image_model.id, "image": { "name": image.name, "url": image.url, "width": image.width, "height": image.height, }, }], ) # Cleanup image.delete()
def test_add_media_message_wrong_channel( telegram_receiver, telegram_media_message_1, telegram_media_message_2, blog_page_factory, mocker, ): page = blog_page_factory(channel_id="100") assert len(page.live_posts) == 0 mocker.patch.object(TelegramWebhookReceiver, "get_file_path", return_value="photos/file_1.jpg") mocker.patch.object(TelegramWebhookReceiver, "get_image_content", return_value=get_test_image_file()) telegram_receiver.dispatch_event(event=telegram_media_message_1) page.refresh_from_db() assert len(page.live_posts) == 1 post_content = page.live_posts[0].value["content"] assert len(post_content) == 2 assert post_content[1].block_type == IMAGE telegram_media_message_2["message"]["chat"]["id"] = "99" telegram_receiver.dispatch_event(event=telegram_media_message_2) page.refresh_from_db() assert len(page.live_posts) == 1 post_content = page.live_posts[0].value["content"] assert len(post_content) == 2
def test_get_image_content(webapp_receiver, image): channel = Channel.objects.create(channel_name="test_channel") message = Message.objects.create(channel=channel, content="Some content") image_content = get_test_image_file(filename="test.png", size=(100, 100)) image_model = Image.objects.create(message=message, image=image_content) got = webapp_receiver.get_image_content(image=image) assert got == image_model.image # Cleanup got.delete()
def test_edit_message_with_image(self): self.assertEqual(Message.objects.get(id=1).images.count(), 0) data = { "channel": self.channel_name, "content": "Edited content", } files = {"images": [get_test_image_file()]} with patch.object(Request, "_parse", return_value=(data, files)): response = self.client.put("/webapp/api/messages/1/", {}) self.assertEqual(response.status_code, 200) self.assertEqual(Message.objects.get(id=1).images.count(), 1) # Cleanup Image.objects.first().image.delete()
def setUp(self): channel = Channel.objects.create(channel_name="test_channel") message = Message.objects.create(channel=channel, content="Some content") image_content = get_test_image_file() self.image = Image.objects.create(message=message, image=image_content)
def setUp(self): shutil.rmtree(TEST_MEDIA_DIR, ignore_errors=True) self.home = Site.objects.get().root_page self.mountain_wagtail = Image.objects.create( title="Mountain wagtail", file=get_test_image_file(filename='mountain-wagtail.png', colour='grey'), ) self.wagtail_video = Media.objects.create( title="Wagtail in flight", # not actually a video file, but nobody's checking so it's good enough for a test file=get_test_image_file(filename='wagtail-in-flight.webm', colour='green'), duration=0, ) self.page_data = [('page', { 'id': 'cover', 'html': """ <amp-story-page id="cover"> <amp-story-grid-layer template="vertical"> <h1>Wagtail spotting</h1> </amp-story-grid-layer> </amp-story-page> """ }), ('page', { 'id': 'page-1', 'html': """ <amp-story-page id="page-1"> <amp-story-grid-layer template="vertical"> <p>Today we went out wagtail spotting</p> <amp-img src="/pied-wagtail.jpg" alt="A pied wagtail"> </amp-img> <amp-img data-wagtail-image-id="%d" alt="A mountain wagtail"> </amp-img> <amp-img src="https://example.com/broken.jpg" alt="A broken image"> </amp-img> </amp-story-grid-layer> </amp-story-page> """ % self.mountain_wagtail.id }), ('page', { 'id': 'page-2', 'html': """ <amp-story-page id="page-2"> <amp-story-grid-layer template="vertical"> <amp-video poster="/wagtail-poster.png" width="600" height="800"> <source data-wagtail-media-id="%d" type="video/webm" /> <source src="/wagtail-in-flight.mp4" type="video/mp4" /> <source src="https://example.com/broken.mp4" type="video/mp4" /> </amp-video> </amp-story-grid-layer> </amp-story-page> """ % self.wagtail_video.id })]
def test_save_live_page_different_images(blog_page_factory): # Setup signal callback count = 0 _channel_id = _renders = _removals = None def callback(sender, channel_id, renders, removals, **kwargs): nonlocal count, _channel_id, _renders, _removals _channel_id, _renders, _removals = channel_id, renders, removals count += 1 live_page_update.connect(callback) live_posts = json.dumps([ { "type": "live_post", "id": "some-id", "value": { "message_id": "some-id", "created": "2021-01-01T12:00:00", "modified": None, "show": True, "content": [ { "type": "image", "value": "1", }, ], }, }, ]) page = blog_page_factory(channel_id="channel_id", live_posts=live_posts) last_updated_at = page.last_updated_at # Replace image post = page.get_live_post_by_index(0) image1 = get_image_model().objects.create(title="image1", file=get_test_image_file( filename="image1.png", size=(100, 100))) post.value["content"][0].value = image1 try: page.save() # last_updated_at field is modified. assert page.last_updated_at > last_updated_at post = page.get_live_post_by_message_id(message_id="some-id") # The value of the modified field for the first post should change. assert post.value["modified"] == page.last_updated_at # Signal is sent with correct renders. assert count == 1 assert _channel_id == "channel_id" assert _renders == [post] assert _removals == [] finally: live_page_update.disconnect(callback)