include auth headers with using ntfy sub --poll --from-config

This commit is contained in:
Hunter Kehoe 2023-03-06 18:14:52 -07:00
parent 25be5b47e4
commit 1797dec2ba
3 changed files with 78 additions and 68 deletions

View file

@ -9,7 +9,6 @@ import (
"path/filepath"
"strings"
"testing"
"time"
)
func TestCLI_Subscribe_Default_UserPass_Subscription_Token(t *testing.T) {
@ -35,9 +34,7 @@ subscribe:
app, _, stdout, _ := newTestApp()
go app.Run([]string{"ntfy", "subscribe", "--from-config", "--config=" + filename})
// Sleep to give the app time to subscribe
time.Sleep(time.Millisecond * 100)
require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
require.Equal(t, message, strings.TrimSpace(stdout.String()))
}
@ -65,9 +62,7 @@ subscribe:
app, _, stdout, _ := newTestApp()
go app.Run([]string{"ntfy", "subscribe", "--from-config", "--config=" + filename})
// Sleep to give the app time to subscribe
time.Sleep(time.Millisecond * 100)
require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
require.Equal(t, message, strings.TrimSpace(stdout.String()))
}
@ -94,9 +89,7 @@ subscribe:
app, _, stdout, _ := newTestApp()
go app.Run([]string{"ntfy", "subscribe", "--from-config", "--config=" + filename})
// Sleep to give the app time to subscribe
time.Sleep(time.Millisecond * 100)
require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
require.Equal(t, message, strings.TrimSpace(stdout.String()))
}
@ -125,9 +118,7 @@ subscribe:
app, _, stdout, _ := newTestApp()
go app.Run([]string{"ntfy", "subscribe", "--from-config", "--config=" + filename})
// Sleep to give the app time to subscribe
time.Sleep(time.Millisecond * 100)
require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
require.Equal(t, message, strings.TrimSpace(stdout.String()))
}
@ -153,9 +144,7 @@ subscribe:
app, _, stdout, _ := newTestApp()
go app.Run([]string{"ntfy", "subscribe", "--from-config", "--config=" + filename})
// Sleep to give the app time to subscribe
time.Sleep(time.Millisecond * 100)
require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
require.Equal(t, message, strings.TrimSpace(stdout.String()))
}
@ -182,9 +171,7 @@ subscribe:
app, _, stdout, _ := newTestApp()
go app.Run([]string{"ntfy", "subscribe", "--from-config", "--config=" + filename})
// Sleep to give the app time to subscribe
time.Sleep(time.Millisecond * 100)
require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
require.Equal(t, message, strings.TrimSpace(stdout.String()))
}
@ -210,9 +197,7 @@ subscribe:
app, _, stdout, _ := newTestApp()
go app.Run([]string{"ntfy", "subscribe", "--from-config", "--config=" + filename})
// Sleep to give the app time to subscribe
time.Sleep(time.Millisecond * 100)
require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
require.Equal(t, message, strings.TrimSpace(stdout.String()))
}
@ -239,9 +224,7 @@ subscribe:
app, _, stdout, _ := newTestApp()
go app.Run([]string{"ntfy", "subscribe", "--from-config", "--config=" + filename})
// Sleep to give the app time to subscribe
time.Sleep(time.Millisecond * 100)
require.Nil(t, app.Run([]string{"ntfy", "subscribe", "--poll", "--from-config", "--config=" + filename}))
require.Equal(t, message, strings.TrimSpace(stdout.String()))
}
@ -265,9 +248,7 @@ default-token: tk_FAKETOKEN0123456789FAKETOKEN
app, _, stdout, _ := newTestApp()
go app.Run([]string{"ntfy", "subscribe", "--from-config", "--config=" + filename, "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "mytopic"})
// Sleep to give the app time to subscribe
time.Sleep(time.Millisecond * 100)
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()))
}
@ -291,16 +272,41 @@ default-token: tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2
app, _, stdout, _ := newTestApp()
go app.Run([]string{"ntfy", "subscribe", "--from-config", "--config=" + filename, "--user", "philipp:mypass", "mytopic"})
// Sleep to give the app time to subscribe
time.Sleep(time.Millisecond * 100)
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", "--token", "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2", "--user", "philipp:mypass", "mytopic", "triggered"})
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())
}