Skip to content

Commit

Permalink
add tests for regex
Browse files Browse the repository at this point in the history
  • Loading branch information
fairclothjm committed May 14, 2024
1 parent 43e50c0 commit 3036917
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 30 deletions.
4 changes: 2 additions & 2 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ func Backend(client ldapClient) *backend {

// These paths are more generic than the above. They must be
// appended last.
b.pathListStaticRoles(),
b.pathConfig(),
b.pathDynamicRoles(),
b.pathDynamicCredsCreate(),
b.pathStaticRoles(),
b.pathStaticCredsCreate(),
b.pathListStaticRoles(),
b.pathRotateCredentials(),
b.pathListSets(),
b.pathSets(),
b.pathListSets(),
),
InitializeFunc: b.initialize,
Secrets: []*framework.Secret{
Expand Down
2 changes: 0 additions & 2 deletions path_dynamic_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,6 @@ func TestDynamicRoleList(t *testing.T) {
}
}

// TODO(JM): List regex should support optional trailing slash
// rolePaths := []string{"org", "org/", "org/platform", "org/platform/"}
rolePaths := []string{"org/", "org/platform/"}
for _, rolePath := range rolePaths {
req := &logical.Request{
Expand Down
3 changes: 1 addition & 2 deletions path_static_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ func genericNameWithForwardSlashRegex(name string) string {
// including a role path in list options. The role path can be used to list
// nested roles at arbitrary depth.
func optionalGenericNameWithForwardSlashListRegex(name string) string {
// TODO(JM): List regex should support optional trailing slash
return fmt.Sprintf("/?(/(?P<%s>.+/))?", name)
return fmt.Sprintf("/?(?P<%s>.+)?", name)
}

func (b *backend) pathListStaticRoles() []*framework.Path {
Expand Down
3 changes: 0 additions & 3 deletions path_static_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,6 @@ func TestListRoles(t *testing.T) {
}
}

// TODO(JM): List regex should support optional trailing slash
// rolePaths := []string{"org", "org/", "org/platform", "org/platform/"}
rolePaths := []string{"org/", "org/platform/"}
for _, rolePath := range rolePaths {
req := &logical.Request{
Expand All @@ -509,7 +507,6 @@ func TestListRoles(t *testing.T) {
Data: nil,
}
resp, err := b.HandleRequest(context.Background(), req)

if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("path: %s, err:%s resp:%#v\n", rolePath, err, resp)
}
Expand Down
120 changes: 99 additions & 21 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,105 @@ import (
"github.com/stretchr/testify/require"
)

func TestPathRegexList(t *testing.T) {
tests := map[string]struct {
pattern string
input string
want map[string]string
wantErr bool
}{
"single-part": {
pattern: "prefix" + optionalGenericNameWithForwardSlashListRegex("value"),
input: "prefix/foo",
want: map[string]string{"value": "foo"},
},
"single-part-trailing-slash": {
pattern: "prefix" + optionalGenericNameWithForwardSlashListRegex("value"),
input: "prefix/foo/",
want: map[string]string{"value": "foo/"},
},
"single-part-with-suffix": {
pattern: "prefix" + optionalGenericNameWithForwardSlashListRegex("value") + "/suffix",
input: "prefix/foo/suffix",
want: map[string]string{"value": "foo"},
},
"multi-part": {
pattern: "prefix" + optionalGenericNameWithForwardSlashListRegex("value"),
input: "prefix/foo/bar",
want: map[string]string{"value": "foo/bar"},
},
"multi-part-trailing-slash": {
pattern: "prefix" + optionalGenericNameWithForwardSlashListRegex("value"),
input: "prefix/foo/bar/",
want: map[string]string{"value": "foo/bar/"},
},
"multi-part-with-suffix": {
pattern: "prefix" + optionalGenericNameWithForwardSlashListRegex("value") + "/suffix",
input: "prefix/foo/bar/suffix",
want: map[string]string{"value": "foo/bar"},
},
"multi-prefix-single-part": {
pattern: "prefix/a" + optionalGenericNameWithForwardSlashListRegex("value"),
input: "prefix/a/foo",
want: map[string]string{"value": "foo"},
},
"multi-prefix-single-part-with-suffix": {
pattern: "prefix/a" + optionalGenericNameWithForwardSlashListRegex("value") + "/suffix",
input: "prefix/a/foo/suffix",
want: map[string]string{"value": "foo"},
},
"multi-prefix-multi-part": {
pattern: "prefix/a" + optionalGenericNameWithForwardSlashListRegex("value"),
input: "prefix/a/foo/bar",
want: map[string]string{"value": "foo/bar"},
},
"multi-prefix-multi-part-trailing-slash": {
pattern: "prefix/a" + optionalGenericNameWithForwardSlashListRegex("value"),
input: "prefix/a/foo/bar/",
want: map[string]string{"value": "foo/bar/"},
},
"multi-prefix-multi-part-with-suffix": {
pattern: "prefix/a" + optionalGenericNameWithForwardSlashListRegex("value") + "/suffix",
input: "prefix/a/foo/bar/suffix",
want: map[string]string{"value": "foo/bar"},
},
"multi-prefix-single-part-with-multi-suffix": {
pattern: "prefix/a" + optionalGenericNameWithForwardSlashListRegex("value") + "/b/suffix",
input: "prefix/a/foo/b/suffix",
want: map[string]string{"value": "foo"},
},
"multi-prefix-multi-part-with-multi-suffix": {
pattern: "prefix/a" + optionalGenericNameWithForwardSlashListRegex("value") + "/b/suffix",
input: "prefix/a/foo/bar/b/suffix",
want: map[string]string{"value": "foo/bar"},
},
"single-part-special-chars": {
pattern: "prefix" + optionalGenericNameWithForwardSlashListRegex("value"),
input: "prefix/foo-.bar",
want: map[string]string{"value": "foo-.bar"},
},
"multi-part-special-chars": {
pattern: "prefix" + optionalGenericNameWithForwardSlashListRegex("value"),
input: "prefix/foo-.bar/baz-.qux",
want: map[string]string{"value": "foo-.bar/baz-.qux"},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
re, err := regexp.Compile(tc.pattern)
require.NoError(t, err)
got, err := getCaptures(re, tc.input)
if tc.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tc.want, got)
}
})
}
}

func TestPathRegex(t *testing.T) {
tests := map[string]struct {
pattern string
Expand Down Expand Up @@ -75,27 +174,6 @@ func TestPathRegex(t *testing.T) {
input: "prefix/foo-.bar/baz-.qux",
want: map[string]string{"value": "foo-.bar/baz-.qux"},
},
// TODO(JM): this should error
// "multi-part-special-chars-error": {
// pattern: "prefix" + genericNameWithForwardSlashRegex("value"),
// input: "prefix/foo-.bar/baz-.",
// want: nil,
// wantErr: true,
// },
// TODO(JM): this should error
// "multi-part-error-special-chars": {
// pattern: "prefix" + genericNameWithForwardSlashRegex("value"),
// input: "prefix/foo-.$bar/baz/check-in",
// want: nil,
// wantErr: true,
// },
// TODO(JM): this should error
// "multi-part-error-special-chars": {
// pattern: "prefix" + genericNameWithForwardSlashRegex("value"),
// input: "prefix/foo-.@bar/baz",
// want: nil,
// wantErr: true,
// },
}

for name, tc := range tests {
Expand Down

0 comments on commit 3036917

Please sign in to comment.