LibraryElements: Enables creating library elements with specific UID (#39019)

* LibraryPanels: Enables create/update library panels with specific UID

* Chore: added check for uid length after PR comments

* Refactor: creates IsShortUIDTooLong function

* Refactor: adds UID to PATCH endpoint

* Refactor: clarifies the patch code

* Refactor: changes  after PR comments
This commit is contained in:
Hugo Häggmark
2021-09-10 11:22:13 +02:00
committed by GitHub
parent 580b03c952
commit fc73bc1161
9 changed files with 224 additions and 6 deletions
+5
View File
@@ -20,6 +20,11 @@ func IsValidShortUID(uid string) bool {
return validUIDPattern(uid)
}
// IsShortUIDTooLong checks if short unique identifier is too long
func IsShortUIDTooLong(uid string) bool {
return len(uid) > 40
}
// GenerateShortUID generates a short unique identifier.
func GenerateShortUID() string {
return shortid.MustGenerate()
+35 -1
View File
@@ -1,6 +1,10 @@
package util
import "testing"
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestAllowedCharMatchesUidPattern(t *testing.T) {
for _, c := range allowedChars {
@@ -9,3 +13,33 @@ func TestAllowedCharMatchesUidPattern(t *testing.T) {
}
}
}
func TestIsShortUIDTooLong(t *testing.T) {
var tests = []struct {
name string
uid string
expected bool
}{
{
name: "when the length of uid is longer than 40 chars then IsShortUIDTooLong should return true",
uid: allowedChars,
expected: true,
},
{
name: "when the length of uid is equal too 40 chars then IsShortUIDTooLong should return false",
uid: "0123456789012345678901234567890123456789",
expected: false,
},
{
name: "when the length of uid is shorter than 40 chars then IsShortUIDTooLong should return false",
uid: "012345678901234567890123456789012345678",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, IsShortUIDTooLong(tt.uid))
})
}
}