Skip to content

Commit

Permalink
bugfix: fix #336: usernames with dot rejected by api.
Browse files Browse the repository at this point in the history
  • Loading branch information
mik3y committed Jun 25, 2017
1 parent 4f298f2 commit 57743b1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pykeg/core/kb_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@

# Low volume threshold: 15% full
KEG_VOLUME_LOW_PERCENT = 0.15

# Valid usernames.
USERNAME_REGEX = '^[\w.@+-]+$'
2 changes: 1 addition & 1 deletion pykeg/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class User(AbstractBaseUser):
'@/./+/-/_ characters'),
validators=[
validators.RegexValidator(
re.compile('^[\w.@+-]+$'),
re.compile(kb_common.USERNAME_REGEX),
_('Enter a valid username.'),
'invalid')])
display_name = models.CharField(
Expand Down
16 changes: 16 additions & 0 deletions pykeg/web/api/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@ def test_record_drink(self):
active_user = users[0]
self.assertEquals(self.normal_user.username, active_user.username)

def test_record_drink_usernames(self):
new_keg_data = {
'keg_size': 'half-barrel',
'beverage_name': 'Test Brew',
'producer_name': 'Test Producer',
'style_name': 'Test Style,'
}
response, data = self.post('taps/1/activate', data=new_keg_data,
HTTP_X_KEGBOT_API_KEY=self.apikey.key)
self.assertEquals(data.meta.result, 'ok')

user = models.User.objects.create(username='test.123')
response, data = self.post('taps/1', HTTP_X_KEGBOT_API_KEY=self.apikey.key,
data={'ticks': 1000, 'username': 'test.123'})
self.assertEquals(data.meta.result, 'ok')

@override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')
@override_settings(EMAIL_FROM_ADDRESS='test-from@example')
def test_registration(self):
Expand Down
3 changes: 2 additions & 1 deletion pykeg/web/api/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from django import forms

from pykeg.core import models
from pykeg.core.kb_common import USERNAME_REGEX

ALL_METERS = models.FlowMeter.objects.all()
ALL_TOGGLES = models.FlowToggle.objects.all()
Expand All @@ -28,7 +29,7 @@ class DrinkPostForm(forms.Form):
"""Form to handle posts to /tap/<tap_id>/"""
ticks = forms.IntegerField()
volume_ml = forms.FloatField(required=False)
username = forms.RegexField(required=False, max_length=30, regex=r"^[\w-]+$")
username = forms.RegexField(required=False, max_length=30, regex=USERNAME_REGEX)
pour_time = forms.IntegerField(required=False)
now = forms.IntegerField(required=False)
duration = forms.IntegerField(required=False)
Expand Down

0 comments on commit 57743b1

Please sign in to comment.