Skip to content

Commit

Permalink
Merge pull request #257 from checkr/ra/relax-regex-to-allow-numbers-c…
Browse files Browse the repository at this point in the history
…apitals

Allow more capital characters and numbers in flag keys and variant keys
  • Loading branch information
zhouzhuojie authored May 15, 2019
2 parents 11a33a8 + 09aaad6 commit 0373eb2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pkg/entity/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestCreateFlagKey(t *testing.T) {
})

t.Run("invalid key", func(t *testing.T) {
key, err := CreateFlagKey("1-2-3")
key, err := CreateFlagKey(" spaces in key are not allowed 1-2-3")
assert.Error(t, err)
assert.Zero(t, key)
})
Expand All @@ -52,7 +52,7 @@ func TestCreateFlagEntityType(t *testing.T) {
f := GenFixtureFlag()
db := PopulateTestDB(f)

err := CreateFlagEntityType(db, "123-invalid-key")
err := CreateFlagEntityType(db, " spaces in key are not allowed 123-invalid-key")
assert.Error(t, err)
})
}
8 changes: 4 additions & 4 deletions pkg/handler/crud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func TestCrudFlagsWithFailures(t *testing.T) {
t.Run("CreateFlag - invalid key error", func(t *testing.T) {
res = c.CreateFlag(flag.CreateFlagParams{
Body: &models.CreateFlagRequest{
Description: util.StringPtr("funny flag"),
Key: "1-2-3", // invalid key
Description: util.StringPtr(" flag with a space"),
Key: " 1-2-3", // invalid key
},
})
assert.NotZero(t, res.(*flag.CreateFlagDefault).Payload)
Expand Down Expand Up @@ -811,7 +811,7 @@ func TestCrudVariantsWithFailures(t *testing.T) {
res = c.CreateVariant(variant.CreateVariantParams{
FlagID: int64(1),
Body: &models.CreateVariantRequest{
Key: util.StringPtr("123_invalid_key"),
Key: util.StringPtr(" 123_invalid_key"),
},
})
assert.NotZero(t, res.(*variant.CreateVariantDefault).Payload)
Expand Down Expand Up @@ -868,7 +868,7 @@ func TestCrudVariantsWithFailures(t *testing.T) {
FlagID: int64(1),
VariantID: int64(1),
Body: &models.PutVariantRequest{
Key: util.StringPtr("123_invalid_key"),
Key: util.StringPtr(" spaces in key 123_invalid_key"),
},
})
assert.NotZero(t, *res.(*variant.PutVariantDefault).Payload)
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var (
keyLengthLimit = 63
keyRegex = regexp.MustCompile("^[a-z]+[a-z0-9_]*$")
keyRegex = regexp.MustCompile(`^[\w\d-]+$`)

randomKeyCharset = []byte("123456789abcdefghijkmnopqrstuvwxyz")
randomKeyPrefix = "k"
Expand Down
10 changes: 7 additions & 3 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ func TestIsSafeKey(t *testing.T) {
assert.Empty(t, msg)

b, msg = IsSafeKey("1a")
assert.True(t, b)
assert.Empty(t, msg)

b, msg = IsSafeKey(" spaces in key are not allowed ")
assert.False(t, b)
assert.NotEmpty(t, msg)
assert.NotEmpty(t, msg)

b, msg = IsSafeKey("_a")
assert.False(t, b)
assert.NotEmpty(t, msg)
assert.True(t, b)
assert.Empty(t, msg)

b, msg = IsSafeKey(strings.Repeat("a", 64))
assert.False(t, b)
Expand Down

0 comments on commit 0373eb2

Please sign in to comment.