chore: add mail check
This commit is contained in:
parent
81183d2307
commit
4c6be5cf54
6 changed files with 112 additions and 2 deletions
|
|
@ -8,4 +8,5 @@ var (
|
||||||
ErrPasswordNotComplexEnough = errors.New("password not complex enough")
|
ErrPasswordNotComplexEnough = errors.New("password not complex enough")
|
||||||
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")
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
package miniauth
|
package miniauth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
"git.keks.cloud/kekskurse/miniauth/pkg/smtpclient"
|
||||||
"git.keks.cloud/kekskurse/miniauth/pkg/userstore"
|
"git.keks.cloud/kekskurse/miniauth/pkg/userstore"
|
||||||
"git.keks.cloud/kekskurse/miniauth/pkg/utils"
|
"git.keks.cloud/kekskurse/miniauth/pkg/utils"
|
||||||
"github.com/go-passwd/validator"
|
"github.com/go-passwd/validator"
|
||||||
|
|
@ -13,6 +15,7 @@ import (
|
||||||
type Miniauth struct {
|
type Miniauth struct {
|
||||||
store userstore.Store
|
store userstore.Store
|
||||||
log zerolog.Logger
|
log zerolog.Logger
|
||||||
|
smtp smtpclient.SMTPClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMiniauth(us userstore.Store) Miniauth {
|
func NewMiniauth(us userstore.Store) Miniauth {
|
||||||
|
|
@ -38,6 +41,15 @@ func (m Miniauth) RegisterUser(username string, email string, password string) e
|
||||||
return utils.WrapError(ErrMailAddressInvalide, err, log)
|
return utils.WrapError(ErrMailAddressInvalide, err, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
used, err := m.store.MailCheckExists(email)
|
||||||
|
if err != nil {
|
||||||
|
return utils.WrapError(ErrMailAddressInvalide, err, log)
|
||||||
|
}
|
||||||
|
|
||||||
|
if used {
|
||||||
|
return utils.WrapError(ErrMailAddressInvalide, errors.New("email alreadys used"), log)
|
||||||
|
}
|
||||||
|
|
||||||
id, err := m.store.UserWrite(username, password)
|
id, err := m.store.UserWrite(username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return utils.WrapError(ErrCantCreateUser, err, log)
|
return utils.WrapError(ErrCantCreateUser, err, log)
|
||||||
|
|
@ -50,7 +62,10 @@ func (m Miniauth) RegisterUser(username string, email string, password string) e
|
||||||
return utils.WrapError(ErrCantCreateUser, err, log)
|
return utils.WrapError(ErrCantCreateUser, err, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Send mail
|
err = m.smtp.SendMail("register", email, map[string]any{"hash": "abc"})
|
||||||
|
if err != nil {
|
||||||
|
return utils.WrapError(ErrCantSendMail, err, log)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,30 @@ func TestRegistration(t *testing.T) {
|
||||||
exptErr: ErrMailAddressInvalide,
|
exptErr: ErrMailAddressInvalide,
|
||||||
exptErrString: "email address invalide: mail: missing '@' or angle-addr",
|
exptErrString: "email address invalide: mail: missing '@' or angle-addr",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "invaloid password",
|
||||||
|
username: "kekskurse",
|
||||||
|
password: "abc123d,.,jfhfh",
|
||||||
|
mail: "error@error.error",
|
||||||
|
exptErr: ErrCantSendMail,
|
||||||
|
exptErrString: "cant send mail: error mail send",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "success",
|
||||||
|
username: "someuser",
|
||||||
|
password: "abc123d,.,jfhfh",
|
||||||
|
mail: "hello@example.com",
|
||||||
|
exptErr: nil,
|
||||||
|
exptErrString: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mail already used",
|
||||||
|
username: "someuser2",
|
||||||
|
password: "abc123d,.,jfhfh",
|
||||||
|
mail: "hello@example.com",
|
||||||
|
exptErr: ErrMailAddressInvalide,
|
||||||
|
exptErrString: "email address invalide: email alreadys used",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
ma := getMiniAuth(t)
|
ma := getMiniAuth(t)
|
||||||
|
|
@ -47,7 +71,9 @@ func TestRegistration(t *testing.T) {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
err := ma.RegisterUser(tt.username, tt.mail, tt.password)
|
err := ma.RegisterUser(tt.username, tt.mail, tt.password)
|
||||||
assert.ErrorIs(t, err, tt.exptErr)
|
assert.ErrorIs(t, err, tt.exptErr)
|
||||||
assert.Equal(t, tt.exptErrString, err.Error())
|
if tt.exptErrString != "" {
|
||||||
|
assert.Equal(t, tt.exptErrString, err.Error())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
pkg/smtpclient/smtpclient.go
Normal file
12
pkg/smtpclient/smtpclient.go
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
package smtpclient
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
type SMTPClient struct{}
|
||||||
|
|
||||||
|
func (s SMTPClient) SendMail(templateName string, to string, data any) error {
|
||||||
|
if to == "error@error.error" {
|
||||||
|
return errors.New("error mail send")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -17,3 +17,20 @@ func (s Store) MailAdd(mail string, userID int64, primary bool) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Store) MailCheckExists(email string) (bool, error) {
|
||||||
|
query := "SELECT COUNT(*) FROM mail WHERE mail = ?"
|
||||||
|
log := s.log.With().Str("func", "MailCheckExists").Str("email", email).Str("query", query).Logger()
|
||||||
|
|
||||||
|
var count int
|
||||||
|
err := s.db.QueryRow(query, email).Scan(&count)
|
||||||
|
if err != nil {
|
||||||
|
return true, utils.WrapError(ErrCantExecuteQuery, err, log)
|
||||||
|
}
|
||||||
|
|
||||||
|
if count > 0 {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,45 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestCheckMailExist(t *testing.T) {
|
||||||
|
store, err := NewDummyStore()
|
||||||
|
assert.Nil(t, err, "[setup] should be abel to create dummystore")
|
||||||
|
|
||||||
|
id, err := store.UserWrite("foo", "foo")
|
||||||
|
assert.Nil(t, err, "[setup] should be abel to create user")
|
||||||
|
|
||||||
|
err = store.MailAdd("mail@kekskurse.de", id, false)
|
||||||
|
assert.Nil(t, err, "[setup] should be abel to add mail")
|
||||||
|
|
||||||
|
_, err = store.db.Exec("UPDATE mail SET is_validated = 1 WHERE mail = ?", "mail@kekskurse.de")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
tts := []struct {
|
||||||
|
name string
|
||||||
|
mail string
|
||||||
|
used bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "mail addresse unused",
|
||||||
|
mail: "foo@bar.de",
|
||||||
|
used: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mail address used",
|
||||||
|
mail: "mail@kekskurse.de",
|
||||||
|
used: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tts {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
used, err := store.MailCheckExists(tt.mail)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, tt.used, used)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestAddMail(t *testing.T) {
|
func TestAddMail(t *testing.T) {
|
||||||
store, err := NewDummyStore()
|
store, err := NewDummyStore()
|
||||||
assert.Nil(t, err, "[setup] should be abel to create dummystore")
|
assert.Nil(t, err, "[setup] should be abel to create dummystore")
|
||||||
|
|
|
||||||
Reference in a new issue