-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce validation on fullname, username and email for increase secur…
…ity #224
- Loading branch information
Showing
6 changed files
with
106 additions
and
75 deletions.
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
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
from unittest.mock import ANY, MagicMock | ||
|
||
import cherrypy | ||
from parameterized import parameterized | ||
|
||
import rdiffweb.test | ||
from rdiffweb.core.model import UserObject | ||
|
@@ -170,40 +171,58 @@ def test_add_edit_delete(self): | |
self.assertInBody("User account removed.") | ||
self.assertNotInBody("test2") | ||
|
||
def test_edit_fullname(self): | ||
@parameterized.expand( | ||
[ | ||
# Invalid | ||
('evil.com', False), | ||
('http://test', False), | ||
('[email protected]', False), | ||
('/test/', False), | ||
# Valid | ||
('My fullname', True), | ||
('Test Test', True), | ||
('Éric Terrien-Pascal', True), | ||
("Tel'c", True), | ||
] | ||
) | ||
def test_edit_fullname_with_special_character(self, new_fullname, expected_valid): | ||
# Given an existing user | ||
# When updating the user's fullname | ||
self.getPage( | ||
"/admin/users/", | ||
method='POST', | ||
body={'action': 'edit', 'username': self.USERNAME, 'fullname': 'My fullname'}, | ||
body={'action': 'edit', 'username': self.USERNAME, 'fullname': new_fullname}, | ||
) | ||
self.assertStatus(200) | ||
# Then user is updated successfully | ||
self.assertInBody("User information modified successfully.") | ||
# Then database is updated | ||
obj = UserObject.query.filter(UserObject.username == self.USERNAME).first() | ||
self.assertEqual('My fullname', obj.fullname) | ||
|
||
def test_add_edit_delete_user_with_encoding(self): | ||
""" | ||
Check creation of user with non-ascii char. | ||
""" | ||
self._add_user("Éric", "é[email protected]", "pr3j5Dwi", "/home/", UserObject.USER_ROLE) | ||
self.assertInBody("User added successfully.") | ||
self.assertInBody("Éric") | ||
self.assertInBody("é[email protected]") | ||
# Update user | ||
self._edit_user("Éric", "eric.lé[email protected]", "écureuil", "/tmp/", UserObject.ADMIN_ROLE) | ||
self.assertInBody("User information modified successfully.") | ||
self.assertInBody("Éric") | ||
self.assertInBody("eric.lé[email protected]") | ||
self.assertNotInBody("/home/") | ||
self.assertInBody("/tmp/") | ||
|
||
self._delete_user("Éric") | ||
self.assertInBody("User account removed.") | ||
self.assertNotInBody("Éric") | ||
if expected_valid: | ||
self.assertInBody("User information modified successfully.") | ||
self.assertNotInBody("Fullname: Must not contain any special characters.") | ||
else: | ||
self.assertNotInBody("User information modified successfully.") | ||
self.assertInBody("Fullname: Must not contain any special characters.") | ||
|
||
@parameterized.expand( | ||
[ | ||
# Invalid | ||
('http://username', False), | ||
('[email protected]', False), | ||
('/username/', False), | ||
# Valid | ||
('username.com', True), | ||
('admin_user', True), | ||
('test.test', True), | ||
('test-test', True), | ||
] | ||
) | ||
def test_add_user_with_special_character(self, new_username, expected_valid): | ||
self._add_user(new_username, "[email protected]", "pr3j5Dwi", "/home/", UserObject.USER_ROLE) | ||
self.assertStatus(200) | ||
if expected_valid: | ||
self.assertInBody("User added successfully.") | ||
self.assertNotInBody("Username: Must not contain any special characters.") | ||
else: | ||
self.assertNotInBody("User added successfully.") | ||
self.assertInBody("Username: Must not contain any special characters.") | ||
|
||
def test_add_user_with_empty_username(self): | ||
""" | ||
|
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
from unittest.mock import MagicMock | ||
|
||
import cherrypy | ||
from parameterized import parameterized | ||
|
||
import rdiffweb.test | ||
from rdiffweb.core.model import RepoObject, UserObject | ||
|
@@ -88,16 +89,31 @@ def test_change_username_noop(self): | |
self.assertIsNotNone(user) | ||
self.assertEqual("[email protected]", user.email) | ||
|
||
def test_change_fullname(self): | ||
@parameterized.expand( | ||
[ | ||
# Invalid | ||
('@test.com', False), | ||
('test.com', False), | ||
('test@te_st.com', False), | ||
('[email protected], [email protected]', False), | ||
# Valid | ||
('test', True), | ||
('My Fullname', True), | ||
] | ||
) | ||
def test_change_fullname(self, new_fullname, expected_valid): | ||
# Given an authenticated user | ||
# When update the fullname | ||
self._set_profile_info("[email protected]", "My Fullname") | ||
self._set_profile_info("[email protected]", new_fullname) | ||
self.assertStatus(200) | ||
self.assertInBody("Profile updated successfully.") | ||
# Then database is updated with fullname | ||
self.assertInBody("My Fullname") | ||
user = UserObject.query.filter(UserObject.username == self.USERNAME).first() | ||
self.assertEqual("My Fullname", user.fullname) | ||
if expected_valid: | ||
self.assertInBody("Profile updated successfully.") | ||
# Then database is updated with fullname | ||
self.assertInBody(new_fullname) | ||
user = UserObject.query.filter(UserObject.username == self.USERNAME).first() | ||
self.assertEqual(new_fullname, user.fullname) | ||
else: | ||
self.assertNotInBody("Profile updated successfully.") | ||
|
||
def test_change_fullname_method_get(self): | ||
# Given an authenticated user | ||
|
@@ -126,30 +142,31 @@ def test_change_email(self): | |
self.assertStatus(200) | ||
self.assertInBody("Profile updated successfully.") | ||
|
||
def test_change_email_with_invalid_email(self): | ||
self._set_profile_info("@test.com") | ||
self.assertStatus(200) | ||
self.assertInBody("Invalid email") | ||
|
||
self._set_profile_info("test.com") | ||
self.assertStatus(200) | ||
self.assertInBody("Invalid email") | ||
|
||
self._set_profile_info("test") | ||
self.assertStatus(200) | ||
self.assertInBody("Invalid email") | ||
|
||
self._set_profile_info("test@te_st.com") | ||
self.assertStatus(200) | ||
self.assertInBody("Invalid email") | ||
|
||
self._set_profile_info("[email protected], [email protected]") | ||
@parameterized.expand( | ||
[ | ||
# Invalid | ||
('@test.com', False), | ||
('test.com', False), | ||
('test', False), | ||
('test@te_st.com', False), | ||
('[email protected], [email protected]', False), | ||
# Valid | ||
('[email protected]', True), | ||
] | ||
) | ||
def test_change_email_with_invalid_email(self, new_email, expected_valid): | ||
self._set_profile_info(new_email) | ||
self.assertStatus(200) | ||
self.assertInBody("Invalid email") | ||
if expected_valid: | ||
self.assertInBody("Profile updated successfully.") | ||
self.assertNotInBody("Must be a valid email address.") | ||
else: | ||
self.assertNotInBody("Profile updated successfully.") | ||
self.assertInBody("Must be a valid email address.") | ||
|
||
def test_change_email_with_too_long(self): | ||
self._set_profile_info(("test1" * 50) + "@test.com") | ||
self.assertInBody("Invalid email") | ||
self.assertInBody("Email too long.") | ||
|
||
def test_change_password(self): | ||
self.listener.user_password_changed.reset_mock() | ||
|
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