From 1af62a5bff72392bd10a7692e01479c79f8ddee4 Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Mon, 19 Oct 2026 12:55:14 -0700 Subject: [PATCH] Remove the fake push subscription after its system test The browser is shared across system tests, and a script added to every new document stays until it's removed, so later tests would have seen the granted permission and the fake subscription too. --- test/system/push_subscription_resync_test.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/test/system/push_subscription_resync_test.rb b/test/system/push_subscription_resync_test.rb index fab3682..459b0a6 100644 --- a/test/system/push_subscription_resync_test.rb +++ b/test/system/push_subscription_resync_test.rb @@ -9,6 +9,7 @@ class PushSubscriptionResyncTest < ApplicationSystemTestCase end teardown do + page.driver.browser.execute_cdp "Page.removeScriptToEvaluateOnNewDocument", identifier: @fake_subscription_script page.execute_script("localStorage.clear()") if page.current_url.start_with?("http") end @@ -40,15 +41,18 @@ class PushSubscriptionResyncTest < ApplicationSystemTestCase end private - # Headless Chrome has no push service, so stand in for the subscription the service worker would hold. + # Headless Chrome has no push service, so stand in for the subscription the service worker would hold. The + # browser is shared with other system tests, so teardown removes this again. def fake_a_browser_push_subscription - page.driver.browser.execute_cdp "Page.addScriptToEvaluateOnNewDocument", source: <<~JS - Object.defineProperty(Notification, "permission", { get: () => "granted" }) - const subscription = { - toJSON: () => ({ endpoint: "#{ENDPOINT}", keys: { p256dh: "p256dh-key", auth: "auth-key" } }), - unsubscribe: async () => { localStorage.setItem("unsubscribed-for-test", "true") } - } - navigator.serviceWorker.getRegistration = async () => ({ pushManager: { getSubscription: async () => subscription } }) + @fake_subscription_script = page.driver.browser.execute_cdp("Page.addScriptToEvaluateOnNewDocument", source: <<~JS)["identifier"] + (() => { + Object.defineProperty(Notification, "permission", { get: () => "granted" }) + const subscription = { + toJSON: () => ({ endpoint: "#{ENDPOINT}", keys: { p256dh: "p256dh-key", auth: "auth-key" } }), + unsubscribe: async () => { localStorage.setItem("unsubscribed-for-test", "true") } + } + navigator.serviceWorker.getRegistration = async () => ({ pushManager: { getSubscription: async () => subscription } }) + })() JS end