-
Notifications
You must be signed in to change notification settings - Fork 186
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
Add basic auth support for interface #353
Changes from 5 commits
afe4ef0
aae286a
3b60bb7
daa3326
ec045eb
2e0e557
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 |
---|---|---|
|
@@ -58,7 +58,7 @@ func TestSetupGlobalMiddleware(t *testing.T) { | |
Config.PProfEnabled = true | ||
} | ||
|
||
func TestAuthMiddleware(t *testing.T) { | ||
func TestJWTAuthMiddleware(t *testing.T) { | ||
h := &okHandler{} | ||
|
||
t.Run("it will redirect if jwt enabled but no cookie passed", func(t *testing.T) { | ||
|
@@ -255,7 +255,7 @@ o2kQ+X5xK9cipRgEKwIDAQAB | |
}) | ||
} | ||
|
||
func TestAuthMiddlewareWithUnauthorized(t *testing.T) { | ||
func TestJWTAuthMiddlewareWithUnauthorized(t *testing.T) { | ||
h := &okHandler{} | ||
|
||
t.Run("it will return 401 if no cookie passed", func(t *testing.T) { | ||
|
@@ -315,3 +315,62 @@ func TestAuthMiddlewareWithUnauthorized(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
func TestBasicAuthMiddleware(t *testing.T) { | ||
h := &okHandler{} | ||
|
||
t.Run("it will return 200 for web paths when disabled", func(t *testing.T) { | ||
testPaths := []string{"/", "", "/#", "/#/", "/static", "/static/"} | ||
for _, path := range testPaths { | ||
t.Run(fmt.Sprintf("path: %s", path), func(t *testing.T) { | ||
hh := SetupGlobalMiddleware(h) | ||
res := httptest.NewRecorder() | ||
res.Body = new(bytes.Buffer) | ||
req, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:18000%s", path), nil) | ||
hh.ServeHTTP(res, req) | ||
assert.Equal(t, http.StatusOK, res.Code) | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("it will return 200 for whitelist path if basic auth is enabled", func(t *testing.T) { | ||
Config.BasicAuthEnabled = true | ||
Config.BasicAuthUsername = "admin" | ||
Config.BasicAuthPassword = "password" | ||
defer func() { | ||
Config.BasicAuthEnabled = false | ||
Config.BasicAuthUsername = "" | ||
Config.BasicAuthPassword = "" | ||
}() | ||
|
||
hh := SetupGlobalMiddleware(h) | ||
res := httptest.NewRecorder() | ||
res.Body = new(bytes.Buffer) | ||
req, _ := http.NewRequest("GET", "http://localhost:18000/api/v1/flags", nil) | ||
jheth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
hh.ServeHTTP(res, req) | ||
assert.Equal(t, http.StatusOK, res.Code) | ||
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. actually one more thing to test - a happy code path that the basicauth is serving the traffic with username/password passed. this test didn't cover that because That's why this line wasn't covered yet. https://codecov.io/gh/checkr/flagr/pull/353/diff?src=pr&el=tree#D2-266 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. Good call. Thanks for catching that. It's been added. |
||
}) | ||
|
||
t.Run("it will return 401 for web paths when enabled", func(t *testing.T) { | ||
Config.BasicAuthEnabled = true | ||
Config.BasicAuthUsername = "admin" | ||
Config.BasicAuthPassword = "password" | ||
defer func() { | ||
Config.BasicAuthEnabled = false | ||
Config.BasicAuthUsername = "" | ||
Config.BasicAuthPassword = "" | ||
}() | ||
|
||
testPaths := []string{"/", "", "/#", "/#/", "/static", "/static/"} | ||
for _, path := range testPaths { | ||
t.Run(fmt.Sprintf("path: %s", path), func(t *testing.T) { | ||
hh := SetupGlobalMiddleware(h) | ||
res := httptest.NewRecorder() | ||
res.Body = new(bytes.Buffer) | ||
req, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:18000%s", path), nil) | ||
hh.ServeHTTP(res, req) | ||
assert.Equal(t, http.StatusUnauthorized, res.Code) | ||
}) | ||
} | ||
}) | ||
} |
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.
do you mind adding a unit test for it? thanks