echo-contrib icon indicating copy to clipboard operation
echo-contrib copied to clipboard

Run all tests allows unauthorized access with casbin

Open soasada opened this issue 3 years ago • 0 comments

Issue Description

First of all, I'm new to golang so bear with me. I think that I'm missing something but I have an issue that is driving me crazy.

I'm using casbin_mw as follows:

e.Use(casbin_mw.MiddlewareWithConfig(casbin_mw.Config{
		Skipper:    authorization.Skipper([]string{"/login", "/register"}),
		Enforcer:   enforcer,
		UserGetter: authorization.UserGetter(),
	}))

And I'm doing some testing to check unauthorized access to some resources of my HTTP API. The issue here is: when I run all tests with go test -v ./... a test that should fail because unauthorized access is passing but if I run that same test in isolation it is failing as expected, but I don't know why this is happening.

I tried several things and came up with something like this:

	e.Use(casbin_mw.MiddlewareWithConfig(casbin_mw.Config{
		Skipper:    authorization.Skipper([]string{"/login", "/register"}),
		Enforcer:   enforcer,
		UserGetter: authorization.UserGetter(),
		// TODO: This ErrorHandler is added
		ErrorHandler: func(c echo.Context, internal error, proposedStatus int) error {
			if proposedStatus == http.StatusForbidden {
				log.Warn().Str("path", c.Path()).Str("internal", internal.Error()).Msg("Unauthorized access")
			}
			err := echo.NewHTTPError(proposedStatus, internal.Error())
			err.Internal = internal
			return err
		},
	}))

Checklist

  • [x] Dependencies installed
  • [x] No typos
  • [x] Searched existing issues and docs

Expected behaviour

If I run all tests or just one test should fail always with my configuration without an ErrorHandler.

Actual behaviour

Test fails in isolation but not when running it along with other tests.

I have the following package structure:

project
  | - test
    |- integration
      |- example_test.go // some tests
      |- example2_test.go // my tests that fails in isolation but not when running it along with other tests
      |- main_test.go // initializes the database and creates an *echo.Echo for all integration tests

Working code to debug


}

Version/commit

v0.13.0

soasada avatar Oct 06 '22 23:10 soasada