2020-02-15 22:13:50 +00:00
|
|
|
package accessctl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-02-29 20:02:25 +00:00
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2020-02-15 22:13:50 +00:00
|
|
|
)
|
|
|
|
|
2020-02-29 20:02:25 +00:00
|
|
|
func normalizeResult(res MatchResult) MatchResult {
|
|
|
|
if len(res.ChangeAccessControls) == 0 {
|
|
|
|
res.ChangeAccessControls = nil
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMatch(t *testing.T) {
|
|
|
|
secondCond := Condition{
|
|
|
|
Signature: &ConditionSignature{
|
|
|
|
AnyAccount: true,
|
|
|
|
Count: "2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-15 22:13:50 +00:00
|
|
|
tests := []struct {
|
2020-02-29 20:02:25 +00:00
|
|
|
descr string
|
|
|
|
|
|
|
|
branchACs []BranchAccessControl
|
|
|
|
interactions MatchInteractions
|
|
|
|
result MatchResult
|
2020-02-15 22:13:50 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-02-29 20:02:25 +00:00
|
|
|
descr: "empty input empty result",
|
|
|
|
result: MatchResult{
|
|
|
|
BranchPattern: "**",
|
|
|
|
},
|
2020-02-15 22:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-29 20:02:25 +00:00
|
|
|
descr: "empty access controls",
|
|
|
|
interactions: MatchInteractions{
|
2020-03-04 19:14:54 +00:00
|
|
|
Branch: "main",
|
2020-02-29 20:02:25 +00:00
|
|
|
FilePathsChanged: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
result: MatchResult{
|
2020-03-04 19:14:54 +00:00
|
|
|
BranchPattern: "main",
|
2020-02-29 20:02:25 +00:00
|
|
|
ChangeAccessControls: []MatchedChangeAccessControl{
|
|
|
|
{
|
|
|
|
ChangeAccessControl: DefaultChangeAccessControl,
|
|
|
|
FilePaths: []string{"foo", "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-15 22:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-29 20:02:25 +00:00
|
|
|
descr: "empty filesPathsChanged",
|
|
|
|
branchACs: DefaultBranchAccessControls,
|
2020-03-04 19:14:54 +00:00
|
|
|
interactions: MatchInteractions{Branch: "main"},
|
|
|
|
result: MatchResult{BranchPattern: "main"},
|
2020-02-15 22:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-29 20:02:25 +00:00
|
|
|
descr: "no matching branch patterns",
|
|
|
|
branchACs: []BranchAccessControl{{
|
|
|
|
BranchPattern: "dunk",
|
|
|
|
ChangeAccessControls: []ChangeAccessControl{{
|
|
|
|
FilePathPattern: "**",
|
|
|
|
Condition: secondCond,
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
interactions: MatchInteractions{
|
|
|
|
Branch: "crunk",
|
|
|
|
FilePathsChanged: []string{"foo"},
|
|
|
|
},
|
|
|
|
result: MatchResult{
|
|
|
|
BranchPattern: "**",
|
|
|
|
ChangeAccessControls: []MatchedChangeAccessControl{{
|
|
|
|
ChangeAccessControl: DefaultChangeAccessControl,
|
|
|
|
FilePaths: []string{"foo"},
|
|
|
|
}},
|
|
|
|
},
|
2020-02-15 22:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-29 20:02:25 +00:00
|
|
|
descr: "no matching files",
|
|
|
|
branchACs: []BranchAccessControl{{
|
2020-03-04 19:14:54 +00:00
|
|
|
BranchPattern: "main",
|
2020-02-29 20:02:25 +00:00
|
|
|
ChangeAccessControls: []ChangeAccessControl{{
|
|
|
|
FilePathPattern: "boo",
|
|
|
|
Condition: secondCond,
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
interactions: MatchInteractions{
|
2020-03-04 19:14:54 +00:00
|
|
|
Branch: "main",
|
2020-02-29 20:02:25 +00:00
|
|
|
FilePathsChanged: []string{"foo"},
|
|
|
|
},
|
|
|
|
result: MatchResult{
|
2020-03-04 19:14:54 +00:00
|
|
|
BranchPattern: "main",
|
2020-02-29 20:02:25 +00:00
|
|
|
ChangeAccessControls: []MatchedChangeAccessControl{{
|
|
|
|
ChangeAccessControl: DefaultChangeAccessControl,
|
|
|
|
FilePaths: []string{"foo"},
|
|
|
|
}},
|
|
|
|
},
|
2020-02-15 22:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-29 20:02:25 +00:00
|
|
|
descr: "branch pattern precedent",
|
|
|
|
branchACs: []BranchAccessControl{
|
|
|
|
{
|
2020-03-04 19:14:54 +00:00
|
|
|
BranchPattern: "main",
|
2020-02-29 20:02:25 +00:00
|
|
|
ChangeAccessControls: []ChangeAccessControl{{
|
|
|
|
FilePathPattern: "foo",
|
|
|
|
Condition: secondCond,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
BranchPattern: "**",
|
|
|
|
ChangeAccessControls: []ChangeAccessControl{
|
|
|
|
DefaultChangeAccessControl,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
interactions: MatchInteractions{
|
2020-03-04 19:14:54 +00:00
|
|
|
Branch: "main",
|
2020-02-29 20:02:25 +00:00
|
|
|
FilePathsChanged: []string{"foo"},
|
|
|
|
},
|
|
|
|
result: MatchResult{
|
2020-03-04 19:14:54 +00:00
|
|
|
BranchPattern: "main",
|
2020-02-29 20:02:25 +00:00
|
|
|
ChangeAccessControls: []MatchedChangeAccessControl{{
|
|
|
|
ChangeAccessControl: ChangeAccessControl{
|
|
|
|
FilePathPattern: "foo",
|
|
|
|
Condition: secondCond,
|
|
|
|
},
|
|
|
|
FilePaths: []string{"foo"},
|
|
|
|
}},
|
|
|
|
},
|
2020-02-15 22:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-29 20:02:25 +00:00
|
|
|
descr: "multiple files matching FilePathPatterns",
|
|
|
|
branchACs: []BranchAccessControl{{
|
2020-03-04 19:14:54 +00:00
|
|
|
BranchPattern: "main",
|
2020-02-29 20:02:25 +00:00
|
|
|
ChangeAccessControls: []ChangeAccessControl{{
|
|
|
|
FilePathPattern: "foo*",
|
|
|
|
Condition: secondCond,
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
interactions: MatchInteractions{
|
2020-03-04 19:14:54 +00:00
|
|
|
Branch: "main",
|
2020-02-29 20:02:25 +00:00
|
|
|
FilePathsChanged: []string{"foo_a", "bar", "foo_b"},
|
|
|
|
},
|
|
|
|
result: MatchResult{
|
2020-03-04 19:14:54 +00:00
|
|
|
BranchPattern: "main",
|
2020-02-29 20:02:25 +00:00
|
|
|
ChangeAccessControls: []MatchedChangeAccessControl{
|
|
|
|
{
|
|
|
|
ChangeAccessControl: DefaultChangeAccessControl,
|
|
|
|
FilePaths: []string{"bar"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ChangeAccessControl: ChangeAccessControl{
|
|
|
|
FilePathPattern: "foo*",
|
|
|
|
Condition: secondCond,
|
|
|
|
},
|
|
|
|
FilePaths: []string{"foo_a", "foo_b"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-15 22:13:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.descr, func(t *testing.T) {
|
2020-02-29 20:02:25 +00:00
|
|
|
res, err := Match(test.branchACs, test.interactions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error matching: %v", err)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
2020-02-29 20:02:25 +00:00
|
|
|
res, expRes := normalizeResult(res), normalizeResult(test.result)
|
|
|
|
if !reflect.DeepEqual(res, expRes) {
|
|
|
|
t.Fatalf("expected:%s\ngot: %s", spew.Sdump(expRes), spew.Sdump(res))
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|