2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2021-12-04 16:27:24 +00:00
|
|
|
"github.com/lithammer/shortuuid/v3"
|
|
|
|
"github.com/rs/xid"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
|
|
|
"github.com/sftpgo/sdk/plugin/notifier"
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/plugin"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2020-07-24 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewActionNotification(t *testing.T) {
|
2022-12-27 17:51:53 +00:00
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "username",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.LocalFilesystemProvider
|
2020-07-24 21:39:38 +00:00
|
|
|
user.FsConfig.S3Config = vfs.S3FsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
BaseS3FsConfig: sdk.BaseS3FsConfig{
|
2021-07-11 13:26:51 +00:00
|
|
|
Bucket: "s3bucket",
|
|
|
|
Endpoint: "endpoint",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
user.FsConfig.GCSConfig = vfs.GCSFsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
BaseGCSFsConfig: sdk.BaseGCSFsConfig{
|
2021-07-11 13:26:51 +00:00
|
|
|
Bucket: "gcsbucket",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
user.FsConfig.AzBlobConfig = vfs.AzBlobFsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
BaseAzBlobFsConfig: sdk.BaseAzBlobFsConfig{
|
2021-07-11 13:26:51 +00:00
|
|
|
Container: "azcontainer",
|
|
|
|
Endpoint: "azendpoint",
|
|
|
|
},
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2021-05-26 05:48:37 +00:00
|
|
|
user.FsConfig.SFTPConfig = vfs.SFTPFsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
BaseSFTPFsConfig: sdk.BaseSFTPFsConfig{
|
2021-07-11 13:26:51 +00:00
|
|
|
Endpoint: "sftpendpoint",
|
|
|
|
},
|
2021-05-26 05:48:37 +00:00
|
|
|
}
|
2022-06-11 08:41:34 +00:00
|
|
|
user.FsConfig.HTTPConfig = vfs.HTTPFsConfig{
|
|
|
|
BaseHTTPFsConfig: sdk.BaseHTTPFsConfig{
|
|
|
|
Endpoint: "httpendpoint",
|
|
|
|
},
|
|
|
|
}
|
2022-12-27 17:51:53 +00:00
|
|
|
c := NewBaseConnection("id", ProtocolSSH, "", "", user)
|
2021-12-04 16:27:24 +00:00
|
|
|
sessionID := xid.New().String()
|
2022-12-27 17:51:53 +00:00
|
|
|
a := newActionNotification(&user, operationDownload, "path", "vpath", "target", "", "", ProtocolSFTP, "", sessionID,
|
2023-08-12 16:51:47 +00:00
|
|
|
123, 0, c.getNotificationStatus(errors.New("fake error")), 0, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Equal(t, user.Username, a.Username)
|
|
|
|
assert.Equal(t, 0, len(a.Bucket))
|
|
|
|
assert.Equal(t, 0, len(a.Endpoint))
|
2021-10-20 17:39:49 +00:00
|
|
|
assert.Equal(t, 2, a.Status)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.S3FilesystemProvider
|
2022-12-27 17:51:53 +00:00
|
|
|
a = newActionNotification(&user, operationDownload, "path", "vpath", "target", "", "", ProtocolSSH, "", sessionID,
|
2023-08-12 16:51:47 +00:00
|
|
|
123, 0, c.getNotificationStatus(nil), 0, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Equal(t, "s3bucket", a.Bucket)
|
|
|
|
assert.Equal(t, "endpoint", a.Endpoint)
|
|
|
|
assert.Equal(t, 1, a.Status)
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.GCSFilesystemProvider
|
2022-12-27 17:51:53 +00:00
|
|
|
a = newActionNotification(&user, operationDownload, "path", "vpath", "target", "", "", ProtocolSCP, "", sessionID,
|
2023-08-12 16:51:47 +00:00
|
|
|
123, 0, c.getNotificationStatus(ErrQuotaExceeded), 0, nil)
|
2022-12-27 17:51:53 +00:00
|
|
|
assert.Equal(t, "gcsbucket", a.Bucket)
|
|
|
|
assert.Equal(t, 0, len(a.Endpoint))
|
|
|
|
assert.Equal(t, 3, a.Status)
|
|
|
|
a = newActionNotification(&user, operationDownload, "path", "vpath", "target", "", "", ProtocolSCP, "", sessionID,
|
2023-08-12 16:51:47 +00:00
|
|
|
123, 0, c.getNotificationStatus(fmt.Errorf("wrapper quota error: %w", ErrQuotaExceeded)), 0, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Equal(t, "gcsbucket", a.Bucket)
|
|
|
|
assert.Equal(t, 0, len(a.Endpoint))
|
2021-10-20 17:39:49 +00:00
|
|
|
assert.Equal(t, 3, a.Status)
|
2020-10-25 07:18:48 +00:00
|
|
|
|
2022-06-11 08:41:34 +00:00
|
|
|
user.FsConfig.Provider = sdk.HTTPFilesystemProvider
|
2022-12-27 17:51:53 +00:00
|
|
|
a = newActionNotification(&user, operationDownload, "path", "vpath", "target", "", "", ProtocolSSH, "", sessionID,
|
2023-08-12 16:51:47 +00:00
|
|
|
123, 0, c.getNotificationStatus(nil), 0, nil)
|
2022-06-11 08:41:34 +00:00
|
|
|
assert.Equal(t, "httpendpoint", a.Endpoint)
|
|
|
|
assert.Equal(t, 1, a.Status)
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.AzureBlobFilesystemProvider
|
2022-12-27 17:51:53 +00:00
|
|
|
a = newActionNotification(&user, operationDownload, "path", "vpath", "target", "", "", ProtocolSCP, "", sessionID,
|
2023-08-12 16:51:47 +00:00
|
|
|
123, 0, c.getNotificationStatus(nil), 0, nil)
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.Equal(t, "azcontainer", a.Bucket)
|
2021-06-11 20:27:36 +00:00
|
|
|
assert.Equal(t, "azendpoint", a.Endpoint)
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.Equal(t, 1, a.Status)
|
|
|
|
|
2022-12-27 17:51:53 +00:00
|
|
|
a = newActionNotification(&user, operationDownload, "path", "vpath", "target", "", "", ProtocolSCP, "", sessionID,
|
2023-08-12 16:51:47 +00:00
|
|
|
123, os.O_APPEND, c.getNotificationStatus(nil), 0, nil)
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.Equal(t, "azcontainer", a.Bucket)
|
|
|
|
assert.Equal(t, "azendpoint", a.Endpoint)
|
|
|
|
assert.Equal(t, 1, a.Status)
|
2021-05-31 20:33:23 +00:00
|
|
|
assert.Equal(t, os.O_APPEND, a.OpenFlags)
|
2021-05-26 05:48:37 +00:00
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.SFTPFilesystemProvider
|
2022-12-27 17:51:53 +00:00
|
|
|
a = newActionNotification(&user, operationDownload, "path", "vpath", "target", "", "", ProtocolSFTP, "", sessionID,
|
2023-08-12 16:51:47 +00:00
|
|
|
123, 0, c.getNotificationStatus(nil), 0, nil)
|
2021-05-26 05:48:37 +00:00
|
|
|
assert.Equal(t, "sftpendpoint", a.Endpoint)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestActionHTTP(t *testing.T) {
|
|
|
|
actionsCopy := Config.Actions
|
|
|
|
|
|
|
|
Config.Actions = ProtocolActions{
|
|
|
|
ExecuteOn: []string{operationDownload},
|
|
|
|
Hook: fmt.Sprintf("http://%v", httpAddr),
|
|
|
|
}
|
|
|
|
user := &dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "username",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-12-04 16:27:24 +00:00
|
|
|
a := newActionNotification(user, operationDownload, "path", "vpath", "target", "", "", ProtocolSFTP, "",
|
2023-08-12 16:51:47 +00:00
|
|
|
xid.New().String(), 123, 0, 1, 0, nil)
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err := actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
2023-01-02 14:59:00 +00:00
|
|
|
assert.Equal(t, 1, status)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
Config.Actions.Hook = "http://invalid:1234"
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err = actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Error(t, err)
|
2023-01-02 14:59:00 +00:00
|
|
|
assert.Equal(t, 1, status)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
Config.Actions.Hook = fmt.Sprintf("http://%v/404", httpAddr)
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err = actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.EqualError(t, err, errUnexpectedHTTResponse.Error())
|
|
|
|
}
|
2023-01-02 14:59:00 +00:00
|
|
|
assert.Equal(t, 1, status)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
Config.Actions = actionsCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActionCMD(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
actionsCopy := Config.Actions
|
|
|
|
|
|
|
|
hookCmd, err := exec.LookPath("true")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
Config.Actions = ProtocolActions{
|
|
|
|
ExecuteOn: []string{operationDownload},
|
|
|
|
Hook: hookCmd,
|
|
|
|
}
|
|
|
|
user := &dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "username",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-12-04 16:27:24 +00:00
|
|
|
sessionID := shortuuid.New()
|
|
|
|
a := newActionNotification(user, operationDownload, "path", "vpath", "target", "", "", ProtocolSFTP, "", sessionID,
|
2023-08-12 16:51:47 +00:00
|
|
|
123, 0, 1, 0, map[string]string{"key": "value"})
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err := actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
2023-01-02 14:59:00 +00:00
|
|
|
assert.Equal(t, 1, status)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-12-04 16:27:24 +00:00
|
|
|
c := NewBaseConnection("id", ProtocolSFTP, "", "", *user)
|
2023-08-12 16:51:47 +00:00
|
|
|
err = ExecuteActionNotification(c, OperationSSHCmd, "path", "vpath", "target", "vtarget", "sha1sum", 0, nil, 0, nil)
|
2022-06-16 16:42:17 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2023-08-12 16:51:47 +00:00
|
|
|
err = ExecuteActionNotification(c, operationDownload, "path", "vpath", "", "", "", 0, nil, 0, nil)
|
2022-06-16 16:42:17 +00:00
|
|
|
assert.NoError(t, err)
|
2022-01-02 14:16:35 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
Config.Actions = actionsCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrongActions(t *testing.T) {
|
|
|
|
actionsCopy := Config.Actions
|
|
|
|
|
|
|
|
badCommand := "/bad/command"
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
badCommand = "C:\\bad\\command"
|
|
|
|
}
|
|
|
|
Config.Actions = ProtocolActions{
|
|
|
|
ExecuteOn: []string{operationUpload},
|
|
|
|
Hook: badCommand,
|
|
|
|
}
|
|
|
|
user := &dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "username",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-12-04 16:27:24 +00:00
|
|
|
a := newActionNotification(user, operationUpload, "", "", "", "", "", ProtocolSFTP, "", xid.New().String(),
|
2023-08-12 16:51:47 +00:00
|
|
|
123, 0, 1, 0, nil)
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err := actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Error(t, err, "action with bad command must fail")
|
2023-01-02 14:59:00 +00:00
|
|
|
assert.Equal(t, 1, status)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
a.Action = operationDelete
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err = actionHandler.Handle(a)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, status)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
Config.Actions.Hook = "http://foo\x7f.com/"
|
|
|
|
a.Action = operationUpload
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err = actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Error(t, err, "action with bad url must fail")
|
2023-01-02 14:59:00 +00:00
|
|
|
assert.Equal(t, 1, status)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
Config.Actions.Hook = ""
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err = actionHandler.Handle(a)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, status)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
Config.Actions.Hook = "relative path"
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err = actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
if assert.Error(t, err) {
|
2023-01-02 14:59:00 +00:00
|
|
|
assert.EqualError(t, err, fmt.Sprintf("invalid notification command %q", Config.Actions.Hook))
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2023-01-02 14:59:00 +00:00
|
|
|
assert.Equal(t, 1, status)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
Config.Actions = actionsCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreDeleteAction(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
actionsCopy := Config.Actions
|
|
|
|
|
|
|
|
hookCmd, err := exec.LookPath("true")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
Config.Actions = ProtocolActions{
|
|
|
|
ExecuteOn: []string{operationPreDelete},
|
2023-01-02 14:59:00 +00:00
|
|
|
Hook: "missing hook",
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
homeDir := filepath.Join(os.TempDir(), "test_user")
|
|
|
|
err = os.MkdirAll(homeDir, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "username",
|
|
|
|
HomeDir: homeDir,
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
2023-05-16 16:08:14 +00:00
|
|
|
fs := vfs.NewOsFs("id", homeDir, "", nil)
|
2021-07-24 18:11:17 +00:00
|
|
|
c := NewBaseConnection("id", ProtocolSFTP, "", "", user)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
testfile := filepath.Join(user.HomeDir, "testfile")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testfile, []byte("test"), os.ModePerm)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
info, err := os.Stat(testfile)
|
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
err = c.RemoveFile(fs, testfile, "testfile", info)
|
2023-01-02 14:59:00 +00:00
|
|
|
assert.ErrorIs(t, err, c.GetPermissionDeniedError())
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.FileExists(t, testfile)
|
2023-01-02 14:59:00 +00:00
|
|
|
Config.Actions.Hook = hookCmd
|
|
|
|
err = c.RemoveFile(fs, testfile, "testfile", info)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoFileExists(t, testfile)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
os.RemoveAll(homeDir)
|
|
|
|
|
|
|
|
Config.Actions = actionsCopy
|
|
|
|
}
|
2020-10-20 20:51:58 +00:00
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func TestUnconfiguredHook(t *testing.T) {
|
|
|
|
actionsCopy := Config.Actions
|
|
|
|
|
|
|
|
Config.Actions = ProtocolActions{
|
|
|
|
ExecuteOn: []string{operationDownload},
|
|
|
|
Hook: "",
|
|
|
|
}
|
|
|
|
pluginsConfig := []plugin.Config{
|
|
|
|
{
|
|
|
|
Type: "notifier",
|
|
|
|
},
|
|
|
|
}
|
2022-07-13 08:40:24 +00:00
|
|
|
err := plugin.Initialize(pluginsConfig, "debug")
|
2022-01-02 14:16:35 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, plugin.Handler.HasNotifiers())
|
|
|
|
|
|
|
|
c := NewBaseConnection("id", ProtocolSFTP, "", "", dataprovider.User{})
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err := ExecutePreAction(c, OperationPreDownload, "", "", 0, 0)
|
2022-01-02 14:16:35 +00:00
|
|
|
assert.NoError(t, err)
|
2023-01-02 14:59:00 +00:00
|
|
|
assert.Equal(t, status, 0)
|
|
|
|
status, err = ExecutePreAction(c, operationPreDelete, "", "", 0, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, status, 0)
|
2022-01-02 14:16:35 +00:00
|
|
|
|
2023-08-12 16:51:47 +00:00
|
|
|
err = ExecuteActionNotification(c, operationDownload, "", "", "", "", "", 0, nil, 0, nil)
|
2022-06-16 16:42:17 +00:00
|
|
|
assert.NoError(t, err)
|
2022-01-02 14:16:35 +00:00
|
|
|
|
2022-07-13 08:40:24 +00:00
|
|
|
err = plugin.Initialize(nil, "debug")
|
2022-01-02 14:16:35 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, plugin.Handler.HasNotifiers())
|
|
|
|
|
|
|
|
Config.Actions = actionsCopy
|
|
|
|
}
|
|
|
|
|
2020-10-20 20:51:58 +00:00
|
|
|
type actionHandlerStub struct {
|
|
|
|
called bool
|
|
|
|
}
|
|
|
|
|
2023-03-22 18:02:54 +00:00
|
|
|
func (h *actionHandlerStub) Handle(_ *notifier.FsEvent) (int, error) {
|
2020-10-20 20:51:58 +00:00
|
|
|
h.called = true
|
|
|
|
|
2023-01-02 14:59:00 +00:00
|
|
|
return 1, nil
|
2020-10-20 20:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitializeActionHandler(t *testing.T) {
|
|
|
|
handler := &actionHandlerStub{}
|
|
|
|
|
|
|
|
InitializeActionHandler(handler)
|
|
|
|
t.Cleanup(func() {
|
2021-02-12 20:42:49 +00:00
|
|
|
InitializeActionHandler(&defaultActionHandler{})
|
2020-10-20 20:51:58 +00:00
|
|
|
})
|
|
|
|
|
2023-01-02 14:59:00 +00:00
|
|
|
status, err := actionHandler.Handle(¬ifier.FsEvent{})
|
2020-10-20 20:51:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, handler.called)
|
2023-01-02 14:59:00 +00:00
|
|
|
assert.Equal(t, 1, status)
|
2020-10-20 20:51:58 +00:00
|
|
|
}
|