File: test_contrib.py

package info (click to toggle)
python-django-csp 3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: python: 935; makefile: 135; sh: 6
file content (23 lines) | stat: -rw-r--r-- 706 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django.http import HttpResponse
from django.test import RequestFactory
from django.test.utils import override_settings

from csp.contrib.rate_limiting import RateLimitedCSPMiddleware
from csp.tests.utils import response

HEADER = "Content-Security-Policy"
mw = RateLimitedCSPMiddleware(response())
rf = RequestFactory()


@override_settings(CSP_REPORT_PERCENTAGE=0.1, CSP_REPORT_URI="x")
def test_report_percentage():
    times_seen = 0
    for _ in range(5000):
        request = rf.get("/")
        response = HttpResponse()
        mw.process_response(request, response)
        if "report-uri" in response[HEADER]:
            times_seen += 1
    # Roughly 10%
    assert 400 <= times_seen <= 600