-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error on signup with taken username #102
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c53bd5
typo
lambdaTotoro d269308
add test_create_user_twice for #91
lambdaTotoro 72c106b
rewrite get_or_create into two methods, adapt test, error on signup w…
lambdaTotoro 4e75ea8
incorporate requested changes
lambdaTotoro 4aea615
add test for getting (non-)existing users
lambdaTotoro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,7 +128,7 @@ def authenticate(self, handler, data): | |
username = self.normalize_username(data['username']) | ||
password = data['password'] | ||
|
||
user = UserInfo.find(self.db, username) | ||
user = self.get_user(username) | ||
if not user: | ||
return | ||
|
||
|
@@ -166,13 +166,22 @@ def is_password_strong(self, password): | |
checks.append(not self.is_password_common(password)) | ||
|
||
return all(checks) | ||
|
||
def get_or_create_user(self, username, pw, **kwargs): | ||
username = self.normalize_username(username) | ||
user = UserInfo.find(self.db, username) | ||
|
||
def get_user(self, username): | ||
user = UserInfo.find(self.db, self.normalize_username(username)) | ||
if user: | ||
return user | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then this won't be necessary |
||
|
||
def user_exists(self, username): | ||
return self.get_user(username) is not None | ||
|
||
def create_user(self, username, pw, **kwargs): | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you remove this line to follow patterns please? |
||
username = self.normalize_username(username) | ||
|
||
if self.user_exists(username): | ||
return | ||
|
||
if not self.is_password_strong(pw) or \ | ||
not self.validate_username(username): | ||
return | ||
|
@@ -193,7 +202,7 @@ def get_or_create_user(self, username, pw, **kwargs): | |
return user_info | ||
|
||
def change_password(self, username, new_password): | ||
user = UserInfo.find(self.db, username) | ||
user = self.get_user(username) | ||
user.password = bcrypt.hashpw(new_password.encode(), bcrypt.gensalt()) | ||
self.db.commit() | ||
|
||
|
@@ -214,7 +223,7 @@ def get_handlers(self, app): | |
return native_handlers | ||
|
||
def delete_user(self, user): | ||
user_info = UserInfo.find(self.db, user.name) | ||
user_info = self.get_user(user.name) | ||
if user_info is not None: | ||
self.db.delete(user_info) | ||
self.db.commit() | ||
|
@@ -236,7 +245,7 @@ def add_data_from_firstuse(self): | |
with dbm.open(self.firstuse_db_path, 'c', 0o600) as db: | ||
for user in db.keys(): | ||
password = db[user].decode() | ||
new_user = self.get_or_create_user(user.decode(), password) | ||
new_user = self.create_user(user.decode(), password) | ||
if not new_user: | ||
error = '''User {} was not created. Check password | ||
restrictions or username problems before trying | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I am not mistaken, if the user doesn't exist it already returns None, so we can remove the following if and just return the result of this search