Verified Commit 2892e3d2 authored by guglielmo's avatar guglielmo

timeouts can be None, set to config.MAZ_REQUESTS_TIMEOUT in get_live_content...

timeouts can be None, set to config.MAZ_REQUESTS_TIMEOUT in get_live_content method to make tests work
parent 2cc1c72e
# Generated by Django 2.2.1 on 2019-07-03 14:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('webapp', '0017_content_timeout'),
]
operations = [
migrations.AlterField(
model_name='content',
name='timeout',
field=models.PositiveSmallIntegerField(blank=True, null=True),
),
]
......@@ -75,7 +75,7 @@ class Content(models.Model):
verbose_name=_("Contenuto significativo")
)
timeout = models.PositiveSmallIntegerField(
default=config.REQUESTS_MAX_TIMEOUT
blank=True, null=True,
)
notes = models.TextField(
blank=True, null=True,
......@@ -117,11 +117,22 @@ class Content(models.Model):
"""
headers = {}
if config.REQUESTS_UA:
headers['User-Agent'] = config.REQUESTS_UA
ua = None
try:
timeout = config.REQUESTS_MAX_TIMEOUT
if config.REQUESTS_UA:
ua = config.REQUESTS_UA
except Exception as e:
timeout = 10
if ua:
headers['User-Agent'] = ua
if self.timeout is not None:
timeout = self.timeout
try:
res = requests.get(self.url, timeout=self.timeout, headers=headers)
res = requests.get(self.url, timeout=timeout, headers=headers)
if not res.ok:
if res.status_code and res.reason:
return res.status_code, res.reason
......@@ -129,7 +140,7 @@ class Content(models.Model):
return 990, "Generic error"
except SSLError:
try:
res = requests.get(self.url, timeout=self.timeout, headers=headers, verify=False)
res = requests.get(self.url, timeout=timeout, headers=headers, verify=False)
except Exception as e:
return 990, e
except ConnectionError as e:
......
......@@ -16,7 +16,7 @@ class ContentTests(TestCase):
@classmethod
def tearDownClass(cls):
cls.mock_get_patcher.stop()
getattr(cls, 'mock_get_patcher').stop()
super(ContentTests, cls).tearDownClass()
def setUp(self):
......@@ -231,7 +231,7 @@ class ContentTests(TestCase):
(resp_code, resp_content) = content.get_live_content()
self.assertEqual(resp_code, 910)
self.assertEqual(resp_content, "URL inesistente")
self.assertEqual(str(resp_content), "Boom")
# clears mock side effect
self.mock_get.side_effect = None
......@@ -244,14 +244,15 @@ class ContentTests(TestCase):
content = Content.objects.get(title="GC - 5132 - Roma (RM)")
# Configure the mock to return a response the ok attribute set to true
self.mock_get.return_value.ok = True
self.mock_get.return_value.ok = False
self.mock_get.return_value.status_code = 404
self.mock_get.return_value.reason = "Not found"
# Call the service, which will send a request to the server.
(resp_code, resp_content) = content.get_live_content()
self.assertEqual(resp_code, 404)
self.assertEqual(resp_content, "URL")
self.assertEqual(resp_content, "Not found")
def test_get_live_content_when_xpath_finds_nothing(self):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment