Awesome IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

88 lines
2.5 KiB

  1. from tests.lib import * # NOQA - fixtures
  2. from unittest.mock import MagicMock, call
  3. from pyircbot.rpc import BotRPC
  4. from pyircbot.rpcclient import connect
  5. from random import randint
  6. from time import sleep
  7. def test_rpc(monkeypatch):
  8. port = randint(40000, 65000)
  9. m = MagicMock()
  10. m.botconfig = {"bot": {"rpcbind": "127.0.0.1", "rpcport": port}}
  11. server = BotRPC(m)
  12. sleep(0.05)
  13. calltrack = MagicMock()
  14. def fake(*args):
  15. calltrack(*args)
  16. return args
  17. for k, v in server.server.funcs.items():
  18. server.server.funcs[k] = fake
  19. methods = [["importModule", "foo"],
  20. ["deportModule", "foo"],
  21. ["loadModule", "foo"],
  22. ["unloadModule", "foo"],
  23. ["reloadModule", "foo"],
  24. ["redoModule", "foo"],
  25. ["getLoadedModules"],
  26. ["pluginCommand", "foo", "foo", "foo"],
  27. ["setPluginVar", "foo", "foo"],
  28. ["getPluginVar", "foo", "foo", "foo"],
  29. ["eval", "foo"],
  30. ["exec", "foo"],
  31. ["quit", "foo"]]
  32. client = connect("127.0.0.1", port)
  33. for test in methods:
  34. method = test[0]
  35. args = test[1:]
  36. server.server.funcs[method] = fake
  37. print("Calling {} with: {}".format(method, args))
  38. getattr(client, method)(*args)
  39. calltrack.assert_called_once_with(*args)
  40. calltrack.reset_mock()
  41. def test_rpc_internal(monkeypatch):
  42. port = randint(40000, 65000)
  43. m = MagicMock()
  44. m.botconfig = {"bot": {"rpcbind": "127.0.0.1", "rpcport": port}}
  45. server = BotRPC(m)
  46. methods = [["importModule", "foo"],
  47. ["deportModule", "foo"],
  48. ["loadModule", "foo"],
  49. ["unloadModule", "foo"],
  50. ["redoModule", "foo"],]
  51. for test in methods:
  52. method = test[0]
  53. args = test[1:]
  54. getattr(server, method)(*args)
  55. getattr(m, method.lower()).assert_called_once_with(*args)
  56. getattr(m, method.lower()).reset_mock()
  57. m.moduleInstances = {"Foo": None, "Bar": None}
  58. assert server.getLoadedModules() == ["Foo", "Bar"]
  59. m.reset_mock()
  60. server.reloadModule("Foo")
  61. m.unloadmodule.assert_called_once_with("Foo")
  62. m.loadmodule.assert_called_once_with("Foo")
  63. m.reset_mock()
  64. # ["pluginCommand", "foo", "foo", "foo"],
  65. # ["setPluginVar", "foo", "foo"],
  66. # ["getPluginVar", "foo", "foo", "foo"]
  67. # ["eval", "foo"],
  68. # ["exec", "foo"],
  69. # ["quit", "foo"]]