Files
kopia/cli/command_notification_profile_test.go
Jarek Kowalski c0bd372d29 feat(cli): support for defining notification profiles and templates via CLI (#4034)
* 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
2024-10-06 16:28:39 +00:00

61 lines
2.2 KiB
Go

package cli_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/kopia/kopia/internal/testutil"
"github.com/kopia/kopia/notification/notifyprofile"
"github.com/kopia/kopia/tests/testenv"
)
func TestNotificationProfile(t *testing.T) {
t.Parallel()
e := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, testenv.NewInProcRunner(t))
defer e.RunAndExpectSuccess(t, "repo", "disconnect")
e.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", e.RepoDir)
var profiles []notifyprofile.Summary
testutil.MustParseJSONLines(t, e.RunAndExpectSuccess(t, "notification", "profile", "list", "--json"), &profiles)
require.Empty(t, profiles)
// setup a profile
e.RunAndExpectSuccess(t, "notification", "profile", "configure", "testsender", "--profile-name=mywebhook")
testutil.MustParseJSONLines(t, e.RunAndExpectSuccess(t, "notification", "profile", "list", "--json"), &profiles)
require.Len(t, profiles, 1)
require.Equal(t, "testsender", profiles[0].Type)
// nothing is sent so far
require.Empty(t, e.NotificationsSent())
// now send a test message
e.RunAndExpectSuccess(t, "notification", "profile", "test", "--profile-name=mywebhook")
// make sure we received the test request
require.Len(t, e.NotificationsSent(), 1)
require.Contains(t, e.NotificationsSent()[0].Body, "If you received this, your notification configuration")
// define another profile
e.RunAndExpectSuccess(t, "notification", "profile", "configure", "testsender", "--profile-name=myotherwebhook", "--min-severity=warning")
lines := e.RunAndExpectSuccess(t, "notification", "profile", "list")
require.Contains(t, lines, "Profile \"mywebhook\" Type \"testsender\" Minimum Severity: report")
require.Contains(t, lines, "Profile \"myotherwebhook\" Type \"testsender\" Minimum Severity: warning")
// delete non-existent profile does not fail
e.RunAndExpectSuccess(t, "notification", "profile", "delete", "--profile-name=unknown")
// delete existing profiles
e.RunAndExpectSuccess(t, "notification", "profile", "delete", "--profile-name=myotherwebhook")
e.RunAndExpectSuccess(t, "notification", "profile", "delete", "--profile-name=mywebhook")
// no profiles left
require.Empty(t, e.RunAndExpectSuccess(t, "notification", "profile", "list"))
}