Skip to content

Commit

Permalink
Test key fix (#6685) (#6686)
Browse files Browse the repository at this point in the history
* 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

(cherry picked from commit 6389493)

Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
  • Loading branch information
github-actions[bot] and SchrodingersGat committed Mar 13, 2024
1 parent 40e9b33 commit b807e47
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
19 changes: 16 additions & 3 deletions InvenTree/InvenTree/helpers.py
Expand Up @@ -87,11 +87,24 @@ def generateTestKey(test_name: str) -> str:
key = test_name.strip().lower()
key = key.replace(' ', '')

def valid_char(char: str):
"""Determine if a particular character is valid for use in a test key."""
if not char.isprintable():
return False

if char.isidentifier():
return True

if char.isalnum():
return True

return False

# Remove any characters that cannot be used to represent a variable
key = re.sub(r'[^a-zA-Z0-9_]', '', key)
key = ''.join([c for c in key if valid_char(c)])

# If the key starts with a digit, prefix with an underscore
if key[0].isdigit():
# If the key starts with a non-identifier character, prefix with an underscore
if len(key) > 0 and not key[0].isidentifier():
key = '_' + key

return key
Expand Down
11 changes: 10 additions & 1 deletion InvenTree/part/models.py
Expand Up @@ -3428,6 +3428,13 @@ def clean(self):

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()
super().clean()

Expand All @@ -3445,7 +3452,9 @@ def validate_unique(self, exclude=None):

if tests.exists():
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)
Expand Down
23 changes: 23 additions & 0 deletions InvenTree/part/test_part.py
Expand Up @@ -431,6 +431,29 @@ def test_uniqueness(self):

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):
"""Tests to ensure that the user-configurable default values work as expected.
Expand Down

0 comments on commit b807e47

Please sign in to comment.