[GH-16199] add IsValid to model OpenDialogRequest (#26526)
* [GH-16199] add IsValid to model OpenDialogRequest * [GH-16199] add IsValid to model OpenDialogRequest, revert remove of translations * [GH-16199] fix tests after revert * [GH-16199] add IsValid to model OpenDialogRequest * [GH-16199] revert validation of icon url * [GH-16199] update go sum * [GH-16199] log warning for invalid dialog * [GH-16199] log error for invalid dialog * [GH-16199] log warning for invalid dialog * [GH-16199] fix golang-ci --------- Co-authored-by: Lukas Eipert <git@leipert.io> Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
@@ -8,6 +8,7 @@ import (
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -180,3 +181,190 @@ func TestPostActionIntegrationEquals(t *testing.T) {
|
||||
require.True(t, pa1.Equals(pa2))
|
||||
})
|
||||
}
|
||||
|
||||
func TestOpenDialogRequestIsValid(t *testing.T) {
|
||||
getBaseOpenDialogRequest := func() OpenDialogRequest {
|
||||
return OpenDialogRequest{
|
||||
TriggerId: "triggerId",
|
||||
URL: "http://localhost:8065",
|
||||
Dialog: Dialog{
|
||||
CallbackId: "callbackid",
|
||||
Title: "Some Title",
|
||||
Elements: []DialogElement{
|
||||
{
|
||||
DisplayName: "Element Name",
|
||||
Name: "element_name",
|
||||
Type: "text",
|
||||
Placeholder: "Enter a value",
|
||||
},
|
||||
},
|
||||
SubmitLabel: "Submit",
|
||||
NotifyOnCancel: false,
|
||||
State: "somestate",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("should pass validation", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
err := request.IsValid()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("should fail on empty url", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.URL = ""
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "empty URL")
|
||||
})
|
||||
|
||||
t.Run("should fail on empty trigger", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.TriggerId = ""
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "empty trigger id")
|
||||
})
|
||||
|
||||
t.Run("should fail on empty dialog title", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.Title = ""
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "invalid dialog title")
|
||||
})
|
||||
|
||||
t.Run("should fail on wrong subtype and long dialog title", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.Elements[0].SubType = "wrong SubType"
|
||||
request.Dialog.Title = "Very very long Dialog Name"
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "invalid subtype")
|
||||
assert.ErrorContains(t, err, "invalid dialog title")
|
||||
t.Cleanup(func() {
|
||||
request.Dialog.Elements[0].SubType = ""
|
||||
request.Dialog.Title = "Some Title"
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("should fail on wrong dialog icon url", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.IconURL = "wrong url"
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "invalid icon url")
|
||||
})
|
||||
|
||||
t.Run("should pass on empty dialog icon url", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.IconURL = ""
|
||||
err := request.IsValid()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("should fail on wrong minimal and maximal element length", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.Elements[0].MinLength = 10
|
||||
request.Dialog.Elements[0].MaxLength = 9
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "field is not valid")
|
||||
assert.ErrorContains(t, err, "min length should be less then max length")
|
||||
})
|
||||
|
||||
t.Run("should fail on wrong element type", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.Elements[0].Type = "wrong type"
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "invalid element type")
|
||||
})
|
||||
|
||||
t.Run("should fail on duplicate element name", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.Elements = append(request.Dialog.Elements, DialogElement{
|
||||
DisplayName: "Radio element name",
|
||||
Name: "element_name",
|
||||
Type: "radio",
|
||||
})
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "duplicate dialog element")
|
||||
})
|
||||
|
||||
t.Run("should fail on wrong bool default value", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.Elements = append(request.Dialog.Elements, DialogElement{
|
||||
DisplayName: "Bool element name",
|
||||
Name: "bool_element_name",
|
||||
Type: "bool",
|
||||
Default: "wrong default",
|
||||
})
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "invalid default of bool")
|
||||
})
|
||||
|
||||
t.Run("should pass on bool default value", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.Elements = append(request.Dialog.Elements, DialogElement{
|
||||
DisplayName: "Bool element name",
|
||||
Name: "bool_element_name",
|
||||
Type: "bool",
|
||||
Default: "true",
|
||||
})
|
||||
err := request.IsValid()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("should fail on wrong select datasource value", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.Elements = append(request.Dialog.Elements, DialogElement{
|
||||
DisplayName: "Select element name",
|
||||
Name: "select_element_name",
|
||||
Type: "select",
|
||||
DataSource: "wrong DataSource",
|
||||
})
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "invalid data source")
|
||||
})
|
||||
|
||||
t.Run("should fail on wrong radio default value", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.Elements = append(request.Dialog.Elements, DialogElement{
|
||||
DisplayName: "Radio element name",
|
||||
Name: "radio_element_name",
|
||||
Type: "radio",
|
||||
Default: "default",
|
||||
Options: []*PostActionOptions{
|
||||
{
|
||||
Text: "Text 1",
|
||||
Value: "value 1",
|
||||
},
|
||||
},
|
||||
})
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "default value \"default\" doesn't exist in options")
|
||||
})
|
||||
t.Run("should pass radio default value", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.Elements = append(request.Dialog.Elements, DialogElement{
|
||||
DisplayName: "Radio element name",
|
||||
Name: "radio_element_name",
|
||||
Type: "radio",
|
||||
Default: "default",
|
||||
Options: []*PostActionOptions{
|
||||
{
|
||||
Text: "Text 1",
|
||||
Value: "value 1",
|
||||
},
|
||||
{
|
||||
Text: "Text 2",
|
||||
Value: "default",
|
||||
},
|
||||
},
|
||||
})
|
||||
err := request.IsValid()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("should fail on too long text placeholder", func(t *testing.T) {
|
||||
request := getBaseOpenDialogRequest()
|
||||
request.Dialog.Elements[0].Placeholder = strings.Repeat("x", 151)
|
||||
err := request.IsValid()
|
||||
assert.ErrorContains(t, err, "Placeholder cannot be longer than 150 characters")
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user