2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 03:26:45 +00:00

Fix news feed task timeout (#6250) (#6265)

Co-authored-by: Lavissa <lavissawow@gmail.com>
This commit is contained in:
Oliver 2024-01-17 21:07:50 +11:00 committed by GitHub
parent 1abdb1fd46
commit ddd65cff5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 86 additions and 5 deletions

View File

@ -10,6 +10,7 @@ from django.db.utils import IntegrityError, OperationalError
from django.utils import timezone
import feedparser
import requests
from InvenTree.helpers_model import getModelsWithMixin
from InvenTree.models import InvenTreeNotesMixin
@ -45,11 +46,16 @@ def update_news_feed():
logger.info("Could not perform 'update_news_feed' - App registry not ready")
return
# News feed isn't defined, no need to continue
if not settings.INVENTREE_NEWS_URL or type(settings.INVENTREE_NEWS_URL) is not str:
return
# Fetch and parse feed
try:
d = feedparser.parse(settings.INVENTREE_NEWS_URL)
except Exception as entry: # pragma: no cover
logger.warning("update_news_feed: Error parsing the newsfeed", entry)
feed = requests.get(settings.INVENTREE_NEWS_URL)
d = feedparser.parse(feed.content)
except Exception: # pragma: no cover
logger.warning('update_news_feed: Error parsing the newsfeed')
return
# Get a reference list

View File

@ -1,8 +1,9 @@
"""Tests for tasks in app common."""
from django.conf import settings
from django.test import TestCase
from common.models import NotificationEntry
from common.models import NewsFeedEntry, NotificationEntry
from InvenTree.tasks import offload_task
from . import tasks as common_tasks
@ -15,4 +16,78 @@ class TaskTest(TestCase):
"""Test that the task `delete_old_notifications` runs through without errors."""
# check empty run
self.assertEqual(NotificationEntry.objects.all().count(), 0)
offload_task(common_tasks.delete_old_notifications,)
offload_task(common_tasks.delete_old_notifications)
class NewsFeedTests(TestCase):
"""Tests for update_news_feed task.
Tests cover different networking and addressing possibilities.
"""
def setUp(self):
"""Setup for tests."""
# Needs to be set to allow SQLite to store entries
settings.USE_TZ = True
# Store setting to restore on teardown
self.news_url = settings.INVENTREE_NEWS_URL
NewsFeedEntry.objects.all().delete()
def tearDown(self):
"""Teardown for tests."""
# Restore proper setting
settings.INVENTREE_NEWS_URL = self.news_url
NewsFeedEntry.objects.all().delete()
def test_valid_url(self):
"""Tests that news feed is updated when accessing a valid URL."""
try:
common_tasks.update_news_feed()
except Exception as ex:
self.fail(f'News feed raised exceptions: {ex}')
self.assertNotEqual(NewsFeedEntry.objects.all().count(), 0)
def test_connection_error(self):
"""Test connecting to an unavailable endpoint.
This also emulates calling the endpoint behind a blocking proxy.
"""
settings.INVENTREE_NEWS_URL = 'http://10.255.255.1:81'
with self.assertLogs('inventree', level='WARNING'):
common_tasks.update_news_feed()
self.assertEqual(NewsFeedEntry.objects.all().count(), 0)
def test_unset_url(self):
"""Test that no call is made to news feed if URL setting is invalid."""
settings.INVENTREE_NEWS_URL = ''
self.assertTrue(
offload_task(common_tasks.update_news_feed)
) # Task considered complete
self.assertEqual(
NewsFeedEntry.objects.all().count(), 0
) # No Feed entries created
settings.INVENTREE_NEWS_URL = 0
self.assertTrue(
offload_task(common_tasks.update_news_feed)
) # Task considered complete
self.assertEqual(
NewsFeedEntry.objects.all().count(), 0
) # No Feed entries created
settings.INVENTREE_NEWS_URL = None
self.assertTrue(
offload_task(common_tasks.update_news_feed)
) # Task considered complete
self.assertEqual(
NewsFeedEntry.objects.all().count(), 0
) # No Feed entries created