Merge branch 'main' into metrics
This commit is contained in:
		
						commit
						754b456320
					
				
					 43 changed files with 939 additions and 100 deletions
				
			
		|  | @ -154,8 +154,7 @@ func execPublish(c *cli.Context) error { | |||
| 	} | ||||
| 	if token != "" { | ||||
| 		options = append(options, client.WithBearerAuth(token)) | ||||
| 	} | ||||
| 	if user != "" { | ||||
| 	} else if user != "" { | ||||
| 		var pass string | ||||
| 		parts := strings.SplitN(user, ":", 2) | ||||
| 		if len(parts) == 2 { | ||||
|  | @ -171,7 +170,9 @@ func execPublish(c *cli.Context) error { | |||
| 			fmt.Fprintf(c.App.ErrWriter, "\r%s\r", strings.Repeat(" ", 20)) | ||||
| 		} | ||||
| 		options = append(options, client.WithBasicAuth(user, pass)) | ||||
| 	} else if token == "" && conf.DefaultUser != "" && conf.DefaultPassword != nil { | ||||
| 	} else if conf.DefaultToken != "" { | ||||
| 		options = append(options, client.WithBearerAuth(conf.DefaultToken)) | ||||
| 	} else if conf.DefaultUser != "" && conf.DefaultPassword != nil { | ||||
| 		options = append(options, client.WithBasicAuth(conf.DefaultUser, *conf.DefaultPassword)) | ||||
| 	} | ||||
| 	if pid > 0 { | ||||
|  |  | |||
|  | @ -5,8 +5,11 @@ import ( | |||
| 	"github.com/stretchr/testify/require" | ||||
| 	"heckel.io/ntfy/test" | ||||
| 	"heckel.io/ntfy/util" | ||||
| 	"net/http" | ||||
| 	"net/http/httptest" | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| 	"path/filepath" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
|  | @ -130,7 +133,7 @@ func TestCLI_Publish_Wait_PID_And_Cmd(t *testing.T) { | |||
| 	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()) | ||||
| 
 | ||||
| 	// Tests with NTFY_TOPIC set //// | ||||
| 	require.Nil(t, os.Setenv("NTFY_TOPIC", topic)) | ||||
| 	t.Setenv("NTFY_TOPIC", topic) | ||||
| 
 | ||||
| 	// Test: Successful command with NTFY_TOPIC | ||||
| 	app, _, stdout, _ = newTestApp() | ||||
|  | @ -147,3 +150,151 @@ func TestCLI_Publish_Wait_PID_And_Cmd(t *testing.T) { | |||
| 	m = toMessage(t, stdout.String()) | ||||
| 	require.Regexp(t, `Process with PID \d+ exited after .+ms`, m.Message) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Publish_Default_UserPass(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic", r.URL.Path) | ||||
| 		require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-user: philipp | ||||
| default-password: mypass | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "mytopic", "triggered"})) | ||||
| 	m := toMessage(t, stdout.String()) | ||||
| 	require.Equal(t, "triggered", m.Message) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Publish_Default_Token(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic", r.URL.Path) | ||||
| 		require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2 | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "mytopic", "triggered"})) | ||||
| 	m := toMessage(t, stdout.String()) | ||||
| 	require.Equal(t, "triggered", m.Message) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Publish_Default_UserPass_CLI_Token(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic", r.URL.Path) | ||||
| 		require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-user: philipp | ||||
| default-password: mypass | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "mytopic", "triggered"})) | ||||
| 	m := toMessage(t, stdout.String()) | ||||
| 	require.Equal(t, "triggered", m.Message) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Publish_Default_Token_CLI_UserPass(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic", r.URL.Path) | ||||
| 		require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2 | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "--user", "philipp:mypass", "mytopic", "triggered"})) | ||||
| 	m := toMessage(t, stdout.String()) | ||||
| 	require.Equal(t, "triggered", m.Message) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Publish_Default_Token_CLI_Token(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic", r.URL.Path) | ||||
| 		require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-token: tk_FAKETOKEN01234567890FAKETOKEN | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "mytopic", "triggered"})) | ||||
| 	m := toMessage(t, stdout.String()) | ||||
| 	require.Equal(t, "triggered", m.Message) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Publish_Default_UserPass_CLI_UserPass(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic", r.URL.Path) | ||||
| 		require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-user: philipp | ||||
| default-password: fakepass | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "publish", "--config=" + filename, "--user", "philipp:mypass", "mytopic", "triggered"})) | ||||
| 	m := toMessage(t, stdout.String()) | ||||
| 	require.Equal(t, "triggered", m.Message) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Publish_Token_And_UserPass(t *testing.T) { | ||||
| 	app, _, _, _ := newTestApp() | ||||
| 	err := app.Run([]string{"ntfy", "publish", "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "--user", "philipp:mypass", "mytopic", "triggered"}) | ||||
| 	require.Error(t, err) | ||||
| 	require.Equal(t, "cannot set both --user and --token", err.Error()) | ||||
| } | ||||
|  |  | |||
|  | @ -30,6 +30,7 @@ var flagsSubscribe = append( | |||
| 	&cli.StringFlag{Name: "config", Aliases: []string{"c"}, Usage: "client config file"}, | ||||
| 	&cli.StringFlag{Name: "since", Aliases: []string{"s"}, Usage: "return events since `SINCE` (Unix timestamp, or all)"}, | ||||
| 	&cli.StringFlag{Name: "user", Aliases: []string{"u"}, EnvVars: []string{"NTFY_USER"}, Usage: "username[:password] used to auth against the server"}, | ||||
| 	&cli.StringFlag{Name: "token", Aliases: []string{"k"}, EnvVars: []string{"NTFY_TOKEN"}, Usage: "access token used to auth against the server"}, | ||||
| 	&cli.BoolFlag{Name: "from-config", Aliases: []string{"from_config", "C"}, Usage: "read subscriptions from config file (service mode)"}, | ||||
| 	&cli.BoolFlag{Name: "poll", Aliases: []string{"p"}, Usage: "return events and exit, do not listen for new events"}, | ||||
| 	&cli.BoolFlag{Name: "scheduled", Aliases: []string{"sched", "S"}, Usage: "also return scheduled/delayed events"}, | ||||
|  | @ -97,11 +98,18 @@ func execSubscribe(c *cli.Context) error { | |||
| 	cl := client.New(conf) | ||||
| 	since := c.String("since") | ||||
| 	user := c.String("user") | ||||
| 	token := c.String("token") | ||||
| 	poll := c.Bool("poll") | ||||
| 	scheduled := c.Bool("scheduled") | ||||
| 	fromConfig := c.Bool("from-config") | ||||
| 	topic := c.Args().Get(0) | ||||
| 	command := c.Args().Get(1) | ||||
| 
 | ||||
| 	// Checks | ||||
| 	if user != "" && token != "" { | ||||
| 		return errors.New("cannot set both --user and --token") | ||||
| 	} | ||||
| 
 | ||||
| 	if !fromConfig { | ||||
| 		conf.Subscribe = nil // wipe if --from-config not passed | ||||
| 	} | ||||
|  | @ -109,6 +117,9 @@ func execSubscribe(c *cli.Context) error { | |||
| 	if since != "" { | ||||
| 		options = append(options, client.WithSince(since)) | ||||
| 	} | ||||
| 	if token != "" { | ||||
| 		options = append(options, client.WithBearerAuth(token)) | ||||
| 	} | ||||
| 	if user != "" { | ||||
| 		var pass string | ||||
| 		parts := strings.SplitN(user, ":", 2) | ||||
|  | @ -126,9 +137,6 @@ func execSubscribe(c *cli.Context) error { | |||
| 		} | ||||
| 		options = append(options, client.WithBasicAuth(user, pass)) | ||||
| 	} | ||||
| 	if poll { | ||||
| 		options = append(options, client.WithPoll()) | ||||
| 	} | ||||
| 	if scheduled { | ||||
| 		options = append(options, client.WithScheduled()) | ||||
| 	} | ||||
|  | @ -145,6 +153,9 @@ func execSubscribe(c *cli.Context) error { | |||
| 
 | ||||
| func doPoll(c *cli.Context, cl *client.Client, conf *client.Config, topic, command string, options ...client.SubscribeOption) error { | ||||
| 	for _, s := range conf.Subscribe { // may be nil | ||||
| 		if auth := maybeAddAuthHeader(s, conf); auth != nil { | ||||
| 			options = append(options, auth) | ||||
| 		} | ||||
| 		if err := doPollSingle(c, cl, s.Topic, s.Command, options...); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | @ -175,21 +186,11 @@ func doSubscribe(c *cli.Context, cl *client.Client, conf *client.Config, topic, | |||
| 		for filter, value := range s.If { | ||||
| 			topicOptions = append(topicOptions, client.WithFilter(filter, value)) | ||||
| 		} | ||||
| 		var user string | ||||
| 		var password *string | ||||
| 		if s.User != "" { | ||||
| 			user = s.User | ||||
| 		} else if conf.DefaultUser != "" { | ||||
| 			user = conf.DefaultUser | ||||
| 		} | ||||
| 		if s.Password != nil { | ||||
| 			password = s.Password | ||||
| 		} else if conf.DefaultPassword != nil { | ||||
| 			password = conf.DefaultPassword | ||||
| 		} | ||||
| 		if user != "" && password != nil { | ||||
| 			topicOptions = append(topicOptions, client.WithBasicAuth(user, *password)) | ||||
| 
 | ||||
| 		if auth := maybeAddAuthHeader(s, conf); auth != nil { | ||||
| 			topicOptions = append(topicOptions, auth) | ||||
| 		} | ||||
| 
 | ||||
| 		subscriptionID := cl.Subscribe(s.Topic, topicOptions...) | ||||
| 		if s.Command != "" { | ||||
| 			cmds[subscriptionID] = s.Command | ||||
|  | @ -214,6 +215,25 @@ func doSubscribe(c *cli.Context, cl *client.Client, conf *client.Config, topic, | |||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func maybeAddAuthHeader(s client.Subscribe, conf *client.Config) client.SubscribeOption { | ||||
| 	// check for subscription token then subscription user:pass | ||||
| 	if s.Token != "" { | ||||
| 		return client.WithBearerAuth(s.Token) | ||||
| 	} | ||||
| 	if s.User != "" && s.Password != nil { | ||||
| 		return client.WithBasicAuth(s.User, *s.Password) | ||||
| 	} | ||||
| 
 | ||||
| 	// if no subscription token nor subscription user:pass, check for default token then default user:pass | ||||
| 	if conf.DefaultToken != "" { | ||||
| 		return client.WithBearerAuth(conf.DefaultToken) | ||||
| 	} | ||||
| 	if conf.DefaultUser != "" && conf.DefaultPassword != nil { | ||||
| 		return client.WithBasicAuth(conf.DefaultUser, *conf.DefaultPassword) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func printMessageOrRunCommand(c *cli.Context, m *client.Message, command string) { | ||||
| 	if command != "" { | ||||
| 		runCommand(c, command, m) | ||||
|  |  | |||
							
								
								
									
										312
									
								
								cmd/subscribe_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										312
									
								
								cmd/subscribe_test.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,312 @@ | |||
| package cmd | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"github.com/stretchr/testify/require" | ||||
| 	"net/http" | ||||
| 	"net/http/httptest" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| ) | ||||
| 
 | ||||
| func TestCLI_Subscribe_Default_UserPass_Subscription_Token(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic/json", r.URL.Path) | ||||
| 		require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-user: philipp | ||||
| default-password: mypass | ||||
| subscribe: | ||||
|   - topic: mytopic | ||||
|     token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2 | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 
 | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename})) | ||||
| 
 | ||||
| 	require.Equal(t, message, strings.TrimSpace(stdout.String())) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Subscribe_Default_Token_Subscription_UserPass(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic/json", r.URL.Path) | ||||
| 		require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2 | ||||
| subscribe: | ||||
|   - topic: mytopic | ||||
|     user: philipp | ||||
|     password: mypass | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 
 | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename})) | ||||
| 
 | ||||
| 	require.Equal(t, message, strings.TrimSpace(stdout.String())) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Subscribe_Default_Token_Subscription_Token(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic/json", r.URL.Path) | ||||
| 		require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-token: tk_FAKETOKEN01234567890FAKETOKEN | ||||
| subscribe: | ||||
|   - topic: mytopic | ||||
|     token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2 | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 
 | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename})) | ||||
| 
 | ||||
| 	require.Equal(t, message, strings.TrimSpace(stdout.String())) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Subscribe_Default_UserPass_Subscription_UserPass(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic/json", r.URL.Path) | ||||
| 		require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-user: fake | ||||
| default-password: password | ||||
| subscribe: | ||||
|   - topic: mytopic | ||||
|     user: philipp | ||||
|     password: mypass | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 
 | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename})) | ||||
| 
 | ||||
| 	require.Equal(t, message, strings.TrimSpace(stdout.String())) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Subscribe_Default_Token_Subscription_Empty(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic/json", r.URL.Path) | ||||
| 		require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2 | ||||
| subscribe: | ||||
|   - topic: mytopic | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 
 | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename})) | ||||
| 
 | ||||
| 	require.Equal(t, message, strings.TrimSpace(stdout.String())) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Subscribe_Default_UserPass_Subscription_Empty(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic/json", r.URL.Path) | ||||
| 		require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-user: philipp | ||||
| default-password: mypass | ||||
| subscribe: | ||||
|   - topic: mytopic | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 
 | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename})) | ||||
| 
 | ||||
| 	require.Equal(t, message, strings.TrimSpace(stdout.String())) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Subscribe_Default_Empty_Subscription_Token(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic/json", r.URL.Path) | ||||
| 		require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| subscribe: | ||||
|   - topic: mytopic | ||||
|     token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2 | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 
 | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename})) | ||||
| 
 | ||||
| 	require.Equal(t, message, strings.TrimSpace(stdout.String())) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Subscribe_Default_Empty_Subscription_UserPass(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic/json", r.URL.Path) | ||||
| 		require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| subscribe: | ||||
|   - topic: mytopic | ||||
|     user: philipp | ||||
|     password: mypass | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 
 | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename})) | ||||
