2021-12-21 12:18:17 -08:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
2021-12-21 17:29:15 -08:00
|
|
|
"testing"
|
2021-12-21 12:18:17 -08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"goweb/ent"
|
|
|
|
|
2021-12-21 17:29:15 -08:00
|
|
|
"github.com/go-playground/assert/v2"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-12-21 12:18:17 -08:00
|
|
|
"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 {
|
2021-12-22 11:38:00 -08:00
|
|
|
handler := mw(func(c echo.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
2021-12-21 12:18:17 -08:00
|
|
|
return handler(ctx)
|
|
|
|
}
|
|
|
|
|
2021-12-21 17:29:15 -08:00
|
|
|
func AssertHTTPErrorCode(t *testing.T, err error, code int) {
|
|
|
|
httpError, ok := err.(*echo.HTTPError)
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, code, httpError.Code)
|
|
|
|
}
|
|
|
|
|
2021-12-21 12:18:17 -08:00
|
|
|
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())
|
|
|
|
}
|