-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace comma in username to avoid casbin issue
Check username when creating user by API Replace comma with underscore in username for OnboardUser Fixes #19356 Signed-off-by: stonezdj <[email protected]>
- Loading branch information
Showing
7 changed files
with
69 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,3 +143,27 @@ func TestInjectPasswd(t *testing.T) { | |
assert.Equal(t, "sha256", u.PasswordVersion) | ||
assert.Equal(t, utils.Encrypt(p, u.Salt, "sha256"), u.Password) | ||
} | ||
|
||
func (m *mgrTestSuite) TestCreate() { | ||
m.dao.On("Create", mock.Anything, testifymock.Anything).Return(3, nil) | ||
u := &models.User{ | ||
Username: "test", | ||
Email: "[email protected]", | ||
Realname: "test", | ||
} | ||
id, err := m.mgr.Create(context.Background(), u) | ||
m.Nil(err) | ||
m.Equal(3, id) | ||
m.Equal(u.Username, "test") | ||
|
||
u2 := &models.User{ | ||
Username: "test,test", | ||
Email: "[email protected]", | ||
Realname: "test", | ||
} | ||
|
||
id, err = m.mgr.Create(context.Background(), u2) | ||
m.Nil(err) | ||
m.Equal(3, id) | ||
m.Equal(u2.Username, "test_test", "username should be sanitized") | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ package handler | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
@@ -113,3 +114,30 @@ func (uts *UserTestSuite) TestGetRandomSecret() { | |
func TestUserTestSuite(t *testing.T) { | ||
suite.Run(t, &UserTestSuite{}) | ||
} | ||
|
||
func Test_validateUserProfile(t *testing.T) { | ||
tooLongUsername := "mike012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789mike012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789mike012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" | ||
type args struct { | ||
user *commonmodels.User | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr assert.ErrorAssertionFunc | ||
}{ | ||
{"normal_test", args{&commonmodels.User{Username: "mike", Realname: "mike", Email: "[email protected]"}}, assert.NoError}, | ||
{"illegall_username_,", args{&commonmodels.User{Username: "mike,mike", Realname: "mike", Email: "[email protected]"}}, assert.Error}, | ||
{"illegall_username_$", args{&commonmodels.User{Username: "mike$mike", Realname: "mike", Email: "[email protected]"}}, assert.Error}, | ||
{"illegall_username_%", args{&commonmodels.User{Username: "mike%mike", Realname: "mike", Email: "[email protected]"}}, assert.Error}, | ||
{"illegall_username_#", args{&commonmodels.User{Username: "mike#mike", Realname: "mike", Email: "[email protected]"}}, assert.Error}, | ||
{"illegall_realname", args{&commonmodels.User{Username: "mike", Realname: "mike,mike", Email: "[email protected]"}}, assert.Error}, | ||
{"username_too_long", args{&commonmodels.User{Username: tooLongUsername, Realname: "mike", Email: "[email protected]"}}, assert.Error}, | ||
{"invalid_email", args{&commonmodels.User{Username: "mike", Realname: "mike", Email: "mike#example.com"}}, assert.Error}, | ||
{"invalid_comment", args{&commonmodels.User{Username: "mike", Realname: "mike", Email: "[email protected]", Comment: tooLongUsername}}, assert.Error}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.wantErr(t, validateUserProfile(tt.args.user), fmt.Sprintf("validateUserProfile(%v)", tt.args.user)) | ||
}) | ||
} | ||
} |