chore: login
This commit is contained in:
parent
be92a9bfee
commit
47affd6fe5
5 changed files with 85 additions and 14 deletions
13
main.go
13
main.go
|
@ -25,11 +25,12 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type gloableConfig struct {
|
type gloableConfig struct {
|
||||||
UserStoreConfig userstore.Config `env:", prefix=USERSTORE_"`
|
UserStoreConfig userstore.Config `env:", prefix=USERSTORE_"`
|
||||||
Port int `env:"PORT, default=8080"`
|
Port int `env:"PORT, default=8080"`
|
||||||
SMTPConfig smtpclient.SMTPConfig `env:", prefix=SMTP_"`
|
SMTPConfig smtpclient.SMTPConfig `env:", prefix=SMTP_"`
|
||||||
WebConfig web.WebConfig `env:", prefix=WEB_"`
|
WebConfig web.WebConfig `env:", prefix=WEB_"`
|
||||||
DummyDatabase bool `env:"DUMMY_DATABASE"`
|
MiniauthConfig miniauth.MiniauthConfig `env:", prefix=MINIAUTH_"`
|
||||||
|
DummyDatabase bool `env:"DUMMY_DATABASE"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func config() gloableConfig {
|
func config() gloableConfig {
|
||||||
|
@ -71,7 +72,7 @@ func setupRouter(cfg gloableConfig) *gin.Engine {
|
||||||
|
|
||||||
sc := smtpclient.NewSMTPClient(cfg.SMTPConfig)
|
sc := smtpclient.NewSMTPClient(cfg.SMTPConfig)
|
||||||
|
|
||||||
ma := miniauth.NewMiniauth(us, sc)
|
ma := miniauth.NewMiniauth(cfg.MiniauthConfig, us, sc)
|
||||||
|
|
||||||
webServer := web.NewWeb(cfg.WebConfig, ma)
|
webServer := web.NewWeb(cfg.WebConfig, ma)
|
||||||
webServer.RegisterRoutes(routesWeb)
|
webServer.RegisterRoutes(routesWeb)
|
||||||
|
|
|
@ -9,4 +9,5 @@ var (
|
||||||
ErrMailAddressInvalide = errors.New("email address invalide")
|
ErrMailAddressInvalide = errors.New("email address invalide")
|
||||||
ErrCantCreateUser = errors.New("cant create user")
|
ErrCantCreateUser = errors.New("cant create user")
|
||||||
ErrCantSendMail = errors.New("cant send mail")
|
ErrCantSendMail = errors.New("cant send mail")
|
||||||
|
ErrLoginFailed = errors.New("cant login")
|
||||||
)
|
)
|
||||||
|
|
|
@ -12,13 +12,18 @@ import (
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Miniauth struct {
|
type MiniauthConfig struct {
|
||||||
store userstore.Store
|
LoginWithNotApprovedMail bool `env:"LOGIN_WITH_NOT_APPROVED_MAIL"`
|
||||||
log zerolog.Logger
|
|
||||||
smtp smtpclient.SMTPClient
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMiniauth(us userstore.Store, sm smtpclient.SMTPClient) Miniauth {
|
type Miniauth struct {
|
||||||
|
store userstore.Store
|
||||||
|
log zerolog.Logger
|
||||||
|
smtp smtpclient.SMTPClient
|
||||||
|
config MiniauthConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMiniauth(conf MiniauthConfig, us userstore.Store, sm smtpclient.SMTPClient) Miniauth {
|
||||||
m := Miniauth{}
|
m := Miniauth{}
|
||||||
m.store = us
|
m.store = us
|
||||||
m.smtp = sm
|
m.smtp = sm
|
||||||
|
@ -73,6 +78,16 @@ func (m Miniauth) RegisterUser(username string, email string, password string) e
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m Miniauth) UserLogin(username, password string) error {
|
||||||
|
log := m.log.With().Str("func", "RegisterUser").Str("username", username).Logger()
|
||||||
|
_, err := m.store.UserCheckPassword(username, password)
|
||||||
|
if err != nil {
|
||||||
|
return utils.WrapError(ErrLoginFailed, err, log)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m Miniauth) checkPasswordForRegistration(password string) error {
|
func (m Miniauth) checkPasswordForRegistration(password string) error {
|
||||||
passwordValidator := validator.New(
|
passwordValidator := validator.New(
|
||||||
validator.MinLength(15, nil),
|
validator.MinLength(15, nil),
|
||||||
|
|
|
@ -183,11 +183,61 @@ func TestUserAndMailIsDeletedIfMailCantBeSend(t *testing.T) {
|
||||||
assert.False(t, res, "should not found user")
|
assert.False(t, res, "should not found user")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLogin(t *testing.T) {
|
||||||
|
ma := getMiniAuth(t)
|
||||||
|
err := ma.RegisterUser("kekskurser", "foobar@example.com", "oDzry!!!YPEtHuofKE9hnea8TLTe2Doabi6ddy")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
tts := []struct {
|
||||||
|
name string
|
||||||
|
username string
|
||||||
|
password string
|
||||||
|
ExpectErr error
|
||||||
|
EcpectErrString string
|
||||||
|
config MiniauthConfig
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "login-successfull",
|
||||||
|
username: "kekskurser",
|
||||||
|
password: "oDzry!!!YPEtHuofKE9hnea8TLTe2Doabi6ddy",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "login-failed-user-not-found",
|
||||||
|
username: "kekstest",
|
||||||
|
password: "oDzry!!!YPEtHuofKE9hnea8TLTe2Doabi6ddy",
|
||||||
|
ExpectErr: ErrLoginFailed,
|
||||||
|
EcpectErrString: "cant login: cant execute query: sql: no rows in result set",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "login-failed-user-not-found",
|
||||||
|
username: "kekskurser",
|
||||||
|
password: "oDzry!!!YPEtHuofKE9hnea8TLTe2Doabi6ddyTEST",
|
||||||
|
ExpectErr: ErrLoginFailed,
|
||||||
|
EcpectErrString: "cant login: user password dont match: crypto/bcrypt: hashedPassword is not the hash of the given password",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tts {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
ma.config = tt.config
|
||||||
|
err := ma.UserLogin(tt.username, tt.password)
|
||||||
|
if tt.ExpectErr == nil {
|
||||||
|
assert.Nil(t, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert.ErrorIs(t, err, tt.ExpectErr)
|
||||||
|
assert.Equal(t, tt.EcpectErrString, err.Error())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getMiniAuth(t *testing.T) Miniauth {
|
func getMiniAuth(t *testing.T) Miniauth {
|
||||||
us, err := userstore.NewDummyStore()
|
us, err := userstore.NewDummyStore()
|
||||||
assert.Nil(t, err, "[setup] should be abel to creat dummy store")
|
assert.Nil(t, err, "[setup] should be abel to creat dummy store")
|
||||||
|
|
||||||
|
config := MiniauthConfig{}
|
||||||
|
|
||||||
sc := smtpclient.NewDummySMTPClient()
|
sc := smtpclient.NewDummySMTPClient()
|
||||||
m := NewMiniauth(us, sc)
|
m := NewMiniauth(config, us, sc)
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,10 +17,12 @@ func TestRegistrationDisabled(t *testing.T) {
|
||||||
webConfig := WebConfig{}
|
webConfig := WebConfig{}
|
||||||
webConfig.PublicRegistration = false
|
webConfig.PublicRegistration = false
|
||||||
|
|
||||||
|
config := miniauth.MiniauthConfig{}
|
||||||
|
|
||||||
us, err := userstore.NewDummyStore()
|
us, err := userstore.NewDummyStore()
|
||||||
assert.Nil(t, err, "[setup] should be abel to create userstore")
|
assert.Nil(t, err, "[setup] should be abel to create userstore")
|
||||||
sc := smtpclient.NewDummySMTPClient()
|
sc := smtpclient.NewDummySMTPClient()
|
||||||
web := NewWeb(webConfig, miniauth.NewMiniauth(us, sc))
|
web := NewWeb(webConfig, miniauth.NewMiniauth(config, us, sc))
|
||||||
|
|
||||||
router := gin.New()
|
router := gin.New()
|
||||||
loadTemplates(router)
|
loadTemplates(router)
|
||||||
|
@ -40,10 +42,12 @@ func TestRegistrationDisabledOnPost(t *testing.T) {
|
||||||
webConfig := WebConfig{}
|
webConfig := WebConfig{}
|
||||||
webConfig.PublicRegistration = false
|
webConfig.PublicRegistration = false
|
||||||
|
|
||||||
|
config := miniauth.MiniauthConfig{}
|
||||||
|
|
||||||
us, err := userstore.NewDummyStore()
|
us, err := userstore.NewDummyStore()
|
||||||
assert.Nil(t, err, "[setup] should be abel to create userstore")
|
assert.Nil(t, err, "[setup] should be abel to create userstore")
|
||||||
sc := smtpclient.NewDummySMTPClient()
|
sc := smtpclient.NewDummySMTPClient()
|
||||||
web := NewWeb(webConfig, miniauth.NewMiniauth(us, sc))
|
web := NewWeb(webConfig, miniauth.NewMiniauth(config, us, sc))
|
||||||
|
|
||||||
router := gin.New()
|
router := gin.New()
|
||||||
loadTemplates(router)
|
loadTemplates(router)
|
||||||
|
|
Loading…
Add table
Reference in a new issue