2020-02-15 22:13:50 +00:00
|
|
|
package accessctl
|
|
|
|
|
|
|
|
import (
|
2020-03-18 22:35:32 +00:00
|
|
|
"dehub/sigcred"
|
|
|
|
"errors"
|
2020-02-15 22:13:50 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-03-18 22:35:32 +00:00
|
|
|
func TestAssertCanCommit(t *testing.T) {
|
2020-02-15 22:13:50 +00:00
|
|
|
tests := []struct {
|
2020-03-18 22:35:32 +00:00
|
|
|
descr string
|
|
|
|
acl []AccessControl
|
|
|
|
req CommitRequest
|
|
|
|
allowed bool
|
2020-02-15 22:13:50 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-03-18 22:35:32 +00:00
|
|
|
descr: "first allows",
|
|
|
|
acl: []AccessControl{
|
|
|
|
{
|
|
|
|
Action: ActionAllow,
|
|
|
|
Filters: []Filter{{
|
|
|
|
CommitType: &FilterCommitType{Type: "foo"},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: ActionDeny,
|
|
|
|
Filters: []Filter{{
|
|
|
|
CommitType: &FilterCommitType{Type: "foo"},
|
|
|
|
}},
|
2020-02-29 20:02:25 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-18 22:35:32 +00:00
|
|
|
req: CommitRequest{Type: "foo"},
|
|
|
|
allowed: true,
|
2020-02-15 22:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
2020-03-18 22:35:32 +00:00
|
|
|
descr: "first denies",
|
|
|
|
acl: []AccessControl{
|
|
|
|
{
|
|
|
|
Action: ActionDeny,
|
|
|
|
Filters: []Filter{{
|
|
|
|
CommitType: &FilterCommitType{Type: "foo"},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: ActionAllow,
|
|
|
|
Filters: []Filter{{
|
|
|
|
CommitType: &FilterCommitType{Type: "foo"},
|
|
|
|
}},
|
|
|
|
},
|
2020-02-29 20:02:25 +00:00
|
|
|
},
|
2020-03-18 22:35:32 +00:00
|
|
|
req: CommitRequest{Type: "foo"},
|
|
|
|
allowed: false,
|
2020-02-15 22:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
2020-03-18 22:35:32 +00:00
|
|
|
descr: "second allows",
|
|
|
|
acl: []AccessControl{
|
2020-02-29 20:02:25 +00:00
|
|
|
{
|
2020-03-18 22:35:32 +00:00
|
|
|
Action: ActionDeny,
|
|
|
|
Filters: []Filter{{
|
|
|
|
CommitType: &FilterCommitType{Type: "bar"},
|
2020-02-29 20:02:25 +00:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
2020-03-18 22:35:32 +00:00
|
|
|
Action: ActionAllow,
|
|
|
|
Filters: []Filter{{
|
|
|
|
CommitType: &FilterCommitType{Type: "foo"},
|
|
|
|
}},
|
2020-02-29 20:02:25 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-18 22:35:32 +00:00
|
|
|
req: CommitRequest{Type: "foo"},
|
|
|
|
allowed: true,
|
2020-02-15 22:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
2020-03-18 22:35:32 +00:00
|
|
|
descr: "second denies",
|
|
|
|
acl: []AccessControl{
|
|
|
|
{
|
|
|
|
Action: ActionDeny,
|
|
|
|
Filters: []Filter{{
|
|
|
|
CommitType: &FilterCommitType{Type: "bar"},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: ActionDeny,
|
|
|
|
Filters: []Filter{{
|
|
|
|
CommitType: &FilterCommitType{Type: "foo"},
|
|
|
|
}},
|
2020-02-29 20:02:25 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-18 22:35:32 +00:00
|
|
|
req: CommitRequest{Type: "foo"},
|
|
|
|
allowed: false,
|
2020-02-15 22:13:50 +00:00
|
|
|
},
|
2020-03-14 22:14:18 +00:00
|
|
|
{
|
2020-03-18 22:35:32 +00:00
|
|
|
descr: "default allows",
|
|
|
|
acl: []AccessControl{
|
|
|
|
{
|
|
|
|
Action: ActionDeny,
|
|
|
|
Filters: []Filter{{
|
|
|
|
CommitType: &FilterCommitType{Type: "bar"},
|
|
|
|
}},
|
2020-03-14 22:14:18 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-18 22:35:32 +00:00
|
|
|
req: CommitRequest{
|
|
|
|
Branch: "not_main",
|
|
|
|
Type: "foo",
|
|
|
|
Credentials: []sigcred.Credential{{
|
|
|
|
PGPSignature: new(sigcred.CredentialPGPSignature),
|
|
|
|
AccountID: "a",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
allowed: true,
|
2020-03-14 22:14:18 +00:00
|
|
|
},
|
|
|
|
{
|
2020-03-18 22:35:32 +00:00
|
|
|
descr: "default denies",
|
|
|
|
acl: []AccessControl{
|
|
|
|
{
|
|
|
|
Action: ActionDeny,
|
|
|
|
Filters: []Filter{{
|
|
|
|
CommitType: &FilterCommitType{Type: "bar"},
|
|
|
|
}},
|
2020-03-14 22:14:18 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-18 22:35:32 +00:00
|
|
|
req: CommitRequest{
|
|
|
|
Branch: "main",
|
|
|
|
Type: "foo",
|
|
|
|
Credentials: []sigcred.Credential{{
|
|
|
|
PGPSignature: new(sigcred.CredentialPGPSignature),
|
|
|
|
AccountID: "a",
|
|
|
|
}},
|
2020-03-14 22:14:18 +00:00
|
|
|
},
|
2020-03-18 22:35:32 +00:00
|
|
|
allowed: false,
|
2020-03-14 22:14:18 +00:00
|
|
|
},
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.descr, func(t *testing.T) {
|
2020-03-18 22:35:32 +00:00
|
|
|
err := AssertCanCommit(test.acl, test.req)
|
|
|
|
if test.allowed && err != nil {
|
|
|
|
t.Fatalf("expected to be allowed but got: %v", err)
|
|
|
|
} else if !test.allowed && !errors.As(err, new(ErrCommitRequestDenied)) {
|
|
|
|
t.Fatalf("expected to be denied but got: %v", err)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|