Add web push tests

This commit is contained in:
nimbleghost 2023-05-29 17:57:21 +02:00
parent ff5c854192
commit a9fef387fa
20 changed files with 372 additions and 41 deletions

View file

@ -194,7 +194,7 @@ func execServe(c *cli.Context) error {
if firebaseKeyFile != "" && !util.FileExists(firebaseKeyFile) {
return errors.New("if set, FCM key file must exist")
} else if webPushEnabled && (webPushPrivateKey == "" || webPushPublicKey == "" || webPushSubscriptionsFile == "" || webPushEmailAddress == "" || baseURL == "") {
return errors.New("if web push is enabled, web-push-private-key, web-push-public-key, web-push-subscriptions-file, web-push-email-address, and base-url should be set. run 'ntfy web-push-keys' to generate keys")
return errors.New("if web push is enabled, web-push-private-key, web-push-public-key, web-push-subscriptions-file, web-push-email-address, and base-url should be set. run 'ntfy web-push generate-keys' to generate keys")
} else if keepaliveInterval < 5*time.Second {
return errors.New("keepalive interval cannot be lower than five seconds")
} else if managerInterval < 5*time.Second {

View file

@ -14,11 +14,20 @@ func init() {
}
var cmdWebPush = &cli.Command{
Name: "web-push-keys",
Usage: "Generate web push VAPID keys",
UsageText: "ntfy web-push-keys",
Name: "web-push",
Usage: "Generate keys, in the future manage web push subscriptions",
UsageText: "ntfy web-push [generate-keys]",
Category: categoryServer,
Action: generateWebPushKeys,
Subcommands: []*cli.Command{
{
Action: generateWebPushKeys,
Name: "generate-keys",
Usage: "Generate VAPID keys to enable browser background push notifications",
UsageText: "ntfy web-push generate-keys",
Category: categoryServer,
},
},
}
func generateWebPushKeys(c *cli.Context) error {
@ -27,13 +36,28 @@ func generateWebPushKeys(c *cli.Context) error {
return err
}
fmt.Fprintf(c.App.ErrWriter, `Add the following lines to your config file:
fmt.Fprintf(c.App.ErrWriter, `Keys generated.
VAPID Public Key:
%s
VAPID Private Key:
%s
---
Add the following lines to your config file:
web-push-enabled: true
web-push-public-key: %s
web-push-private-key: %s
web-push-subscriptions-file: <filename>
web-push-email-address: <email address>
`, publicKey, privateKey)
Look at the docs for other methods (e.g. command line flags & environment variables).
You will also need to set a base-url.
`, publicKey, privateKey, publicKey, privateKey)
return nil
}

24
cmd/web_push_test.go Normal file
View file

@ -0,0 +1,24 @@
package cmd
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
"heckel.io/ntfy/server"
)
func TestCLI_WebPush_GenerateKeys(t *testing.T) {
app, _, _, stderr := newTestApp()
require.Nil(t, runWebPushCommand(app, server.NewConfig(), "generate-keys"))
require.Contains(t, stderr.String(), "Keys generated.")
}
func runWebPushCommand(app *cli.App, conf *server.Config, args ...string) error {
webPushArgs := []string{
"ntfy",
"--log-level=ERROR",
"web-push",
}
return app.Run(append(webPushArgs, args...))
}