This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/tests/test_dispatcher.py

52 lines
1.6 KiB
Python
Raw Permalink Normal View History

2019-01-23 15:55:04 +00:00
from unittest.mock import Mock
import pytest
from ereuse_devicehub.devicehub import Devicehub
from ereuse_devicehub.dispatchers import PathDispatcher
from tests.conftest import TestConfig
def noop():
pass
@pytest.mark.mvp
2019-01-23 15:55:04 +00:00
@pytest.fixture()
def dispatcher(app: Devicehub, config: TestConfig) -> PathDispatcher:
PathDispatcher.call = Mock(side_effect=lambda *args: args[0])
return PathDispatcher(config_cls=config)
@pytest.mark.mvp
2019-01-23 15:55:04 +00:00
def test_dispatcher_default(dispatcher: PathDispatcher):
"""The dispatcher returns not found for an URL that does not
route to an app.
"""
app = dispatcher({'SCRIPT_NAME:': '/', 'PATH_INFO': '/'}, noop)
assert app == PathDispatcher.NOT_FOUND
app = dispatcher({'SCRIPT_NAME:': '/', 'PATH_INFO': '/foo/foo'}, noop)
assert app == PathDispatcher.NOT_FOUND
@pytest.mark.mvp
2019-01-23 15:55:04 +00:00
def test_dispatcher_return_app(dispatcher: PathDispatcher):
"""The dispatcher returns the correct app for the URL."""
2019-01-23 15:55:04 +00:00
# Note that the dispatcher does not check if the URL points
# to a well-known endpoint for the app.
# Only if can route it to an app. And then the app checks
# if the path exists
app = dispatcher({'SCRIPT_NAME:': '/', 'PATH_INFO': '/test/foo/'}, noop)
assert isinstance(app, Devicehub)
assert app.id == 'test'
@pytest.mark.mvp
2019-01-23 15:55:04 +00:00
def test_dispatcher_users(dispatcher: PathDispatcher):
"""Users special endpoint returns an app."""
2019-01-23 15:55:04 +00:00
# For now returns the first app, as all apps
# can answer {}/users/login
app = dispatcher({'SCRIPT_NAME:': '/', 'PATH_INFO': '/users/'}, noop)
assert isinstance(app, Devicehub)
assert app.id == 'test'