diff --git a/.idea/machine-access-control.iml b/.idea/machine-access-control.iml index 4d4e8f5..68bc3a5 100644 --- a/.idea/machine-access-control.iml +++ b/.idea/machine-access-control.iml @@ -5,11 +5,11 @@ - + - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 62ab98b..d52bdf2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,8 +3,8 @@ - + - + \ No newline at end of file diff --git a/src/dm_mac/__init__.py b/src/dm_mac/__init__.py index 30ebed2..0940b09 100644 --- a/src/dm_mac/__init__.py +++ b/src/dm_mac/__init__.py @@ -1,7 +1,7 @@ """Decatur Makers Machine Access Control.""" import argparse -import asyncio +from asyncio import AbstractEventLoop, get_event_loop import logging import os import sys @@ -101,7 +101,7 @@ def main() -> None: set_log_debug(logger) else: set_log_info(logger) - loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() + loop: AbstractEventLoop = get_event_loop() app = create_app() if "SLACK_APP_TOKEN" in os.environ: slack: SlackHandler = SlackHandler(app) diff --git a/tests/test_init.py b/tests/test_init.py index 97349bb..5ac6daa 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -1,12 +1,57 @@ +from unittest.mock import patch, DEFAULT, MagicMock, call +from typing import List + from dm_mac import main class TestMain: - def test_happy_path_no_slack(self): - # patch sys.argv - # patch set_log_debug and _info - # patch asyncio.get_event_loop - # patch create_app() - # patch ... all the slack stuff - pass + @patch('sys.argv', ['mac-server']) + @patch.dict('os.environ', {'SLACK_APP_TOKEN': 'app-token'}) + def test_with_slack(self): + mocks: List[MagicMock] + with patch.multiple( + 'dm_mac', + set_log_debug=DEFAULT, + set_log_info=DEFAULT, + get_event_loop=DEFAULT, + SlackHandler=DEFAULT, + AsyncSocketModeHandler=DEFAULT, + logger=DEFAULT, + create_app=DEFAULT, + new_callable=MagicMock + ) as mocks: + slack_app = MagicMock() + type(mocks['SlackHandler']).app = slack_app + loop = MagicMock() + app = MagicMock() + handler = MagicMock() + mocks['get_event_loop'].return_value = loop + mocks['create_app'].return_value = app + mocks['AsyncSocketModeHandler'].return_value = handler + main() + assert mocks['set_log_debug'].mock_calls == [] + assert mocks['set_log_info'].mock_calls == [call(mocks['logger'])] + assert mocks['create_app'].mock_calls == [ + call(), + call().config.update({ + 'SLACK_HANDLER': mocks['SlackHandler'].return_value + }), + call().run(loop=loop, debug=False, host='0.0.0.0') + ] + assert app.mock_calls == [ + call.config.update({ + 'SLACK_HANDLER': mocks['SlackHandler'].return_value + }), + call.run(loop=loop, debug=False, host='0.0.0.0') + ] + assert mocks['SlackHandler'].mock_calls == [call(app)] + assert mocks['AsyncSocketModeHandler'].mock_calls == [ + call(slack_app, 'app-token', loop=loop) + ] + assert loop.mock_calls == [ + call.create_task(handler.start_async()) + ] + assert app.mock_calls == [ + call.run(loop=loop, debug=False, host="0.0.0.0") + ]