| 
 | ||||
| 	require.Equal(t, message, strings.TrimSpace(stdout.String())) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Subscribe_Default_Token_CLI_Token(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic/json", r.URL.Path) | ||||
| 		require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-token: tk_FAKETOKEN0123456789FAKETOKEN | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 
 | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename, "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "mytopic"})) | ||||
| 
 | ||||
| 	require.Equal(t, message, strings.TrimSpace(stdout.String())) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Subscribe_Default_Token_CLI_UserPass(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic/json", r.URL.Path) | ||||
| 		require.Equal(t, "Basic cGhpbGlwcDpteXBhc3M=", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2 | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 
 | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename, "--user", "philipp:mypass", "mytopic"})) | ||||
| 
 | ||||
| 	require.Equal(t, message, strings.TrimSpace(stdout.String())) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Subscribe_Default_Token_Subscription_Token_CLI_UserPass(t *testing.T) { | ||||
| 	message := `{"id":"RXIQBFaieLVr","time":124,"expires":1124,"event":"message","topic":"mytopic","message":"triggered"}` | ||||
| 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
| 		require.Equal(t, "/mytopic/json", r.URL.Path) | ||||
| 		require.Equal(t, "Bearer tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", r.Header.Get("Authorization")) | ||||
| 
 | ||||
| 		w.WriteHeader(http.StatusOK) | ||||
| 		w.Write([]byte(message)) | ||||
| 	})) | ||||
| 	defer server.Close() | ||||
| 
 | ||||
| 	filename := filepath.Join(t.TempDir(), "client.yml") | ||||
| 	require.Nil(t, os.WriteFile(filename, []byte(fmt.Sprintf(` | ||||
| default-host: %s | ||||
| default-token: tk_FAKETOKEN01234567890FAKETOKEN | ||||
| subscribe: | ||||
|   - topic: mytopic | ||||
|     token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2 | ||||
| `, server.URL)), 0600)) | ||||
| 
 | ||||
| 	app, _, stdout, _ := newTestApp() | ||||
| 
 | ||||
| 	require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename, "--user", "philipp:mypass"})) | ||||
| 
 | ||||
| 	require.Equal(t, message, strings.TrimSpace(stdout.String())) | ||||
| } | ||||
| 
 | ||||
| func TestCLI_Subscribe_Token_And_UserPass(t *testing.T) { | ||||
| 	app, _, _, _ := newTestApp() | ||||
| 	err := app.Run([]string{"ntfy", "subscribe", "--poll", "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "--user", "philipp:mypass", "mytopic", "triggered"}) | ||||
| 	require.Error(t, err) | ||||
| 	require.Equal(t, "cannot set both --user and --token", err.Error()) | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue