def clean(self, value): cleaned_data = super().clean(value) # Validating if URL is a valid YouTube URL youtube_embed = cleaned_data.get('youtube_embed').url youtube_finder = OEmbedFinder(providers=[oembed_providers.youtube]) if not youtube_finder.accept(youtube_embed): e = ValidationError('URL must be a YouTube URL') raise ValidationError('Validation error in StructBlock', params={'youtube_embed': ErrorList([e])}) return cleaned_data
def test_oembed_return_values(self, loads, urlopen): urlopen.return_value = self.dummy_response loads.return_value = { 'type': 'something', 'url': 'http://www.example.com', 'title': 'test_title', 'author_name': 'test_author', 'provider_name': 'test_provider_name', 'thumbnail_url': 'test_thumbail_url', 'width': 'test_width', 'height': 'test_height', 'html': 'test_html' } result = OEmbedFinder().find_embed("http://www.youtube.com/watch/") self.assertEqual( result, { 'type': 'something', 'title': 'test_title', 'author_name': 'test_author', 'provider_name': 'test_provider_name', 'thumbnail_url': 'test_thumbail_url', 'width': 'test_width', 'height': 'test_height', 'html': 'test_html' })
def test_oembed_invalid_request(self): config = {'side_effect': URLError('foo')} with patch.object(django.utils.six.moves.urllib.request, 'urlopen', **config): self.assertRaises(EmbedNotFoundException, OEmbedFinder().find_embed, "http://www.youtube.com/watch/")
def test_endpoint_with_format_param(self, loads, urlopen): urlopen.return_value = self.dummy_response loads.return_value = {'type': 'video', 'url': 'http://www.example.com'} result = OEmbedFinder().find_embed("https://vimeo.com/217403396") self.assertEqual(result['type'], 'video') request = urlopen.call_args[0][0] self.assertEqual(request.get_full_url().split('?')[0], "http://www.vimeo.com/api/oembed.json")
def test_oembed_photo_request(self, loads, urlopen): urlopen.return_value = self.dummy_response loads.return_value = {'type': 'photo', 'url': 'http://www.example.com'} result = OEmbedFinder().find_embed("http://www.youtube.com/watch/") self.assertEqual(result['type'], 'photo') self.assertEqual(result['html'], '<img src="http://www.example.com" />') loads.assert_called_with("foo")
def clean(self): from girleffect.utils.blocks import validate_hex if self.hero_strapline_hex: if not validate_hex(self.hero_strapline_hex): raise ValidationError( {'hero_strapline_hex': _('Please enter a valid hex code')}) # Validating if URL is a valid YouTube URL youtube_embed = self.link_youtube if youtube_embed: youtube_finder = OEmbedFinder(providers=[oembed_providers.youtube]) if not youtube_finder.accept(youtube_embed): raise ValidationError( {'link_youtube': _('Please supply a valid YouTube URL.')}) else: try: embed = get_embed(youtube_embed) self.link_youtube_html = embed.html except EmbedException: raise ValidationError( {'link_youtube': _('Embed cannot be found.')}) # Validating links populated_fields = [] for link_field in [self.link_page, self.link_youtube]: if link_field: populated_fields.append(link_field) # Only only one or less fields can be selected if len(populated_fields) > 1: error_message = 'Please choose only one of Link Page or Link YouTube as destination.' raise ValidationError({ 'link_page': error_message, 'link_youtube': error_message }) # Link fields should have link text if len(populated_fields) >= 1 and not self.link_text: raise ValidationError({ 'link_text': 'Link text is required if link destination has been selected' }) return super(HeroVideoFields, self).clean()
def test_oembed_doesnt_accept_unknown_provider(self): finder = OEmbedFinder(providers=[oembed_providers.twitter]) self.assertFalse(finder.accept("http://www.youtube.com/watch/"))
def test_oembed_accepts_known_provider(self): finder = OEmbedFinder(providers=[oembed_providers.youtube]) self.assertTrue(finder.accept("http://www.youtube.com/watch/"))
def test_oembed_invalid_provider(self): self.assertRaises(EmbedNotFoundException, OEmbedFinder().find_embed, "foo")