Repository navigation
Expand file tree
/
Copy pathtest_https.py
More file actions
41 lines (29 loc) · 1.57 KB
/
Copy pathtest_https.py
File metadata and controls
41 lines (29 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Tests for HTTPS enforcement / HSTS checks."""
from sessionguard.checks.https import check_https
from sessionguard.models import ScanResult
class FakeResponse:
def __init__(self, url, headers=None):
self.url = url
self.headers = headers or {}
def test_https_requested_directly_passes():
result = ScanResult(target="https://example.test")
check_https("https://example.test", FakeResponse("https://example.test"), result)
assert "https-enforcement" not in [f.check for f in result.failed]
def test_http_redirected_to_https_passes():
result = ScanResult(target="http://example.test")
check_https("http://example.test", FakeResponse("https://example.test"), result)
assert "https-enforcement" not in [f.check for f in result.failed]
def test_http_without_redirect_fails():
result = ScanResult(target="http://example.test")
check_https("http://example.test", FakeResponse("http://example.test"), result)
assert "https-enforcement" in [f.check for f in result.failed]
def test_hsts_present_passes():
result = ScanResult(target="https://example.test")
response = FakeResponse("https://example.test", {"Strict-Transport-Security": "max-age=31536000"})
check_https("https://example.test", response, result)
assert "hsts-header" not in [f.check for f in result.failed]
def test_hsts_missing_flagged():
result = ScanResult(target="https://example.test")
response = FakeResponse("https://example.test", {})
check_https("https://example.test", response, result)
assert "hsts-header" in [f.check for f in result.failed]