Added tests package with helpers. Started on middleware tests.
This commit is contained in:
parent
0e2625bf51
commit
58ba9f6dcc
9 changed files with 197 additions and 66 deletions
|
|
@ -6,21 +6,18 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"goweb/config"
|
"goweb/config"
|
||||||
"goweb/middleware"
|
"goweb/middleware"
|
||||||
"goweb/msg"
|
"goweb/msg"
|
||||||
"goweb/services"
|
"goweb/services"
|
||||||
|
"goweb/tests"
|
||||||
|
|
||||||
"github.com/eko/gocache/v2/store"
|
"github.com/eko/gocache/v2/store"
|
||||||
|
|
||||||
"github.com/eko/gocache/v2/marshaler"
|
"github.com/eko/gocache/v2/marshaler"
|
||||||
|
|
||||||
"github.com/gorilla/sessions"
|
|
||||||
"github.com/labstack/echo-contrib/session"
|
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
@ -50,21 +47,8 @@ func TestMain(m *testing.M) {
|
||||||
os.Exit(exitVal)
|
os.Exit(exitVal)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newContext(url string) (echo.Context, *httptest.ResponseRecorder) {
|
|
||||||
req := httptest.NewRequest(http.MethodGet, url, strings.NewReader(""))
|
|
||||||
rec := httptest.NewRecorder()
|
|
||||||
return c.Web.NewContext(req, rec), rec
|
|
||||||
}
|
|
||||||
|
|
||||||
func initSesssion(t *testing.T, ctx echo.Context) {
|
|
||||||
// Simulate an HTTP request through the session middleware to initiate the session
|
|
||||||
mw := session.Middleware(sessions.NewCookieStore([]byte("secret")))
|
|
||||||
handler := mw(echo.NotFoundHandler)
|
|
||||||
assert.Error(t, handler(ctx))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestController_Redirect(t *testing.T) {
|
func TestController_Redirect(t *testing.T) {
|
||||||
ctx, _ := newContext("/abc")
|
ctx, _ := tests.NewContext(c.Web, "/abc")
|
||||||
ctr := NewController(c)
|
ctr := NewController(c)
|
||||||
err := ctr.Redirect(ctx, "home")
|
err := ctr.Redirect(ctx, "home")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
@ -81,8 +65,8 @@ func TestController_SetValidationErrorMessages(t *testing.T) {
|
||||||
err := v.Struct(e)
|
err := v.Struct(e)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
||||||
ctx, _ := newContext("/")
|
ctx, _ := tests.NewContext(c.Web, "/")
|
||||||
initSesssion(t, ctx)
|
tests.InitSession(ctx)
|
||||||
ctr := NewController(c)
|
ctr := NewController(c)
|
||||||
ctr.SetValidationErrorMessages(ctx, err, e)
|
ctr.SetValidationErrorMessages(ctx, err, e)
|
||||||
|
|
||||||
|
|
@ -93,8 +77,8 @@ func TestController_SetValidationErrorMessages(t *testing.T) {
|
||||||
|
|
||||||
func TestController_RenderPage(t *testing.T) {
|
func TestController_RenderPage(t *testing.T) {
|
||||||
setup := func() (echo.Context, *httptest.ResponseRecorder, Controller, Page) {
|
setup := func() (echo.Context, *httptest.ResponseRecorder, Controller, Page) {
|
||||||
ctx, rec := newContext("/test/TestController_RenderPage")
|
ctx, rec := tests.NewContext(c.Web, "/test/TestController_RenderPage")
|
||||||
initSesssion(t, ctx)
|
tests.InitSession(ctx)
|
||||||
ctr := NewController(c)
|
ctr := NewController(c)
|
||||||
|
|
||||||
p := NewPage(ctx)
|
p := NewPage(ctx)
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,14 @@ import (
|
||||||
|
|
||||||
"goweb/context"
|
"goweb/context"
|
||||||
"goweb/msg"
|
"goweb/msg"
|
||||||
|
"goweb/tests"
|
||||||
|
|
||||||
echomw "github.com/labstack/echo/v4/middleware"
|
echomw "github.com/labstack/echo/v4/middleware"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewPage(t *testing.T) {
|
func TestNewPage(t *testing.T) {
|
||||||
ctx, _ := newContext("/")
|
ctx, _ := tests.NewContext(c.Web, "/")
|
||||||
p := NewPage(ctx)
|
p := NewPage(ctx)
|
||||||
assert.Same(t, ctx, p.Context)
|
assert.Same(t, ctx, p.Context)
|
||||||
assert.NotNil(t, p.ToURL)
|
assert.NotNil(t, p.ToURL)
|
||||||
|
|
@ -27,7 +28,7 @@ func TestNewPage(t *testing.T) {
|
||||||
assert.Empty(t, p.RequestID)
|
assert.Empty(t, p.RequestID)
|
||||||
assert.False(t, p.Cache.Enabled)
|
assert.False(t, p.Cache.Enabled)
|
||||||
|
|
||||||
ctx, _ = newContext("/abc?def=123")
|
ctx, _ = tests.NewContext(c.Web, "/abc?def=123")
|
||||||
ctx.Set(context.AuthenticatedUserKey, 1)
|
ctx.Set(context.AuthenticatedUserKey, 1)
|
||||||
ctx.Set(echomw.DefaultCSRFConfig.ContextKey, "csrf")
|
ctx.Set(echomw.DefaultCSRFConfig.ContextKey, "csrf")
|
||||||
p = NewPage(ctx)
|
p = NewPage(ctx)
|
||||||
|
|
@ -39,9 +40,9 @@ func TestNewPage(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPage_GetMessages(t *testing.T) {
|
func TestPage_GetMessages(t *testing.T) {
|
||||||
ctx, _ := newContext("/")
|
ctx, _ := tests.NewContext(c.Web, "/")
|
||||||
|
tests.InitSession(ctx)
|
||||||
p := NewPage(ctx)
|
p := NewPage(ctx)
|
||||||
initSesssion(t, ctx)
|
|
||||||
|
|
||||||
// Set messages
|
// Set messages
|
||||||
msgTests := make(map[msg.Type][]string)
|
msgTests := make(map[msg.Type][]string)
|
||||||
|
|
|
||||||
|
|
@ -4,28 +4,30 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"goweb/tests"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewPager(t *testing.T) {
|
func TestNewPager(t *testing.T) {
|
||||||
ctx, _ := newContext("/")
|
ctx, _ := tests.NewContext(c.Web, "/")
|
||||||
pgr := NewPager(ctx, 10)
|
pgr := NewPager(ctx, 10)
|
||||||
assert.Equal(t, 10, pgr.ItemsPerPage)
|
assert.Equal(t, 10, pgr.ItemsPerPage)
|
||||||
assert.Equal(t, 1, pgr.Page)
|
assert.Equal(t, 1, pgr.Page)
|
||||||
assert.Equal(t, 0, pgr.Items)
|
assert.Equal(t, 0, pgr.Items)
|
||||||
assert.Equal(t, 0, pgr.Pages)
|
assert.Equal(t, 0, pgr.Pages)
|
||||||
|
|
||||||
ctx, _ = newContext(fmt.Sprintf("/abc?%s=%d", PageQueryKey, 2))
|
ctx, _ = tests.NewContext(c.Web, fmt.Sprintf("/abc?%s=%d", PageQueryKey, 2))
|
||||||
pgr = NewPager(ctx, 10)
|
pgr = NewPager(ctx, 10)
|
||||||
assert.Equal(t, 2, pgr.Page)
|
assert.Equal(t, 2, pgr.Page)
|
||||||
|
|
||||||
ctx, _ = newContext(fmt.Sprintf("/abc?%s=%d", PageQueryKey, -2))
|
ctx, _ = tests.NewContext(c.Web, fmt.Sprintf("/abc?%s=%d", PageQueryKey, -2))
|
||||||
pgr = NewPager(ctx, 10)
|
pgr = NewPager(ctx, 10)
|
||||||
assert.Equal(t, 1, pgr.Page)
|
assert.Equal(t, 1, pgr.Page)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPager_SetItems(t *testing.T) {
|
func TestPager_SetItems(t *testing.T) {
|
||||||
ctx, _ := newContext("/")
|
ctx, _ := tests.NewContext(c.Web, "/")
|
||||||
pgr := NewPager(ctx, 20)
|
pgr := NewPager(ctx, 20)
|
||||||
pgr.SetItems(100)
|
pgr.SetItems(100)
|
||||||
assert.Equal(t, 100, pgr.Items)
|
assert.Equal(t, 100, pgr.Items)
|
||||||
|
|
@ -33,7 +35,7 @@ func TestPager_SetItems(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPager_IsBeginning(t *testing.T) {
|
func TestPager_IsBeginning(t *testing.T) {
|
||||||
ctx, _ := newContext("/")
|
ctx, _ := tests.NewContext(c.Web, "/")
|
||||||
pgr := NewPager(ctx, 20)
|
pgr := NewPager(ctx, 20)
|
||||||
pgr.Pages = 10
|
pgr.Pages = 10
|
||||||
assert.True(t, pgr.IsBeginning())
|
assert.True(t, pgr.IsBeginning())
|
||||||
|
|
@ -44,7 +46,7 @@ func TestPager_IsBeginning(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPager_IsEnd(t *testing.T) {
|
func TestPager_IsEnd(t *testing.T) {
|
||||||
ctx, _ := newContext("/")
|
ctx, _ := tests.NewContext(c.Web, "/")
|
||||||
pgr := NewPager(ctx, 20)
|
pgr := NewPager(ctx, 20)
|
||||||
pgr.Pages = 10
|
pgr.Pages = 10
|
||||||
assert.False(t, pgr.IsEnd())
|
assert.False(t, pgr.IsEnd())
|
||||||
|
|
@ -55,7 +57,7 @@ func TestPager_IsEnd(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPager_GetOffset(t *testing.T) {
|
func TestPager_GetOffset(t *testing.T) {
|
||||||
ctx, _ := newContext("/")
|
ctx, _ := tests.NewContext(c.Web, "/")
|
||||||
pgr := NewPager(ctx, 20)
|
pgr := NewPager(ctx, 20)
|
||||||
assert.Equal(t, 0, pgr.GetOffset())
|
assert.Equal(t, 0, pgr.GetOffset())
|
||||||
pgr.Page = 2
|
pgr.Page = 2
|
||||||
|
|
|
||||||
85
middleware/auth_test.go
Normal file
85
middleware/auth_test.go
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"goweb/context"
|
||||||
|
"goweb/ent"
|
||||||
|
"goweb/tests"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoadAuthenticatedUser(t *testing.T) {
|
||||||
|
ctx, _ := tests.NewContext(c.Web, "/")
|
||||||
|
tests.InitSession(ctx)
|
||||||
|
mw := LoadAuthenticatedUser(c.Auth)
|
||||||
|
|
||||||
|
// Not authenticated
|
||||||
|
_ = tests.ExecuteMiddleware(ctx, mw)
|
||||||
|
assert.Nil(t, ctx.Get(context.AuthenticatedUserKey))
|
||||||
|
|
||||||
|
// Login
|
||||||
|
err := c.Auth.Login(ctx, usr.ID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Verify the midldeware returns the authenticated user
|
||||||
|
_ = tests.ExecuteMiddleware(ctx, mw)
|
||||||
|
require.NotNil(t, ctx.Get(context.AuthenticatedUserKey))
|
||||||
|
ctxUsr, ok := ctx.Get(context.AuthenticatedUserKey).(*ent.User)
|
||||||
|
require.True(t, ok)
|
||||||
|
assert.Equal(t, usr.ID, ctxUsr.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRequireAuthentication(t *testing.T) {
|
||||||
|
ctx, _ := tests.NewContext(c.Web, "/")
|
||||||
|
tests.InitSession(ctx)
|
||||||
|
|
||||||
|
// Not logged in
|
||||||
|
err := tests.ExecuteMiddleware(ctx, RequireAuthentication())
|
||||||
|
httpError, ok := err.(*echo.HTTPError)
|
||||||
|
require.True(t, ok)
|
||||||
|
assert.Equal(t, http.StatusUnauthorized, httpError.Code)
|
||||||
|
|
||||||
|
// Login
|
||||||
|
err = c.Auth.Login(ctx, usr.ID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_ = tests.ExecuteMiddleware(ctx, LoadAuthenticatedUser(c.Auth))
|
||||||
|
|
||||||
|
// Logged in
|
||||||
|
err = tests.ExecuteMiddleware(ctx, RequireAuthentication())
|
||||||
|
httpError, ok = err.(*echo.HTTPError)
|
||||||
|
require.True(t, ok)
|
||||||
|
assert.NotEqual(t, http.StatusUnauthorized, httpError.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRequireNoAuthentication(t *testing.T) {
|
||||||
|
ctx, _ := tests.NewContext(c.Web, "/")
|
||||||
|
tests.InitSession(ctx)
|
||||||
|
|
||||||
|
// Not logged in
|
||||||
|
err := tests.ExecuteMiddleware(ctx, RequireNoAuthentication())
|
||||||
|
httpError, ok := err.(*echo.HTTPError)
|
||||||
|
require.True(t, ok)
|
||||||
|
assert.NotEqual(t, http.StatusForbidden, httpError.Code)
|
||||||
|
|
||||||
|
// Login
|
||||||
|
err = c.Auth.Login(ctx, usr.ID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_ = tests.ExecuteMiddleware(ctx, LoadAuthenticatedUser(c.Auth))
|
||||||
|
|
||||||
|
// Logged in
|
||||||
|
err = tests.ExecuteMiddleware(ctx, RequireNoAuthentication())
|
||||||
|
httpError, ok = err.(*echo.HTTPError)
|
||||||
|
require.True(t, ok)
|
||||||
|
assert.Equal(t, http.StatusForbidden, httpError.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadValidPasswordToken(t *testing.T) {
|
||||||
|
|
||||||
|
}
|
||||||
39
middleware/middleware_test.go
Normal file
39
middleware/middleware_test.go
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"goweb/config"
|
||||||
|
"goweb/ent"
|
||||||
|
"goweb/services"
|
||||||
|
"goweb/tests"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
c *services.Container
|
||||||
|
usr *ent.User
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
// Set the environment to test
|
||||||
|
config.SwitchEnvironment(config.EnvTest)
|
||||||
|
|
||||||
|
// Create a new container
|
||||||
|
c = services.NewContainer()
|
||||||
|
defer func() {
|
||||||
|
if err := c.Shutdown(); err != nil {
|
||||||
|
c.Web.Logger.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Create a user
|
||||||
|
var err error
|
||||||
|
if usr, err = tests.CreateUser(c.ORM); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run tests
|
||||||
|
exitVal := m.Run()
|
||||||
|
os.Exit(exitVal)
|
||||||
|
}
|
||||||
|
|
@ -9,20 +9,12 @@ import (
|
||||||
"goweb/ent/passwordtoken"
|
"goweb/ent/passwordtoken"
|
||||||
"goweb/ent/user"
|
"goweb/ent/user"
|
||||||
|
|
||||||
"github.com/gorilla/sessions"
|
|
||||||
"github.com/labstack/echo-contrib/session"
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAuth(t *testing.T) {
|
func TestAuth(t *testing.T) {
|
||||||
// Simulate an HTTP request through the session middleware to initiate the session
|
|
||||||
mw := session.Middleware(sessions.NewCookieStore([]byte("secret")))
|
|
||||||
handler := mw(echo.NotFoundHandler)
|
|
||||||
assert.Error(t, handler(ctx))
|
|
||||||
|
|
||||||
assertNoAuth := func() {
|
assertNoAuth := func() {
|
||||||
_, err := c.Auth.GetAuthenticatedUserID(ctx)
|
_, err := c.Auth.GetAuthenticatedUserID(ctx)
|
||||||
assert.True(t, errors.Is(err, NotAuthenticatedError{}))
|
assert.True(t, errors.Is(err, NotAuthenticatedError{}))
|
||||||
|
|
|
||||||
|
|
@ -45,20 +45,16 @@ func (c *MailClient) SendTemplate(ctx echo.Context, to, template string, data in
|
||||||
ctx.Logger().Debugf("skipping template email sent to: %s")
|
ctx.Logger().Debugf("skipping template email sent to: %s")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the template, if needed
|
// Parse and execute template
|
||||||
if err := c.templates.Parse(
|
// Uncomment the first variable when ready to use
|
||||||
|
_, err := c.templates.ParseAndExecute(
|
||||||
"mail",
|
"mail",
|
||||||
template,
|
template,
|
||||||
template,
|
template,
|
||||||
[]string{fmt.Sprintf("email/%s", template)},
|
[]string{fmt.Sprintf("email/%s", template)},
|
||||||
[]string{},
|
[]string{},
|
||||||
); err != nil {
|
data,
|
||||||
return err
|
)
|
||||||
}
|
|
||||||
|
|
||||||
// Execute the template
|
|
||||||
// Uncomment the first variable when ready to use
|
|
||||||
_, err := c.templates.Execute("mail", template, template, data)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,12 @@
|
||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"goweb/config"
|
"goweb/config"
|
||||||
"goweb/ent"
|
"goweb/ent"
|
||||||
|
"goweb/tests"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
@ -18,7 +15,6 @@ var (
|
||||||
c *Container
|
c *Container
|
||||||
ctx echo.Context
|
ctx echo.Context
|
||||||
usr *ent.User
|
usr *ent.User
|
||||||
rec *httptest.ResponseRecorder
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
|
|
@ -34,20 +30,12 @@ func TestMain(m *testing.M) {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Create a web context
|
// Create a web context
|
||||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
ctx, _ = tests.NewContext(c.Web, "/")
|
||||||
rec = httptest.NewRecorder()
|
tests.InitSession(ctx)
|
||||||
ctx = c.Web.NewContext(req, rec)
|
|
||||||
|
|
||||||
// Create a test user
|
// Create a test user
|
||||||
var err error
|
var err error
|
||||||
usr, err = c.ORM.User.
|
if usr, err = tests.CreateUser(c.ORM); err != nil {
|
||||||
Create().
|
|
||||||
SetEmail("test@test.dev").
|
|
||||||
SetPassword("abc").
|
|
||||||
SetName("Test User").
|
|
||||||
Save(context.Background())
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
44
tests/tests.go
Normal file
44
tests/tests.go
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"goweb/ent"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/util/rand"
|
||||||
|
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
|
"github.com/labstack/echo-contrib/session"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewContext(e *echo.Echo, url string) (echo.Context, *httptest.ResponseRecorder) {
|
||||||
|
req := httptest.NewRequest(http.MethodGet, url, strings.NewReader(""))
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
return e.NewContext(req, rec), rec
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitSession(ctx echo.Context) {
|
||||||
|
mw := session.Middleware(sessions.NewCookieStore([]byte("secret")))
|
||||||
|
_ = ExecuteMiddleware(ctx, mw)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExecuteMiddleware(ctx echo.Context, mw echo.MiddlewareFunc) error {
|
||||||
|
handler := mw(echo.NotFoundHandler)
|
||||||
|
return handler(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateUser(orm *ent.Client) (*ent.User, error) {
|
||||||
|
seed := fmt.Sprintf("%d-%d", time.Now().UnixMilli(), rand.IntnRange(10, 1000000))
|
||||||
|
return orm.User.
|
||||||
|
Create().
|
||||||
|
SetEmail(fmt.Sprintf("testuser-%s@localhost.localhost", seed)).
|
||||||
|
SetPassword("password").
|
||||||
|
SetName(fmt.Sprintf("Test User %s", seed)).
|
||||||
|
Save(context.Background())
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue