Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
test: add coverage for onion-location meta tag retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Sep 17, 2020
1 parent 56dc2b5 commit e6bad08
Showing 1 changed file with 86 additions and 13 deletions.
99 changes: 86 additions & 13 deletions sites/management/commands/tests/test_scan.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from sites.management.commands.scan import is_onion_available
import requests
from unittest import mock

from sites.management.commands.scan import (is_onion_available,
is_onion_loc_in_meta_tag)
from django.test import TestCase


META_TAG = '<meta http-equiv="onion-location" content="http://myonion.onion">'


class TestScan(TestCase):
def test_invalid_onion_available_over_http(self):
@mock.patch('sites.management.commands.scan.is_onion_loc_in_meta_tag',
return_value=False)
def test_invalid_onion_available_over_http(self, mock_onion):
"""Simulates a HTTP site with the onion location header,
which is invalid: Onion-Location is ignored if not served
over HTTPS
Expand All @@ -14,19 +23,23 @@ def test_invalid_onion_available_over_http(self):
"headers": {
"onion-location": "https://www.foobar.onion/",
},
"url": "http://example.com"
},
"https": {"headers": {}},
"httpswww": {"headers": {}},
"https": {"headers": {}, "url": "https://example.com"},
"httpswww": {"headers": {}, "url": "https://www.example.com"},
"httpwww": {
"headers": {
"onion-location": "https://www.foobar.onion/",
"url": "http://www.example.com",
},
},
}
}
assert not is_onion_available(http_onion_available_pshtt)
assert is_onion_available(http_onion_available_pshtt) is False

def test_onion_available_over_https(self):
@mock.patch('sites.management.commands.scan.is_onion_loc_in_meta_tag',
return_value=False)
def test_onion_available_over_https(self, mock_onion):
"""Simulates a HTTPS site with the onion location header"""
https_onion_available_pshtt = {
"endpoints": {
Expand Down Expand Up @@ -54,14 +67,74 @@ def test_onion_available_over_https(self):
}
assert is_onion_available(https_onion_available_pshtt)

def test_no_onion_available_over_https(self):
"""Simulates a HTTPS site without the Onion-Location header"""
@mock.patch('sites.management.commands.scan.is_onion_loc_in_meta_tag',
return_value=False)
def test_no_onion_available_over_https(self, mock_onion):
"""
Simulates a HTTPS site without the Onion-Location either in the
header or the meta tag
"""
https_onion_not_available_pshtt = {
"endpoints": {
"http": {"headers": {}},
"https": {"headers": {}},
"httpswww": {"headers": {}},
"httpwww": {"headers": {}},
"http": {"headers": {}, "url": "http://example.com"},
"https": {"headers": {}, "url": "https://example.com"},
"httpswww": {"headers": {}, "url": "https://www.example.com"},
"httpwww": {"headers": {}, "url": "http://www.example.com"},
}
}
assert not is_onion_available(https_onion_not_available_pshtt)
assert is_onion_available(https_onion_not_available_pshtt) is False

@mock.patch('sites.management.commands.scan.is_onion_loc_in_meta_tag',
return_value=True)
def test_onion_available_over_https_meta_tag(self, mock_onion):
"""
Simulates a HTTPS site with the Onion-Location header provided
only in the meta tag
"""
https_onion_not_available_in_header_pshtt = {
"endpoints": {
"http": {"headers": {}, "url": "http://example.com"},
"https": {"headers": {}, "url": "https://example.com"},
"httpswww": {"headers": {}, "url": "https://www.example.com"},
"httpwww": {"headers": {}, "url": "http://www.example.com"},
}
}
assert is_onion_available(https_onion_not_available_in_header_pshtt)

def test_is_onion_loc_in_meta_tag_found(self):
"""
Check we return True if the onion-location meta tag is found.
"""
url = "example.com"

resp = mock.MagicMock()
type(resp).content = mock.PropertyMock(return_value=META_TAG)

with mock.patch('requests.get', return_value=resp):
assert is_onion_loc_in_meta_tag(url)

def test_is_onion_loc_in_meta_tag_not_found(self):
"""
Check we return False if no onion-location meta tag is found.
"""
url = "example.com"

resp = mock.MagicMock()
type(resp).content = mock.PropertyMock(return_value="<html></html>")

with mock.patch('requests.get', return_value=resp):
assert is_onion_loc_in_meta_tag(url) is False

def test_is_onion_loc_in_meta_tag_error(self):
"""
Check we return None if the scan fails.
"""
url = "example.com"

resp = mock.MagicMock()
type(resp).content = mock.PropertyMock(
side_effect=requests.exceptions.RequestException()
)

with mock.patch('requests.get', return_value=resp):
assert is_onion_loc_in_meta_tag(url) is None

0 comments on commit e6bad08

Please sign in to comment.