feat(bot_cli): add register subcommand (Telegram /1 analog)
This commit is contained in:
parent
c6e49c6240
commit
f5d4a554d6
@ -12,12 +12,27 @@ def cmd_interactive(_args):
|
|||||||
raise NotImplementedError("cmd_interactive is implemented in a later task")
|
raise NotImplementedError("cmd_interactive is implemented in a later task")
|
||||||
|
|
||||||
|
|
||||||
|
def _print_user(user: dict) -> None:
|
||||||
|
print(f"Username: {user['username']}")
|
||||||
|
print(f"Password: {user['password']}")
|
||||||
|
print(f"Link: {user['link']}")
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_register(_args):
|
||||||
|
bot = CM_BOT_HAL()
|
||||||
|
_print_user(bot.get_user_api())
|
||||||
|
|
||||||
|
|
||||||
def build_parser() -> argparse.ArgumentParser:
|
def build_parser() -> argparse.ArgumentParser:
|
||||||
p = argparse.ArgumentParser(
|
p = argparse.ArgumentParser(
|
||||||
prog="bot_cli",
|
prog="bot_cli",
|
||||||
description="CM Bot dev CLI (mirrors Telegram triggers).",
|
description="CM Bot dev CLI (mirrors Telegram triggers).",
|
||||||
)
|
)
|
||||||
p.add_subparsers(dest="command")
|
sub = p.add_subparsers(dest="command")
|
||||||
|
|
||||||
|
sp = sub.add_parser("register", aliases=["get-acc"], help="Get next available account (Telegram /1).")
|
||||||
|
sp.set_defaults(func=cmd_register)
|
||||||
|
|
||||||
return p
|
return p
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -36,5 +36,34 @@ class ParserSanityTests(unittest.TestCase):
|
|||||||
self.assertEqual(rc, 0)
|
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)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user