-
Notifications
You must be signed in to change notification settings - Fork 57
/
helpers.go
56 lines (47 loc) · 1.93 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package test
import (
"context"
"fmt"
"github.com/Legit-Labs/legitify/internal/common/scm_type"
"testing"
"github.com/stretchr/testify/require"
githubcollected "github.com/Legit-Labs/legitify/internal/collected/github"
"github.com/Legit-Labs/legitify/internal/common/namespace"
"github.com/Legit-Labs/legitify/internal/common/permissions"
"github.com/Legit-Labs/legitify/internal/opa"
"github.com/Legit-Labs/legitify/internal/opa/opa_engine"
"github.com/google/go-github/v53/github"
)
var defaultOrg = githubcollected.NewExtendedOrg(&github.Organization{}, permissions.OrgRoleNone)
func FindPolicy(opaResults []opa_engine.QueryResult, policyName string) (*opa_engine.QueryResult, error) {
for _, s := range opaResults {
if policyName == s.PolicyName {
return &s, nil
}
}
return nil, fmt.Errorf("policy doesn't exists %s", policyName)
}
func BuildFailedString(expected bool, policy string) string {
if expected {
return fmt.Sprintf("expected policy %s to exist in results", policy)
}
return fmt.Sprintf("did not expect expect policy %s to exist in results", policy)
}
func AssertQueryResult(opaResults []opa_engine.QueryResult, policyName string, shouldBeViolated bool, t *testing.T) {
require.NotNil(t, opaResults, "query result == nil")
policy, err := FindPolicy(opaResults, policyName)
require.NoErrorf(t, err, "failed to find policy")
if policy.IsViolation != shouldBeViolated {
t.Error(BuildFailedString(shouldBeViolated, policyName))
}
}
func PolicyTestTemplate(t *testing.T, name string, mockData interface{}, ns namespace.Namespace, testedPolicyName string, expectFailure bool, scmType scm_type.ScmType) {
t.Run(name, func(t *testing.T) {
engine, err := opa.Load([]string{}, scmType)
require.Nil(t, err, "failed initializing opa client")
ctx := context.Background()
result, err := engine.Query(ctx, ns, mockData)
require.Nil(t, err, "failed query")
AssertQueryResult(result, testedPolicyName, expectFailure, t)
})
}