Example #1
0
def test_load_auth_module():
    def func(filename):
        m = bsa.load_auth_module(filename)
        assert isinstance(m, ModuleType)
        assert [x for x in sorted(dir(m)) if not x.startswith("__")
                ] == ['LoginHandler', 'get_login_url', 'logout_url']

    with_file_contents(_source, func, suffix='.py')
Example #2
0
    def test_no_login(self, user_func) -> None:
        def func(filename):
            with pytest.raises(ValueError) as e:
                bsa.AuthModule(filename)
                assert str(e) == "When user authentication is enabled, one of login_url or get_login_url must be supplied"

        with_file_contents("""
def %s(handler): return 10
    """ % user_func, func, suffix='.py')
Example #3
0
    def test_both_user(self) -> None:
        def func(filename):
            with pytest.raises(ValueError) as e:
                bsa.AuthModule(filename)
                assert str(e) == "Only one of get_user or get_user_async should be supplied"

        with_file_contents("""
def get_user(handler): return 10
def get_user_async(handler): return 20
    """, func, suffix='.py')
Example #4
0
    def test_lifecycle_bad_syntax(self):
        result = {}
        def load(filename):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("This is a syntax error", load)

        handler = result['handler']
        assert handler.error is not None
        assert 'Invalid syntax' in handler.error
Example #5
0
    def test_logout_handler_wrong_url(self, logout_url) -> None:
        def func(filename):
            with pytest.raises(ValueError) as e:
                bsa.AuthModule(filename)
                assert str(e) == "LoginHandler can only be used with a relative login_url"

        with_file_contents("""
def get_user(handler): return 10
logout_url = %r
    """ % logout_url, func, suffix='.py')
Example #6
0
    def test_lifecycle_bad_syntax(self) -> None:
        result: Dict[str, Handler] = {}
        def load(filename: str):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("This is a syntax error", load)

        handler = result['handler']
        assert handler.error is not None
        assert 'Invalid syntax' in handler.error
Example #7
0
    def test_get_user(self) -> None:
        def func(filename):
            am = bsa.AuthModule(filename)
            assert am.get_user is not None
            assert am.get_user('handler') == 10

        with_file_contents("""
def get_user(handler): return 10
login_url = "/foo"
        """, func, suffix='.py')
Example #8
0
    def test_lifecycle_runtime_error(self):
        result = {}
        def load(filename):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("raise RuntimeError('nope')", load)

        handler = result['handler']
        assert handler.error is not None
        assert 'nope' in handler.error
Example #9
0
    def test_lifecycle_runtime_error(self) -> None:
        result: Dict[str, Handler] = {}
        def load(filename: str):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("raise RuntimeError('nope')", load)

        handler = result['handler']
        assert handler.error is not None
        assert 'nope' in handler.error
Example #10
0
    def test_login_url_endpoint(self) -> None:
        def func(filename):
            am = bsa.AuthModule(filename)
            assert am.endpoints[0][0] == '/foo'
            assert issubclass(am.endpoints[0][1], RequestHandler)
        with_file_contents("""
from tornado.web import RequestHandler
def get_user(): pass
login_url = "/foo"
class LoginHandler(RequestHandler): pass
        """, func, suffix='.py')
Example #11
0
    def test_logout_handler_wrong_type(self) -> None:
        def func(filename):
            with pytest.raises(ValueError) as e:
                bsa.AuthModule(filename)
                assert str(e) == "LoginHandler must be a Tornado RequestHandler"

        with_file_contents("""
def get_user(handler): return 10
login_url = "/foo"
class LogoutHandler(object): pass
    """, func, suffix='.py')
Example #12
0
    def test_both_login(self) -> None:
        def func(filename):
            with pytest.raises(ValueError) as e:
                bsa.AuthModule(filename)
                assert str(e) == "At most one of login_url or get_login_url should be supplied"

        with_file_contents("""
def get_user(handler): return 10
def get_login_url(handler): return 20
login_url = "/foo"
    """, func, suffix='.py')
Example #13
0
    def test_request_bad_syntax(self) -> None:
        result = {}

        def load(filename):
            handler = basrh.ServerRequestHandler(filename=filename)
            result['handler'] = handler

        with_file_contents("This is a syntax error", load)

        handler = result['handler']
        assert handler.error is not None
        assert 'Invalid syntax' in handler.error
Example #14
0
    def test_request_runtime_error(self) -> None:
        result = {}

        def load(filename):
            handler = basrh.ServerRequestHandler(filename=filename)
            result['handler'] = handler

        with_file_contents("raise RuntimeError('nope')", load)

        handler = result['handler']
        assert handler.error is not None
        assert 'nope' in handler.error
Example #15
0
    def test_url_path(self) -> None:
        result = {}

        def load(filename):
            handler = basrh.ServerRequestHandler(filename=filename)
            result['handler'] = handler

        with_file_contents("def process_request(request): return {}", load)

        handler = result['handler']
        assert handler.error is None
        assert handler.url_path().startswith("/")
