Use nil error response for testing middleware execution.
This commit is contained in:
parent
4b91ed2f70
commit
fcf1800ac0
@ -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)
|
||||
|
@ -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) {
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user