Skip to content

Commit

Permalink
Merge ea93911 into cf03f6d
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Sep 1, 2018
2 parents cf03f6d + ea93911 commit 309b0c0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
46 changes: 26 additions & 20 deletions misago/users/management/commands/createsuperuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ def add_arguments(self, parser):
'--email',
dest='email',
default=None,
help="Specifies the username for the superuser.",
help="Specifies the e-mail for the superuser.",
)
parser.add_argument(
'--password',
dest='password',
default=None,
help="Specifies the username for the superuser.",
help="Specifies the password for the superuser.",
)
parser.add_argument(
'--noinput',
'--no-input',
action='store_false',
dest='interactive',
default=True,
Expand Down Expand Up @@ -84,24 +85,21 @@ def handle(self, *args, **options):
username = username.strip()
validate_username(username)
except ValidationError as e:
self.stderr.write(e.messages[0])
self.stderr.write(u'\n'.join(e.messages))
username = None

if email is not None:
try:
email = email.strip()
validate_email(email)
except ValidationError as e:
self.stderr.write(e.messages[0])
self.stderr.write(u'\n'.join(e.messages))
email = None

if password is not None:
try:
password = password.strip()
validate_password(password)
except ValidationError as e:
self.stderr.write(e.messages[0])
password = None
password = password.strip()
if password == '':
self.stderr.write("Error: Blank passwords aren't allowed.")

if not interactive:
if username and email and password:
Expand All @@ -122,29 +120,37 @@ def handle(self, *args, **options):
validate_username(raw_value)
username = raw_value
except ValidationError as e:
self.stderr.write(e.messages[0])
self.stderr.write(u'\n'.join(e.messages))

while not email:
try:
raw_value = input("Enter E-mail address: ").strip()
raw_value = input("Enter e-mail address: ").strip()
validate_email(raw_value)
email = raw_value
except ValidationError as e:
self.stderr.write(e.messages[0])
self.stderr.write(u'\n'.join(e.messages))

while not password:
raw_value = getpass("Enter password: ")
password_repeat = getpass("Repeat password:")
if raw_value != password_repeat:
self.stderr.write("Error: Your passwords didn't match.")
# Don't validate passwords that don't match.
continue
if raw_value.strip() == '':
self.stderr.write("Error: Blank passwords aren't allowed.")
# Don't validate blank passwords.
continue
try:
raw_value = getpass("Enter password: ").strip()
validate_password(
raw_value, user=UserModel(username=username, email=email)
)

repeat_raw_value = getpass("Repeat password: ").strip()
if raw_value != repeat_raw_value:
raise ValidationError("Entered passwords are different.")
password = raw_value
except ValidationError as e:
self.stderr.write(e.messages[0])
self.stderr.write(u'\n'.join(e.messages))
response = input('Bypass password validation and create user anyway? [y/N]: ')
if response.lower() != 'y':
continue
password = raw_value

# Call User manager's create_superuser using our wrapper
self.create_superuser(username, email, password, verbosity)
Expand Down
4 changes: 0 additions & 4 deletions misago/users/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ def create_user(

validate_username(username)
validate_email(email)

if password:
# password is conditional: users created with social-auth don't have one
validate_password(password, user=user)

if not 'rank' in extra_fields:
user.rank = Rank.objects.get_default()
Expand Down
2 changes: 1 addition & 1 deletion misago/users/tests/test_createsuperuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class CreateSuperuserTests(TestCase):
def test_create_superuser(self):
def test_valid_input_creates_superuser(self):
"""command creates superuser"""
out = StringIO()

Expand Down
13 changes: 13 additions & 0 deletions misago/users/tests/test_user_create_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ def test_registration_validates_email_registration_ban(self):
'email': ["You can't register account like this."],
})

def test_registration_requires_password(self):
"""api uses django's validate_password to validate registrations"""
response = self.client.post(
self.api_link,
data={
'username': 'Bob',
'email': 'loremipsum@dolor.met',
'password': '',
},
)

self.assertContains(response, "This field is required", status_code=400)

def test_registration_validates_password(self):
"""api uses django's validate_password to validate registrations"""
response = self.client.post(
Expand Down

0 comments on commit 309b0c0

Please sign in to comment.