Skip to content

Commit

Permalink
Pass message which contains URL as-is to iicmd.py, add tests
Browse files Browse the repository at this point in the history
I found case when URL was being ignored by bot due to the fact it was
being stripped. It seems to me this handling is no longer necessary.
  • Loading branch information
zstyblik committed Mar 30, 2024
1 parent 295e178 commit d370b81
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion iibot-ng
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ monitor()
export IICMD_BITLY_API_TOKEN="${bitly_api_token}"
exec "${ircdir}/iicmd.py" \
--nick "${nick}" \
--message "url ${msg#* }" \
--message "url ${msg#\!}" \
--ircd "${ircdir}" \
--network "${network}" \
--channel "${channel}" \
Expand Down
78 changes: 78 additions & 0 deletions tests/test_iicmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,84 @@ def test_cmd_url_title(rsp_text, expected_title, capsys, fixture_mock_requests):
assert mock_http_url.called is True


def test_cmd_url_two_links(capsys, fixture_mock_requests):
"""Test cmd_url() when there are multiple URLs in message.
Only the last URL should be matched.
"""
url = "https://www.example.org"
expected_title = "Little title"
expected_msg = "Title for {:s} - {:s}\n".format(url, expected_title)

rsp_text = (
"<html><head><title>Little title</title></head>" "<body></body></html>"
)
mock_http_url = fixture_mock_requests.get(url, text=rsp_text)

args = [
"./iicmd.py",
"--nick=irc_user",
"--message=url foo bar http://ignored.url {:s} message".format(url),
"--ircd=irc_ircd",
"--network=irc_network",
"--channel=irc_channel",
"--self=irc_botuser",
]
with patch.object(sys, "argv", args):
iicmd.main()

captured = capsys.readouterr()
assert captured.out == expected_msg
assert captured.err == ""

assert mock_http_url.called is True


@pytest.mark.parametrize(
"irc_message",
[
"url https://www.example.org",
"url https://www.example.org ~ here",
"url https://www.example.org <<< here",
"url bla bla https://www.example.org blablabla",
"url blabla https://www.example.org",
"url https://www.example.org also blabla",
],
)
def test_cmd_url_parse_url(irc_message, capsys, fixture_mock_requests):
"""Test extraction of URL in cmd_url() code path.
Based on bug in iibot-ng when URL would be 'parsed out' completely from the
message itself.
"""
url = "https://www.example.org"
expected_title = "Little title"
expected_msg = "Title for {:s} - {:s}\n".format(url, expected_title)

rsp_text = (
"<html><head><title>Little title</title></head>" "<body></body></html>"
)
mock_http_url = fixture_mock_requests.get(url, text=rsp_text)

args = [
"./iicmd.py",
"--nick=irc_user",
"--message={:s}".format(irc_message),
"--ircd=irc_ircd",
"--network=irc_network",
"--channel=irc_channel",
"--self=irc_botuser",
]
with patch.object(sys, "argv", args):
iicmd.main()

captured = capsys.readouterr()
assert captured.out == expected_msg
assert captured.err == ""

assert mock_http_url.called is True


@pytest.mark.parametrize(
"url,bitly_token,bitly_gid,bitly_rsp,bitly_called",
[
Expand Down

0 comments on commit d370b81

Please sign in to comment.