2020-02-15 22:13:50 +00:00
|
|
|
package accessctl
|
|
|
|
|
|
|
|
import (
|
2020-03-18 22:35:32 +00:00
|
|
|
"errors"
|
2020-02-15 22:13:50 +00:00
|
|
|
"testing"
|
2020-04-26 20:23:03 +00:00
|
|
|
|
|
|
|
"dehub.dev/src/dehub.git/sigcred"
|
2020-02-15 22:13:50 +00:00
|
|
|
)
|
|
|
|
|
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,
|
2020-04-26 20:23:03 +00:00
|
|
|
Filters: []FilterUnion{{
|
|
|
|
PayloadType: &FilterPayloadType{Type: "foo"},
|
2020-03-18 22:35:32 +00:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: ActionDeny,
|
2020-04-26 20:23:03 +00:00
|
|
|
Filters: []FilterUnion{{
|
|
|
|
PayloadType: &FilterPayloadType{Type: "foo"},
|
2020-03-18 22:35:32 +00:00
|
|
|
}},
|
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,
|
2020-04-26 20:23:03 +00:00
|
|
|
Filters: []FilterUnion{{
|
|
|
|
PayloadType: &FilterPayloadType{Type: "foo"},
|
2020-03-18 22:35:32 +00:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: ActionAllow,
|
2020-04-26 20:23:03 +00:00
|
|
|
Filters: []FilterUnion{{
|
|
|
|
PayloadType: &FilterPayloadType{Type: "foo"},
|
2020-03-18 22:35:32 +00:00
|
|
|
}},
|
|
|
|
},
|
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,
|
2020-04-26 20:23:03 +00:00
|
|
|
Filters: []FilterUnion{{
|
|
|
|
PayloadType: &FilterPayloadType{Type: "bar"},
|
2020-02-29 20:02:25 +00:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
2020-03-18 22:35:32 +00:00
|
|
|
Action: ActionAllow,
|
2020-04-26 20:23:03 +00:00
|
|
|
Filters: []FilterUnion{{
|
|
|
|
PayloadType: &FilterPayloadType{Type: "foo"},
|
2020-03-18 22:35:32 +00:00
|
|
|
}},
|
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,
|
2020-04-26 20:23:03 +00:00
|
|
|
Filters: []FilterUnion{{
|
|
|
|
PayloadType: &FilterPayloadType{Type: "bar"},
|
2020-03-18 22:35:32 +00:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: ActionDeny,
|
2020-04-26 20:23:03 +00:00
|
|
|
Filters: []FilterUnion{{
|
|
|
|
PayloadType: &FilterPayloadType{Type: "foo"},
|
2020-03-18 22:35:32 +00:00
|
|
|
}},
|
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,
|
2020-04-26 20:23:03 +00:00
|
|
|
Filters: []FilterUnion{{
|
|
|
|
PayloadType: &FilterPayloadType{Type: "bar"},
|
2020-03-18 22:35:32 +00:00
|
|
|
}},
|
2020-03-14 22:14:18 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-18 22:35:32 +00:00
|
|
|
req: CommitRequest{
|
|
|
|
Branch: "not_main",
|
|
|
|
Type: "foo",
|
2020-04-26 20:23:03 +00:00
|
|
|
Credentials: []sigcred.CredentialUnion{{
|
2020-03-18 22:35:32 +00:00
|
|
|
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,
|
2020-04-26 20:23:03 +00:00
|
|
|
Filters: []FilterUnion{{
|
|
|
|
PayloadType: &FilterPayloadType{Type: "bar"},
|
2020-03-18 22:35:32 +00:00
|
|
|
}},
|
2020-03-14 22:14:18 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-18 22:35:32 +00:00
|
|
|
req: CommitRequest{
|
|
|
|
Branch: "main",
|
|
|
|
Type: "foo",
|
2020-04-26 20:23:03 +00:00
|
|
|
Credentials: []sigcred.CredentialUnion{{
|
2020-03-18 22:35:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|