diff --git a/src/backend/InvenTree/InvenTree/api.py b/src/backend/InvenTree/InvenTree/api.py index c12f6480c4..04bfd77719 100644 --- a/src/backend/InvenTree/InvenTree/api.py +++ b/src/backend/InvenTree/InvenTree/api.py @@ -50,11 +50,8 @@ def read_license_file(path: Path) -> list: try: data = json.loads(path.read_text()) - except json.JSONDecodeError as e: - logger.exception("Failed to parse license file '%s': %s", path, e) - return [] except Exception as e: - logger.exception("Exception while reading license file '%s': %s", path, e) + logger.exception("Failed to parse license file '%s': %s", path, e) return [] output = [] diff --git a/src/backend/InvenTree/InvenTree/test_api.py b/src/backend/InvenTree/InvenTree/test_api.py index 7b4577c339..598ff10920 100644 --- a/src/backend/InvenTree/InvenTree/test_api.py +++ b/src/backend/InvenTree/InvenTree/test_api.py @@ -1,11 +1,14 @@ """Low level tests for the InvenTree API.""" from base64 import b64encode +from pathlib import Path +from tempfile import NamedTemporaryFile from django.urls import reverse from rest_framework import status +from InvenTree.api import read_license_file from InvenTree.api_version import INVENTREE_API_VERSION from InvenTree.unit_test import InvenTreeAPITestCase, InvenTreeTestCase from InvenTree.version import inventreeApiText, parse_version_text @@ -425,8 +428,8 @@ class SearchTests(InvenTreeAPITestCase): ) -class ApiVersionTests(InvenTreeAPITestCase): - """Tests for api_version functions and APIs.""" +class GeneralApiTests(InvenTreeAPITestCase): + """Tests for versions and license endpoints.""" def test_api_version(self): """Test that the API text is correct.""" @@ -461,3 +464,27 @@ class ApiVersionTests(InvenTreeAPITestCase): # Check that all texts are parsed self.assertEqual(len(resp), INVENTREE_API_VERSION - 1) + + def test_api_license(self): + """Test that the license endpoint is working.""" + response = self.get(reverse('api-license')).json() + self.assertIn('backend', response) + self.assertIn('frontend', response) + + # Various problem cases + # File does not exist + with self.assertLogs(logger='inventree', level='ERROR') as log: + respo = read_license_file(Path('does not exsist')) + self.assertEqual(respo, []) + + self.assertIn('License file not found at', str(log.output)) + + with NamedTemporaryFile('w', encoding='utf8') as sample_file: + sample_file.write('abc') + + # File is not a json + with self.assertLogs(logger='inventree', level='ERROR') as log: + respo = read_license_file(Path(sample_file.file.name)) + self.assertEqual(respo, []) + + self.assertIn('Failed to parse license file', str(log.output))