cm_bot_v2/tests/test_bot_cli.py

313 lines
13 KiB
Python

"""Tests for the bot CLI (app.bot_cli).
The CLI mirrors the Telegram bot's manual-trigger surface (Telegram
handlers /1, /2, /3) plus the operational ops (credit, transfer,
monitor-once). With no args, it drops into a stdlib TUI menu.
These tests mock app.bot_cli.CM_BOT_HAL so they never touch the database
or cm99.net. The HAL class is imported at module load (which is a pure
import — no env reads), so we can patch the symbol bound on app.bot_cli
without affecting other tests.
"""
import argparse
import contextlib
import io
import os
import sys
import unittest
from unittest import mock
import app.bot_cli as bot_cli
class ParserSanityTests(unittest.TestCase):
def test_build_parser_returns_argument_parser(self):
parser = bot_cli.build_parser()
self.assertIsInstance(parser, argparse.ArgumentParser)
def test_main_with_no_args_dispatches_to_interactive(self):
# When invoked with no subcommand, main() should drop into the
# TUI loop. We verify the dispatch by patching cmd_interactive to
# a no-op recorder.
with mock.patch.object(bot_cli, "cmd_interactive", return_value=0) as mocked:
rc = bot_cli.main([])
mocked.assert_called_once()
self.assertEqual(rc, 0)
class CmdRegisterTests(unittest.TestCase):
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_prints_username_password_link(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.get_user_api.return_value = {
"username": "13c1234",
"password": "abc12345",
"link": "https://example.com/r/foo",
}
out = io.StringIO()
with contextlib.redirect_stdout(out):
bot_cli.cmd_register(argparse.Namespace())
text = out.getvalue()
self.assertIn("Username: 13c1234", text)
self.assertIn("Password: abc12345", text)
self.assertIn("Link: https://example.com/r/foo", text)
mock_hal.get_user_api.assert_called_once_with()
def test_register_subparser_dispatches_to_cmd_register(self):
parser = bot_cli.build_parser()
args = parser.parse_args(["register"])
self.assertIs(args.func, bot_cli.cmd_register)
def test_get_acc_alias_dispatches_to_cmd_register(self):
parser = bot_cli.build_parser()
args = parser.parse_args(["get-acc"])
self.assertIs(args.func, bot_cli.cmd_register)
class CmdSetPinTests(unittest.TestCase):
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_rejects_invalid_whatsapp_url(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.is_whatsapp_url.return_value = False
with self.assertRaises(SystemExit) as cm:
bot_cli.cmd_set_pin(argparse.Namespace(link="https://not-whatsapp.example/x"))
self.assertEqual(cm.exception.code, 2)
mock_hal.set_security_pin_api.assert_not_called()
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_extracts_names_locally_and_succeeds(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.is_whatsapp_url.return_value = True
mock_hal.get_whatsapp_link_username.return_value = ("t_user_42", "f_user_42")
mock_hal.set_security_pin_api.return_value = True
out = io.StringIO()
with contextlib.redirect_stdout(out):
bot_cli.cmd_set_pin(argparse.Namespace(link="https://chat.whatsapp.com/abc"))
text = out.getvalue()
self.assertIn("f_username=f_user_42", text)
self.assertIn("t_username=t_user_42", text)
mock_hal.set_security_pin_api.assert_called_once_with("https://chat.whatsapp.com/abc")
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_falsy_set_security_pin_result_exits_nonzero(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.is_whatsapp_url.return_value = True
mock_hal.get_whatsapp_link_username.return_value = ("t", "f")
mock_hal.set_security_pin_api.return_value = False
with self.assertRaises(SystemExit) as cm:
bot_cli.cmd_set_pin(argparse.Namespace(link="https://chat.whatsapp.com/abc"))
self.assertEqual(cm.exception.code, 1)
def test_set_pin_subparser_dispatches(self):
parser = bot_cli.build_parser()
args = parser.parse_args(["set-pin", "https://chat.whatsapp.com/abc"])
self.assertIs(args.func, bot_cli.cmd_set_pin)
self.assertEqual(args.link, "https://chat.whatsapp.com/abc")
class CmdInsertUserTests(unittest.TestCase):
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_inserts_using_password_lookup_and_security_pin(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.get_user_pass_from_acc.return_value = "abc12345"
mock_hal.security_pin = "999111"
mock_hal.insert_user_to_table_user.return_value = True
out = io.StringIO()
with contextlib.redirect_stdout(out):
bot_cli.cmd_insert_user(argparse.Namespace(f_username="13c1234", t_username="player_x"))
self.assertIn("OK: inserted 13c1234 → player_x", out.getvalue())
mock_hal.insert_user_to_table_user.assert_called_once_with({
"f_username": "13c1234",
"f_password": "abc12345",
"t_username": "player_x",
"t_password": "999111",
})
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_no_password_for_f_user_exits_2(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.get_user_pass_from_acc.return_value = None
with self.assertRaises(SystemExit) as cm:
bot_cli.cmd_insert_user(argparse.Namespace(f_username="missing", t_username="player_x"))
self.assertEqual(cm.exception.code, 2)
mock_hal.insert_user_to_table_user.assert_not_called()
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_insert_failure_exits_1(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.get_user_pass_from_acc.return_value = "abc"
mock_hal.security_pin = "000"
mock_hal.insert_user_to_table_user.return_value = False
with self.assertRaises(SystemExit) as cm:
bot_cli.cmd_insert_user(argparse.Namespace(f_username="13c1234", t_username="player_x"))
self.assertEqual(cm.exception.code, 1)
def test_insert_user_subparser_dispatches(self):
parser = bot_cli.build_parser()
args = parser.parse_args(["insert-user", "13c1234", "player_x"])
self.assertIs(args.func, bot_cli.cmd_insert_user)
self.assertEqual(args.f_username, "13c1234")
self.assertEqual(args.t_username, "player_x")
class CmdCreditTests(unittest.TestCase):
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_prints_credit(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.get_user_credit.return_value = 42.5
out = io.StringIO()
with contextlib.redirect_stdout(out):
bot_cli.cmd_credit(argparse.Namespace(username="13c1234", password="abc"))
self.assertIn("Credit: 42.5", out.getvalue())
mock_hal.get_user_credit.assert_called_once_with("13c1234", "abc")
def test_credit_subparser_dispatches(self):
parser = bot_cli.build_parser()
args = parser.parse_args(["credit", "13c1234", "abc"])
self.assertIs(args.func, bot_cli.cmd_credit)
class CmdTransferTests(unittest.TestCase):
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_prints_transfer_result(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.transfer_credit_api.return_value = "Successfully transfer amount: 10.0 from 13c1234 to player_x"
out = io.StringIO()
with contextlib.redirect_stdout(out):
bot_cli.cmd_transfer(argparse.Namespace(
f_username="13c1234", f_password="abc",
t_username="player_x", t_password="0000",
))
self.assertIn("Successfully transfer", out.getvalue())
mock_hal.transfer_credit_api.assert_called_once_with("13c1234", "abc", "player_x", "0000")
def test_transfer_subparser_dispatches(self):
parser = bot_cli.build_parser()
args = parser.parse_args(["transfer", "13c1234", "abc", "player_x", "0000"])
self.assertIs(args.func, bot_cli.cmd_transfer)
class CmdMonitorOnceTests(unittest.TestCase):
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_does_nothing_when_already_at_target(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.get_all_available_acc.return_value = [{"username": f"u{i}"} for i in range(20)]
out = io.StringIO()
with contextlib.redirect_stdout(out):
bot_cli.cmd_monitor_once(argparse.Namespace(target=20))
text = out.getvalue()
self.assertIn("Available accounts: 20", text)
self.assertIn("Already at target", text)
mock_hal.create_new_acc.assert_not_called()
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_creates_accounts_until_target(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.get_all_available_acc.return_value = [{"username": "u1"}, {"username": "u2"}]
mock_hal.create_new_acc.side_effect = [
{"username": "u3", "password": "p3", "link": "l3"},
{"username": "u4", "password": "p4", "link": "l4"},
{"username": "u5", "password": "p5", "link": "l5"},
]
out = io.StringIO()
with contextlib.redirect_stdout(out):
bot_cli.cmd_monitor_once(argparse.Namespace(target=5))
text = out.getvalue()
self.assertEqual(mock_hal.create_new_acc.call_count, 3)
self.assertIn("Created: u3", text)
self.assertIn("Created: u4", text)
self.assertIn("Created: u5", text)
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_create_failure_exits_1(self, mock_hal_class):
mock_hal = mock_hal_class.return_value
mock_hal.get_all_available_acc.return_value = []
mock_hal.create_new_acc.side_effect = RuntimeError("fail login")
with self.assertRaises(SystemExit) as cm:
bot_cli.cmd_monitor_once(argparse.Namespace(target=1))
self.assertEqual(cm.exception.code, 1)
def test_monitor_once_subparser_dispatches(self):
parser = bot_cli.build_parser()
args = parser.parse_args(["monitor-once", "--target", "7"])
self.assertIs(args.func, bot_cli.cmd_monitor_once)
self.assertEqual(args.target, 7)
def test_monitor_alias_dispatches(self):
parser = bot_cli.build_parser()
args = parser.parse_args(["monitor"])
self.assertIs(args.func, bot_cli.cmd_monitor_once)
self.assertEqual(args.target, 20)
class CmdInteractiveTests(unittest.TestCase):
@mock.patch("builtins.input", side_effect=["q"])
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_q_exits_cleanly(self, mock_hal_class, mock_input):
out = io.StringIO()
with contextlib.redirect_stdout(out):
bot_cli.cmd_interactive(argparse.Namespace())
self.assertIn("CM Bot CLI", out.getvalue())
@mock.patch("builtins.input", side_effect=["", "q"])
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_blank_line_continues_loop(self, mock_hal_class, mock_input):
out = io.StringIO()
with contextlib.redirect_stdout(out):
bot_cli.cmd_interactive(argparse.Namespace())
self.assertGreaterEqual(out.getvalue().count("Register / get next account"), 2)
@mock.patch("builtins.input", side_effect=EOFError)
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_eof_exits_cleanly(self, mock_hal_class, mock_input):
out = io.StringIO()
with contextlib.redirect_stdout(out):
bot_cli.cmd_interactive(argparse.Namespace())
self.assertIn("CM Bot CLI", out.getvalue())
@mock.patch("builtins.input", side_effect=["1", "q"])
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_alias_1_dispatches_to_register(self, mock_hal_class, mock_input):
mock_hal = mock_hal_class.return_value
mock_hal.get_user_api.return_value = {
"username": "u", "password": "p", "link": "l",
}
out = io.StringIO()
with contextlib.redirect_stdout(out):
bot_cli.cmd_interactive(argparse.Namespace())
self.assertIn("Username: u", out.getvalue())
mock_hal.get_user_api.assert_called_once_with()
@mock.patch("builtins.input", side_effect=["nonsense", "q"])
@mock.patch.object(bot_cli, "CM_BOT_HAL")
def test_unknown_subcommand_keeps_loop_alive(self, mock_hal_class, mock_input):
out = io.StringIO()
err = io.StringIO()
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
bot_cli.cmd_interactive(argparse.Namespace())
self.assertIn("CM Bot CLI", out.getvalue())
class CreateAppFactoryTests(unittest.TestCase):
"""The gunicorn entrypoint loads `app.cm_api:create_app()`. The factory
must exist as a module-level callable that returns the Flask app
object — not the CM_API wrapper class."""
def test_create_app_returns_flask_instance(self):
from flask import Flask
from app.cm_api import create_app
wsgi = create_app()
self.assertIsInstance(wsgi, Flask)
def test_create_app_registers_acc_route(self):
from app.cm_api import create_app
wsgi = create_app()
rules = {r.rule for r in wsgi.url_map.iter_rules()}
self.assertIn("/acc/", rules)
if __name__ == "__main__":
unittest.main()