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"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2017-06-15 04:05:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMiddlewareWrapHandler(t *testing.T) {
|
|
|
|
server := authZPluginTestServer{t: t}
|
|
|
|
server.start()
|
|
|
|
defer server.stop()
|
|
|
|
|
|
|
|
authZPlugin := createTestPlugin(t)
|
|
|
|
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"
|
|
|
|
req, _ := http.NewRequest("GET", addr, nil)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|