2017-06-15 04:05:40 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package authorization // import "github.com/docker/docker/pkg/authorization"
|
2017-06-15 04:05:40 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2017-06-15 04:05:40 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-06-15 04:05:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMiddlewareWrapHandler(t *testing.T) {
|
|
|
|
server := authZPluginTestServer{t: t}
|
|
|
|
server.start()
|
|
|
|
defer server.stop()
|
|
|
|
|
2020-04-07 06:05:25 -04:00
|
|
|
authZPlugin := createTestPlugin(t, server.socketAddress())
|
2017-06-15 04:05:40 -04: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 15:28:34 -04:00
|
|
|
assert.Assert(t, mdHandler != nil)
|
2017-06-15 04:05:40 -04:00
|
|
|
|
|
|
|
addr := "www.example.com/auth"
|
2019-10-12 14:43:46 -04:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, addr, nil)
|
2017-06-15 04:05:40 -04: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 15:28:34 -04:00
|
|
|
assert.Assert(t, is.ErrorContains(err, ""))
|
2017-06-15 04:05:40 -04: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 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-15 04:05:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|