def test_use_existing_session(): cookie_name = 'session_cookie' secret = 'mischief managed' session_id = 'some_session_id' data = {'some': 'thing'} expires = datetime.datetime.utcnow() + datetime.timedelta(seconds=10) add_session({'session_id': session_id, 'data': json.dumps(data), 'expires': expires}) mod.request.get_cookie.return_value = session_id callback = mock.Mock(__name__='callback') plugin = mod.session_plugin(cookie_name=cookie_name, secret=secret) wrapped = plugin(callback) wrapped('test') assert_session_count_is(1) assert mod.request.session.data == data assert mod.request.session.id == session_id assert mod.request.session.expires == expires callback.assert_called_once_with('test')
def test_session_plugin_create(): mod.request.get_cookie.return_value = None mod.request.app.config = {'session.lifetime': 1} cookie_name = 'session_cookie' secret = 'mischief managed' callback = mock.Mock(__name__='callback') plugin = mod.session_plugin(cookie_name=cookie_name, secret=secret) wrapped = plugin(callback) wrapped('test') assert_session_count_is(1) sess = get_session(mod.request.session.id) assert sess['data'] == '{}' assert sess['session_id'] == mod.request.session.id assert isinstance(sess['expires'], datetime.datetime) callback.assert_called_once_with('test')
def start(databases, config, no_auth=False, repl=False, debug=False): """ Start the application """ debug = debug or config['librarian.debug'] servers = ServerManager() version = full_version_info(__version__, config) # Srart the server logging.info('===== Starting Librarian v%s =====', version) # Install Librarian plugins install_plugins(app) logging.info('Installed all plugins') # Set some basic configuration # add `lang_name_safe` to template helpers template_helper(lang.lang_name_safe) bottle.TEMPLATE_PATH.insert(0, in_pkg('views')) bottle.BaseTemplate.defaults.update({ 'app_version': version, 'request': request, 'style': 'screen', # Default stylesheet 'h': helpers, 'th': template_helper, 'updates': Lazy(lambda: len(list(get_downloads( config['content.spooldir'], config['content.output_ext'] )))), 'readable_license': lambda s: _(dict(LICENSES).get(s, LICENSES[0][1])), 'is_rtl': Lazy(lambda: request.locale in lang.RTL_LANGS), 'dir': lambda l: 'rtl' if l in lang.RTL_LANGS else 'auto', 'LANGS': lang.LANGS, 'UI_LANGS': lang.UI_LANGS, 'SELECT_LANGS': lang.SELECT_LANGS, 'u': to_unicode, 'url': app.get_url, }) # Install bottle plugins and WSGI middleware app.install(request_timer('Total')) app.install(squery.database_plugin(databases, debug=debug and not repl)) app.install(sessions.session_plugin( cookie_name=config['session.cookie_name'], secret=app.setup.get('session.secret') )) app.install(auth.user_plugin(no_auth)) wsgiapp = I18NPlugin(app, langs=lang.UI_LANGS, default_locale=lang.DEFAULT_LOCALE, domain='librarian', locale_dir=in_pkg('locales')) app.install(lock_plugin) app.install(setup.setup_plugin(setup_path=i18n_url('setup:main'))) app.install(content_resolver_plugin( root_url=config['librarian.root_url'], ap_client_ip_range=config['librarian.ap_client_ip_range'] )) app.install(request_timer('Handler')) # setup cache backend app.cache = cache.setup(backend=config['cache.backend'], timeout=config['cache.timeout'], servers=config['cache.servers']) # Install routes add_routes(app, ROUTES) app.error(403)(system.show_access_denied_page) app.error(404)(system.show_page_missing) app.error(500)(system.show_error_page) app.error(503)(system.show_maint_page) # Prepare to start bottle.debug(debug) # We are passing the ``wsgiapp`` object here because that's the one that # contains the I18N middleware. If we pass ``app`` object, then we won't # have the I18N middleware active at all. servers.start_server('librarian', config, wsgiapp) if repl: repl_thread = start_repl( locals(), 'Press Ctrl-C to shut down Librarian.') else: repl_thread = None print('Press Ctrl-C to shut down Librarian.') def shutdown(*args, **kwargs): """ Cleanly shut down the server """ logging.info('Librarian is going down.') if repl_thread: repl_thread.join() servers.stop_all(5) for db in databases.values(): db.close() logging.info('Clean shutdown completed') print('Bye! Have a nice day! Those books are due Tuesday, by the way!') on_interrupt(shutdown) try: while True: time.sleep(10) except KeyboardInterrupt: logging.debug('Keyboard interrupt received') return shutdown()