Files
gluetun/internal/server/middlewares/auth/lookup_test.go
T
Quentin McGaw 4a78989d9d chore: do not use sentinel errors when unneeded
- main reason being it's a burden to always define sentinel errors at global scope, wrap them with `%w` instead of using a string directly
- only use sentinel errors when it has to be checked using `errors.Is`
- replace all usage of these sentinel errors in `fmt.Errorf` with direct strings that were in the sentinel error
- exclude the sentinel error definition requirement from .golangci.yml
- update unit tests to use ContainersError instead of ErrorIs so it stays as a "not a change detector test" without requiring a sentinel error
2026-05-02 03:29:46 +00:00

60 lines
1.3 KiB
Go

package auth
import (
"testing"
"github.com/stretchr/testify/assert"
)
// Read reads the toml file specified by the filepath given.
func Test_settingsToLookupMap(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
settings Settings
routeToRoles map[string][]internalRole
errMessage string
}{
"empty_settings": {
routeToRoles: map[string][]internalRole{},
},
"auth_method_not_supported": {
settings: Settings{
Roles: []Role{{Name: "a", Auth: "bad"}},
},
errMessage: "authentication method not supported: bad",
},
"success": {
settings: Settings{
Roles: []Role{
{Name: "a", Auth: AuthNone, Routes: []string{"GET /path"}},
{Name: "b", Auth: AuthNone, Routes: []string{"GET /path", "PUT /path"}},
},
},
routeToRoles: map[string][]internalRole{
"GET /path": {
{name: "a", checker: newNoneMethod()}, // deduplicated method
},
"PUT /path": {
{name: "b", checker: newNoneMethod()},
},
},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
routeToRoles, err := settingsToLookupMap(testCase.settings)
assert.Equal(t, testCase.routeToRoles, routeToRoles)
if testCase.errMessage != "" {
assert.EqualError(t, err, testCase.errMessage)
} else {
assert.NoError(t, err)
}
})
}
}