def setup_unit_test(): try: while True: REGISTRY.cleanup() except: pass REGISTRY.prepare() REGISTRY.register(ew.widget_context, ew.core.WidgetContext('http', ew.ResourceManager())) REGISTRY.register(g, Globals()) REGISTRY.register(c, mock.Mock()) REGISTRY.register(url, lambda: None) REGISTRY.register(request, Request.blank('/', remote_addr='127.0.0.1')) REGISTRY.register(response, Response()) REGISTRY.register(allura.credentials, allura.lib.security.Credentials()) c.model_cache = None ThreadLocalORMSession.close_all()
def setup_unit_test(): try: while True: REGISTRY.cleanup() except: pass REGISTRY.prepare() REGISTRY.register(ew.widget_context, ew.core.WidgetContext('http', ew.ResourceManager())) REGISTRY.register(g, Globals()) REGISTRY.register(c, mock.Mock()) REGISTRY.register(url, lambda: None) REGISTRY.register(response, Response()) REGISTRY.register(session, beaker.session.SessionObject({})) REGISTRY.register(allura.credentials, allura.lib.security.Credentials()) c.memoize_cache = {} c.queued_messages = None c.model_cache = None ThreadLocalORMSession.close_all()
def setup_unit_test(): try: while True: REGISTRY.cleanup() except Exception: pass REGISTRY.prepare() REGISTRY.register(ew.widget_context, ew.core.WidgetContext('http', ew.ResourceManager())) REGISTRY.register(allura.credentials, allura.lib.security.Credentials()) # turbogears has its own special magic wired up for its globals, can't use a regular Registry tgl = RequestLocals() tgl.app_globals = Globals() tgl.tmpl_context = mock.Mock() tgl.url = lambda: None tgl.request = Request.blank('/', remote_addr='127.0.0.1') tgl.response = Response() tg.request_local.context._push_object(tgl) c.model_cache = None ThreadLocalORMSession.close_all()
def bootstrap(command, conf, vars): """Place any commands to setup allura here""" # are we being called by the test suite? test_run = conf.get('__file__', '').endswith('test.ini') if not test_run: # when this is run via the `setup-app` cmd, some macro rendering needs this set up REGISTRY.register(ew.widget_context, ew.core.WidgetContext('http', ew.ResourceManager())) create_test_data = asbool(os.getenv('ALLURA_TEST_DATA', True)) # if this is a test_run, skip user project creation to save time make_user_projects = not test_run def make_user(*args, **kw): kw.update(make_project=make_user_projects) return create_user(*args, **kw) # Temporarily disable auth extensions to prevent unintended side-effects tg.config['auth.method'] = tg.config['registration.method'] = 'local' assert tg.config['auth.method'] == 'local' conf['auth.method'] = conf['registration.method'] = 'local' # Clean up all old stuff ThreadLocalORMSession.close_all() c.user = c.project = c.app = None wipe_database() try: g.solr.delete(q='*:*') except: # pragma no cover log.error('SOLR server is %s', g.solr_server) log.error('Error clearing solr index') # set up mongo indexes index = EnsureIndexCommand('ensure_index') index.run(['']) if create_test_data and asbool(conf.get('cache_test_data')): if restore_test_data(): h.set_context('test', neighborhood='Projects') return log.info('Initializing search') log.info('Registering root user & default neighborhoods') M.User( _id=None, username='******', display_name='Anonymous') # never make a user project for the root user if create_test_data: root = create_user('Root', make_project=False) else: from getpass import getpass root_name = raw_input('Enter username for root user (default "root"): ').strip() if not root_name: root_name = 'root' ok = False while not ok: root_password1 = getpass('Enter password: '******'Password must not be empty') continue root_password2 = getpass('Confirm password: '******'t match") continue root = create_user(root_name, password=root_password1, make_project=False) ok = True n_projects = M.Neighborhood(name='Projects', url_prefix='/p/', features=dict(private_projects=True, max_projects=None, css='none', google_analytics=False)) n_users = M.Neighborhood(name='Users', url_prefix='/u/', shortname_prefix='u/', anchored_tools='profile:Profile,userstats:Statistics', features=dict(private_projects=True, max_projects=None, css='none', google_analytics=False)) assert tg.config['auth.method'] == 'local' project_reg = plugin.ProjectRegistrationProvider.get() p_projects = project_reg.register_neighborhood_project( n_projects, [root], allow_register=True) p_users = project_reg.register_neighborhood_project(n_users, [root]) def set_nbhd_wiki_content(nbhd_proj, content): wiki = nbhd_proj.app_instance('wiki') page = WM.Page.query.get( app_config_id=wiki.config._id, title=wiki.root_page_name) page.text = content set_nbhd_wiki_content(p_projects, dedent(''' Welcome to the "Projects" neighborhood. It is the default neighborhood in Allura. You can edit this wiki page as you see fit. Here's a few ways to get started: [Register a new project](/p/add_project) [Neighborhood administration](/p/admin) [[projects show_total=yes]] ''')) set_nbhd_wiki_content(p_users, dedent(''' This is the "Users" neighborhood. All users automatically get a user-project created for them, using their username. [Neighborhood administration](/u/admin) [[projects show_total=yes]] ''')) if create_test_data: n_adobe = M.Neighborhood( name='Adobe', url_prefix='/adobe/', project_list_url='/adobe/', features=dict(private_projects=True, max_projects=None, css='custom', google_analytics=True)) p_adobe = project_reg.register_neighborhood_project(n_adobe, [root]) set_nbhd_wiki_content(p_adobe, dedent(''' This is the "Adobe" neighborhood. It is just an example of having projects in a different neighborhood. [Neighborhood administration](/adobe/admin) [[projects show_total=yes]] ''')) # add the adobe icon file_name = 'adobe_icon.png' file_path = os.path.join( allura.__path__[0], 'public', 'nf', 'images', file_name) M.NeighborhoodFile.from_path(file_path, neighborhood_id=n_adobe._id) ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all() if create_test_data: # Add some test users for unum in range(10): make_user('Test User %d' % unum) log.info('Creating basic project categories') M.ProjectCategory(name='clustering', label='Clustering') cat2 = M.ProjectCategory(name='communications', label='Communications') M.ProjectCategory( name='synchronization', label='Synchronization', parent_id=cat2._id) M.ProjectCategory( name='streaming', label='Streaming', parent_id=cat2._id) M.ProjectCategory(name='fax', label='Fax', parent_id=cat2._id) M.ProjectCategory(name='bbs', label='BBS', parent_id=cat2._id) cat3 = M.ProjectCategory(name='database', label='Database') M.ProjectCategory( name='front_ends', label='Front-Ends', parent_id=cat3._id) M.ProjectCategory( name='engines_servers', label='Engines/Servers', parent_id=cat3._id) if create_test_data: log.info('Registering "regular users" (non-root) and default projects') # since this runs a lot for tests, separate test and default users and # do the minimal needed if asbool(conf.get('load_test_data')): u_admin = make_user('Test Admin') u_admin.preferences = dict(email_address='*****@*****.**') u_admin.email_addresses = ['*****@*****.**'] u_admin.set_password('foo') u_admin.claim_address('*****@*****.**') ThreadLocalORMSession.flush_all() admin_email = M.EmailAddress.get(email='*****@*****.**') admin_email.confirmed = True else: u_admin = make_user('Admin 1', username='******') # Admin1 is almost root, with admin access for Users and Projects # neighborhoods p_projects.add_user(u_admin, ['Admin']) p_users.add_user(u_admin, ['Admin']) n_projects.register_project('allura', u_admin, 'Allura') make_user('Test User') n_adobe.register_project('adobe-1', u_admin, 'Adobe project 1') p_adobe.add_user(u_admin, ['Admin']) p0 = n_projects.register_project('test', u_admin, 'Test Project') n_projects.register_project('test2', u_admin, 'Test 2') p0._extra_tool_status = ['alpha', 'beta'] sess = session(M.Neighborhood) # all the sessions are the same _list = (n_projects, n_users, p_projects, p_users) if create_test_data: _list += (n_adobe, p_adobe) for x in _list: # Ming doesn't detect substructural changes in newly created objects # (vs loaded from DB) state(x).status = 'dirty' # TODO: Hope that Ming can be improved to at least avoid stuff below sess.flush(x) ThreadLocalORMSession.flush_all() if asbool(conf.get('load_test_data')): if asbool(conf.get('cache_test_data')): cache_test_data() else: # pragma no cover # regular first-time setup create_trove_categories = CreateTroveCategoriesCommand('create_trove_categories') create_trove_categories.run(['']) if create_test_data: p0.add_user(u_admin, ['Admin']) log.info('Registering initial apps') with h.push_config(c, user=u_admin): p0.install_apps([{'ep_name': ep_name} for ep_name, app in g.entry_points['tool'].iteritems() if app._installable(tool_name=ep_name, nbhd=n_projects, project_tools=[]) ]) ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all() if create_test_data: # reload our p0 project so that p0.app_configs is accurate with all the # newly installed apps p0 = M.Project.query.get(_id=p0._id) sub = p0.new_subproject('sub1', project_name='A Subproject') with h.push_config(c, user=u_admin): sub.install_app('wiki') ThreadLocalORMSession.flush_all() ThreadLocalORMSession.close_all()