-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added strict validation for username and password in backend. (#4670)
* Added strict validation for username and password in backend. Signed-off-by: aryan <[email protected]> * fixed silly mistake Signed-off-by: aryan <[email protected]> * some requested changes. Signed-off-by: aryan <[email protected]> * modified tests Signed-off-by: aryan <[email protected]> * small change Signed-off-by: aryan <[email protected]> * Update message string in chaoscenter/authentication/api/handlers/doc.go Co-authored-by: Vedant Shrotria <[email protected]> Signed-off-by: Aryan Bhokare <[email protected]> * Update error message chaoscenter/authentication/pkg/utils/sanitizers.go Co-authored-by: Vedant Shrotria <[email protected]> Signed-off-by: Aryan Bhokare <[email protected]> * Modified swagger with requested changes. Signed-off-by: aryan <[email protected]> * added negative tests for requested functions and fixed some conflicts. Signed-off-by: aryan <[email protected]> --------- Signed-off-by: aryan <[email protected]> Signed-off-by: Aryan Bhokare <[email protected]> Co-authored-by: Vedant Shrotria <[email protected]> Co-authored-by: Saranya Jena <[email protected]>
- Loading branch information
1 parent
544d324
commit 9d2c93a
Showing
8 changed files
with
148 additions
and
23 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
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 |
---|---|---|
|
@@ -55,7 +55,7 @@ func TestCreateUser(t *testing.T) { | |
name: "successfully", | ||
inputBody: &entities.User{ | ||
Username: "newUser", | ||
Password: "validPassword123", | ||
Password: "ValidPassword@1", | ||
Email: "[email protected]", | ||
Name: "John Doe", | ||
Role: entities.RoleUser, | ||
|
@@ -68,7 +68,7 @@ func TestCreateUser(t *testing.T) { | |
Email: "[email protected]", | ||
Name: "John Doe", | ||
Role: entities.RoleUser, | ||
}, nil) | ||
}, nil).Once() | ||
}, | ||
expectedCode: 200, | ||
}, | ||
|
@@ -80,8 +80,12 @@ func TestCreateUser(t *testing.T) { | |
c, _ := gin.CreateTestContext(w) | ||
c.Set("role", tc.mockRole) | ||
if tc.inputBody != nil { | ||
b, _ := json.Marshal(tc.inputBody) | ||
b, err := json.Marshal(tc.inputBody) | ||
if err != nil { | ||
t.Fatalf("could not marshal input body: %v", err) | ||
} | ||
c.Request = httptest.NewRequest(http.MethodPost, "/users", bytes.NewBuffer(b)) | ||
c.Request.Header.Set("Content-Type", "application/json") | ||
} | ||
|
||
tc.given() | ||
|
@@ -477,13 +481,22 @@ func TestUpdatePassword(t *testing.T) { | |
}{ | ||
{ | ||
name: "Successfully update password", | ||
givenBody: `{"oldPassword":"oldPass", "newPassword":"newPass"}`, | ||
givenBody: `{"oldPassword":"oldPass@123", "newPassword":"newPass@123"}`, | ||
givenUsername: "testUser", | ||
givenStrictPassword: false, | ||
givenServiceResponse: nil, | ||
expectedCode: http.StatusOK, | ||
expectedOutput: `{"message":"password has been updated successfully"}`, | ||
}, | ||
{ | ||
name: "Invalid new password", | ||
givenBody: `{"oldPassword":"oldPass@123", "newPassword":"short"}`, | ||
givenUsername: "testUser", | ||
givenStrictPassword: false, | ||
givenServiceResponse: errors.New("invalid password"), | ||
expectedCode: utils.ErrorStatusCodes[utils.ErrStrictPasswordPolicyViolation], | ||
expectedOutput: `{"error":"password_policy_violation","errorDescription":"Please ensure the password is atleast 8 characters long and atmost 16 characters long and has atleast 1 digit, 1 lowercase alphabet, 1 uppercase alphabet and 1 special character"}`, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
|
@@ -498,8 +511,8 @@ func TestUpdatePassword(t *testing.T) { | |
|
||
userPassword := entities.UserPassword{ | ||
Username: tt.givenUsername, | ||
OldPassword: "oldPass", | ||
NewPassword: "newPass", | ||
OldPassword: "oldPass@123", | ||
NewPassword: "newPass@123", | ||
} | ||
user := &entities.User{ | ||
ID: "testUID", | ||
|
@@ -532,11 +545,11 @@ func TestResetPassword(t *testing.T) { | |
expectedCode int | ||
}{ | ||
{ | ||
name: "Non-admin role", | ||
name: "Admin role", | ||
inputBody: &entities.UserPassword{ | ||
Username: "testUser", | ||
OldPassword: "", | ||
NewPassword: "validPassword123", | ||
NewPassword: "ValidPass@123", | ||
}, | ||
mockRole: "admin", | ||
mockUID: "testUID", | ||
|
@@ -559,7 +572,7 @@ func TestResetPassword(t *testing.T) { | |
inputBody: &entities.UserPassword{ | ||
Username: "testUser", | ||
OldPassword: "", | ||
NewPassword: "validPassword123", | ||
NewPassword: "validPass@123", | ||
}, | ||
mockRole: "user", | ||
mockUID: "testUID", | ||
|
@@ -581,6 +594,29 @@ func TestResetPassword(t *testing.T) { | |
mockUsername: "adminUser", | ||
expectedCode: utils.ErrorStatusCodes[utils.ErrInvalidRequest], | ||
}, | ||
{ | ||
name: "Admin role wrong password", | ||
inputBody: &entities.UserPassword{ | ||
Username: "testUser", | ||
OldPassword: "", | ||
NewPassword: "short", | ||
}, | ||
mockRole: "admin", | ||
mockUID: "testUID", | ||
mockUsername: "adminUser", | ||
given: func() { | ||
user := &entities.User{ | ||
ID: "testUID", | ||
Username: "testUser", | ||
Email: "[email protected]", | ||
IsInitialLogin: false, | ||
} | ||
service.On("GetUser", "testUID").Return(user, nil) | ||
service.On("IsAdministrator", mock.AnythingOfType("*entities.User")).Return(nil) | ||
service.On("UpdatePassword", mock.AnythingOfType("*entities.UserPassword"), false).Return(nil) | ||
}, | ||
expectedCode: utils.ErrorStatusCodes[utils.ErrStrictPasswordPolicyViolation], | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
|
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