Adapt for online testing

When using tox, pass `-e offline` to exclude online tests.
When using pytest, pass `-m "not online"` to do the same.
This commit is contained in:
Paul Pfeister 2024-05-21 21:19:35 -04:00
parent d46775802e
commit 67258b58a4
No known key found for this signature in database
GPG Key ID: 70D33A96CBD7A994
4 changed files with 78 additions and 63 deletions

4
pytest.ini Normal file
View File

@ -0,0 +1,4 @@
[pytest]
addopts = --strict-markers
markers =
online: mark tests are requiring interest access.

View File

@ -19,6 +19,7 @@ def test_validate_manifest_against_local_schema():
validate(instance=jsondat, schema=schemadat) validate(instance=jsondat, schema=schemadat)
@pytest.mark.online
def test_validate_manifest_against_remote_schema(remote_schema): def test_validate_manifest_against_remote_schema(remote_schema):
"""Ensures that the manifest matches the remote schema, so as to not unexpectedly break clients.""" """Ensures that the manifest matches the remote schema, so as to not unexpectedly break clients."""
json_relative: str = '../sherlock/resources/data.json' json_relative: str = '../sherlock/resources/data.json'

View File

@ -19,77 +19,80 @@ def simple_query(sites_info: dict, site: str, username: str) -> QueryStatus:
)[site]['status'].status )[site]['status'].status
# Known positives should only use sites trusted to be reliable and unchanging @pytest.mark.online
@pytest.mark.parametrize('site,username',[ class TestLiveTargets:
('GitLab', 'ppfeister'), """Actively test probes against live and trusted targets"""
('AllMyLinks', 'blue'), # Known positives should only use sites trusted to be reliable and unchanging
]) @pytest.mark.parametrize('site,username',[
def test_known_positives_via_message(sites_info, site, username): ('GitLab', 'ppfeister'),
assert simple_query(sites_info=sites_info, site=site, username=username) is QueryStatus.CLAIMED ('AllMyLinks', 'blue'),
])
def test_known_positives_via_message(self, sites_info, site, username):
assert simple_query(sites_info=sites_info, site=site, username=username) is QueryStatus.CLAIMED
# Known positives should only use sites trusted to be reliable and unchanging # Known positives should only use sites trusted to be reliable and unchanging
@pytest.mark.parametrize('site,username',[ @pytest.mark.parametrize('site,username',[
('GitHub', 'ppfeister'), ('GitHub', 'ppfeister'),
('GitHub', 'sherlock-project'), ('GitHub', 'sherlock-project'),
('Docker Hub', 'ppfeister'), ('Docker Hub', 'ppfeister'),
('Docker Hub', 'sherlock'), ('Docker Hub', 'sherlock'),
]) ])
def test_known_positives_via_status_code(sites_info, site, username): def test_known_positives_via_status_code(self, sites_info, site, username):
assert simple_query(sites_info=sites_info, site=site, username=username) is QueryStatus.CLAIMED assert simple_query(sites_info=sites_info, site=site, username=username) is QueryStatus.CLAIMED
# Known positives should only use sites trusted to be reliable and unchanging # Known positives should only use sites trusted to be reliable and unchanging
@pytest.mark.parametrize('site,username',[ @pytest.mark.parametrize('site,username',[
('BodyBuilding', 'blue'), ('BodyBuilding', 'blue'),
('labpentestit', 'CSV'), ('labpentestit', 'CSV'),
]) ])
def test_known_positives_via_response_url(sites_info, site, username): def test_known_positives_via_response_url(self, sites_info, site, username):
assert simple_query(sites_info=sites_info, site=site, username=username) is QueryStatus.CLAIMED assert simple_query(sites_info=sites_info, site=site, username=username) is QueryStatus.CLAIMED
# Randomly generate usernames of high length and test for positive availability # Randomly generate usernames of high length and test for positive availability
# Randomly generated usernames should be simple alnum for simplicity and high # Randomly generated usernames should be simple alnum for simplicity and high
# compatibility. Several attempts may be made ~just in case~ a real username is # compatibility. Several attempts may be made ~just in case~ a real username is
# generated. # generated.
@pytest.mark.parametrize('site,random_len',[ @pytest.mark.parametrize('site,random_len',[
('GitLab', 255), ('GitLab', 255),
('Codecademy', 30) ('Codecademy', 30)
]) ])
def test_likely_negatives_via_message(sites_info, site, random_len): def test_likely_negatives_via_message(self, sites_info, site, random_len):
num_attempts: int = 3 num_attempts: int = 3
attempted_usernames: list[str] = [] attempted_usernames: list[str] = []
status: QueryStatus = QueryStatus.CLAIMED status: QueryStatus = QueryStatus.CLAIMED
for i in range(num_attempts): for i in range(num_attempts):
acceptable_types = string.ascii_letters + string.digits acceptable_types = string.ascii_letters + string.digits
random_handle = ''.join(random.choice(acceptable_types) for _ in range (random_len)) random_handle = ''.join(random.choice(acceptable_types) for _ in range (random_len))
attempted_usernames.append(random_handle) attempted_usernames.append(random_handle)
status = simple_query(sites_info=sites_info, site=site, username=random_handle) status = simple_query(sites_info=sites_info, site=site, username=random_handle)
if status is QueryStatus.AVAILABLE: if status is QueryStatus.AVAILABLE:
break break
assert status is QueryStatus.AVAILABLE, f"Could not validate available username after {num_attempts} attempts with randomly generated usernames {attempted_usernames}." assert status is QueryStatus.AVAILABLE, f"Could not validate available username after {num_attempts} attempts with randomly generated usernames {attempted_usernames}."
# Randomly generate usernames of high length and test for positive availability # Randomly generate usernames of high length and test for positive availability
# Randomly generated usernames should be simple alnum for simplicity and high # Randomly generated usernames should be simple alnum for simplicity and high
# compatibility. Several attempts may be made ~just in case~ a real username is # compatibility. Several attempts may be made ~just in case~ a real username is
# generated. # generated.
@pytest.mark.parametrize('site,random_len',[ @pytest.mark.parametrize('site,random_len',[
('GitHub', 39), ('GitHub', 39),
('Docker Hub', 30) ('Docker Hub', 30)
]) ])
def test_likely_negatives_via_status_code(sites_info, site, random_len): def test_likely_negatives_via_status_code(self, sites_info, site, random_len):
num_attempts: int = 3 num_attempts: int = 3
attempted_usernames: list[str] = [] attempted_usernames: list[str] = []
status: QueryStatus = QueryStatus.CLAIMED status: QueryStatus = QueryStatus.CLAIMED
for i in range(num_attempts): for i in range(num_attempts):
acceptable_types = string.ascii_letters + string.digits acceptable_types = string.ascii_letters + string.digits
random_handle = ''.join(random.choice(acceptable_types) for _ in range (random_len)) random_handle = ''.join(random.choice(acceptable_types) for _ in range (random_len))
attempted_usernames.append(random_handle) attempted_usernames.append(random_handle)
status = simple_query(sites_info=sites_info, site=site, username=random_handle) status = simple_query(sites_info=sites_info, site=site, username=random_handle)
if status is QueryStatus.AVAILABLE: if status is QueryStatus.AVAILABLE:
break break
assert status is QueryStatus.AVAILABLE, f"Could not validate available username after {num_attempts} attempts with randomly generated usernames {attempted_usernames}." assert status is QueryStatus.AVAILABLE, f"Could not validate available username after {num_attempts} attempts with randomly generated usernames {attempted_usernames}."
def test_username_illegal_regex(sites_info): def test_username_illegal_regex(sites_info):

View File

@ -21,6 +21,13 @@ commands =
coverage run --source=sherlock --module pytest -v coverage run --source=sherlock --module pytest -v
coverage report --show-missing coverage report --show-missing
[testenv:offline]
deps =
jsonschema
pytest
commands =
pytest -v -m "not online"
[testenv:lint] [testenv:lint]
description = Lint with Ruff description = Lint with Ruff
deps = deps =