2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2017-06-15 08:05:40 +00:00
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package authorization // import "github.com/docker/docker/pkg/authorization"
|
2017-06-15 08:05:40 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2017-06-15 08:05:40 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-06-15 08:05:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMiddlewareWrapHandler(t *testing.T) {
|
|
|
|
server := authZPluginTestServer{t: t}
|
|
|
|
server.start()
|
|
|
|
defer server.stop()
|
|
|
|
|
2020-04-07 10:05:25 +00:00
|
|
|
authZPlugin := createTestPlugin(t, server.socketAddress())
|
2017-06-15 08:05:40 +00:00
|
|
|
pluginNames := []string{authZPlugin.name}
|
|
|
|
|
|
|
|
var pluginGetter plugingetter.PluginGetter
|
|
|
|
middleWare := NewMiddleware(pluginNames, pluginGetter)
|
|
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
authList := []Plugin{authZPlugin}
|
|
|
|
middleWare.SetPlugins([]string{"My Test Plugin"})
|
|
|
|
setAuthzPlugins(middleWare, authList)
|
|
|
|
mdHandler := middleWare.WrapHandler(handler)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Assert(t, mdHandler != nil)
|
2017-06-15 08:05:40 +00:00
|
|
|
|
|
|
|
addr := "www.example.com/auth"
|
2019-10-12 18:43:46 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, addr, nil)
|
2017-06-15 08:05:40 +00:00
|
|
|
req.RequestURI = addr
|
|
|
|
req.Header.Add("header", "value")
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
t.Run("Error Test Case :", func(t *testing.T) {
|
|
|
|
server.replayResponse = Response{
|
|
|
|
Allow: false,
|
|
|
|
Msg: "Server Auth Not Allowed",
|
|
|
|
}
|
|
|
|
if err := mdHandler(ctx, resp, req, map[string]string{}); err == nil {
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Assert(t, is.ErrorContains(err, ""))
|
2017-06-15 08:05:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Positive Test Case :", func(t *testing.T) {
|
|
|
|
server.replayResponse = Response{
|
|
|
|
Allow: true,
|
|
|
|
Msg: "Server Auth Allowed",
|
|
|
|
}
|
|
|
|
if err := mdHandler(ctx, resp, req, map[string]string{}); err != nil {
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-06-15 08:05:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|