Add default probe prompts and SSRF validation for channel monitor targets, with focused tests for blocked internal addresses.
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package service
|
|
|
|
import (
|
|
"math/rand"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDefaultChannelMonitorRequestTemplatesHasTwentyShortEnglishPrompts(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
require.Len(t, DefaultChannelMonitorRequestTemplates, 20)
|
|
for _, template := range DefaultChannelMonitorRequestTemplates {
|
|
require.NotEmpty(t, template)
|
|
require.LessOrEqual(t, len(template), 120)
|
|
}
|
|
}
|
|
|
|
func TestParseChannelMonitorRequestTemplatesUsesCustomTemplates(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
templates := ParseChannelMonitorRequestTemplates(`["Say hello.","Solve 2+2."]`)
|
|
|
|
require.Equal(t, []string{"Say hello.", "Solve 2+2."}, templates)
|
|
}
|
|
|
|
func TestParseChannelMonitorRequestTemplatesFallsBackForEmptyInvalidOrBlankTemplates(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
raw string
|
|
}{
|
|
{name: "empty", raw: ""},
|
|
{name: "blank", raw: " "},
|
|
{name: "invalid json", raw: "{bad json"},
|
|
{name: "wrong json type", raw: `{"prompt":"hello"}`},
|
|
{name: "only empty strings", raw: `["", " "]`},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
templates := ParseChannelMonitorRequestTemplates(tc.raw)
|
|
|
|
require.Equal(t, DefaultChannelMonitorRequestTemplates, templates)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSelectChannelMonitorRequestTemplateUsesDeterministicRandWhenProvided(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
raw := `["first","second","third"]`
|
|
first := SelectChannelMonitorRequestTemplate(raw, rand.New(rand.NewSource(42)))
|
|
second := SelectChannelMonitorRequestTemplate(raw, rand.New(rand.NewSource(42)))
|
|
|
|
require.Equal(t, first, second)
|
|
require.Contains(t, []string{"first", "second", "third"}, first)
|
|
}
|