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.fixture()
|
|
|
|
def dispatcher(app: Devicehub, config: TestConfig) -> PathDispatcher:
|
|
|
|
PathDispatcher.call = Mock(side_effect=lambda *args: args[0])
|
|
|
|
return PathDispatcher(config_cls=config)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def test_dispatcher_return_app(dispatcher: PathDispatcher):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""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'
|
|
|
|
|
|
|
|
|
|
|
|
def test_dispatcher_users(dispatcher: PathDispatcher):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""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'
|