2021-12-22 16:17:50 +03:00
|
|
|
package client_test
|
2021-12-22 15:46:17 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-17 04:54:58 +03:00
|
|
|
"heckel.io/ntfy/v2/client"
|
2021-12-22 15:46:17 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfig_Load(t *testing.T) {
|
|
|
|
filename := filepath.Join(t.TempDir(), "client.yml")
|
|
|
|
require.Nil(t, os.WriteFile(filename, []byte(`
|
|
|
|
default-host: http://localhost
|
2022-10-09 16:50:37 +03:00
|
|
|
default-user: philipp
|
2022-07-31 22:12:15 +03:00
|
|
|
default-password: mypass
|
|
|
|
default-command: 'echo "Got the message: $message"'
|
2021-12-22 15:46:17 +03:00
|
|
|
subscribe:
|
2022-02-17 21:16:01 +03:00
|
|
|
- topic: no-command-with-auth
|
|
|
|
user: phil
|
|
|
|
password: mypass
|
2021-12-22 15:46:17 +03:00
|
|
|
- topic: echo-this
|
|
|
|
command: 'echo "Message received: $message"'
|
|
|
|
- topic: alerts
|
|
|
|
command: notify-send -i /usr/share/ntfy/logo.png "Important" "$m"
|
|
|
|
if:
|
|
|
|
priority: high,urgent
|
2022-07-31 22:12:15 +03:00
|
|
|
- topic: defaults
|
2021-12-22 15:46:17 +03:00
|
|
|
`), 0600))
|
|
|
|
|
2021-12-22 16:17:50 +03:00
|
|
|
conf, err := client.LoadConfig(filename)
|
2021-12-22 15:46:17 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "http://localhost", conf.DefaultHost)
|
2022-10-09 16:50:37 +03:00
|
|
|
require.Equal(t, "philipp", conf.DefaultUser)
|
|
|
|
require.Equal(t, "mypass", *conf.DefaultPassword)
|
2022-07-31 22:12:15 +03:00
|
|
|
require.Equal(t, `echo "Got the message: $message"`, conf.DefaultCommand)
|
|
|
|
require.Equal(t, 4, len(conf.Subscribe))
|
2022-02-17 21:16:01 +03:00
|
|
|
require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic)
|
2021-12-22 15:46:17 +03:00
|
|
|
require.Equal(t, "", conf.Subscribe[0].Command)
|
2023-08-07 07:44:35 +03:00
|
|
|
require.Equal(t, "phil", *conf.Subscribe[0].User)
|
2022-10-09 16:50:37 +03:00
|
|
|
require.Equal(t, "mypass", *conf.Subscribe[0].Password)
|
2021-12-22 15:46:17 +03:00
|
|
|
require.Equal(t, "echo-this", conf.Subscribe[1].Topic)
|
|
|
|
require.Equal(t, `echo "Message received: $message"`, conf.Subscribe[1].Command)
|
|
|
|
require.Equal(t, "alerts", conf.Subscribe[2].Topic)
|
|
|
|
require.Equal(t, `notify-send -i /usr/share/ntfy/logo.png "Important" "$m"`, conf.Subscribe[2].Command)
|
|
|
|
require.Equal(t, "high,urgent", conf.Subscribe[2].If["priority"])
|
2022-07-31 22:12:15 +03:00
|
|
|
require.Equal(t, "defaults", conf.Subscribe[3].Topic)
|
2021-12-22 15:46:17 +03:00
|
|
|
}
|
2022-10-09 16:50:37 +03:00
|
|
|
|
|
|
|
func TestConfig_EmptyPassword(t *testing.T) {
|
|
|
|
filename := filepath.Join(t.TempDir(), "client.yml")
|
|
|
|
require.Nil(t, os.WriteFile(filename, []byte(`
|
|
|
|
default-host: http://localhost
|
|
|
|
default-user: philipp
|
|
|
|
default-password: ""
|
|
|
|
subscribe:
|
|
|
|
- topic: no-command-with-auth
|
|
|
|
user: phil
|
|
|
|
password: ""
|
|
|
|
`), 0600))
|
|
|
|
|
|
|
|
conf, err := client.LoadConfig(filename)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "http://localhost", conf.DefaultHost)
|
|
|
|
require.Equal(t, "philipp", conf.DefaultUser)
|
|
|
|
require.Equal(t, "", *conf.DefaultPassword)
|
|
|
|
require.Equal(t, 1, len(conf.Subscribe))
|
|
|
|
require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic)
|
|
|
|
require.Equal(t, "", conf.Subscribe[0].Command)
|
2023-08-07 07:44:35 +03:00
|
|
|
require.Equal(t, "phil", *conf.Subscribe[0].User)
|
2022-10-09 16:50:37 +03:00
|
|
|
require.Equal(t, "", *conf.Subscribe[0].Password)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_NullPassword(t *testing.T) {
|
|
|
|
filename := filepath.Join(t.TempDir(), "client.yml")
|
|
|
|
require.Nil(t, os.WriteFile(filename, []byte(`
|
|
|
|
default-host: http://localhost
|
|
|
|
default-user: philipp
|
|
|
|
default-password: ~
|
|
|
|
subscribe:
|
|
|
|
- topic: no-command-with-auth
|
|
|
|
user: phil
|
|
|
|
password: ~
|
|
|
|
`), 0600))
|
|
|
|
|
|
|
|
conf, err := client.LoadConfig(filename)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "http://localhost", conf.DefaultHost)
|
|
|
|
require.Equal(t, "philipp", conf.DefaultUser)
|
|
|
|
require.Nil(t, conf.DefaultPassword)
|
|
|
|
require.Equal(t, 1, len(conf.Subscribe))
|
|
|
|
require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic)
|
|
|
|
require.Equal(t, "", conf.Subscribe[0].Command)
|
2023-08-07 07:44:35 +03:00
|
|
|
require.Equal(t, "phil", *conf.Subscribe[0].User)
|
2022-10-09 16:50:37 +03:00
|
|
|
require.Nil(t, conf.Subscribe[0].Password)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_NoPassword(t *testing.T) {
|
|
|
|
filename := filepath.Join(t.TempDir(), "client.yml")
|
|
|
|
require.Nil(t, os.WriteFile(filename, []byte(`
|
|
|
|
default-host: http://localhost
|
|
|
|
default-user: philipp
|
|
|
|
subscribe:
|
|
|
|
- topic: no-command-with-auth
|
|
|
|
user: phil
|
|
|
|
`), 0600))
|
|
|
|
|
|
|
|
conf, err := client.LoadConfig(filename)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "http://localhost", conf.DefaultHost)
|
|
|
|
require.Equal(t, "philipp", conf.DefaultUser)
|
|
|
|
require.Nil(t, conf.DefaultPassword)
|
|
|
|
require.Equal(t, 1, len(conf.Subscribe))
|
|
|
|
require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic)
|
|
|
|
require.Equal(t, "", conf.Subscribe[0].Command)
|
2023-08-07 07:44:35 +03:00
|
|
|
require.Equal(t, "phil", *conf.Subscribe[0].User)
|
2022-10-09 16:50:37 +03:00
|
|
|
require.Nil(t, conf.Subscribe[0].Password)
|
|
|
|
}
|
2023-03-06 08:57:51 +03:00
|
|
|
|
|
|
|
func TestConfig_DefaultToken(t *testing.T) {
|
|
|
|
filename := filepath.Join(t.TempDir(), "client.yml")
|
|
|
|
require.Nil(t, os.WriteFile(filename, []byte(`
|
|
|
|
default-host: http://localhost
|
|
|
|
default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2
|
|
|
|
subscribe:
|
|
|
|
- topic: mytopic
|
|
|
|
`), 0600))
|
|
|
|
|
|
|
|
conf, err := client.LoadConfig(filename)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "http://localhost", conf.DefaultHost)
|
|
|
|
require.Equal(t, "", conf.DefaultUser)
|
|
|
|
require.Nil(t, conf.DefaultPassword)
|
|
|
|
require.Equal(t, "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", conf.DefaultToken)
|
|
|
|
require.Equal(t, 1, len(conf.Subscribe))
|
|
|
|
require.Equal(t, "mytopic", conf.Subscribe[0].Topic)
|
2023-08-07 07:44:35 +03:00
|
|
|
require.Nil(t, conf.Subscribe[0].User)
|
2023-03-06 08:57:51 +03:00
|
|
|
require.Nil(t, conf.Subscribe[0].Password)
|
2023-08-07 07:44:35 +03:00
|
|
|
require.Nil(t, conf.Subscribe[0].Token)
|
2023-03-06 08:57:51 +03:00
|
|
|
}
|