Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add futher tests for badwords plugin #679

Merged
merged 1 commit into from
Mar 17, 2024
Merged
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
2 changes: 1 addition & 1 deletion plugins/badwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def load_bad(db):
words.append(word)

new_regex = re.compile(
r"(\s|^|[^\w\s])({})(\s|$|[^\w\s])".format("|".join(words)),
r"(?:\s|^|[^\w\s])({})(?:\s|$|[^\w\s])".format("|".join(words)),
re.IGNORECASE,
)

Expand Down
122 changes: 122 additions & 0 deletions tests/plugin_tests/badwords_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
from unittest.mock import MagicMock, call

import pytest

from cloudbot.clients.irc import IrcClient
from plugins import badwords
from tests.util.mock_db import MockDB


@pytest.fixture()
def clear_bad_words():
badwords.badcache.clear()
yield
badwords.badcache.clear()


pytestmark = pytest.mark.usefixtures("clear_bad_words")


def test_add_bad(mock_db: MockDB):
badwords.table.create(mock_db.engine)
with mock_db.session() as session:
res = badwords.add_bad("foo #bar", "testnick", session)
assert res == "Current badwords: foo"
assert mock_db.get_data(badwords.table) == [
("foo", "testnick", "#bar"),
]


def test_del_bad(mock_db: MockDB):
badwords.table.create(mock_db.engine)
with mock_db.session() as session:
mock_db.load_data(
badwords.table,
[
{"word": "foo", "nick": "testnick", "chan": "#bar"},
],
)
badwords.load_bad(session)
res = badwords.del_bad("foo #bar", session)
assert res == "Removing foo new bad word list for #bar is: foo"
assert mock_db.get_data(badwords.table) == []


def test_check_badwords(mock_db: MockDB):
badwords.table.create(mock_db.engine)
with mock_db.session() as session:
mock_db.load_data(
badwords.table,
[
{"word": "foo", "nick": "testnick", "chan": "#bar"},
],
)

badwords.load_bad(session)
assert badwords.matcher.regex
assert badwords.badcache["#bar"] == ["foo"]
conn = MagicMock()
# conn.configure_mock()
conn.mock_add_spec(spec=IrcClient, spec_set=True)
message = MagicMock()
res = badwords.check_badwords(conn, message, "#bar", "foo", "user123")
assert res is None
assert message.mock_calls == [
call("user123, congratulations you've won!")
]
assert conn.mock_calls == [
call.cmd(
"KICK",
"#bar",
"user123",
"that fucking word is so damn offensive",
)
]


def test_check_badwords_wrong_channel(mock_db: MockDB):
badwords.table.create(mock_db.engine)
with mock_db.session() as session:
mock_db.load_data(
badwords.table,
[
{"word": "foo", "nick": "testnick", "chan": "#bar"},
],
)

badwords.load_bad(session)
assert badwords.matcher.regex
assert badwords.badcache["#bar"] == ["foo"]
conn = MagicMock()
# conn.configure_mock()
conn.mock_add_spec(spec=IrcClient, spec_set=True)
message = MagicMock()
res = badwords.check_badwords(conn, message, "#bar2", "foo", "user123")
assert res is None
assert message.mock_calls == []
assert conn.mock_calls == []


def test_check_badwords_no_match(mock_db: MockDB):
badwords.table.create(mock_db.engine)
with mock_db.session() as session:
mock_db.load_data(
badwords.table,
[
{"word": "foo", "nick": "testnick", "chan": "#bar"},
],
)

badwords.load_bad(session)
assert badwords.matcher.regex
assert badwords.badcache["#bar"] == ["foo"]
conn = MagicMock()
# conn.configure_mock()
conn.mock_add_spec(spec=IrcClient, spec_set=True)
message = MagicMock()
res = badwords.check_badwords(
conn, message, "#bar", "foobar", "user123"
)
assert res is None
assert message.mock_calls == []
assert conn.mock_calls == []