Example #16
0
    def test_handler_with_get_login_url(self) -> None:
        def func(filename):
            with pytest.raises(ValueError) as e:
                bsa.AuthModule(filename)
                assert str(e) == "LoginHandler cannot be used with a get_login_url() function"

        with_file_contents("""
def get_user(handler): return 10
def get_login_url(handler): return 20
from tornado.web import RequestHandler
class LoginHandler(RequestHandler): pass
    """, func, suffix='.py')
Example #17
0
    async def test_calling_lifecycle_hooks(self) -> None:
        result: Dict[str, Handler] = {}
        def load(filename: str):
            handler = result['handler'] = bahs.ServerLifecycleHandler(filename=filename)
            if handler.failed:
                raise RuntimeError(handler.error)
        with_file_contents(script_adds_four_handlers, load)

        handler = result['handler']
        assert "on_server_loaded" == handler.on_server_loaded(None)
        assert "on_server_unloaded" == handler.on_server_unloaded(None)
        assert "on_session_created" == await handler.on_session_created(None)
        assert "on_session_destroyed" == await handler.on_session_destroyed(None)
Example #18
0
    def test_url_path(self):
        result = {}
        def load(filename):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("""
def on_server_unloaded(server_context):
    pass
""", load)

        handler = result['handler']
        assert handler.error is None
        assert handler.url_path().startswith("/")
Example #19
0
    def test_get_login_url(self) -> None:
        def func(filename):
            am = bsa.AuthModule(filename)
            assert am.login_url is None
            assert am.get_login_url('handler') == 20
            assert am.login_handler is None
            assert am.logout_url is None
            assert am.logout_handler is None

        with_file_contents("""
def get_user(handler): return 10
def get_login_url(handler): return 20
        """, func, suffix='.py')
Example #20
0
    def test_login_url(self) -> None:
        def func(filename: str):
            am = bsa.AuthModule(filename)
            assert am.login_url == "/foo"
            assert am.get_login_url is None
            assert am.login_handler is None
            assert am.logout_url is None
            assert am.logout_handler is None

        with_file_contents("""
def get_user(handler): return 10
login_url = "/foo"
        """, func, suffix='.py')
Example #21
0
    def test_url_path(self):
        result = {}
        def load(filename):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("""
def on_server_unloaded(server_context):
    pass
""", load)

        handler = result['handler']
        assert handler.error is None
        assert handler.url_path().startswith("/")
Example #22
0
    def test_calling_lifecycle_hooks(self):
        result = {}
        def load(filename):
            handler = result['handler'] = bahs.ServerLifecycleHandler(filename=filename)
            if handler.failed:
                raise RuntimeError(handler.error)
        with_file_contents(script_adds_four_handlers, load)

        handler = result['handler']
        assert "on_server_loaded" == handler.on_server_loaded(None)
        assert "on_server_unloaded" == handler.on_server_unloaded(None)
        assert "on_session_created" == handler.on_session_created(None)
        assert "on_session_destroyed" == handler.on_session_destroyed(None)
    async def test_empty_request_handler(self) -> None:
        result: Dict[str, Handler] = {}

        def load(filename: str):
            handler = basrh.ServerRequestHandler(filename=filename)
            result['handler'] = handler

        with_file_contents("# This script does nothing", load)
        handler = result['handler']
        payload = handler.process_request(None)
        if handler.failed:
            raise RuntimeError(handler.error)
        assert payload == {}
    async def test_calling_request_handler(self) -> None:
        result: Dict[str, Handler] = {}

        def load(filename: str):
            handler = result['handler'] = basrh.ServerRequestHandler(
                filename=filename)
            if handler.failed:
                raise RuntimeError(handler.error)

        with_file_contents(script_adds_handler, load)

        handler = result['handler']
        assert {"Custom": "Test"} == handler.process_request(None)
Example #25
0
    def test_no_endpoints(self) -> None:
        def func(filename):
            am = bsa.AuthModule(filename)
            assert am.endpoints == []

        with_file_contents("""
def get_user(): pass
def get_login_url(): pass
        """, func, suffix='.py')

        with_file_contents("""
def get_user(): pass
login_url = "/foo"
        """, func, suffix='.py')
Example #26
0
    def test_url_path(self) -> None:
        result: Dict[str, Handler] = {}
        def load(filename: str):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("""
def on_server_unloaded(server_context):
    pass
""", load)

        handler = result['handler']
        assert handler.error is None
        url_path = handler.url_path()
        assert url_path is not None and url_path.startswith("/")
Example #27
0
    def test_runner_script_with_encoding(self) -> None:
        doc = Document()
        source = "# -*- coding: utf-8 -*-\nimport os"
        result = {}
        def load(filename):
            handler = bahs.ScriptHandler(filename=filename)
            handler.modify_document(doc)
            result['handler'] = handler
            result['filename'] = filename
        with_file_contents(source, load)

        assert result['handler'].error is None
        assert result['handler'].failed is False
        assert not doc.roots
