Skip to content

Commit

Permalink
Fixed majority of Unit (non CI) tests to run green based on current f…
Browse files Browse the repository at this point in the history
…unctionality
  • Loading branch information
telnetdoogie committed Jan 9, 2025
1 parent 2af3940 commit f1c772f
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 170 deletions.
17 changes: 11 additions & 6 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ def app(mocker):
config["SCREENED_ACTIONS"] = ("answer", "greeting", "record_message")
config["SCREENED_RINGS_BEFORE_ANSWER"] = 0
config["PERMITTED_ACTIONS"] = ("ignore",)
config["PERMITTED_RINGS_BEFORE_ANSWER"] = 4
config["PERMITTED_RINGS_BEFORE_ANSWER"] = 0
config["SREENING_MODE"] = ("whitelist", "blacklist")

# Mock the hardware interfaces
mocker.patch("hardware.modem.Modem._open_serial_port", return_value=True)
mocker.patch("hardware.modem.Modem.start", return_value=True)
mocker.patch("hardware.modem.Modem.stop", return_value=True)
mocker.patch("hardware.modem.Modem.pick_up", return_value=True)
mocker.patch("hardware.modem.Modem.hang_up", return_value=True)
mocker.patch("hardware.modem.Modem.play_audio", return_value=True)
Expand All @@ -85,6 +87,9 @@ def app(mocker):
mocker.patch("hardware.indicators.RingIndicator.blink")
mocker.patch("hardware.indicators.RingIndicator.close")

# Mock signal.signal to avoid errors in test threads
mocker.patch("signal.signal", return_value=None)

def mock_is_whitelisted(caller):
if caller["NAME"] in ["CALLER1", "CALLER3"]:
return (True, "whitelisted")
Expand Down Expand Up @@ -175,12 +180,12 @@ def test_ignore_permitted(app):
ignore_call_called = False

app.handle_caller(caller1) # Queue a permitted caller with 4 rings
time.sleep(15)
time.sleep(2)

assert ignore_call_called

# Stop the run thread
app._stop_event.set()
app.set_stop_flag()


def test_answer_blocked(app):
Expand All @@ -199,7 +204,7 @@ def test_answer_blocked(app):
assert answer_call_called

# Stop the run thread
app._stop_event.set()
app.set_stop_flag()


def test_answer_screened(app):
Expand All @@ -218,7 +223,7 @@ def test_answer_screened(app):
assert answer_call_called

# Stop the run thread
app._stop_event.set()
app.set_stop_flag()


def test_queued_call(app):
Expand All @@ -245,7 +250,7 @@ def test_queued_call(app):
assert answer_call_called

# Stop the run thread
app._stop_event.set()
app.set_stop_flag()


def test_answer_call_no_actions(app, mocker):
Expand Down
104 changes: 45 additions & 59 deletions tests/test_blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@
# SOFTWARE.

import sqlite3
from pprint import pprint

import pytest

from callattendant.screening.blacklist import Blacklist

BRUCE = {"NAME": "Bruce", "NMBR": "1234567890", "DATE": "1012", "TIME": "0600"}

@pytest.fixture(scope='module')
def blacklist():

@pytest.fixture(scope='function')
def blacklist():
# Create the test db in RAM
db = sqlite3.connect(":memory:")

# Mock the application config, which is a dict-based object
config = {}
config['DEBUG'] = True
config['DEBUG'] = False
config['TESTING'] = True

# Create the blacklist to be tested
Expand All @@ -48,68 +48,54 @@ def blacklist():
return blacklist


def test_add_caller(blacklist):
# Add a record
callerid = {"NAME": "Bruce", "NMBR": "1234567890", "DATE": "1012", "TIME": "0600", }
assert blacklist.add_caller(callerid, "Test")
def test_can_add_caller_to_blacklist(blacklist):
assert blacklist.add_caller(BRUCE, "Test")


def test_check_number(blacklist):
number = "1234567890"
def test_adding_duplicate_fails(blacklist):
assert blacklist.add_caller(BRUCE, "Test Add")
assert blacklist.add_caller(BRUCE, "Test Update") is False

is_blacklisted, reason = blacklist.check_number(number)

assert is_blacklisted
assert reason == "Test"
def test_check_number_returns_correct_reason(blacklist):
test_reason = "SpecificReason"
blacklist.add_caller(BRUCE, test_reason)
is_blacklisted, (reason, name) = blacklist.check_number(BRUCE.get("NMBR"))

number = "1111111111"
assert is_blacklisted is True
assert reason == test_reason

is_blacklisted, reason = blacklist.check_number(number)

def test_check_number_returns_none_if_not_present(blacklist):
blacklist.add_caller(BRUCE, "Test Reason")
test_number = "1111111111"
is_blacklisted, reason = blacklist.check_number(test_number)
assert not is_blacklisted


def test_get_number(blacklist):
number = "1234567890"

caller = blacklist.get_number(number)
pprint(caller)

assert caller[0][0] == number


def test_multiple(blacklist):
new_caller = {"NAME": "New Caller", "NMBR": "12312351234", "DATE": "1012", "TIME": "0600"}
number = new_caller["NMBR"]
name = new_caller["NAME"]
reason = "Test"
pprint(new_caller)

assert blacklist.add_caller(new_caller, reason)

caller = blacklist.get_number(number)
pprint(caller)

assert caller[0][0] == number
assert caller[0][1] == name
assert caller[0][2] == reason

name = "Joe"
reason = "Confirm"

assert blacklist.update_number(number, name, reason)

caller = blacklist.get_number(number)
pprint(caller)

assert caller[0][0] == number
assert caller[0][1] == name
assert caller[0][2] == reason

assert blacklist.remove_number(number)

is_blacklisted, reason = blacklist.check_number(number)
assert not is_blacklisted

caller = blacklist.get_number(number)
pprint(caller)
def test_get_number_returns_entry(blacklist):
test_reason = "ABC12345"
blacklist.add_caller(BRUCE, test_reason)
caller = blacklist.get_number(BRUCE.get("NMBR"))
assert caller[0][0] == BRUCE.get("NMBR")
assert caller[0][1] == BRUCE.get("NAME")
assert caller[0][2] == test_reason


def test_update_number(blacklist):
test_reason = "ABC12345"
new_reason = "XYZ98765"
new_name = "Joe"
blacklist.add_caller(BRUCE, test_reason)
blacklist.update_number(BRUCE.get("NMBR"), new_name, new_reason)
caller = blacklist.get_number(BRUCE.get("NMBR"))
assert caller[0][0] == BRUCE.get("NMBR")
assert caller[0][1] == new_name
assert caller[0][2] == new_reason


def test_removing_number(blacklist):
blacklist.add_caller(BRUCE, "Test Reason")
assert len(blacklist.get_number(BRUCE.get("NMBR"))) == 1
blacklist.remove_number(BRUCE.get("NMBR"))
assert len(blacklist.get_number(BRUCE.get("NMBR"))) == 0
4 changes: 2 additions & 2 deletions tests/test_calllogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from callattendant.screening.calllogger import CallLogger


@pytest.fixture(scope='module')
@pytest.fixture(scope='function')
def calllogger():

# Create the test db in RAM
Expand All @@ -52,7 +52,7 @@ def test_add_caller(calllogger):
callerid = {
"NAME": "Bruce",
"NMBR": "1234567890",
"DATE": "1012",
"DATE": "10122023",
"TIME": "0600",
}

Expand Down
39 changes: 22 additions & 17 deletions tests/test_callscreener.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from callattendant.config import Config
from callattendant.screening.callscreener import CallScreener


# Create a blocked caller
caller1 = {"NAME": "CALLER1", "NMBR": "1234567890", "DATE": "1012", "TIME": "0600"}
# Create a permitted caller
Expand All @@ -44,36 +43,35 @@
caller6 = {"NAME": "JOHN DOE", "NMBR": "0987654321", "DATE": "1012", "TIME": "0600"}
# Create a unique number (permitted number pattern match)
caller7 = {"NAME": "CALLER7", "NMBR": "09876543210", "DATE": "1012", "TIME": "0600"}
# ShouldIAnswer Spam
caller8 = {"NAME": "CALLER8", "NMBR": "8554188397", "DATE": "1012", "TIME": "0600"}


@pytest.fixture(scope='module')
@pytest.fixture(scope='function')
def screener():

# Create the test db in RAM
db = sqlite3.connect(":memory:")

# Create a config object with default settings
config = Config()
config['DEBUG'] = True
config['TESTING'] = True
config['BLOCK_NAME_PATTERNS'] = {
"V[0-9]{15}": "Telemarketer Caller ID",
}
config['BLOCK_NUMBER_PATTERNS'] = {
"P": "Private number",
}
config['PERMIT_NAME_PATTERNS'] = {
".*DOE": "Anyone",
}
config['PERMIT_NUMBER_PATTERNS'] = {
"987654": "Anyone",
}

config['BLOCK_SERVICE_THRESHOLD'] = 1
config['BLOCK_SERVICE'] = "NOMOROBO"
# Create the blacklist to be tested
screener = CallScreener(db, config)

screener.config['CALLERID_PATTERNS'] = {
'blocknames': {'V[0-9]{15}': "Telemarketer Caller ID"},
'blocknumbers': {'P': "Private Number"},
'permitnames': {'.*DOE': "Anyone"},
'permitnumbers': {'987654': "Anyone"}
}
# Add a record to the blacklist
screener._blacklist.add_caller(caller1)
screener.blacklist_caller(caller1, "Test Blacklist")
# Add a record to the whitelist
screener._whitelist.add_caller(caller2)
screener.whitelist_caller(caller2, "Test Whitelist")

return screener

Expand Down Expand Up @@ -104,10 +102,17 @@ def test_blocked_name_pattern(screener):


def test_is_blacklisted_by_nomorobo(screener):
screener.config['BLOCK_SERVICE'] = "NOMOROBO"
is_blacklisted, reason = screener.is_blacklisted(caller4)
assert is_blacklisted, "caller4 should be blocked by nomorobo"


def test_is_blacklisted_by_shouldianswer(screener):
screener.config['BLOCK_SERVICE'] = "SHOULDIANSWER"
is_blacklisted, reason = screener.is_blacklisted(caller8)
assert is_blacklisted, "caller8 should be blocked by nomorobo"


def test_blocked_number_pattern(screener):
is_blacklisted, reason = screener.is_blacklisted(caller5)
assert is_blacklisted, "caller1 should be blocked by number pattern"
Expand Down
35 changes: 21 additions & 14 deletions tests/test_nomorobo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,54 @@

from pprint import pprint

import pytest

from callattendant.screening.nomorobo import NomoroboService


def test_5622862616_should_score_1():
@pytest.fixture(scope='function')
def nomorobo():
nomorobo = NomoroboService()
return nomorobo


def test_5622862616_should_score_1(nomorobo):
result = nomorobo.lookup_number("5622862616")
pprint(result)
assert result["score"] == 1

def test_8886727156_should_be_spam():
nomorobo = NomoroboService()

def test_8886727156_should_be_spam(nomorobo):
result = nomorobo.lookup_number("8886727156")
pprint(result)
assert result["spam"] is True

def test_8886727156_should_score_2():
nomorobo = NomoroboService()

def test_8886727156_should_score_2(nomorobo):
result = nomorobo.lookup_number("8886727156")
pprint(result)
assert result["score"] == 2

def test_404_not_marked_as_spam():
nomorobo = NomoroboService()

def test_404_not_marked_as_spam(nomorobo):
result = nomorobo.lookup_number("1234567890")
pprint(result)
assert result["spam"] is False

def test_9725551356_is_unknown():
nomorobo = NomoroboService()

def test_9725551356_is_unknown(nomorobo):
result = nomorobo.lookup_number("9725551356")
pprint(result)
assert result["score"] == 0

def test_9725551356_is_not_spam():
nomorobo = NomoroboService()

def test_9725551356_is_not_spam(nomorobo):
result = nomorobo.lookup_number("9725551356")
pprint(result)
assert result["spam"] is False

def test_8554188397_should_score_2():
nomorobo = NomoroboService()

def test_8554188397_should_score_2(nomorobo):
result = nomorobo.lookup_number("8554188397")
pprint(result)
assert result["score"] == 2
assert result["score"] == 2
Loading

0 comments on commit f1c772f

Please sign in to comment.