Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions sherlock_project/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
from colorama import Fore, Style
import webbrowser

# Global variable to count the number of results.
globvar = 0


class QueryNotify:
"""Query Notify Object.
Expand Down Expand Up @@ -132,6 +129,7 @@ def __init__(self, result=None, verbose=False, print_all=False, browse=False):
self.verbose = verbose
self.print_all = print_all
self.browse = browse
self._result_count = 0


def start(self, message):
Expand Down Expand Up @@ -169,9 +167,8 @@ def countResults(self):
Return Value:
The number of results by the time we call the function.
"""
global globvar
globvar += 1
return globvar
self._result_count += 1
return self._result_count

def update(self, result):
"""Notify Update.
Expand Down Expand Up @@ -258,7 +255,7 @@ def finish(self, message="The processing has been finished."):
Return Value:
Nothing.
"""
NumberOfResults = self.countResults() - 1
NumberOfResults = self._result_count

print(Style.BRIGHT + Fore.GREEN + "[" +
Fore.YELLOW + "*" +
Expand Down
53 changes: 53 additions & 0 deletions tests/test_notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Tests for QueryNotifyPrint result counter behaviour."""
import pytest
from sherlock_project.notify import QueryNotifyPrint
from sherlock_project.result import QueryResult, QueryStatus


def _make_result(status):
return QueryResult(
username="testuser",
site_name="TestSite",
site_url_user="https://testsite.com/testuser",
status=status,
)


class TestResultCounter:
def test_count_starts_at_zero(self):
notify = QueryNotifyPrint()
assert notify._result_count == 0

def test_count_increments_on_claimed(self):
notify = QueryNotifyPrint()
notify.update(_make_result(QueryStatus.CLAIMED))
notify.update(_make_result(QueryStatus.CLAIMED))
assert notify._result_count == 2

def test_count_does_not_increment_on_available(self):
notify = QueryNotifyPrint()
notify.update(_make_result(QueryStatus.AVAILABLE))
assert notify._result_count == 0

def test_independent_instances_do_not_share_count(self):
"""Two QueryNotifyPrint objects must not share a global counter."""
n1 = QueryNotifyPrint()
n2 = QueryNotifyPrint()
n1.update(_make_result(QueryStatus.CLAIMED))
n1.update(_make_result(QueryStatus.CLAIMED))
n1.update(_make_result(QueryStatus.CLAIMED))
# n2 was never updated — its count must still be 0
assert n2._result_count == 0

def test_second_username_count_is_not_cumulative(self):
"""Simulates two separate username scans using separate notify objects."""
notify1 = QueryNotifyPrint()
notify1.update(_make_result(QueryStatus.CLAIMED))
notify1.update(_make_result(QueryStatus.CLAIMED))

notify2 = QueryNotifyPrint()
notify2.update(_make_result(QueryStatus.CLAIMED))

# Each object tracks its own scan independently
assert notify1._result_count == 2
assert notify2._result_count == 1