diff options
Diffstat (limited to 'internal/route')
-rw-r--r-- | internal/route/lfs/mocks_test.go | 124 | ||||
-rw-r--r-- | internal/route/org/setting.go | 7 | ||||
-rw-r--r-- | internal/route/user/setting.go | 6 |
3 files changed, 131 insertions, 6 deletions
diff --git a/internal/route/lfs/mocks_test.go b/internal/route/lfs/mocks_test.go index 3677261c..a1e89f85 100644 --- a/internal/route/lfs/mocks_test.go +++ b/internal/route/lfs/mocks_test.go @@ -2295,6 +2295,9 @@ type MockUsersStore struct { // AuthenticateFunc is an instance of a mock function object controlling // the behavior of the method Authenticate. AuthenticateFunc *UsersStoreAuthenticateFunc + // ChangeUsernameFunc is an instance of a mock function object + // controlling the behavior of the method ChangeUsername. + ChangeUsernameFunc *UsersStoreChangeUsernameFunc // CountFunc is an instance of a mock function object controlling the // behavior of the method Count. CountFunc *UsersStoreCountFunc @@ -2342,6 +2345,11 @@ func NewMockUsersStore() *MockUsersStore { return }, }, + ChangeUsernameFunc: &UsersStoreChangeUsernameFunc{ + defaultHook: func(context.Context, int64, string) (r0 error) { + return + }, + }, CountFunc: &UsersStoreCountFunc{ defaultHook: func(context.Context) (r0 int64) { return @@ -2414,6 +2422,11 @@ func NewStrictMockUsersStore() *MockUsersStore { panic("unexpected invocation of MockUsersStore.Authenticate") }, }, + ChangeUsernameFunc: &UsersStoreChangeUsernameFunc{ + defaultHook: func(context.Context, int64, string) error { + panic("unexpected invocation of MockUsersStore.ChangeUsername") + }, + }, CountFunc: &UsersStoreCountFunc{ defaultHook: func(context.Context) int64 { panic("unexpected invocation of MockUsersStore.Count") @@ -2484,6 +2497,9 @@ func NewMockUsersStoreFrom(i db.UsersStore) *MockUsersStore { AuthenticateFunc: &UsersStoreAuthenticateFunc{ defaultHook: i.Authenticate, }, + ChangeUsernameFunc: &UsersStoreChangeUsernameFunc{ + defaultHook: i.ChangeUsername, + }, CountFunc: &UsersStoreCountFunc{ defaultHook: i.Count, }, @@ -2637,6 +2653,114 @@ func (c UsersStoreAuthenticateFuncCall) Results() []interface{} { return []interface{}{c.Result0, c.Result1} } +// UsersStoreChangeUsernameFunc describes the behavior when the +// ChangeUsername method of the parent MockUsersStore instance is invoked. +type UsersStoreChangeUsernameFunc struct { + defaultHook func(context.Context, int64, string) error + hooks []func(context.Context, int64, string) error + history []UsersStoreChangeUsernameFuncCall + mutex sync.Mutex +} + +// ChangeUsername delegates to the next hook function in the queue and +// stores the parameter and result values of this invocation. +func (m *MockUsersStore) ChangeUsername(v0 context.Context, v1 int64, v2 string) error { + r0 := m.ChangeUsernameFunc.nextHook()(v0, v1, v2) + m.ChangeUsernameFunc.appendCall(UsersStoreChangeUsernameFuncCall{v0, v1, v2, r0}) + return r0 +} + +// SetDefaultHook sets function that is called when the ChangeUsername +// method of the parent MockUsersStore instance is invoked and the hook +// queue is empty. +func (f *UsersStoreChangeUsernameFunc) SetDefaultHook(hook func(context.Context, int64, string) error) { + f.defaultHook = hook +} + +// PushHook adds a function to the end of hook queue. Each invocation of the +// ChangeUsername method of the parent MockUsersStore instance invokes the +// hook at the front of the queue and discards it. After the queue is empty, +// the default hook function is invoked for any future action. +func (f *UsersStoreChangeUsernameFunc) PushHook(hook func(context.Context, int64, string) error) { + f.mutex.Lock() + f.hooks = append(f.hooks, hook) + f.mutex.Unlock() +} + +// SetDefaultReturn calls SetDefaultHook with a function that returns the +// given values. +func (f *UsersStoreChangeUsernameFunc) SetDefaultReturn(r0 error) { + f.SetDefaultHook(func(context.Context, int64, string) error { + return r0 + }) +} + +// PushReturn calls PushHook with a function that returns the given values. +func (f *UsersStoreChangeUsernameFunc) PushReturn(r0 error) { + f.PushHook(func(context.Context, int64, string) error { + return r0 + }) +} + +func (f *UsersStoreChangeUsernameFunc) nextHook() func(context.Context, int64, string) error { + f.mutex.Lock() + defer f.mutex.Unlock() + + if len(f.hooks) == 0 { + return f.defaultHook + } + + hook := f.hooks[0] + f.hooks = f.hooks[1:] + return hook +} + +func (f *UsersStoreChangeUsernameFunc) appendCall(r0 UsersStoreChangeUsernameFuncCall) { + f.mutex.Lock() + f.history = append(f.history, r0) + f.mutex.Unlock() +} + +// History returns a sequence of UsersStoreChangeUsernameFuncCall objects +// describing the invocations of this function. +func (f *UsersStoreChangeUsernameFunc) History() []UsersStoreChangeUsernameFuncCall { + f.mutex.Lock() + history := make([]UsersStoreChangeUsernameFuncCall, len(f.history)) + copy(history, f.history) + f.mutex.Unlock() + + return history +} + +// UsersStoreChangeUsernameFuncCall is an object that describes an +// invocation of method ChangeUsername on an instance of MockUsersStore. +type UsersStoreChangeUsernameFuncCall struct { + // Arg0 is the value of the 1st argument passed to this method + // invocation. + Arg0 context.Context + // Arg1 is the value of the 2nd argument passed to this method + // invocation. + Arg1 int64 + // Arg2 is the value of the 3rd argument passed to this method + // invocation. + Arg2 string + // Result0 is the value of the 1st result returned from this method + // invocation. + Result0 error +} + +// Args returns an interface slice containing the arguments of this +// invocation. +func (c UsersStoreChangeUsernameFuncCall) Args() []interface{} { + return []interface{}{c.Arg0, c.Arg1, c.Arg2} +} + +// Results returns an interface slice containing the results of this +// invocation. +func (c UsersStoreChangeUsernameFuncCall) Results() []interface{} { + return []interface{}{c.Result0} +} + // UsersStoreCountFunc describes the behavior when the Count method of the // parent MockUsersStore instance is invoked. type UsersStoreCountFunc struct { diff --git a/internal/route/org/setting.go b/internal/route/org/setting.go index 198012ef..f3e65b9b 100644 --- a/internal/route/org/setting.go +++ b/internal/route/org/setting.go @@ -7,6 +7,7 @@ package org import ( "strings" + "github.com/pkg/errors" log "unknwon.dev/clog/v2" "gogs.io/gogs/internal/auth" @@ -45,13 +46,13 @@ func SettingsPost(c *context.Context, f form.UpdateOrgSetting) { c.Data["OrgName"] = true c.RenderWithErr(c.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &f) return - } else if err := db.ChangeUserName(org, f.Name); err != nil { + } else if err := db.Users.ChangeUsername(c.Req.Context(), org.ID, f.Name); err != nil { c.Data["OrgName"] = true switch { - case db.IsErrNameNotAllowed(err): + case db.IsErrNameNotAllowed(errors.Cause(err)): c.RenderWithErr(c.Tr("user.form.name_not_allowed", err.(db.ErrNameNotAllowed).Value()), SETTINGS_OPTIONS, &f) default: - c.Error(err, "change user name") + c.Error(err, "change organization name") } return } diff --git a/internal/route/user/setting.go b/internal/route/user/setting.go index c873aeaf..2924a8f0 100644 --- a/internal/route/user/setting.go +++ b/internal/route/user/setting.go @@ -71,13 +71,13 @@ func SettingsPost(c *context.Context, f form.UpdateProfile) { if c.User.IsLocal() { // Check if username characters have been changed if c.User.LowerName != strings.ToLower(f.Name) { - if err := db.ChangeUserName(c.User, f.Name); err != nil { + if err := db.Users.ChangeUsername(c.Req.Context(), c.User.ID, f.Name); err != nil { c.FormErr("Name") var msg string switch { - case db.IsErrUserAlreadyExist(err): + case db.IsErrUserAlreadyExist(errors.Cause(err)): msg = c.Tr("form.username_been_taken") - case db.IsErrNameNotAllowed(err): + case db.IsErrNameNotAllowed(errors.Cause(err)): msg = c.Tr("user.form.name_not_allowed", err.(db.ErrNameNotAllowed).Value()) default: c.Error(err, "change user name") |