2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-05-01 22:35:28 +00:00
|
|
|
package authmethod
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-bexpr"
|
|
|
|
"github.com/mitchellh/go-testing-interface"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RequireIdentityMatch tests to see if the given Identity matches the provided
|
|
|
|
// projected vars and filters for testing purpose.
|
|
|
|
func RequireIdentityMatch(t testing.T, id *Identity, projectedVars map[string]string, filters ...string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
gotNames := id.ProjectedVarNames()
|
|
|
|
|
|
|
|
require.Equal(t, projectedVars, id.ProjectedVars)
|
|
|
|
|
|
|
|
expectNames := make([]string, 0, len(projectedVars))
|
2020-06-16 17:19:31 +00:00
|
|
|
for k := range projectedVars {
|
2020-05-01 22:35:28 +00:00
|
|
|
expectNames = append(expectNames, k)
|
|
|
|
}
|
|
|
|
sort.Strings(expectNames)
|
|
|
|
sort.Strings(gotNames)
|
|
|
|
|
|
|
|
require.Equal(t, expectNames, gotNames)
|
|
|
|
require.Nil(t, id.EnterpriseMeta)
|
|
|
|
|
|
|
|
for _, filter := range filters {
|
|
|
|
eval, err := bexpr.CreateEvaluatorForType(filter, nil, id.SelectableFields)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("filter %q got err: %v", filter, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := eval.Evaluate(id.SelectableFields)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("filter %q got err: %v", filter, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !result {
|
|
|
|
t.Fatalf("filter %q did not match", filter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|