Skip to content

Commit

Permalink
add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviagolden0 committed May 16, 2024
1 parent dab4b37 commit baa135e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rules/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type v3Locker struct {
}

func (v3l *v3Locker) Lock(key string, options ...Option) (RuleLock, error) {
validPath := regexp.MustCompile(`^[[:alnum:] \"\'\_\.\,\*\=\-]+$`)
validPath := regexp.MustCompile(`^[[:alnum:] \/\"\'\_\.\,\*\=\-]+$`)
if !validPath.MatchString(key) {
return nil, fmt.Errorf("Path variable contains an invalid character")
}
Expand Down
37 changes: 37 additions & 0 deletions rules/lock/lock_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lock

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -58,3 +59,39 @@ func Test_V3Locker(t *testing.T) {
})
}
}

func Test_V3LockerRegex(t *testing.T) {
cfg, cl := teststore.InitV3Etcd(t)
_, err := v3.New(cfg)
require.NoError(t, err)
newSession := func(_ context.Context) (*v3c.Session, error) {
return v3c.NewSession(cl, v3c.WithTTL(30))
}

testcases := []struct {
name string
lockKey string
err error
}{
{
name: "bad regex",
lockKey: "/test?/",
err: fmt.Errorf("Path variable contains an invalid character"),
},
{
name: "good regex",
lockKey: "/test/",
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
rlckr := v3Locker{
newSession: newSession,
lockTimeout: 5,
}
_, err := rlckr.Lock(tc.lockKey)
assert.Equal(t, err, tc.err)
})
}
}

0 comments on commit baa135e

Please sign in to comment.