Test key fix (#6685)

* Fix for generateTestKey

- Check for zero-length key

* Ensure test template name would generate valid key

* Add unit test

* Improve generateTestKey method

- Allow for non-latin chars

* Update generateTestKey

- Improve check for valid char
This commit is contained in:
Oliver 2024-03-13 12:36:06 +11:00 committed by GitHub
parent 6033e87359
commit 6389493c33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 5 deletions

View File

@ -87,11 +87,24 @@ def generateTestKey(test_name: str) -> str:
key = test_name.strip().lower() key = test_name.strip().lower()
key = key.replace(' ', '') key = key.replace(' ', '')
# Remove any characters that cannot be used to represent a variable def valid_char(char: str):
key = re.sub(r'[^a-zA-Z0-9_]', '', key) """Determine if a particular character is valid for use in a test key."""
if not char.isprintable():
return False
# If the key starts with a digit, prefix with an underscore if char.isidentifier():
if key[0].isdigit(): return True
if char.isalnum():
return True
return False
# Remove any characters that cannot be used to represent a variable
key = ''.join([c for c in key if valid_char(c)])
# If the key starts with a non-identifier character, prefix with an underscore
if len(key) > 0 and not key[0].isidentifier():
key = '_' + key key = '_' + key
return key return key

View File

@ -3428,6 +3428,13 @@ class PartTestTemplate(InvenTree.models.InvenTreeMetadataModel):
self.key = helpers.generateTestKey(self.test_name) self.key = helpers.generateTestKey(self.test_name)
if len(self.key) == 0:
raise ValidationError({
'test_name': _(
'Invalid template name - must include at least one alphanumeric character'
)
})
self.validate_unique() self.validate_unique()
super().clean() super().clean()
@ -3445,7 +3452,9 @@ class PartTestTemplate(InvenTree.models.InvenTreeMetadataModel):
if tests.exists(): if tests.exists():
raise ValidationError({ raise ValidationError({
'test_name': _('Test with this name already exists for this part') 'test_name': _(
'Test template with the same key already exists for part'
)
}) })
super().validate_unique(exclude) super().validate_unique(exclude)

View File

@ -431,6 +431,29 @@ class TestTemplateTest(TestCase):
self.assertEqual(variant.getTestTemplates().count(), n + 1) self.assertEqual(variant.getTestTemplates().count(), n + 1)
def test_key_generation(self):
"""Test the key generation method."""
variant = Part.objects.get(pk=10004)
invalid_names = ['', '+', '+++++++', ' ', '<>$&&&']
for name in invalid_names:
template = PartTestTemplate(part=variant, test_name=name)
with self.assertRaises(ValidationError):
template.clean()
valid_names = [
'Собранный щит',
'!! 123 Собранный щит <><><> $$$$$ !!!',
'----hello world----',
'Olá Mundo',
'我不懂中文',
]
for name in valid_names:
template = PartTestTemplate(part=variant, test_name=name)
template.clean()
class PartSettingsTest(InvenTreeTestCase): class PartSettingsTest(InvenTreeTestCase):
"""Tests to ensure that the user-configurable default values work as expected. """Tests to ensure that the user-configurable default values work as expected.