-
Notifications
You must be signed in to change notification settings - Fork 21
Add basicauth authorizer migration #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -802,3 +802,28 @@ var _ = timeout.New(func(c fiber.Ctx) error { return nil }, 2*time.Second)`) | |
| assert.Contains(t, content, `timeout.New(func(c fiber.Ctx) error { return nil }, timeout.Config{Timeout: 2*time.Second})`) | ||
| assert.Contains(t, buf.String(), "Migrating timeout middleware configs") | ||
| } | ||
|
|
||
| func Test_MigrateBasicauthAuthorizer(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mbauthorizer") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| import ( | ||
| "github.com/gofiber/fiber/v2" | ||
| "github.com/gofiber/fiber/v2/middleware/basicauth" | ||
| ) | ||
| var _ = basicauth.New(basicauth.Config{ | ||
| Authorizer: func(u, p string) bool { return true }, | ||
| })`) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateBasicauthAuthorizer(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, `Authorizer: func(u, p string, _ fiber.Ctx) bool`) | ||
| assert.Contains(t, buf.String(), "Migrating basicauth authorizer") | ||
| } | ||
|
Comment on lines
+806
to
+829
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test for This would allow you to easily add and test edge cases, such as:
This will provide greater confidence that the migration works correctly for all users, especially with a more robust implementation of func Test_MigrateBasicauthAuthorizer(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
input string
expected string
}{
{
name: "with parameters",
input: `var _ = basicauth.New(basicauth.Config{\n Authorizer: func(u, p string) bool { return true },\n})`,
expected: `Authorizer: func(u, p string, _ fiber.Ctx) bool { return true }`,
},
{
name: "with parameters and trailing comma",
input: `var _ = basicauth.New(basicauth.Config{\n Authorizer: func(u, p string,) bool { return true },\n})`,
expected: `Authorizer: func(u, p string, _ fiber.Ctx) bool { return true }`,
},
{
name: "without parameters",
input: `var _ = basicauth.New(basicauth.Config{\n Authorizer: func() bool { return true },\n})`,
expected: `Authorizer: func(_ fiber.Ctx) bool { return true }`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
dir, err := os.MkdirTemp("", "mbauthorizer-"+tc.name)
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(dir)) }()
fileContent := "package main\nimport (\n \"github.com/gofiber/fiber/v2\"\n \"github.com/gofiber/fiber/v2/middleware/basicauth\"\n)\n" + tc.input
file := writeTempFile(t, dir, fileContent)
var buf bytes.Buffer
cmd := newCmd(&buf)
require.NoError(t, v3.MigrateBasicauthAuthorizer(cmd, dir, nil, nil))
content := readFile(t, file)
assert.Contains(t, content, tc.expected)
assert.Contains(t, buf.String(), "Migrating basicauth authorizer")
})
}
} |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation for migrating the
basicauth.Authorizersignature is not fully robust. It correctly handles the common case where parameters are present, but it may produce invalid Go code if the authorizer function has no parameters (e.g.,func() bool).While the
basicauth.Authorizertype requires parameters, making this an edge case, a migration script should ideally be resilient to such variations.I suggest a more robust implementation using
regexp.ReplaceAllStringFuncto handle cases with and without parameters, as well as with optional trailing commas, correctly. This will ensure the migration is safer across a wider range of user code.