config_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package client_test
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "heckel.io/ntfy/client"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. )
  9. func TestConfig_Load(t *testing.T) {
  10. filename := filepath.Join(t.TempDir(), "client.yml")
  11. require.Nil(t, os.WriteFile(filename, []byte(`
  12. default-host: http://localhost
  13. default-user: philipp
  14. default-password: mypass
  15. default-command: 'echo "Got the message: $message"'
  16. subscribe:
  17. - topic: no-command-with-auth
  18. user: phil
  19. password: mypass
  20. - topic: echo-this
  21. command: 'echo "Message received: $message"'
  22. - topic: alerts
  23. command: notify-send -i /usr/share/ntfy/logo.png "Important" "$m"
  24. if:
  25. priority: high,urgent
  26. - topic: defaults
  27. `), 0600))
  28. conf, err := client.LoadConfig(filename)
  29. require.Nil(t, err)
  30. require.Equal(t, "http://localhost", conf.DefaultHost)
  31. require.Equal(t, "philipp", conf.DefaultUser)
  32. require.Equal(t, "mypass", *conf.DefaultPassword)
  33. require.Equal(t, `echo "Got the message: $message"`, conf.DefaultCommand)
  34. require.Equal(t, 4, len(conf.Subscribe))
  35. require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic)
  36. require.Equal(t, "", conf.Subscribe[0].Command)
  37. require.Equal(t, "phil", *conf.Subscribe[0].User)
  38. require.Equal(t, "mypass", *conf.Subscribe[0].Password)
  39. require.Equal(t, "echo-this", conf.Subscribe[1].Topic)
  40. require.Equal(t, `echo "Message received: $message"`, conf.Subscribe[1].Command)
  41. require.Equal(t, "alerts", conf.Subscribe[2].Topic)
  42. require.Equal(t, `notify-send -i /usr/share/ntfy/logo.png "Important" "$m"`, conf.Subscribe[2].Command)
  43. require.Equal(t, "high,urgent", conf.Subscribe[2].If["priority"])
  44. require.Equal(t, "defaults", conf.Subscribe[3].Topic)
  45. }
  46. func TestConfig_EmptyPassword(t *testing.T) {
  47. filename := filepath.Join(t.TempDir(), "client.yml")
  48. require.Nil(t, os.WriteFile(filename, []byte(`
  49. default-host: http://localhost
  50. default-user: philipp
  51. default-password: ""
  52. subscribe:
  53. - topic: no-command-with-auth
  54. user: phil
  55. password: ""
  56. `), 0600))
  57. conf, err := client.LoadConfig(filename)
  58. require.Nil(t, err)
  59. require.Equal(t, "http://localhost", conf.DefaultHost)
  60. require.Equal(t, "philipp", conf.DefaultUser)
  61. require.Equal(t, "", *conf.DefaultPassword)
  62. require.Equal(t, 1, len(conf.Subscribe))
  63. require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic)
  64. require.Equal(t, "", conf.Subscribe[0].Command)
  65. require.Equal(t, "phil", *conf.Subscribe[0].User)
  66. require.Equal(t, "", *conf.Subscribe[0].Password)
  67. }
  68. func TestConfig_NullPassword(t *testing.T) {
  69. filename := filepath.Join(t.TempDir(), "client.yml")
  70. require.Nil(t, os.WriteFile(filename, []byte(`
  71. default-host: http://localhost
  72. default-user: philipp
  73. default-password: ~
  74. subscribe:
  75. - topic: no-command-with-auth
  76. user: phil
  77. password: ~
  78. `), 0600))
  79. conf, err := client.LoadConfig(filename)
  80. require.Nil(t, err)
  81. require.Equal(t, "http://localhost", conf.DefaultHost)
  82. require.Equal(t, "philipp", conf.DefaultUser)
  83. require.Nil(t, conf.DefaultPassword)
  84. require.Equal(t, 1, len(conf.Subscribe))
  85. require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic)
  86. require.Equal(t, "", conf.Subscribe[0].Command)
  87. require.Equal(t, "phil", *conf.Subscribe[0].User)
  88. require.Nil(t, conf.Subscribe[0].Password)
  89. }
  90. func TestConfig_NoPassword(t *testing.T) {
  91. filename := filepath.Join(t.TempDir(), "client.yml")
  92. require.Nil(t, os.WriteFile(filename, []byte(`
  93. default-host: http://localhost
  94. default-user: philipp
  95. subscribe:
  96. - topic: no-command-with-auth
  97. user: phil
  98. `), 0600))
  99. conf, err := client.LoadConfig(filename)
  100. require.Nil(t, err)
  101. require.Equal(t, "http://localhost", conf.DefaultHost)
  102. require.Equal(t, "philipp", conf.DefaultUser)
  103. require.Nil(t, conf.DefaultPassword)
  104. require.Equal(t, 1, len(conf.Subscribe))
  105. require.Equal(t, "no-command-with-auth", conf.Subscribe[0].Topic)
  106. require.Equal(t, "", conf.Subscribe[0].Command)
  107. require.Equal(t, "phil", *conf.Subscribe[0].User)
  108. require.Nil(t, conf.Subscribe[0].Password)
  109. }
  110. func TestConfig_DefaultToken(t *testing.T) {
  111. filename := filepath.Join(t.TempDir(), "client.yml")
  112. require.Nil(t, os.WriteFile(filename, []byte(`
  113. default-host: http://localhost
  114. default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2
  115. subscribe:
  116. - topic: mytopic
  117. `), 0600))
  118. conf, err := client.LoadConfig(filename)
  119. require.Nil(t, err)
  120. require.Equal(t, "http://localhost", conf.DefaultHost)
  121. require.Equal(t, "", conf.DefaultUser)
  122. require.Nil(t, conf.DefaultPassword)
  123. require.Equal(t, "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", conf.DefaultToken)
  124. require.Equal(t, 1, len(conf.Subscribe))
  125. require.Equal(t, "mytopic", conf.Subscribe[0].Topic)
  126. require.Nil(t, conf.Subscribe[0].User)
  127. require.Nil(t, conf.Subscribe[0].Password)
  128. require.Nil(t, conf.Subscribe[0].Token)
  129. }