mirror of
https://github.com/muety/wakapi.git
synced 2025-12-05 22:20:24 -08:00
chore: allow to log in via email address as a fallback (resolve #878)
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/duke-git/lancet/v2/convertor"
|
||||
"github.com/duke-git/lancet/v2/datetime"
|
||||
"github.com/duke-git/lancet/v2/validator"
|
||||
"github.com/gofrs/uuid/v5"
|
||||
"github.com/leandro-lugaresi/hub"
|
||||
"github.com/patrickmn/go-cache"
|
||||
@@ -127,6 +128,9 @@ func (srv *UserService) GetUserByEmail(email string) (*models.User, error) {
|
||||
if email == "" {
|
||||
return nil, errors.New("email must not be empty")
|
||||
}
|
||||
if !validator.IsEmail(email) {
|
||||
return nil, errors.New("not a valid email")
|
||||
}
|
||||
return srv.repository.FindOne(models.User{Email: email})
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,38 @@ func TestUserServiceTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(UserServiceTestSuite))
|
||||
}
|
||||
|
||||
func (suite *UserServiceTestSuite) TestUserService_GetByEmail_Empty() {
|
||||
sut := NewUserService(suite.KeyValueService, suite.MailService, suite.ApiKeyService, suite.UserRepo)
|
||||
|
||||
result, err := sut.GetUserByEmail("")
|
||||
|
||||
suite.Nil(result)
|
||||
suite.NotNil(err)
|
||||
suite.Equal(err, errors.New("email must not be empty"))
|
||||
}
|
||||
|
||||
func (suite *UserServiceTestSuite) TestUserService_GetByEmail_Invalid() {
|
||||
sut := NewUserService(suite.KeyValueService, suite.MailService, suite.ApiKeyService, suite.UserRepo)
|
||||
|
||||
result, err := sut.GetUserByEmail("notanemailaddress")
|
||||
|
||||
suite.Nil(result)
|
||||
suite.NotNil(err)
|
||||
suite.Equal(err, errors.New("not a valid email"))
|
||||
}
|
||||
|
||||
func (suite *UserServiceTestSuite) TestUserService_GetByEmail_Valid() {
|
||||
const testEmail = "foo@bar.com"
|
||||
|
||||
suite.UserRepo.On("FindOne", models.User{Email: testEmail}).Return(suite.TestUser, nil)
|
||||
|
||||
sut := NewUserService(suite.KeyValueService, suite.MailService, suite.ApiKeyService, suite.UserRepo)
|
||||
result, err := sut.GetUserByEmail(testEmail)
|
||||
|
||||
suite.Equal(suite.TestUser, result)
|
||||
suite.Nil(err)
|
||||
}
|
||||
|
||||
func (suite *UserServiceTestSuite) TestUserService_GetByEmptyKey_Failed() {
|
||||
sut := NewUserService(suite.KeyValueService, suite.MailService, suite.ApiKeyService, suite.UserRepo)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user