2020-03-18 22:35:32 +00:00
|
|
|
package accessctl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type filterCommitMatchTest struct {
|
|
|
|
descr string
|
2020-04-26 20:23:03 +00:00
|
|
|
filter Filter
|
2020-03-18 22:35:32 +00:00
|
|
|
req CommitRequest
|
|
|
|
match bool
|
|
|
|
|
2020-05-10 00:00:00 +00:00
|
|
|
// assumes match == false, and will ensure that the returned wrapped error
|
|
|
|
// is this one.
|
2020-03-18 22:35:32 +00:00
|
|
|
matchErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCommitMatchTests(t *testing.T, tests []filterCommitMatchTest) {
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.descr, func(t *testing.T) {
|
|
|
|
err := test.filter.MatchCommit(test.req)
|
|
|
|
shouldMatch := test.match && test.matchErr == nil
|
|
|
|
if shouldMatch && err != nil {
|
|
|
|
t.Fatalf("expected to match, got %v", err)
|
|
|
|
|
|
|
|
} else if shouldMatch {
|
|
|
|
return
|
|
|
|
|
|
|
|
} else if fErr := new(ErrFilterNoMatch); !errors.As(err, fErr) {
|
2020-05-10 00:00:00 +00:00
|
|
|
t.Fatalf("expected ErrFilterNoMatch, got: %#v", err)
|
2020-03-18 22:35:32 +00:00
|
|
|
|
|
|
|
} else if test.matchErr != nil && !reflect.DeepEqual(fErr.Err, test.matchErr) {
|
|
|
|
t.Fatalf("expected err %#v, not %#v", test.matchErr, fErr.Err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 20:23:03 +00:00
|
|
|
func TestFilterPayloadType(t *testing.T) {
|
2020-03-18 22:35:32 +00:00
|
|
|
mkReq := func(commitType string) CommitRequest {
|
|
|
|
return CommitRequest{Type: commitType}
|
|
|
|
}
|
|
|
|
|
|
|
|
runCommitMatchTests(t, []filterCommitMatchTest{
|
|
|
|
{
|
|
|
|
descr: "single match",
|
2020-04-26 20:23:03 +00:00
|
|
|
filter: FilterPayloadType{
|
2020-03-18 22:35:32 +00:00
|
|
|
Type: "foo",
|
|
|
|
},
|
|
|
|
req: mkReq("foo"),
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "single no match",
|
2020-04-26 20:23:03 +00:00
|
|
|
filter: FilterPayloadType{
|
2020-03-18 22:35:32 +00:00
|
|
|
Type: "foo",
|
|
|
|
},
|
|
|
|
req: mkReq("bar"),
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "multi match first",
|
2020-04-26 20:23:03 +00:00
|
|
|
filter: FilterPayloadType{
|
2020-03-18 22:35:32 +00:00
|
|
|
Types: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
req: mkReq("foo"),
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "multi match second",
|
2020-04-26 20:23:03 +00:00
|
|
|
filter: FilterPayloadType{
|
2020-03-18 22:35:32 +00:00
|
|
|
Types: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
req: mkReq("bar"),
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "multi no match",
|
2020-04-26 20:23:03 +00:00
|
|
|
filter: FilterPayloadType{
|
2020-03-18 22:35:32 +00:00
|
|
|
Types: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
req: mkReq("baz"),
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2020-04-24 19:33:33 +00:00
|
|
|
|
|
|
|
func TestFilterCommitAttributes(t *testing.T) {
|
|
|
|
mkReq := func(nonFF bool) CommitRequest {
|
|
|
|
return CommitRequest{NonFastForward: nonFF}
|
|
|
|
}
|
|
|
|
|
|
|
|
runCommitMatchTests(t, []filterCommitMatchTest{
|
|
|
|
{
|
|
|
|
descr: "ff with empty filter",
|
|
|
|
filter: FilterCommitAttributes{},
|
|
|
|
req: mkReq(false),
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "non-ff with empty filter",
|
|
|
|
filter: FilterCommitAttributes{},
|
|
|
|
req: mkReq(true),
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "ff with non-ff filter",
|
|
|
|
filter: FilterCommitAttributes{NonFastForward: true},
|
|
|
|
req: mkReq(false),
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "non-ff with non-ff filter",
|
|
|
|
filter: FilterCommitAttributes{NonFastForward: true},
|
|
|
|
req: mkReq(true),
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "ff with inverted non-ff filter",
|
2020-04-26 20:23:03 +00:00
|
|
|
filter: FilterNot{Filter: FilterUnion{
|
2020-04-24 19:33:33 +00:00
|
|
|
CommitAttributes: &FilterCommitAttributes{NonFastForward: true},
|
|
|
|
}},
|
|
|
|
req: mkReq(false),
|
|
|
|
match: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
descr: "non-ff with inverted non-ff filter",
|
2020-04-26 20:23:03 +00:00
|
|
|
filter: FilterNot{Filter: FilterUnion{
|
2020-04-24 19:33:33 +00:00
|
|
|
CommitAttributes: &FilterCommitAttributes{NonFastForward: true},
|
|
|
|
}},
|
|
|
|
req: mkReq(true),
|
|
|
|
match: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|