2021-12-21 12:18:17 -08:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-12-25 18:04:19 -08:00
|
|
|
"math/rand"
|
2021-12-21 12:18:17 -08:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
2021-12-21 17:29:15 -08:00
|
|
|
"testing"
|
2021-12-21 12:18:17 -08:00
|
|
|
"time"
|
|
|
|
|
2022-01-20 15:02:14 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-12-21 17:29:15 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-12-21 12:18:17 -08:00
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-07-09 17:57:05 -07:00
|
|
|
|
2024-07-11 21:09:15 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/models/sqlc"
|
2024-07-09 17:57:05 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/session"
|
2021-12-21 12:18:17 -08:00
|
|
|
)
|
|
|
|
|
2022-02-10 05:56:07 -08:00
|
|
|
// NewContext creates a new Echo context for tests using an HTTP test request and response recorder
|
2021-12-21 12:18:17 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-10 05:56:07 -08:00
|
|
|
// InitSession initializes a session for a given Echo context
|
2021-12-21 12:18:17 -08:00
|
|
|
func InitSession(ctx echo.Context) {
|
2024-06-15 13:56:47 -07:00
|
|
|
session.Store(ctx, sessions.NewCookieStore([]byte("secret")))
|
2021-12-21 12:18:17 -08:00
|
|
|
}
|
|
|
|
|
2022-02-10 05:56:07 -08:00
|
|
|
// ExecuteMiddleware executes a middleware function on a given Echo context
|
2021-12-21 12:18:17 -08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-06-14 18:01:48 -07:00
|
|
|
// ExecuteHandler executes a handler with an optional stack of middleware
|
|
|
|
func ExecuteHandler(ctx echo.Context, handler echo.HandlerFunc, mw ...echo.MiddlewareFunc) error {
|
|
|
|
return ExecuteMiddleware(ctx, func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(ctx echo.Context) error {
|
|
|
|
run := handler
|
|
|
|
|
|
|
|
for _, w := range mw {
|
|
|
|
run = w(run)
|
|
|
|
}
|
|
|
|
|
|
|
|
return run(ctx)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-10 05:56:07 -08:00
|
|
|
// AssertHTTPErrorCode asserts an HTTP status code on a given Echo HTTP error
|
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)
|
|
|
|
}
|
|
|
|
|
2022-02-10 05:56:07 -08:00
|
|
|
// CreateUser creates a random user entity
|
2024-07-11 21:31:54 -07:00
|
|
|
func CreateUser(db *sqlc.Queries) (*sqlc.User, error) {
|
2021-12-25 18:04:19 -08:00
|
|
|
seed := fmt.Sprintf("%d-%d", time.Now().UnixMilli(), rand.Intn(1000000))
|
2024-07-11 21:09:15 -07:00
|
|
|
|
2024-07-11 21:31:54 -07:00
|
|
|
usr, err := db.CreateUser(context.Background(), sqlc.CreateUserParams{
|
2024-07-11 21:09:15 -07:00
|
|
|
Name: fmt.Sprintf("Test User %s", seed),
|
|
|
|
Email: fmt.Sprintf("testuser-%s@localhost.localhost", seed),
|
|
|
|
Password: "password",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &usr, nil
|
2021-12-21 12:18:17 -08:00
|
|
|
}
|