mirror of
https://github.com/kopia/kopia.git
synced 2026-05-18 11:44:36 -04:00
* feat(cli): support for defining notification profiles via CLI
Profile management:
```
$ kopia notification profile configure email \
--profile-name=X \
--smtp-server=smtp.gmail.com \
--smtp-port=587 \
--smtp-username=X \
--smtp-password=X \
--mail-from=X \
--mail-to=X \
--format=html|txt \
[--send-test-notification]
$ kopia notification profile configure pushover --profile-name=X \
--user-key=X \
--app-token=X \
--format=html|txt \
[--send-test-notification]
$ kopia notification profile configure webhook --profile-name=X \
--endpooint=http://some-address:port/path \
--method=POST|PUT \
--format=html|txt \
[--send-test-notification]
$ kopia notification profile test --profile-name=X
$ kopia notification profile delete --profile-name=X
$ kopia notification profile list
```
Template management:
```
$ kopia notification template show X
$ kopia notification template set X \
--from-stdin | --from-file=X | --editor
$ kopia notification template remove X
$ kopia notification template list
```
Implements #1958
* additional refactoring for testability, various naming tweaks
117 lines
3.0 KiB
Go
117 lines
3.0 KiB
Go
package sender_test
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/kopia/kopia/internal/testlogging"
|
|
"github.com/kopia/kopia/notification/sender"
|
|
)
|
|
|
|
func TestParseMessage(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
input string
|
|
expected *sender.Message
|
|
}{
|
|
{
|
|
name: "ValidMessage",
|
|
input: `Subject: Test Subject
|
|
Header1: Value1
|
|
InvalidHeaderLine will be dropped
|
|
Header2: Value2
|
|
|
|
This is the body of the message.`,
|
|
expected: &sender.Message{
|
|
Subject: "Test Subject",
|
|
Headers: map[string]string{
|
|
"Header1": "Value1",
|
|
"Header2": "Value2",
|
|
},
|
|
Body: "This is the body of the message.",
|
|
},
|
|
},
|
|
{
|
|
name: "ValidMessage",
|
|
input: `Subject: Test Subject
|
|
Header1: Value1
|
|
InvalidHeaderLine will be dropped
|
|
Header2: Value2
|
|
|
|
This is the body of the message.`,
|
|
expected: &sender.Message{
|
|
Subject: "Test Subject",
|
|
Headers: map[string]string{
|
|
"Header1": "Value1",
|
|
"Header2": "Value2",
|
|
},
|
|
Body: "This is the body of the message.",
|
|
},
|
|
}, // Add more test cases here...
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
reader := strings.NewReader(tc.input)
|
|
ctx := testlogging.Context(t)
|
|
actual, err := sender.ParseMessage(ctx, reader)
|
|
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.expected.Subject, actual.Subject, "ParseMessage() Subject mismatch")
|
|
require.Equal(t, tc.expected.Body, actual.Body, "ParseMessage() Body mismatch")
|
|
require.Equal(t, tc.expected.Headers, actual.Headers, "ParseMessage() Headers mismatch")
|
|
|
|
actualString := actual.ToString()
|
|
roundTrip, err := sender.ParseMessage(ctx, strings.NewReader(actualString))
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expected, roundTrip, "ToString() did not roundtrip")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseMessageNoBody(t *testing.T) {
|
|
reader := strings.NewReader(`Subject: Test Subject`)
|
|
ctx := testlogging.Context(t)
|
|
_, err := sender.ParseMessage(ctx, reader)
|
|
require.ErrorContains(t, err, "no body found in message")
|
|
}
|
|
|
|
func TestToString(t *testing.T) {
|
|
msg := &sender.Message{
|
|
Subject: "Test Subject",
|
|
Headers: map[string]string{
|
|
"Header1": "Value1",
|
|
"Header2": "Value2",
|
|
},
|
|
Body: "This is the body of the message.",
|
|
}
|
|
|
|
expected := "Subject: Test Subject\nHeader1: Value1\nHeader2: Value2\n\nThis is the body of the message."
|
|
actual := msg.ToString()
|
|
|
|
if actual != expected {
|
|
t.Errorf("ToString() = %v, want %v", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestValidateMessageFormatAndSetDefault(t *testing.T) {
|
|
var f string
|
|
|
|
require.NoError(t, sender.ValidateMessageFormatAndSetDefault(&f, "html"))
|
|
require.Equal(t, "html", f)
|
|
|
|
f = "txt"
|
|
require.NoError(t, sender.ValidateMessageFormatAndSetDefault(&f, "html"))
|
|
require.Equal(t, "txt", f)
|
|
|
|
f = "html"
|
|
require.NoError(t, sender.ValidateMessageFormatAndSetDefault(&f, "html"))
|
|
require.Equal(t, "html", f)
|
|
|
|
f = "bad"
|
|
require.ErrorContains(t, sender.ValidateMessageFormatAndSetDefault(&f, "html"), "invalid format: bad")
|
|
}
|