Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Update handle empty string input

birjd0022e6e018a50fa
diff --git a/wger/utils/tests/test_username.py b/wger/utils/tests/test_username.py
index a42b801cf..4247ab43e 100644
--- a/wger/utils/tests/test_username.py
+++ b/wger/utils/tests/test_username.py
@@ -52,3 +52,11 @@ class TestGenerateUsernameSuggestions(TestCase):
suggestions = generate_username_suggestions('john.doe!!')
for suggestion in suggestions:
self.assertIn('johndoe', suggestion)
+
+ def test_empty_username_falls_back_to_user(self):
+ """
+ If the username is all special characters, suggestions should fall back to 'user'
+ """
+ suggestions = generate_username_suggestions('!!!!!')
+ for suggestion in suggestions:
+ self.assertIn('user', suggestion)
diff --git a/wger/utils/username.py b/wger/utils/username.py
index 00e317037..3542707c8 100644
--- a/wger/utils/username.py
+++ b/wger/utils/username.py
@@ -24,7 +24,7 @@ def generate_username_suggestions(username: str, count: int = 3):
"""
Given a username, generates a list of available unique usernames.
"""
- text = re.sub(r'[^\w]', '', username)
+ text = re.sub(r'[^\w]', '', username) or 'user'
candidates = [
f'{text}{random.randint(1, 99)}',