URL preview: Improve test coverage.

This commit is contained in:
Igor Tokarev 2016-12-13 08:20:33 +05:00 committed by Tim Abbott
parent bb43c4d4c9
commit fae59502ab
4 changed files with 141 additions and 11 deletions

View File

@ -192,10 +192,3 @@ def twitter(tweet_id):
return ujson.loads(MEDIA_TWEET)
else:
return None
def fake_urlembed_data():
# type: () -> Dict[text_type, text_type]
return {
'title': 'Test',
'description': 'Short description'}

View File

@ -216,6 +216,17 @@ class HostRequestMock(object):
# type: () -> text_type
return self.host
class MockPythonResponse(object):
def __init__(self, text, status_code):
# type: (text_type, int) -> None
self.text = text
self.status_code = status_code
@property
def ok(self):
# type: () -> bool
return self.status_code == 200
INSTRUMENTING = os.environ.get('TEST_INSTRUMENT_URL_COVERAGE', '') == 'TRUE'
INSTRUMENTED_CALLS = [] # type: List[Dict[str, Any]]

View File

@ -6,9 +6,7 @@ from six import text_type
from typing import Any, Optional
from typing.re import Match
import requests
from django.conf import settings
from zerver.lib.cache import cache_with_key, get_cache_with_key
from zerver.lib.bugdown import testing_mocks
from zerver.lib.url_preview.oembed import get_oembed_data
from zerver.lib.url_preview.parsers import OpenGraphParser, GenericParser
@ -38,8 +36,6 @@ def get_link_embed_data(url, maxwidth=640, maxheight=480):
# type: (text_type, Optional[int], Optional[int]) -> Any
if not is_link(url):
return None
if settings.TEST_SUITE:
return testing_mocks.fake_urlembed_data()
# Fetch information from URL.
# We are using three sources in next order:
# 1. OEmbed

View File

@ -5,14 +5,30 @@ from __future__ import print_function
import mock
import ujson
from typing import Any
from requests.exceptions import ConnectionError
from django.test import override_settings
from zerver.models import Recipient, Message
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import MockPythonResponse
from zerver.worker.queue_processors import FetchLinksEmbedData
from zerver.lib.url_preview.preview import get_link_embed_data
from zerver.lib.url_preview.oembed import get_oembed_data
from zerver.lib.url_preview.parsers import (
OpenGraphParser, GenericParser)
TEST_CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'default',
},
'database': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'url-preview',
}
}
@override_settings(INLINE_URL_EMBED_PREVIEW=True)
class OembedTestCase(ZulipTestCase):
@mock.patch('pyoembed.requests.get')
@ -84,3 +100,117 @@ class GenericParserTestCase(ZulipTestCase):
result = parser.extract_data()
self.assertEqual(result.get('title'), 'Test title')
self.assertEqual(result.get('description'), 'Description text')
def test_extract_image(self):
# type: () -> None
html = """
<html>
<body>
<h1>Main header</h1>
<img src="http://test.com/test.jpg">
<div>
<p>Description text</p>
</div>
</body>
</html>
"""
parser = GenericParser(html)
result = parser.extract_data()
self.assertEqual(result.get('title'), 'Main header')
self.assertEqual(result.get('description'), 'Description text')
self.assertEqual(result.get('image'), 'http://test.com/test.jpg')
def test_extract_description(self):
# type: () -> None
html = """
<html>
<body>
<div>
<div>
<p>Description text</p>
</div>
</div>
</body>
</html>
"""
parser = GenericParser(html)
result = parser.extract_data()
self.assertEqual(result.get('description'), 'Description text')
html = """
<html>
<head><meta name="description" content="description 123"</head>
<body></body>
</html>
"""
parser = GenericParser(html)
result = parser.extract_data()
self.assertEqual(result.get('description'), 'description 123')
html = "<html><body></body></html>"
parser = GenericParser(html)
result = parser.extract_data()
self.assertIsNone(result.get('description'))
class PreviewTestCase(ZulipTestCase):
def test_get_link_embed_data(self):
# type: () -> None
html = """
<html>
<head>
<title>Test title</title>
<meta property="og:title" content="The Rock" />
<meta property="og:type" content="video.movie" />
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/" />
<meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />
</head>
<body>
<h1>Main header</h1>
<p>Description text</p>
</body>
</html>
"""
url = 'http://test.org/'
msg_id = self.send_message(
"hamlet@zulip.com", "cordelia@zulip.com",
Recipient.PERSONAL, subject="url", content=url)
response = MockPythonResponse(html, 200)
mocked_response = mock.Mock(
side_effect=lambda k: {url: response}.get(k, MockPythonResponse('', 404)))
event = {
'message_id': msg_id,
'urls': [url],
'message_content': url}
with self.settings(INLINE_URL_EMBED_PREVIEW=True, TEST_SUITE=False, CACHES=TEST_CACHES):
with mock.patch('requests.get', mocked_response):
FetchLinksEmbedData().consume(event)
msg = Message.objects.get(id=msg_id)
self.assertIn(
'<a href="{0}" target="_blank" title="The Rock">The Rock</a>'.format(url),
msg.rendered_content)
def test_http_error_get_data(self):
# type: () -> None
url = 'http://test.org/'
msg_id = self.send_message(
"hamlet@zulip.com", "cordelia@zulip.com",
Recipient.PERSONAL, subject="url", content=url)
event = {
'message_id': msg_id,
'urls': [url],
'message_content': url}
with self.settings(INLINE_URL_EMBED_PREVIEW=True, TEST_SUITE=False, CACHES=TEST_CACHES):
with mock.patch('requests.get', mock.Mock(side_effect=ConnectionError())):
with mock.patch('logging.error') as error_mock:
FetchLinksEmbedData().consume(event)
self.assertEqual(error_mock.call_count, 1)
msg = Message.objects.get(id=msg_id)
self.assertEqual(
'<p><a href="http://test.org/" target="_blank" title="http://test.org/">http://test.org/</a></p>',
msg.rendered_content)
def test_invalid_link(self):
# type: () -> None
with self.settings(INLINE_URL_EMBED_PREVIEW=True, TEST_SUITE=False, CACHES=TEST_CACHES):
self.assertIsNone(get_link_embed_data('com.notvalidlink'))