Example #28
0
    def test_runner_uses_source_from_filename(self) -> None:
        doc = Document()
        source = "# Test contents for script"
        result = {}
        def load(filename):
            handler = bahs.ScriptHandler(filename=filename)
            handler.modify_document(doc)
            result['handler'] = handler
            result['filename'] = filename
        with_file_contents(source, load)

        assert result['handler']._runner.path == result['filename']
        assert result['handler']._runner.source == source
        assert not doc.roots
Example #29
0
    def test_runner_uses_source_from_filename(self):
        doc = Document()
        source = "# Test contents for script"
        result = {}
        def load(filename):
            handler = bahs.ScriptHandler(filename=filename)
            handler.modify_document(doc)
            result['handler'] = handler
            result['filename'] = filename
        with_file_contents(source, load)

        assert result['handler']._runner.path == result['filename']
        assert result['handler']._runner.source == source
        assert not doc.roots
Example #30
0
    def test_runner_script_with_encoding(self):
        doc = Document()
        source = "# -*- coding: utf-8 -*-\nimport os"
        result = {}
        def load(filename):
            handler = bahs.ScriptHandler(filename=filename)
            handler.modify_document(doc)
            result['handler'] = handler
            result['filename'] = filename
        with_file_contents(source, load)

        assert result['handler'].error is None
        assert result['handler'].failed is False
        assert not doc.roots
Example #31
0
    def test_lifecycle_bad_session_destroyed_signature(self) -> None:
        result: Dict[str, Handler] = {}
        def load(filename: str):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("""
def on_session_destroyed(a,b):
    pass
""", load)

        handler = result['handler']
        assert handler.error is not None
        assert 'on_session_destroyed must have signature func(session_context)' in handler.error
        assert 'func(a, b)' in handler.error
Example #32
0
    def test_lifecycle_bad_session_destroyed_signature(self):
        result = {}
        def load(filename):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("""
def on_session_destroyed(a,b):
    pass
""", load)

        handler = result['handler']
        assert handler.error is not None
        assert 'on_session_destroyed must have signature func(session_context)' in handler.error
        assert 'func(a, b)' in handler.error
Example #33
0
    def test_url_path_failed(self) -> None:
        result: Dict[str, Handler] = {}
        def load(filename: str):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("""
# bad signature
def on_server_unloaded():
    pass
""", load)

        handler = result['handler']
        assert handler.error is not None
        assert handler.url_path() is None
Example #34
0
    def test_empty_lifecycle(self):
        doc = Document()
        def load(filename):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            handler.modify_document(doc)
            handler.on_server_loaded(None)
            handler.on_server_unloaded(None)
            handler.on_session_created(None)
            handler.on_session_destroyed(None)
            if handler.failed:
                raise RuntimeError(handler.error)
        with_file_contents("# This script does nothing", load)

        assert not doc.roots
Example #35
0
    def test_url_path_failed(self):
        result = {}
        def load(filename):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("""
# bad signature
def on_server_unloaded():
    pass
""", load)

        handler = result['handler']
        assert handler.error is not None
        assert handler.url_path() is None
Example #36
0
    def test_login_handler(self) -> None:
        def func(filename):
            am = bsa.AuthModule(filename)
            assert am.login_url == "/foo"
            assert am.get_login_url is None
            assert issubclass(am.login_handler, RequestHandler)
            assert am.logout_url is None
            assert am.logout_handler is None

        with_file_contents("""
def get_user(handler): return 10
login_url = "/foo"
from tornado.web import RequestHandler
class LoginHandler(RequestHandler): pass
        """, func, suffix='.py')
Example #37
0
    def test_lifecycle_bad_server_loaded_signature(self):
        result = {}
        def load(filename):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("""
def on_server_loaded(a,b):
    pass
""", load)

        handler = result['handler']
        assert handler.error is not None
        assert 'on_server_loaded must have signature func(server_context)' in handler.error
        assert 'func(a, b)' in handler.error
        assert "Traceback" in handler.error_detail
Example #38
0
 async def test_empty_lifecycle(self) -> None:
     doc = Document()
     result: Dict[str, Handler] = {}
     def load(filename: str):
         handler = bahs.ServerLifecycleHandler(filename=filename)
         handler.modify_document(doc)
         result['handler'] = handler
     with_file_contents("# This script does nothing", load)
     handler = result['handler']
     handler.on_server_loaded(None)
     handler.on_server_unloaded(None)
     await handler.on_session_created(None)
     await handler.on_session_destroyed(None)
     if handler.failed:
         raise RuntimeError(handler.error)
     assert not doc.roots
    def test_lifecycle_bad_process_request_signature(self) -> None:
        result: Dict[str, Handler] = {}

        def load(filename: str):
            handler = basrh.ServerRequestHandler(filename=filename)
            result['handler'] = handler

        with_file_contents("""
def process_request(a,b):
    pass
""", load)

        handler = result['handler']
        assert handler.error is not None
        assert 'process_request must have signature func(request)' in handler.error
        assert 'func(a, b)' in handler.error