Onboarding step 3 push opt-in and test-push endpoint (Feature 19) (#168)
CI / ci (push) Successful in 7m17s
CI / ci (push) Successful in 7m17s
Co-authored-by: orchestrator-agent <20+orchestrator-agent@code.cubecraftcreations.com>
This commit was merged in pull request #168.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/notify"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/push"
|
||||
)
|
||||
|
||||
@@ -85,6 +86,33 @@ func deletePushSubscription(logger *slog.Logger, svc *push.Service) http.Handler
|
||||
}
|
||||
}
|
||||
|
||||
// postTestPush answers POST /settings/notifications/test-push: it queues
|
||||
// push.TestTitle for every device that turned on push, aimed at /plan, and
|
||||
// answers 202 with the event key and how many subscriptions it goes to.
|
||||
// Delivery follows through the notification outbox. With push switched
|
||||
// off for plan_ready it is 409 push_off, whether or not any device has
|
||||
// turned on push; otherwise with no subscription it is 409
|
||||
// no_subscriptions. Neither sends anything. It never posts to Google Chat.
|
||||
func postTestPush(logger *slog.Logger, svc *push.Service) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
sent, err := svc.SendTest(r.Context())
|
||||
switch {
|
||||
case errors.Is(err, push.ErrNoSubscriptions):
|
||||
writeError(w, r, logger, http.StatusConflict, "no_subscriptions",
|
||||
"no device has turned on notifications, so there is nothing to send the test to")
|
||||
case errors.Is(err, notify.ErrPushOff):
|
||||
writeError(w, r, logger, http.StatusConflict, "push_off",
|
||||
"push is switched off for New weekly plan ready in Notifications settings, so no test was sent")
|
||||
case err != nil:
|
||||
logger.ErrorContext(r.Context(), "send test push", "error", err)
|
||||
writeError(w, r, logger, http.StatusInternalServerError, "internal", "internal error")
|
||||
default:
|
||||
logger.InfoContext(r.Context(), "test push queued", "event_key", sent.EventKey, "subscriptions", sent.Subscriptions)
|
||||
writeJSON(w, r, logger, http.StatusAccepted, sent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// writePushError maps a push.Service error to its API error: an invalid
|
||||
// subscription is 400 invalid_subscription, anything else 500.
|
||||
func writePushError(w http.ResponseWriter, r *http.Request, logger *slog.Logger, action string, err error) {
|
||||
|
||||
@@ -14,6 +14,8 @@ import (
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/notify"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/notify/notifytest"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/push"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/store"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/vapid"
|
||||
@@ -22,6 +24,7 @@ import (
|
||||
const (
|
||||
vapidPublicKeyPath = "/api/v1/push/vapid-public-key"
|
||||
pushSubscriptionsPath = "/api/v1/push/subscriptions"
|
||||
testPushPath = "/api/v1/settings/notifications/test-push"
|
||||
testPushEndpoint = "https://updates.push.services.mozilla.com/wpush/v2/gAAAAABk"
|
||||
)
|
||||
|
||||
@@ -41,7 +44,7 @@ func newTestPushWith(t *testing.T) (*push.Service, *store.Store, vapid.KeyPair)
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := newTestStore(t)
|
||||
svc, err := push.NewService(st, pair.PublicKey)
|
||||
svc, err := push.NewService(st, pair.PublicKey, notifytest.New(t, st))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -282,3 +285,59 @@ func mustP256dh(t *testing.T) string {
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(k.PublicKey().Bytes())
|
||||
}
|
||||
|
||||
func TestTestPushAPI(t *testing.T) {
|
||||
t.Parallel()
|
||||
svc, st, _ := newTestPushWith(t)
|
||||
router := newPushRouter(t, svc)
|
||||
|
||||
// With push off for plan_ready and no device subscribed, push off is
|
||||
// what the test says.
|
||||
if _, err := st.Writer.Exec(`UPDATE notification_prefs SET push = 0 WHERE event = 'plan_ready'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec, body := serve(t, router, signedIn(http.MethodPost, testPushPath, nil))
|
||||
if rec.Code != http.StatusConflict || body["error"].(map[string]any)["code"] != "push_off" {
|
||||
t.Errorf("POST with push off and no subscriptions = %d %s, want 409 push_off", rec.Code, rec.Body)
|
||||
}
|
||||
if _, err := st.Writer.Exec(`UPDATE notification_prefs SET push = 1 WHERE event = 'plan_ready'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// With no device subscribed there is nothing to test.
|
||||
rec, body = serve(t, router, signedIn(http.MethodPost, testPushPath, nil))
|
||||
if rec.Code != http.StatusConflict || body["error"].(map[string]any)["code"] != "no_subscriptions" {
|
||||
t.Errorf("POST with no subscriptions = %d %s, want 409 no_subscriptions", rec.Code, rec.Body)
|
||||
}
|
||||
|
||||
for _, ep := range []string{testPushEndpoint, testPushEndpoint + "-phone"} {
|
||||
req := signedIn(http.MethodPost, pushSubscriptionsPath, strings.NewReader(subscriptionJSON(t, ep, "")))
|
||||
if rec, _ := serve(t, router, req); rec.Code != http.StatusOK {
|
||||
t.Fatalf("POST subscription = %d %s", rec.Code, rec.Body)
|
||||
}
|
||||
}
|
||||
rec, body = serve(t, router, signedIn(http.MethodPost, testPushPath, nil))
|
||||
key, _ := body["event_key"].(string)
|
||||
if rec.Code != http.StatusAccepted || !strings.HasPrefix(key, "plan_ready:test-") || body["subscriptions"] != float64(2) {
|
||||
t.Fatalf("POST test-push = %d %s, want 202 with a plan_ready:test- key and 2 subscriptions", rec.Code, rec.Body)
|
||||
}
|
||||
// One push row, none for the household's Google Chat space, opening /plan.
|
||||
want := []notifytest.Row{{Key: key, Channel: notify.ChannelPush, Payload: notify.Payload{
|
||||
Event: notify.PlanReady, Title: "MealQuest test notification", Body: push.TestBody, URL: "/plan", Tag: key,
|
||||
}}}
|
||||
if got := notifytest.Rows(t, st, notify.PlanReady); len(got) != 1 || got[0] != want[0] {
|
||||
t.Errorf("outbox = %+v, want %+v", got, want)
|
||||
}
|
||||
|
||||
// With push off for plan_ready, the test says so and sends nothing.
|
||||
if _, err := st.Writer.Exec(`UPDATE notification_prefs SET push = 0 WHERE event = 'plan_ready'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec, body = serve(t, router, signedIn(http.MethodPost, testPushPath, nil))
|
||||
if rec.Code != http.StatusConflict || body["error"].(map[string]any)["code"] != "push_off" {
|
||||
t.Errorf("POST with push off = %d %s, want 409 push_off", rec.Code, rec.Body)
|
||||
}
|
||||
if got := notifytest.Rows(t, st, notify.PlanReady); len(got) != 1 {
|
||||
t.Errorf("outbox after push off = %+v, want still one row", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,8 +60,9 @@ type RouteMounter interface {
|
||||
// /api/v1/integrations/google and its /connect, /callback and /calendars,
|
||||
// PUT /api/v1/integrations/google/calendar and DELETE
|
||||
// /api/v1/integrations/google; pushSvc serves GET
|
||||
// /api/v1/push/vapid-public-key and POST and DELETE
|
||||
// /api/v1/push/subscriptions; runner serves
|
||||
// /api/v1/push/vapid-public-key, POST and DELETE
|
||||
// /api/v1/push/subscriptions and POST
|
||||
// /api/v1/settings/notifications/test-push; runner serves
|
||||
// POST /api/v1/jobs and GET /api/v1/jobs/{id}; photos serves
|
||||
// GET /media/photos/{id}-{size}.jpg; assets is the web build
|
||||
// served for every other GET. /health, /auth and the web build are public.
|
||||
@@ -133,6 +134,7 @@ func NewRouter(logger *slog.Logger, version string, trusted forwarded.Trusted, a
|
||||
apiV1.Patch("/settings/household", patchHouseholdSettings(logger, settings))
|
||||
apiV1.Get("/settings/notifications", getNotificationSettings(logger, settings))
|
||||
apiV1.Put("/settings/notifications", putNotificationSettings(logger, settings))
|
||||
apiV1.Post("/settings/notifications/test-push", postTestPush(logger, pushSvc))
|
||||
apiV1.Get("/settings/recipe-search", getRecipeSearchSettings(logger, search))
|
||||
apiV1.Patch("/settings/recipe-search", patchRecipeSearchSettings(logger, search))
|
||||
apiV1.Post("/settings/recipe-search/test", postRecipeSearchTest(logger, search))
|
||||
|
||||
Reference in New Issue
Block a user