Use nil error response for testing middleware execution.

This commit is contained in:
mikestefanello 2021-12-22 14:38:00 -05:00
parent 4b91ed2f70
commit fcf1800ac0
3 changed files with 7 additions and 11 deletions

View File

@ -50,7 +50,7 @@ func TestRequireAuthentication(t *testing.T) {
// Logged in
err = tests.ExecuteMiddleware(ctx, RequireAuthentication())
tests.AssertHTTPErrorCodeNot(t, err, http.StatusUnauthorized)
assert.Nil(t, err)
}
func TestRequireNoAuthentication(t *testing.T) {
@ -59,7 +59,7 @@ func TestRequireNoAuthentication(t *testing.T) {
// Not logged in
err := tests.ExecuteMiddleware(ctx, RequireNoAuthentication())
tests.AssertHTTPErrorCodeNot(t, err, http.StatusForbidden)
assert.Nil(t, err)
// Login
err = c.Auth.Login(ctx, usr.ID)
@ -104,7 +104,7 @@ func TestLoadValidPasswordToken(t *testing.T) {
ctx.SetParamValues(fmt.Sprintf("%d", usr.ID), token)
_ = tests.ExecuteMiddleware(ctx, LoadUser(c.ORM))
err = tests.ExecuteMiddleware(ctx, LoadValidPasswordToken(c.Auth))
tests.AssertHTTPErrorCode(t, err, http.StatusNotFound)
assert.Nil(t, err)
ctxPt, ok := ctx.Get(context.PasswordTokenKey).(*ent.PasswordToken)
require.True(t, ok)
assert.Equal(t, pt.ID, ctxPt.ID)

View File

@ -43,7 +43,7 @@ func TestServeCachedPage(t *testing.T) {
require.NoError(t, err)
_ = tests.ExecuteMiddleware(ctx, LoadAuthenticatedUser(c.Auth))
err = tests.ExecuteMiddleware(ctx, ServeCachedPage(c.Cache))
tests.AssertHTTPErrorCode(t, err, http.StatusNotFound)
assert.Nil(t, err)
}
func TestCacheControl(t *testing.T) {

View File

@ -33,7 +33,9 @@ func InitSession(ctx echo.Context) {
}
func ExecuteMiddleware(ctx echo.Context, mw echo.MiddlewareFunc) error {
handler := mw(echo.NotFoundHandler)
handler := mw(func(c echo.Context) error {
return nil
})
return handler(ctx)
}
@ -43,12 +45,6 @@ func AssertHTTPErrorCode(t *testing.T, err error, code int) {
assert.Equal(t, code, httpError.Code)
}
func AssertHTTPErrorCodeNot(t *testing.T, err error, code int) {
httpError, ok := err.(*echo.HTTPError)
require.True(t, ok)
assert.NotEqual(t, code, httpError.Code)
}
func CreateUser(orm *ent.Client) (*ent.User, error) {
seed := fmt.Sprintf("%d-%d", time.Now().UnixMilli(), rand.IntnRange(10, 1000000))
return orm.User.