mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Update provisioning to validate user-defined UID on create (#73793)
* add ValidateUID to util * provisioning to validate UID on rule creation --------- Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com> Co-authored-by: Alexander Weaver <weaver.alex.d@gmail.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"cuelang.org/go/pkg/strings"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
@@ -84,3 +85,47 @@ func TestIsShortUIDTooLong(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateUID(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
uid string
|
||||
expected error
|
||||
}{
|
||||
{
|
||||
name: "no error when string is of correct length",
|
||||
uid: "f8cc010c-ee72-4681-89d2-d46e1bd47d33",
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "error when string is empty",
|
||||
uid: "",
|
||||
expected: ErrUIDEmpty,
|
||||
},
|
||||
{
|
||||
name: "error when string is too long",
|
||||
uid: strings.Repeat("1", MaxUIDLength+1),
|
||||
expected: ErrUIDTooLong,
|
||||
},
|
||||
{
|
||||
name: "error when string has invalid characters",
|
||||
uid: "f8cc010c.ee72.4681;89d2+d46e1bd47d33",
|
||||
expected: ErrUIDFormatInvalid,
|
||||
},
|
||||
{
|
||||
name: "error when string has only whitespaces",
|
||||
uid: " ",
|
||||
expected: ErrUIDFormatInvalid,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidateUID(tt.uid)
|
||||
if tt.expected == nil {
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
require.ErrorIs(t, err, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user