2021-05-18 14:30:20 +00:00
|
|
|
package aicra_test
|
2021-05-18 07:36:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-18 14:30:20 +00:00
|
|
|
"git.xdrm.io/go/aicra"
|
2021-05-18 07:36:33 +00:00
|
|
|
"git.xdrm.io/go/aicra/api"
|
2021-05-18 14:30:20 +00:00
|
|
|
"git.xdrm.io/go/aicra/datatype/builtin"
|
2021-05-18 07:36:33 +00:00
|
|
|
)
|
|
|
|
|
2021-05-18 14:30:20 +00:00
|
|
|
func addBuiltinTypes(b *aicra.Builder) error {
|
|
|
|
if err := b.AddType(builtin.AnyDataType{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.AddType(builtin.BoolDataType{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.AddType(builtin.FloatDataType{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.AddType(builtin.IntDataType{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.AddType(builtin.StringDataType{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.AddType(builtin.UintDataType{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-18 07:36:33 +00:00
|
|
|
func TestWith(t *testing.T) {
|
2021-05-18 14:30:20 +00:00
|
|
|
builder := &aicra.Builder{}
|
2021-05-18 07:36:33 +00:00
|
|
|
if err := addBuiltinTypes(builder); err != nil {
|
|
|
|
t.Fatalf("unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// build @n middlewares that take data from context and increment it
|
|
|
|
n := 1024
|
|
|
|
|
|
|
|
type ckey int
|
|
|
|
const key ckey = 0
|
|
|
|
|
2021-06-20 00:14:31 +00:00
|
|
|
middleware := func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-05-18 07:36:33 +00:00
|
|
|
newr := r
|
|
|
|
|
|
|
|
// first time -> store 1
|
|
|
|
value := r.Context().Value(key)
|
|
|
|
if value == nil {
|
|
|
|
newr = r.WithContext(context.WithValue(r.Context(), key, int(1)))
|
2021-06-20 00:14:31 +00:00
|
|
|
next.ServeHTTP(w, newr)
|
2021-05-18 07:36:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// get value and increment
|
|
|
|
cast, ok := value.(int)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("value is not an int")
|
|
|
|
}
|
|
|
|
cast++
|
|
|
|
newr = r.WithContext(context.WithValue(r.Context(), key, cast))
|
2021-06-20 00:14:31 +00:00
|
|
|
next.ServeHTTP(w, newr)
|
|
|
|
})
|
2021-05-18 07:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add middleware @n times
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
builder.With(middleware)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := strings.NewReader(`[ { "method": "GET", "path": "/path", "scope": [[]], "info": "info", "in": {}, "out": {} } ]`)
|
|
|
|
err := builder.Setup(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("setup: unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
2021-06-20 00:14:31 +00:00
|
|
|
pathHandler := func(ctx context.Context) (*struct{}, api.Err) {
|
2021-05-18 07:36:33 +00:00
|
|
|
// write value from middlewares into response
|
2021-06-19 22:47:04 +00:00
|
|
|
value := ctx.Value(key)
|
2021-05-18 07:36:33 +00:00
|
|
|
if value == nil {
|
|
|
|
t.Fatalf("nothing found in context")
|
|
|
|
}
|
|
|
|
cast, ok := value.(int)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("cannot cast context data to int")
|
|
|
|
}
|
|
|
|
// write to response
|
2021-06-20 00:14:31 +00:00
|
|
|
api.GetResponseWriter(ctx).Write([]byte(fmt.Sprintf("#%d#", cast)))
|
2021-05-18 07:36:33 +00:00
|
|
|
|
|
|
|
return nil, api.ErrSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := builder.Bind(http.MethodGet, "/path", pathHandler); err != nil {
|
|
|
|
t.Fatalf("bind: unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
handler, err := builder.Build()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("build: unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/path", &bytes.Buffer{})
|
|
|
|
|
|
|
|
// test request
|
|
|
|
handler.ServeHTTP(response, request)
|
|
|
|
if response.Body == nil {
|
|
|
|
t.Fatalf("response has no body")
|
|
|
|
}
|
|
|
|
token := fmt.Sprintf("#%d#", n)
|
|
|
|
if !strings.Contains(response.Body.String(), token) {
|
|
|
|
t.Fatalf("expected '%s' to be in response <%s>", token, response.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-05-18 07:59:49 +00:00
|
|
|
|
|
|
|
func TestWithAuth(t *testing.T) {
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
manifest string
|
|
|
|
permissions []string
|
|
|
|
granted bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "provide only requirement A",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{"A"},
|
|
|
|
granted: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing requirement",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{},
|
|
|
|
granted: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing requirements",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A", "B"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{},
|
|
|
|
granted: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing some requirements",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A", "B"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{"A"},
|
|
|
|
granted: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "provide requirements",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A", "B"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{"A", "B"},
|
|
|
|
granted: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing OR requirements",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A"], ["B"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{"C"},
|
|
|
|
granted: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "provide 1 OR requirement",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A"], ["B"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{"A"},
|
|
|
|
granted: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "provide both OR requirements",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A"], ["B"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{"A", "B"},
|
|
|
|
granted: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing composite OR requirements",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A", "B"], ["C", "D"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{},
|
|
|
|
granted: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing partial composite OR requirements",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A", "B"], ["C", "D"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{"A", "C"},
|
|
|
|
granted: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "provide 1 composite OR requirement",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A", "B"], ["C", "D"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{"A", "B", "C"},
|
|
|
|
granted: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "provide both composite OR requirements",
|
|
|
|
manifest: `[ { "method": "GET", "path": "/path", "scope": [["A", "B"], ["C", "D"]], "info": "info", "in": {}, "out": {} } ]`,
|
|
|
|
permissions: []string{"A", "B", "C", "D"},
|
|
|
|
granted: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-05-18 14:30:20 +00:00
|
|
|
builder := &aicra.Builder{}
|
2021-05-18 07:59:49 +00:00
|
|
|
if err := addBuiltinTypes(builder); err != nil {
|
|
|
|
t.Fatalf("unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// tester middleware (last executed)
|
2021-06-20 00:14:31 +00:00
|
|
|
builder.WithContext(func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
a := api.GetAuth(r.Context())
|
|
|
|
if a == nil {
|
|
|
|
t.Fatalf("cannot access api.Auth form request context")
|
|
|
|
}
|
|
|
|
|
2021-05-18 07:59:49 +00:00
|
|
|
if a.Granted() == tc.granted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if a.Granted() {
|
|
|
|
t.Fatalf("unexpected granted auth")
|
|
|
|
} else {
|
|
|
|
t.Fatalf("expected granted auth")
|
|
|
|
}
|
2021-06-20 00:14:31 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2021-05-18 07:59:49 +00:00
|
|
|
})
|
|
|
|
|
2021-06-20 00:14:31 +00:00
|
|
|
builder.WithContext(func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
a := api.GetAuth(r.Context())
|
|
|
|
if a == nil {
|
|
|
|
t.Fatalf("cannot access api.Auth form request context")
|
|
|
|
}
|
|
|
|
|
2021-05-18 07:59:49 +00:00
|
|
|
a.Active = tc.permissions
|
2021-06-20 00:14:31 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2021-05-18 07:59:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err := builder.Setup(strings.NewReader(tc.manifest))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("setup: unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
2021-06-20 00:14:31 +00:00
|
|
|
pathHandler := func(ctx context.Context) (*struct{}, api.Err) {
|
2021-05-18 07:59:49 +00:00
|
|
|
return nil, api.ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := builder.Bind(http.MethodGet, "/path", pathHandler); err != nil {
|
|
|
|
t.Fatalf("bind: unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
handler, err := builder.Build()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("build: unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/path", &bytes.Buffer{})
|
|
|
|
|
|
|
|
// test request
|
|
|
|
handler.ServeHTTP(response, request)
|
|
|
|
if response.Body == nil {
|
|
|
|
t.Fatalf("response has no body")
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-05-18 14:30:20 +00:00
|
|
|
|
|
|
|
func TestDynamicScope(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
manifest string
|
|
|
|
path string
|
|
|
|
handler interface{}
|
|
|
|
url string
|
|
|
|
body string
|
|
|
|
permissions []string
|
|
|
|
granted bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "replace one granted",
|
|
|
|
manifest: `[
|
|
|
|
{
|
|
|
|
"method": "POST",
|
|
|
|
"path": "/path/{id}",
|
|
|
|
"info": "info",
|
|
|
|
"scope": [["user[Input1]"]],
|
|
|
|
"in": {
|
|
|
|
"{id}": { "info": "info", "name": "Input1", "type": "uint" }
|
|
|
|
},
|
|
|
|
"out": {}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
path: "/path/{id}",
|
2021-06-20 00:14:31 +00:00
|
|
|
handler: func(context.Context, struct{ Input1 uint }) (*struct{}, api.Err) { return nil, api.ErrSuccess },
|
2021-05-18 14:30:20 +00:00
|
|
|
url: "/path/123",
|
|
|
|
body: ``,
|
|
|
|
permissions: []string{"user[123]"},
|
|
|
|
granted: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "replace one mismatch",
|
|
|
|
manifest: `[
|
|
|
|
{
|
|
|
|
"method": "POST",
|
|
|
|
"path": "/path/{id}",
|
|
|
|
"info": "info",
|
|
|
|
"scope": [["user[Input1]"]],
|
|
|
|
"in": {
|
|
|
|
"{id}": { "info": "info", "name": "Input1", "type": "uint" }
|
|
|
|
},
|
|
|
|
"out": {}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
path: "/path/{id}",
|
2021-06-20 00:14:31 +00:00
|
|
|
handler: func(context.Context, struct{ Input1 uint }) (*struct{}, api.Err) { return nil, api.ErrSuccess },
|
2021-05-18 14:30:20 +00:00
|
|
|
url: "/path/666",
|
|
|
|
body: ``,
|
|
|
|
permissions: []string{"user[123]"},
|
|
|
|
granted: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "replace one valid dot separated",
|
|
|
|
manifest: `[
|
|
|
|
{
|
|
|
|
"method": "POST",
|
|
|
|
"path": "/path/{id}",
|
|
|
|
"info": "info",
|
|
|
|
"scope": [["prefix.user[User].suffix"]],
|
|
|
|
"in": {
|
|
|
|
"{id}": { "info": "info", "name": "User", "type": "uint" }
|
|
|
|
},
|
|
|
|
"out": {}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
path: "/path/{id}",
|
2021-06-20 00:14:31 +00:00
|
|
|
handler: func(context.Context, struct{ User uint }) (*struct{}, api.Err) { return nil, api.ErrSuccess },
|
2021-05-18 14:30:20 +00:00
|
|
|
url: "/path/123",
|
|
|
|
body: ``,
|
|
|
|
permissions: []string{"prefix.user[123].suffix"},
|
|
|
|
granted: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "replace two valid dot separated",
|
|
|
|
manifest: `[
|
|
|
|
{
|
|
|
|
"method": "POST",
|
|
|
|
"path": "/prefix/{pid}/user/{uid}",
|
|
|
|
"info": "info",
|
|
|
|
"scope": [["prefix[Prefix].user[User].suffix"]],
|
|
|
|
"in": {
|
|
|
|
"{pid}": { "info": "info", "name": "Prefix", "type": "uint" },
|
|
|
|
"{uid}": { "info": "info", "name": "User", "type": "uint" }
|
|
|
|
},
|
|
|
|
"out": {}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
path: "/prefix/{pid}/user/{uid}",
|
2021-06-20 00:14:31 +00:00
|
|
|
handler: func(context.Context, struct {
|
2021-05-18 14:30:20 +00:00
|
|
|
Prefix uint
|
|
|
|
User uint
|
|
|
|
}) (*struct{}, api.Err) {
|
|
|
|
return nil, api.ErrSuccess
|
|
|
|
},
|
|
|
|
url: "/prefix/123/user/456",
|
|
|
|
body: ``,
|
|
|
|
permissions: []string{"prefix[123].user[456].suffix"},
|
|
|
|
granted: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "replace two invalid dot separated",
|
|
|
|
manifest: `[
|
|
|
|
{
|
|
|
|
"method": "POST",
|
|
|
|
"path": "/prefix/{pid}/user/{uid}",
|
|
|
|
"info": "info",
|
|
|
|
"scope": [["prefix[Prefix].user[User].suffix"]],
|
|
|
|
"in": {
|
|
|
|
"{pid}": { "info": "info", "name": "Prefix", "type": "uint" },
|
|
|
|
"{uid}": { "info": "info", "name": "User", "type": "uint" }
|
|
|
|
},
|
|
|
|
"out": {}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
path: "/prefix/{pid}/user/{uid}",
|
2021-06-20 00:14:31 +00:00
|
|
|
handler: func(context.Context, struct {
|
2021-05-18 14:30:20 +00:00
|
|
|
Prefix uint
|
|
|
|
User uint
|
|
|
|
}) (*struct{}, api.Err) {
|
|
|
|
return nil, api.ErrSuccess
|
|
|
|
},
|
|
|
|
url: "/prefix/123/user/666",
|
|
|
|
body: ``,
|
|
|
|
permissions: []string{"prefix[123].user[456].suffix"},
|
|
|
|
granted: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "replace three valid dot separated",
|
|
|
|
manifest: `[
|
|
|
|
{
|
|
|
|
"method": "POST",
|
|
|
|
"path": "/prefix/{pid}/user/{uid}/suffix/{sid}",
|
|
|
|
"info": "info",
|
|
|
|
"scope": [["prefix[Prefix].user[User].suffix[Suffix]"]],
|
|
|
|
"in": {
|
|
|
|
"{pid}": { "info": "info", "name": "Prefix", "type": "uint" },
|
|
|
|
"{uid}": { "info": "info", "name": "User", "type": "uint" },
|
|
|
|
"{sid}": { "info": "info", "name": "Suffix", "type": "uint" }
|
|
|
|
},
|
|
|
|
"out": {}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
path: "/prefix/{pid}/user/{uid}/suffix/{sid}",
|
2021-06-20 00:14:31 +00:00
|
|
|
handler: func(context.Context, struct {
|
2021-05-18 14:30:20 +00:00
|
|
|
Prefix uint
|
|
|
|
User uint
|
|
|
|
Suffix uint
|
|
|
|
}) (*struct{}, api.Err) {
|
|
|
|
return nil, api.ErrSuccess
|
|
|
|
},
|
|
|
|
url: "/prefix/123/user/456/suffix/789",
|
|
|
|
body: ``,
|
|
|
|
permissions: []string{"prefix[123].user[456].suffix[789]"},
|
|
|
|
granted: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "replace three invalid dot separated",
|
|
|
|
manifest: `[
|
|
|
|
{
|
|
|
|
"method": "POST",
|
|
|
|
"path": "/prefix/{pid}/user/{uid}/suffix/{sid}",
|
|
|
|
"info": "info",
|
|
|
|
"scope": [["prefix[Prefix].user[User].suffix[Suffix]"]],
|
|
|
|
"in": {
|
|
|
|
"{pid}": { "info": "info", "name": "Prefix", "type": "uint" },
|
|
|
|
"{uid}": { "info": "info", "name": "User", "type": "uint" },
|
|
|
|
"{sid}": { "info": "info", "name": "Suffix", "type": "uint" }
|
|
|
|
},
|
|
|
|
"out": {}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
path: "/prefix/{pid}/user/{uid}/suffix/{sid}",
|
2021-06-20 00:14:31 +00:00
|
|
|
handler: func(context.Context, struct {
|
2021-05-18 14:30:20 +00:00
|
|
|
Prefix uint
|
|
|
|
User uint
|
|
|
|
Suffix uint
|
|
|
|
}) (*struct{}, api.Err) {
|
|
|
|
return nil, api.ErrSuccess
|
|
|
|
},
|
|
|
|
url: "/prefix/123/user/666/suffix/789",
|
|
|
|
body: ``,
|
|
|
|
permissions: []string{"prefix[123].user[456].suffix[789]"},
|
|
|
|
granted: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
builder := &aicra.Builder{}
|
|
|
|
if err := addBuiltinTypes(builder); err != nil {
|
|
|
|
t.Fatalf("unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// tester middleware (last executed)
|
2021-06-20 00:14:31 +00:00
|
|
|
builder.WithContext(func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
a := api.GetAuth(r.Context())
|
|
|
|
if a == nil {
|
|
|
|
t.Fatalf("cannot access api.Auth form request context")
|
|
|
|
}
|
2021-05-18 14:30:20 +00:00
|
|
|
if a.Granted() == tc.granted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if a.Granted() {
|
|
|
|
t.Fatalf("unexpected granted auth")
|
|
|
|
} else {
|
|
|
|
t.Fatalf("expected granted auth")
|
|
|
|
}
|
2021-06-20 00:14:31 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2021-05-18 14:30:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// update permissions
|
2021-06-20 00:14:31 +00:00
|
|
|
builder.WithContext(func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
a := api.GetAuth(r.Context())
|
|
|
|
if a == nil {
|
|
|
|
t.Fatalf("cannot access api.Auth form request context")
|
|
|
|
}
|
2021-05-18 14:30:20 +00:00
|
|
|
a.Active = tc.permissions
|
2021-06-20 00:14:31 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2021-05-18 14:30:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err := builder.Setup(strings.NewReader(tc.manifest))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("setup: unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := builder.Bind(http.MethodPost, tc.path, tc.handler); err != nil {
|
|
|
|
t.Fatalf("bind: unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
handler, err := builder.Build()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("build: unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
body := strings.NewReader(tc.body)
|
|
|
|
request := httptest.NewRequest(http.MethodPost, tc.url, body)
|
|
|
|
|
|
|
|
// test request
|
|
|
|
handler.ServeHTTP(response, request)
|
|
|
|
if response.Body == nil {
|
|
|
|
t.Fatalf("response has no body")
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|