diff --git a/server/channels/app/login.go b/server/channels/app/login.go index d7cd45504b..8b07910e44 100644 --- a/server/channels/app/login.go +++ b/server/channels/app/login.go @@ -124,23 +124,25 @@ func (a *App) GetUserForLogin(c request.CTX, id, loginId string) (*model.User, * enableUsername := *a.Config().EmailSettings.EnableSignInWithUsername enableEmail := *a.Config().EmailSettings.EnableSignInWithEmail - // If we are given a userID then fail if we can't find a user with that ID - if id != "" { - user, err := a.GetUser(id) - if err != nil { - if err.Id != MissingAccountError { - err.StatusCode = http.StatusInternalServerError + if enableEmail || enableUsername { + // If we are given a userID then fail if we can't find a user with that ID + if id != "" { + user, err := a.GetUser(id) + if err != nil { + if err.Id != MissingAccountError { + err.StatusCode = http.StatusInternalServerError + return nil, err + } + err.StatusCode = http.StatusBadRequest return nil, err } - err.StatusCode = http.StatusBadRequest - return nil, err + return user, nil } - return user, nil - } - // Try to get the user by username/email - if user, err := a.Srv().Store().User().GetForLogin(loginId, enableUsername, enableEmail); err == nil { - return user, nil + // Try to get the user by username/email + if user, err := a.Srv().Store().User().GetForLogin(loginId, enableUsername, enableEmail); err == nil { + return user, nil + } } // Try to get the user with LDAP if enabled diff --git a/server/channels/app/login_test.go b/server/channels/app/login_test.go index b69b106245..6532f4d8a5 100644 --- a/server/channels/app/login_test.go +++ b/server/channels/app/login_test.go @@ -80,3 +80,88 @@ func TestCWSLogin(t *testing.T) { require.Nil(t, user) }) } + +func TestGetUserForLogin(t *testing.T) { + mainHelper.Parallel(t) + th := Setup(t).InitBasic() + + t.Run("Should get user with username when sign in with username is enabled", func(t *testing.T) { + th.UpdateConfig(func(config *model.Config) { + config.EmailSettings.EnableSignInWithUsername = model.NewPointer(true) + }) + + user, appErr := th.App.GetUserForLogin(th.Context, "", th.BasicUser.Username) + require.Nil(t, appErr) + require.NotNil(t, user) + require.Equal(t, th.BasicUser.Username, user.Username) + }) + + t.Run("Should not get user with username when sign in with username is disabled", func(t *testing.T) { + th.UpdateConfig(func(config *model.Config) { + config.EmailSettings.EnableSignInWithUsername = model.NewPointer(false) + }) + + user, appErr := th.App.GetUserForLogin(th.Context, "", th.BasicUser.Username) + require.NotNil(t, appErr) + require.Equal(t, http.StatusBadRequest, appErr.StatusCode) + require.Nil(t, user) + }) + + t.Run("Should get user with email when sign in with email is enabled", func(t *testing.T) { + th.UpdateConfig(func(config *model.Config) { + config.EmailSettings.EnableSignInWithEmail = model.NewPointer(true) + }) + + user, appErr := th.App.GetUserForLogin(th.Context, "", th.BasicUser.Email) + require.Nil(t, appErr) + require.NotNil(t, user) + require.Equal(t, th.BasicUser.Username, user.Username) + }) + + t.Run("Should not user with email when sign in with email is disabled", func(t *testing.T) { + th.UpdateConfig(func(config *model.Config) { + config.EmailSettings.EnableSignInWithEmail = model.NewPointer(false) + }) + + user, appErr := th.App.GetUserForLogin(th.Context, "", th.BasicUser.Email) + require.NotNil(t, appErr) + require.Equal(t, http.StatusBadRequest, appErr.StatusCode) + require.Nil(t, user) + }) + + t.Run("Should get user with user ID when sign in with email is enabled", func(t *testing.T) { + th.UpdateConfig(func(config *model.Config) { + config.EmailSettings.EnableSignInWithEmail = model.NewPointer(true) + config.EmailSettings.EnableSignInWithUsername = model.NewPointer(false) + }) + + user, appErr := th.App.GetUserForLogin(th.Context, th.BasicUser.Id, "") + require.Nil(t, appErr) + require.NotNil(t, user) + require.Equal(t, th.BasicUser.Username, user.Username) + }) + + t.Run("Should get user with user ID when sign in with username is enabled", func(t *testing.T) { + th.UpdateConfig(func(config *model.Config) { + config.EmailSettings.EnableSignInWithEmail = model.NewPointer(false) + config.EmailSettings.EnableSignInWithUsername = model.NewPointer(true) + }) + + user, appErr := th.App.GetUserForLogin(th.Context, th.BasicUser.Id, "") + require.Nil(t, appErr) + require.NotNil(t, user) + require.Equal(t, th.BasicUser.Username, user.Username) + }) + + t.Run("Should not get user with user ID when both sign in with email and username are disabled", func(t *testing.T) { + th.UpdateConfig(func(config *model.Config) { + config.EmailSettings.EnableSignInWithEmail = model.NewPointer(false) + config.EmailSettings.EnableSignInWithUsername = model.NewPointer(false) + }) + + user, appErr := th.App.GetUserForLogin(th.Context, th.BasicUser.Id, "") + require.NotNil(t, appErr) + require.Equal(t, http.StatusBadRequest, appErr.StatusCode) + require.Nil(t, user) + }) +}