chore: Add AddMail function to userstore
This commit is contained in:
parent
d724363720
commit
b1019dbc69
2 changed files with 73 additions and 0 deletions
19
pkg/userstore/mail.go
Normal file
19
pkg/userstore/mail.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package userstore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.keks.cloud/kekskurse/miniauth/pkg/utils"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s Store) MailAdd(mail string, primary bool) error {
|
||||||
|
query := "INSERT INTO mail (mail, validationCode, isPrimary) VALUES (?, ?, ?);"
|
||||||
|
log := s.log.With().Str("func", "MailAdd").Str("mail", mail).Bool("primary", primary).Str("query", query).Logger()
|
||||||
|
|
||||||
|
validatenCode := uuid.NewString()
|
||||||
|
_, err := s.db.Exec(query, mail, validatenCode, primary)
|
||||||
|
if err != nil {
|
||||||
|
return utils.WrapError(ErrCantExecuteQuery, err, log)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
54
pkg/userstore/mail_test.go
Normal file
54
pkg/userstore/mail_test.go
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
package userstore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAddMail(t *testing.T) {
|
||||||
|
store := getTestStore(t)
|
||||||
|
initDabase(t, store)
|
||||||
|
|
||||||
|
tts := []struct {
|
||||||
|
name string
|
||||||
|
mail string
|
||||||
|
primearyMail bool
|
||||||
|
ExptErr error
|
||||||
|
ExptErrString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "test-add-first-mail",
|
||||||
|
mail: "mail1@kekskurse.de",
|
||||||
|
primearyMail: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "test-add-mail-again-should-return-error",
|
||||||
|
mail: "mail1@kekskurse.de",
|
||||||
|
primearyMail: false,
|
||||||
|
ExptErr: ErrCantExecuteQuery,
|
||||||
|
ExptErrString: "cant execute query: constraint failed: UNIQUE constraint failed: mail.mail (2067)",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tts {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
err := store.MailAdd(tt.mail, tt.primearyMail)
|
||||||
|
if tt.ExptErr == nil {
|
||||||
|
assert.Nil(t, err)
|
||||||
|
} else {
|
||||||
|
assert.ErrorIs(t, err, tt.ExptErr)
|
||||||
|
assert.Equal(t, tt.ExptErrString, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var isValidated bool
|
||||||
|
var validateCode string
|
||||||
|
isValidated = true
|
||||||
|
|
||||||
|
err = store.db.QueryRow("SELECT validationCode, isValidated FROM mail WHERE mail = ?", tt.mail).Scan(&validateCode, &isValidated)
|
||||||
|
assert.Nil(t, err, "should get mail infos from db without error")
|
||||||
|
assert.False(t, isValidated, "mail should not be validated")
|
||||||
|
assert.NotEmpty(t, validateCode, "validaten code should not be empty")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in a new issue