mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 10:20:29 -06:00
d5b9602a79
* Can add allowed custom headers to an email Message. WIP. * adds slug as a custom email header to all outgoing emails * Headers are static - declared as key/value pairs in config. All static headers get added to emails. * updates comment * adds tests for parsing smtp static headers * updates test to assert static headers are included when building email * updates test to use multiple static headers * updates test names * fixes linting issue with error * ignore gocyclo for loading config * updates email headers in tests to be formatted properly * add static headers first * updates tests to assert that regular headers like From cant be overwritten * ensures only the header is in a valid format for smtp and not the value * updates comment and error message wording * adds to docs and ini sample files * updates smtp.static_headers docs examples formatting * removes lines commented with semi colons * prettier:write * renames var
94 lines
2.1 KiB
Go
94 lines
2.1 KiB
Go
package setting
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gopkg.in/ini.v1"
|
|
)
|
|
|
|
func TestLoadSmtpStaticHeaders(t *testing.T) {
|
|
t.Run("will load valid headers", func(t *testing.T) {
|
|
f := ini.Empty()
|
|
cfg := NewCfg()
|
|
s, err := f.NewSection("smtp.static_headers")
|
|
require.NoError(t, err)
|
|
cfg.Raw = f
|
|
_, err = s.NewKey("Foo-Header", "foo_val")
|
|
require.NoError(t, err)
|
|
_, err = s.NewKey("Bar", "bar_val")
|
|
require.NoError(t, err)
|
|
|
|
err = cfg.readGrafanaSmtpStaticHeaders()
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "foo_val", cfg.Smtp.StaticHeaders["Foo-Header"])
|
|
assert.Equal(t, "bar_val", cfg.Smtp.StaticHeaders["Bar"])
|
|
})
|
|
|
|
t.Run("will load no static headers into smtp config when section is defined but has no keys", func(t *testing.T) {
|
|
f := ini.Empty()
|
|
cfg := NewCfg()
|
|
_, err := f.NewSection("smtp.static_headers")
|
|
require.NoError(t, err)
|
|
cfg.Raw = f
|
|
|
|
err = cfg.readGrafanaSmtpStaticHeaders()
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, cfg.Smtp.StaticHeaders)
|
|
})
|
|
|
|
t.Run("will load no static headers into smtp config when section is not defined", func(t *testing.T) {
|
|
f := ini.Empty()
|
|
cfg := NewCfg()
|
|
cfg.Raw = f
|
|
|
|
err := cfg.readGrafanaSmtpStaticHeaders()
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, cfg.Smtp.StaticHeaders)
|
|
})
|
|
|
|
t.Run("will return error when header label is not in valid format", func(t *testing.T) {
|
|
f := ini.Empty()
|
|
cfg := NewCfg()
|
|
s, err := f.NewSection("smtp.static_headers")
|
|
require.NoError(t, err)
|
|
_, err = s.NewKey("header with spaces", "value")
|
|
require.NoError(t, err)
|
|
cfg.Raw = f
|
|
|
|
err = cfg.readGrafanaSmtpStaticHeaders()
|
|
require.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestSmtpHeaderValidation(t *testing.T) {
|
|
testCases := []struct {
|
|
input string
|
|
expected bool
|
|
}{
|
|
//valid
|
|
{"Foo", true},
|
|
{"Foo-Bar", true},
|
|
{"Foo123-Bar123", true},
|
|
|
|
//invalid
|
|
{"foo", false},
|
|
{"Foo Bar", false},
|
|
{"123Foo", false},
|
|
{"Foo.Bar", false},
|
|
{"foo-bar", false},
|
|
{"foo-Bar", false},
|
|
{"Foo-bar", false},
|
|
{"-Bar", false},
|
|
{"Foo--", false},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
assert.Equal(t, validHeader(tc.input), tc.expected)
|
|
}
|
|
}
|