Opened 3 months ago

Closed 2 weeks ago

#37149 closed Cleanup/optimization (fixed)

Make CSP violation checks in selenium tests work for multiple browsers

Reported by: Varun Kasyap Pentamaraju Owned by: Varun Kasyap Pentamaraju
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Varun Kasyap Pentamaraju Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Several integration tests currently verify that no Content Security Policy (CSP) violations occurred by inspecting browser logs in tearDown():

in django\contrib\admin\tests.py:

    def tearDown(self):
        # Ensure that no CSP violations were logged in the browser.
        self.assertEqual(self.get_browser_logs(source="security"), [])

the current logic relying on get_browser_logs() to check no CSP violations:

    def get_browser_logs(self, source=None, level="ALL"):
        """
        Return Chrome console logs filtered by level and optionally source.
        """
        try:
            logs = self.selenium.get_log("browser")
        except AttributeError:
            logs = []
        return [
            log
            for log in logs
            if (level == "ALL" or log["level"] == level)
            and (source is None or log["source"] == source)
        ]

however, get_browser_logs() is only supported for chrome and being skipped for non-chrome browsers.

A browser-independent alternative would be to register a securitypolicyviolation event listener in the test page and collect violations in tearDown().

Change History (10)

comment:1 by Sarah Boyce, 3 months ago

Summary: Use securitypolicyviolation event listener in tearDown() to check CSP violations for integration testsMake CSP violation checks in selenium tests work for multiple browsers
Triage Stage: UnreviewedAccepted

Thank you

comment:2 by Varun Kasyap Pentamaraju, 3 months ago

Cc: Varun Kasyap Pentamaraju added

comment:3 by VIZZARD-X, 3 months ago

Owner: set to VIZZARD-X
Status: newassigned

comment:4 by VIZZARD-X, 2 months ago

Has patch: set
Last edited 2 months ago by VIZZARD-X (previous) (diff)

comment:5 by Sarah Boyce, 8 weeks ago

Patch needs improvement: set

comment:6 by VIZZARD-X, 8 weeks ago

Has patch: unset
Patch needs improvement: unset

PR closed for now. Blocked by the Playwright migration #37154. Will be reimplemented using page.add_init_script() once that lands.

comment:7 by Jacob Walls, 8 weeks ago

Triage Stage: AcceptedSomeday/Maybe

comment:8 by Jacob Walls, 3 weeks ago

Has patch: set
Triage Stage: Someday/MaybeReady for checkin

comment:9 by Jacob Walls, 2 weeks ago

Owner: changed from VIZZARD-X to Varun Kasyap Pentamaraju

The add_init_script() strategy is implemented in PR.

comment:10 by Sarah Boyce <42296566+sarahboyce@…>, 2 weeks ago

Resolution: fixed
Status: assignedclosed

In 464a5e18:

Fixed #37154, #37149 -- Switched from Selenium to Playwright for integration testing.

Note: See TracTickets for help on using tickets.
Back to Top