mirror of https://gitee.com/answerdev/answer.git
feat: add email repo unit test
This commit is contained in:
parent
e4522f280d
commit
dd16b0db74
|
@ -22,6 +22,7 @@ func NewEmailRepo(data *data.Data) export.EmailRepo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetCode The email code is used to verify that the link in the message is out of date
|
||||||
func (e *emailRepo) SetCode(ctx context.Context, code, content string) error {
|
func (e *emailRepo) SetCode(ctx context.Context, code, content string) error {
|
||||||
err := e.data.Cache.SetString(ctx, code, content, 10*time.Minute)
|
err := e.data.Cache.SetString(ctx, code, content, 10*time.Minute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -30,6 +31,7 @@ func (e *emailRepo) SetCode(ctx context.Context, code, content string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VerifyCode verify the code if out of date
|
||||||
func (e *emailRepo) VerifyCode(ctx context.Context, code string) (content string, err error) {
|
func (e *emailRepo) VerifyCode(ctx context.Context, code string) (content string, err error) {
|
||||||
content, err = e.data.Cache.GetString(ctx, code)
|
content, err = e.data.Cache.GetString(ctx, code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package repo_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/answerdev/answer/internal/repo/export"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_emailRepo_VerifyCode(t *testing.T) {
|
||||||
|
emailRepo := export.NewEmailRepo(dataSource)
|
||||||
|
code, content := "1111", "test"
|
||||||
|
err := emailRepo.SetCode(context.TODO(), code, content)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
verifyContent, err := emailRepo.VerifyCode(context.TODO(), code)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, content, verifyContent)
|
||||||
|
}
|
|
@ -11,7 +11,9 @@ import (
|
||||||
"github.com/answerdev/answer/internal/migrations"
|
"github.com/answerdev/answer/internal/migrations"
|
||||||
"github.com/ory/dockertest/v3"
|
"github.com/ory/dockertest/v3"
|
||||||
"github.com/ory/dockertest/v3/docker"
|
"github.com/ory/dockertest/v3/docker"
|
||||||
|
"github.com/segmentfault/pacman/cache"
|
||||||
"github.com/segmentfault/pacman/log"
|
"github.com/segmentfault/pacman/log"
|
||||||
|
"xorm.io/xorm"
|
||||||
"xorm.io/xorm/schemas"
|
"xorm.io/xorm/schemas"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -57,7 +59,7 @@ func TestMain(t *testing.M) {
|
||||||
tearDown()
|
tearDown()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if err := initTestDB(dbSetting); err != nil {
|
if err := initTestDataSource(dbSetting); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
log.Info("init test database successfully")
|
log.Info("init test database successfully")
|
||||||
|
@ -76,17 +78,29 @@ type TestDBSetting struct {
|
||||||
Connection string
|
Connection string
|
||||||
}
|
}
|
||||||
|
|
||||||
func initTestDB(dbSetting TestDBSetting) error {
|
func initTestDataSource(dbSetting TestDBSetting) error {
|
||||||
connection, imageCleanUp, err := initDatabaseImage(dbSetting)
|
connection, imageCleanUp, err := initDatabaseImage(dbSetting)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
dbSetting.Connection = connection
|
dbSetting.Connection = connection
|
||||||
ds, dbCleanUp, err := initDatabase(dbSetting)
|
|
||||||
|
dbEngine, err := initDatabase(dbSetting)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
dataSource = ds
|
|
||||||
|
newCache, err := initCache()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
newData, dbCleanUp, err := data.NewData(dbEngine, newCache)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dataSource = newData
|
||||||
|
|
||||||
tearDown = func() {
|
tearDown = func() {
|
||||||
dbCleanUp()
|
dbCleanUp()
|
||||||
log.Info("cleanup test database successfully")
|
log.Info("cleanup test database successfully")
|
||||||
|
@ -101,7 +115,10 @@ func initDatabaseImage(dbSetting TestDBSetting) (connection string, cleanup func
|
||||||
if dbSetting.Driver == string(schemas.SQLITE) {
|
if dbSetting.Driver == string(schemas.SQLITE) {
|
||||||
return dbSetting.Connection, func() {
|
return dbSetting.Connection, func() {
|
||||||
log.Info("remove database", dbSetting.Connection)
|
log.Info("remove database", dbSetting.Connection)
|
||||||
_ = os.Remove(dbSetting.Connection)
|
err = os.Remove(dbSetting.Connection)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
pool, err := dockertest.NewPool("")
|
pool, err := dockertest.NewPool("")
|
||||||
|
@ -136,20 +153,20 @@ func initDatabaseImage(dbSetting TestDBSetting) (connection string, cleanup func
|
||||||
return connection, func() { _ = pool.Purge(resource) }, nil
|
return connection, func() { _ = pool.Purge(resource) }, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initDatabase(dbSetting TestDBSetting) (dataSource *data.Data, cleanup func(), err error) {
|
func initDatabase(dbSetting TestDBSetting) (dbEngine *xorm.Engine, err error) {
|
||||||
dataConf := &data.Database{Driver: dbSetting.Driver, Connection: dbSetting.Connection}
|
dataConf := &data.Database{Driver: dbSetting.Driver, Connection: dbSetting.Connection}
|
||||||
db, err := data.NewDB(true, dataConf)
|
dbEngine, err = data.NewDB(true, dataConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("connection to database failed: %s", err)
|
return nil, fmt.Errorf("connection to database failed: %s", err)
|
||||||
}
|
}
|
||||||
err = migrations.InitDB(dataConf)
|
err = migrations.InitDB(dataConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("migrations init database failed: %s", err)
|
return nil, fmt.Errorf("migrations init database failed: %s", err)
|
||||||
}
|
}
|
||||||
|
return dbEngine, nil
|
||||||
dataSource, dbCleanUp, err := data.NewData(db, nil)
|
}
|
||||||
if err != nil {
|
|
||||||
return nil, nil, fmt.Errorf("new data failed: %s", err)
|
func initCache() (newCache cache.Cache, err error) {
|
||||||
}
|
newCache, _, err = data.NewCache(&data.CacheConf{})
|
||||||
return dataSource, dbCleanUp, nil
|
return newCache, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue