Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Send User-Agent when fetching the OFF delta index

Roland Geider49082b79e55c
diff --git a/wger/nutrition/management/commands/import-off-products.py b/wger/nutrition/management/commands/import-off-products.py
index e65cc6d5d..041a90b41 100644
--- a/wger/nutrition/management/commands/import-off-products.py
+++ b/wger/nutrition/management/commands/import-off-products.py
@@ -27,6 +27,7 @@ from wger.nutrition.consts import (
)
from wger.nutrition.extract_info.off import extract_info_from_off
from wger.nutrition.management.products import ImportProductCommand
+from wger.utils.requests import wger_headers
logger = logging.getLogger(__name__)
@@ -97,7 +98,8 @@ class Command(ImportProductCommand):
# Fetch the index page with requests and read the result
index_url = self.deltas_base_url + 'index.txt'
- req = requests.get(index_url)
+ req = requests.get(index_url, headers=wger_headers())
+ req.raise_for_status()
index_content = req.text
newest_entry = index_content.split('\n')[0]
diff --git a/wger/nutrition/tests/test_import_products.py b/wger/nutrition/tests/test_import_products.py
index 2782028e4..298373858 100644
--- a/wger/nutrition/tests/test_import_products.py
+++ b/wger/nutrition/tests/test_import_products.py
@@ -17,10 +17,14 @@ import gzip
import json
import os
import tempfile
+from unittest import mock
# Django
from django.core.management import call_command
+# Third Party
+import requests
+
# wger
from wger.core.tests.base_testcase import WgerTestCase
from wger.nutrition.consts import OFF_FULL_DUMP_URL
@@ -164,3 +168,36 @@ class ImportOffProductsTestCase(WgerTestCase):
self.assertEqual(Ingredient.objects.count(), count_before + 1)
self.assertTrue(Ingredient.objects.filter(remote_id='1001').exists())
+
+
+class ImportOffDeltaTestCase(WgerTestCase):
+ """
+ Tests fetching the delta index for the daily delta import
+ """
+
+ delta_file = 'openfoodfacts_products_1_2.json.gz'
+
+ def run_delta_import(self, index_response: mock.Mock):
+ with tempfile.TemporaryDirectory() as folder:
+ with gzip.open(os.path.join(folder, self.delta_file), 'wt') as dump:
+ dump.write(json.dumps(off_product('2000', 'Delta product')) + '\n')
+
+ # The command module name has dashes and cannot be patched by path
+ with mock.patch('requests.get', return_value=index_response) as mock_get:
+ call_command('import-off-products', '--delta-updates', folder=folder)
+ return mock_get
+
+ def test_index_is_fetched_with_user_agent(self):
+ response = mock.Mock(text=f'{self.delta_file}\nolder_file.json.gz\n')
+
+ mock_get = self.run_delta_import(response)
+
+ self.assertIn('wger/', mock_get.call_args.kwargs['headers']['User-Agent'])
+ self.assertTrue(Ingredient.objects.filter(remote_id='2000').exists())
+
+ def test_error_page_from_index_is_not_used_as_file_name(self):
+ response = mock.Mock(text='<html>\n<head><title>403 Forbidden</title></head>')
+ response.raise_for_status.side_effect = requests.HTTPError('403 Forbidden')
+
+ with self.assertRaises(requests.HTTPError):
+ self.run_delta_import(response)