| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/http" |
| 6 |
"net/http/httptest" |
| 7 |
"os" |
| 8 |
"os/exec" |
| 9 |
"path/filepath" |
| 10 |
"strconv" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
"heckel.io/ntfy/v2/test" |
| 17 |
"heckel.io/ntfy/v2/util" |
| 18 |
) |
| 19 |
|
| 20 |
func TestCLI_Publish_Subscribe_Poll_Real_Server(t *testing.T) { |
| 21 |
t.Skip("temporarily disabled") // FIXME |
| 22 |
testMessage := util.RandomString(10) |
| 23 |
app, _, _, _ := newTestApp() |
| 24 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "ntfytest", "ntfy unit test " + testMessage})) |
| 25 |
|
| 26 |
_, err := util.Retry(func() (*int, error) { |
| 27 |
app2, _, stdout, _ := newTestApp() |
| 28 |
if err := app2.Run([]string{"ntfy", "subscribe", "--poll", "ntfytest"}); err != nil { |
| 29 |
return nil, err |
| 30 |
} |
| 31 |
if !strings.Contains(stdout.String(), testMessage) { |
| 32 |
return nil, fmt.Errorf("test message %s not found in topic", testMessage) |
| 33 |
} |
| 34 |
return util.Int(1), nil |
| 35 |
}, time.Second, 2*time.Second, 5*time.Second) // Since #502, ntfy.sh writes messages to the cache asynchronously, after a timeout of ~1.5s |
| 36 |
require.Nil(t, err) |
| 37 |
} |
| 38 |
|
| 39 |
func TestCLI_Publish_Subscribe_Poll(t *testing.T) { |
| 40 |
s, port := test.StartServer(t) |
| 41 |
defer test.StopServer(t, s, port) |
| 42 |
topic := fmt.Sprintf("http://127.0.0.1:%d/mytopic", port) |
| 43 |
|
| 44 |
app, _, stdout, _ := newTestApp() |
| 45 |
require.Nil(t, app.Run([]string{"ntfy", "publish", topic, "some message"})) |
| 46 |
m := toMessage(t, stdout.String()) |
| 47 |
require.Equal(t, "some message", m.Message) |
| 48 |
|
| 49 |
app2, _, stdout, _ := newTestApp() |
| 50 |
require.Nil(t, app2.Run([]string{"ntfy", "subscribe", "--poll", topic})) |
| 51 |
m = toMessage(t, stdout.String()) |
| 52 |
require.Equal(t, "some message", m.Message) |
| 53 |
} |
| 54 |
|
| 55 |
func TestCLI_Publish_All_The_Things(t *testing.T) { |
| 56 |
s, port := test.StartServer(t) |
| 57 |
defer test.StopServer(t, s, port) |
| 58 |
topic := fmt.Sprintf("http://127.0.0.1:%d/mytopic", port) |
| 59 |
|
| 60 |
app, _, stdout, _ := newTestApp() |
| 61 |
require.Nil(t, app.Run([]string{ |
| 62 |
"ntfy", "publish", |
| 63 |
"--title", "this is a title", |
| 64 |
"--priority", "high", |
| 65 |
"--tags", "tag1,tag2", |
| 66 |
// No --delay, --email |
| 67 |
"--click", "https://ntfy.sh", |
| 68 |
"--icon", "https://ntfy.sh/static/img/ntfy.png", |
| 69 |
"--attach", "https://f-droid.org/F-Droid.apk", |
| 70 |
"--filename", "fdroid.apk", |
| 71 |
"--no-cache", |
| 72 |
"--no-firebase", |
| 73 |
topic, |
| 74 |
"some message", |
| 75 |
})) |
| 76 |
m := toMessage(t, stdout.String()) |
| 77 |
require.Equal(t, "message", m.Event) |
| 78 |
require.Equal(t, "mytopic", m.Topic) |
| 79 |
require.Equal(t, "some message", m.Message) |
| 80 |
require.Equal(t, "this is a title", m.Title) |
| 81 |
require.Equal(t, 4, m.Priority) |
| 82 |
require.Equal(t, []string{"tag1", "tag2"}, m.Tags) |
| 83 |
require.Equal(t, "https://ntfy.sh", m.Click) |
| 84 |
require.Equal(t, "https://f-droid.org/F-Droid.apk", m.Attachment.URL) |
| 85 |
require.Equal(t, "fdroid.apk", m.Attachment.Name) |
| 86 |
require.Equal(t, int64(0), m.Attachment.Size) |
| 87 |
require.Equal(t, "", m.Attachment.Owner) |
| 88 |
require.Equal(t, int64(0), m.Attachment.Expires) |
| 89 |
require.Equal(t, "", m.Attachment.Type) |
| 90 |
require.Equal(t, "https://ntfy.sh/static/img/ntfy.png", m.Icon) |
| 91 |
} |
| 92 |
|
| 93 |
func TestCLI_Publish_Wait_PID_And_Cmd(t *testing.T) { |
| 94 |
s, port := test.StartServer(t) |
| 95 |
defer test.StopServer(t, s, port) |
| 96 |
topic := fmt.Sprintf("http://127.0.0.1:%d/mytopic", port) |
| 97 |
|
| 98 |
// Test: sleep 0.5 |
| 99 |
sleep := exec.Command("sleep", "0.5") |
| 100 |
require.Nil(t, sleep.Start()) |
| 101 |
go sleep.Wait() // Must be called to release resources |
| 102 |
start := time.Now() |
| 103 |
app, _, stdout, _ := newTestApp() |
| 104 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "--wait-pid", strconv.Itoa(sleep.Process.Pid), topic})) |
| 105 |
m := toMessage(t, stdout.String()) |
| 106 |
require.True(t, time.Since(start) >= 500*time.Millisecond) |
| 107 |
require.Regexp(t, `Process with PID \d+ exited after `, m.Message) |
| 108 |
|
| 109 |
// Test: PID does not exist |
| 110 |
app, _, _, _ = newTestApp() |
| 111 |
err := app.Run([]string{"ntfy", "publish", "--wait-pid", "1234567", topic}) |
| 112 |
require.Error(t, err) |
| 113 |
require.Equal(t, "process with PID 1234567 not running", err.Error()) |
| 114 |
|
| 115 |
// Test: Successful command (exit 0) |
| 116 |
start = time.Now() |
| 117 |
app, _, stdout, _ = newTestApp() |
| 118 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "--wait-cmd", topic, "sleep", "0.5"})) |
| 119 |
m = toMessage(t, stdout.String()) |
| 120 |
require.True(t, time.Since(start) >= 500*time.Millisecond) |
| 121 |
require.Contains(t, m.Message, `Command succeeded after `) |
| 122 |
require.Contains(t, m.Message, `: sleep 0.5`) |
| 123 |
|
| 124 |
// Test: Failing command (exit 1) |
| 125 |
app, _, stdout, _ = newTestApp() |
| 126 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "--wait-cmd", topic, "/bin/false", "false doesn't care about its args"})) |
| 127 |
m = toMessage(t, stdout.String()) |
| 128 |
require.Contains(t, m.Message, `Command failed after `) |
| 129 |
require.Contains(t, m.Message, `(exit code 1): /bin/false "false doesn't care about its args"`, m.Message) |
| 130 |
|
| 131 |
// Test: Non-existing command (hard fail!) |
| 132 |
app, _, _, _ = newTestApp() |
| 133 |
err = app.Run([]string{"ntfy", "publish", "--wait-cmd", topic, "does-not-exist-no-really", "really though"}) |
| 134 |
require.Error(t, err) |
| 135 |
require.Equal(t, `command failed: does-not-exist-no-really "really though", error: exec: "does-not-exist-no-really": executable file not found in $PATH`, err.Error()) |
| 136 |
|
| 137 |
// Tests with NTFY_TOPIC set //// |
| 138 |
t.Setenv("NTFY_TOPIC", topic) |
| 139 |
|
| 140 |
// Test: Successful command with NTFY_TOPIC |
| 141 |
app, _, stdout, _ = newTestApp() |
| 142 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "--cmd", "echo", "hi there"})) |
| 143 |
m = toMessage(t, stdout.String()) |
| 144 |
require.Equal(t, "mytopic", m.Topic) |
| 145 |
|
| 146 |
// Test: Successful --wait-pid with NTFY_TOPIC |
| 147 |
sleep = exec.Command("sleep", "0.2") |
| 148 |
require.Nil(t, sleep.Start()) |
| 149 |
go sleep.Wait() // Must be called to release resources |
| 150 |
app, _, stdout, _ = newTestApp() |
| 151 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "--wait-pid", strconv.Itoa(sleep.Process.Pid)})) |
| 152 |
m = toMessage(t, stdout.String()) |
| 153 |
require.Regexp(t, `Process with PID \d+ exited after .+ms`, m.Message) |
| 154 |
} |
| 155 |
|
| 156 |
func TestCLI_Publish_Default_UserPass(t *testing.T) { |
| 157 |
message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` |
| 158 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 159 |
require.Equal(t, "/mytopic", r.URL.Path) |
| 160 |
require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization")) |
| 161 |
|
| 162 |
w.WriteHeader(http.StatusOK) |
| 163 |
w.Write([]byte(message)) |
| 164 |
})) |
| 165 |
defer server.Close() |
| 166 |
|
| 167 |
filename := filepath.Join(t.TempDir(), "client.yml") |
| 168 |
require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` |
| 169 |
default-host: %s |
| 170 |
default-user: philipp |
| 171 |
default-password: mypass |
| 172 |
`, server.URL)), 0600)) |
| 173 |
|
| 174 |
app, _, stdout, _ := newTestApp() |
| 175 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "mytopic", "triggered"})) |
| 176 |
m := toMessage(t, stdout.String()) |
| 177 |
require.Equal(t, "triggered", m.Message) |
| 178 |
} |
| 179 |
|
| 180 |
func TestCLI_Publish_Default_Token(t *testing.T) { |
| 181 |
message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` |
| 182 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 183 |
require.Equal(t, "/mytopic", r.URL.Path) |
| 184 |
require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) |
| 185 |
|
| 186 |
w.WriteHeader(http.StatusOK) |
| 187 |
w.Write([]byte(message)) |
| 188 |
})) |
| 189 |
defer server.Close() |
| 190 |
|
| 191 |
filename := filepath.Join(t.TempDir(), "client.yml") |
| 192 |
require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` |
| 193 |
default-host: %s |
| 194 |
default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2 |
| 195 |
`, server.URL)), 0600)) |
| 196 |
|
| 197 |
app, _, stdout, _ := newTestApp() |
| 198 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "mytopic", "triggered"})) |
| 199 |
m := toMessage(t, stdout.String()) |
| 200 |
require.Equal(t, "triggered", m.Message) |
| 201 |
} |
| 202 |
|
| 203 |
func TestCLI_Publish_Default_UserPass_CLI_Token(t *testing.T) { |
| 204 |
message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` |
| 205 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 206 |
require.Equal(t, "/mytopic", r.URL.Path) |
| 207 |
require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) |
| 208 |
|
| 209 |
w.WriteHeader(http.StatusOK) |
| 210 |
w.Write([]byte(message)) |
| 211 |
})) |
| 212 |
defer server.Close() |
| 213 |
|
| 214 |
filename := filepath.Join(t.TempDir(), "client.yml") |
| 215 |
require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` |
| 216 |
default-host: %s |
| 217 |
default-user: philipp |
| 218 |
default-password: mypass |
| 219 |
`, server.URL)), 0600)) |
| 220 |
|
| 221 |
app, _, stdout, _ := newTestApp() |
| 222 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "mytopic", "triggered"})) |
| 223 |
m := toMessage(t, stdout.String()) |
| 224 |
require.Equal(t, "triggered", m.Message) |
| 225 |
} |
| 226 |
|
| 227 |
func TestCLI_Publish_Default_Token_CLI_UserPass(t *testing.T) { |
| 228 |
message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` |
| 229 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 230 |
require.Equal(t, "/mytopic", r.URL.Path) |
| 231 |
require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization")) |
| 232 |
|
| 233 |
w.WriteHeader(http.StatusOK) |
| 234 |
w.Write([]byte(message)) |
| 235 |
})) |
| 236 |
defer server.Close() |
| 237 |
|
| 238 |
filename := filepath.Join(t.TempDir(), "client.yml") |
| 239 |
require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` |
| 240 |
default-host: %s |
| 241 |
default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2 |
| 242 |
`, server.URL)), 0600)) |
| 243 |
|
| 244 |
app, _, stdout, _ := newTestApp() |
| 245 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "--user", "philipp:mypass", "mytopic", "triggered"})) |
| 246 |
m := toMessage(t, stdout.String()) |
| 247 |
require.Equal(t, "triggered", m.Message) |
| 248 |
} |
| 249 |
|
| 250 |
func TestCLI_Publish_Default_Token_CLI_Token(t *testing.T) { |
| 251 |
message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` |
| 252 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 253 |
require.Equal(t, "/mytopic", r.URL.Path) |
| 254 |
require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) |
| 255 |
|
| 256 |
w.WriteHeader(http.StatusOK) |
| 257 |
w.Write([]byte(message)) |
| 258 |
})) |
| 259 |
defer server.Close() |
| 260 |
|
| 261 |
filename := filepath.Join(t.TempDir(), "client.yml") |
| 262 |
require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` |
| 263 |
default-host: %s |
| 264 |
default-token: tk_FAKETOKEN01234567890FAKETOKEN |
| 265 |
`, server.URL)), 0600)) |
| 266 |
|
| 267 |
app, _, stdout, _ := newTestApp() |
| 268 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "mytopic", "triggered"})) |
| 269 |
m := toMessage(t, stdout.String()) |
| 270 |
require.Equal(t, "triggered", m.Message) |
| 271 |
} |
| 272 |
|
| 273 |
func TestCLI_Publish_Default_UserPass_CLI_UserPass(t *testing.T) { |
| 274 |
message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` |
| 275 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 276 |
require.Equal(t, "/mytopic", r.URL.Path) |
| 277 |
require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization")) |
| 278 |
|
| 279 |
w.WriteHeader(http.StatusOK) |
| 280 |
w.Write([]byte(message)) |
| 281 |
})) |
| 282 |
defer server.Close() |
| 283 |
|
| 284 |
filename := filepath.Join(t.TempDir(), "client.yml") |
| 285 |
require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` |
| 286 |
default-host: %s |
| 287 |
default-user: philipp |
| 288 |
default-password: fakepass |
| 289 |
`, server.URL)), 0600)) |
| 290 |
|
| 291 |
app, _, stdout, _ := newTestApp() |
| 292 |
require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "--user", "philipp:mypass", "mytopic", "triggered"})) |
| 293 |
m := toMessage(t, stdout.String()) |
| 294 |
require.Equal(t, "triggered", m.Message) |
| 295 |
} |
| 296 |
|
| 297 |
func TestCLI_Publish_Token_And_UserPass(t *testing.T) { |
| 298 |
app, _, _, _ := newTestApp() |
| 299 |
err := app.Run([]string{"ntfy", "publish", "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "--user", "philipp:mypass", "mytopic", "triggered"}) |
| 300 |
require.Error(t, err) |
| 301 |
require.Equal(t, "cannot set both --user and --token", err.Error()) |
| 302 |
} |