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
|
|
@ -9,20 +9,12 @@ import (
|
|||
"goweb/ent/passwordtoken"
|
||||
"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/assert"
|
||||
)
|
||||
|
||||
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() {
|
||||
_, err := c.Auth.GetAuthenticatedUserID(ctx)
|
||||
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")
|
||||
}
|
||||
|
||||
// Parse the template, if needed
|
||||
if err := c.templates.Parse(
|
||||
// Parse and execute template
|
||||
// Uncomment the first variable when ready to use
|
||||
_, err := c.templates.ParseAndExecute(
|
||||
"mail",
|
||||
template,
|
||||
template,
|
||||
[]string{fmt.Sprintf("email/%s", template)},
|
||||
[]string{},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Execute the template
|
||||
// Uncomment the first variable when ready to use
|
||||
_, err := c.templates.Execute("mail", template, template, data)
|
||||
data,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,12 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"goweb/config"
|
||||
"goweb/ent"
|
||||
"goweb/tests"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
|
@ -18,7 +15,6 @@ var (
|
|||
c *Container
|
||||
ctx echo.Context
|
||||
usr *ent.User
|
||||
rec *httptest.ResponseRecorder
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
|
@ -34,20 +30,12 @@ func TestMain(m *testing.M) {
|
|||
}()
|
||||
|
||||
// Create a web context
|
||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
|
||||
rec = httptest.NewRecorder()
|
||||
ctx = c.Web.NewContext(req, rec)
|
||||
ctx, _ = tests.NewContext(c.Web, "/")
|
||||
tests.InitSession(ctx)
|
||||
|
||||
// Create a test user
|
||||
var err error
|
||||
usr, err = c.ORM.User.
|
||||
Create().
|
||||
SetEmail("test@test.dev").
|
||||
SetPassword("abc").
|
||||
SetName("Test User").
|
||||
Save(context.Background())
|
||||
|
||||
if err != nil {
|
||||
if usr, err = tests.CreateUser(c.ORM); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue