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.pyindex 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_offfrom wger.nutrition.management.products import ImportProductCommand+from wger.utils.requests import wger_headerslogger = logging.getLogger(__name__)@@ -97,7 +98,8 @@ class Command(ImportProductCommand):# Fetch the index page with requests and read the resultindex_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.textnewest_entry = index_content.split('\n')[0]diff --git a/wger/nutrition/tests/test_import_products.py b/wger/nutrition/tests/test_import_products.pyindex 2782028e4..298373858 100644--- a/wger/nutrition/tests/test_import_products.py+++ b/wger/nutrition/tests/test_import_products.py@@ -17,10 +17,14 @@ import gzipimport jsonimport osimport tempfile+from unittest import mock# Djangofrom django.core.management import call_command+# Third Party+import requests+# wgerfrom wger.core.tests.base_testcase import WgerTestCasefrom 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)