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.

From #371.

(cherry picked from commit 1af62a5bff)
This commit is contained in:
Jeremy Daer committed 2026-10-19 12:55:16 -07:00
1 parent 93718f7408
commit 931f6b2c38
1 file changed
+12 -8
+12 -8
View File
@@ -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