Skip to content

Commit

Permalink
Add UsernameMixin to DeleteMacaroonForm and DeleteTOTPForm
Browse files Browse the repository at this point in the history
Avoid passing the username to constructor simplifying base class
  • Loading branch information
Yeray Diaz Diaz committed Aug 15, 2019
1 parent b32a9d6 commit 46fc81f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
5 changes: 1 addition & 4 deletions tests/unit/manage/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,8 @@ def test_verify_totp_valid(self, monkeypatch):
class TestDeleteTOTPForm:
def test_creation(self):
user_service = pretend.stub()
form = forms.DeleteTOTPForm(username="username", user_service=user_service)
form = forms.DeleteTOTPForm(user_service=user_service)

assert form.username == "username"
assert form.user_service is user_service

def test_validate_confirm_password(self):
Expand Down Expand Up @@ -451,10 +450,8 @@ def test_creation(self):
form = forms.DeleteMacaroonForm(
macaroon_service=macaroon_service,
user_service=user_service,
username="username",
)

assert form.username == "username"
assert form.macaroon_service is macaroon_service
assert form.user_service is user_service

Expand Down
9 changes: 1 addition & 8 deletions warehouse/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(self, *args, check_password_metrics_tags=None, **kwargs):
super().__init__(*args, **kwargs)

def validate_password(self, field):
userid = self.user_service.find_userid(self._get_username())
userid = self.user_service.find_userid(self.username.data)
if userid is not None:
try:
if not self.user_service.check_password(
Expand All @@ -110,13 +110,6 @@ def validate_password(self, field):
"try again later."
) from None

def _get_username(self):
# the username may be set directly in the instance or as a field
if isinstance(self.username, str):
return self.username
else:
return self.username.data


class NewPasswordMixin:

Expand Down
10 changes: 4 additions & 6 deletions warehouse/manage/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ def __init__(self, *args, user_service, **kwargs):
self.user_service = user_service


class DeleteTOTPForm(PasswordMixin, forms.Form):
class DeleteTOTPForm(UsernameMixin, PasswordMixin, forms.Form):

__params__ = ["confirm_password"]

def __init__(self, *args, username, user_service, **kwargs):
def __init__(self, *args, user_service, **kwargs):
super().__init__(*args, **kwargs)
self.username = username
self.user_service = user_service


Expand Down Expand Up @@ -247,16 +246,15 @@ def validate_token_scope(self, field):
self.validated_scope = {"projects": [scope_value]}


class DeleteMacaroonForm(PasswordMixin, forms.Form):
class DeleteMacaroonForm(UsernameMixin, PasswordMixin, forms.Form):
__params__ = ["confirm_password", "macaroon_id"]

macaroon_id = wtforms.StringField(
validators=[wtforms.validators.DataRequired(message="Identifier required")]
)

def __init__(self, *args, macaroon_service, username, user_service, **kwargs):
def __init__(self, *args, macaroon_service, user_service, **kwargs):
super().__init__(*args, **kwargs)
self.username = username
self.user_service = user_service
self.macaroon_service = macaroon_service

Expand Down

0 comments on commit 46fc81f

Please sign in to comment.