def app_config(database): settings = { "warehouse.prevent_esi": True, "warehouse.token": "insecure token", "camo.url": "http://localhost:9000/", "camo.key": "insecure key", "celery.broker_url": "amqp://", "celery.result_url": "redis://localhost:0/", "celery.scheduler_url": "redis://localhost:0/", "database.url": database, "docs.url": "http://docs.example.com/", "ratelimit.url": "memory://", "elasticsearch.url": "https://localhost/warehouse", "files.backend": "warehouse.packaging.services.LocalFileStorage", "docs.backend": "warehouse.packaging.services.LocalFileStorage", "mail.backend": "warehouse.email.services.SMTPEmailSender", "malware_check.backend": ( "warehouse.malware.services.PrinterMalwareCheckService" ), "files.url": "http://localhost:7000/", "sessions.secret": "123456", "sessions.url": "redis://localhost:0/", "statuspage.url": "https://2p66nmmycsj3.statuspage.io", "warehouse.xmlrpc.cache.url": "redis://localhost:0/", } with mock.patch.object(config, "ManifestCacheBuster", MockManifestCacheBuster): with mock.patch("warehouse.admin.ManifestCacheBuster", MockManifestCacheBuster): with mock.patch.object(static, "whitenoise_add_manifest"): cfg = config.configure(settings=settings) # Ensure our migrations have been ran. alembic.command.upgrade(cfg.alembic_config(), "head") return cfg
def app_config(database): settings = { "warehouse.prevent_esi": True, "warehouse.token": "insecure token", "camo.url": "http://localhost:9000/", "camo.key": "insecure key", "celery.broker_url": "amqp://", "celery.result_url": "redis://localhost:0/", "celery.scheduler_url": "redis://localhost:0/", "database.url": database, "docs.url": "http://docs.example.com/", "ratelimit.url": "memory://", "elasticsearch.url": "https://localhost/warehouse", "files.backend": "warehouse.packaging.services.LocalFileStorage", "docs.backend": "warehouse.packaging.services.LocalFileStorage", "mail.backend": "warehouse.email.services.SMTPEmailSender", "files.url": "http://localhost:7000/", "sessions.secret": "123456", "sessions.url": "redis://localhost:0/", "statuspage.url": "https://2p66nmmycsj3.statuspage.io", "warehouse.xmlrpc.cache.url": "redis://localhost:0/", } with mock.patch.object(config, "ManifestCacheBuster", MockManifestCacheBuster): with mock.patch("warehouse.admin.ManifestCacheBuster", MockManifestCacheBuster): with mock.patch.object(static, "whitenoise_add_manifest"): cfg = config.configure(settings=settings) # Ensure our migrations have been ran. alembic.command.upgrade(cfg.alembic_config(), "head") return cfg
def app_config(database): config = configure( settings={ "warehouse.prevent_esi": True, "warehouse.token": "insecure token", "camo.url": "http://localhost:9000/", "camo.key": "insecure key", "celery.broker_url": "amqp://", "celery.result_url": "redis://localhost:0/", "database.url": database, "docs.url": "http://docs.example.com/", "download_stats.url": "redis://localhost:0/", "elasticsearch.url": "https://localhost/warehouse", "files.backend": "warehouse.packaging.services.LocalFileStorage", "files.url": "http://localhost:7000/", "sessions.secret": "123456", "sessions.url": "redis://localhost:0/", "statuspage.url": "https://2p66nmmycsj3.statuspage.io", }, ) # Ensure our migrations have been ran. alembic.command.upgrade(config.alembic_config(), "head") return config
def app_config(database): config = configure(settings={ "warehouse.prevent_esi": True, "warehouse.token": "insecure token", "camo.url": "http://localhost:9000/", "camo.key": "insecure key", "celery.broker_url": "amqp://", "celery.result_url": "redis://localhost:0/", "celery.scheduler_url": "redis://localhost:0/", "database.url": database, "docs.url": "http://docs.example.com/", "download_stats.url": "redis://localhost:0/", "ratelimit.url": "memory://", "elasticsearch.url": "https://localhost/warehouse", "files.backend": "warehouse.packaging.services.LocalFileStorage", "files.url": "http://localhost:7000/", "password_reset.secret": "insecure secret", "sessions.secret": "123456", "sessions.url": "redis://localhost:0/", "statuspage.url": "https://2p66nmmycsj3.statuspage.io", }, ) # Ensure our migrations have been ran. alembic.command.upgrade(config.alembic_config(), "head") return config
def app_config(database): config = configure(settings={ "camo.url": "http://localhost:9000/", "camo.key": "insecure key", "database.url": database, "docs.url": "http://docs.example.com/", "download_stats.url": "redis://localhost:0/", "sessions.secret": "123456", "sessions.url": "redis://localhost:0/", }, ) # Ensure our migrations have been ran. alembic.command.upgrade(config.alembic_config(), "head") return config
def app_config(database): config = configure( settings={ "camo.url": "http://localhost:9000/", "camo.key": "insecure key", "database.url": database, "docs.url": "http://docs.example.com/", "download_stats.url": "redis://localhost:0/", "sessions.secret": "123456", "sessions.url": "redis://localhost:0/", }, ) # Ensure our migrations have been ran. alembic.command.upgrade(config.alembic_config(), "head") return config
def _configure_celery(*args, **kwargs): config = configure() register_logger_signal(config.registry["raven.client"]) register_signal(config.registry["raven.client"])
def test_configure(monkeypatch, settings, environment): json_renderer_obj = pretend.stub() json_renderer_cls = pretend.call_recorder(lambda **kw: json_renderer_obj) monkeypatch.setattr(renderers, "JSON", json_renderer_cls) xmlrpc_renderer_obj = pretend.stub() xmlrpc_renderer_cls = pretend.call_recorder( lambda **kw: xmlrpc_renderer_obj ) monkeypatch.setattr(config, "XMLRPCRenderer", xmlrpc_renderer_cls) if environment == config.Environment.development: monkeypatch.setenv("WAREHOUSE_ENV", "development") class FakeRegistry(dict): def __init__(self): self.settings = { "warehouse.token": "insecure token", "warehouse.env": environment, "camo.url": "http://camo.example.com/", "pyramid.reload_assets": False, "dirs.packages": "/srv/data/pypi/packages/", } configurator_settings = {} configurator_obj = pretend.stub( registry=FakeRegistry(), include=pretend.call_recorder(lambda include: None), add_directive=pretend.call_recorder(lambda name, fn: None), add_wsgi_middleware=pretend.call_recorder(lambda m, *a, **kw: None), add_renderer=pretend.call_recorder(lambda name, renderer: None), add_request_method=pretend.call_recorder(lambda fn: None), add_jinja2_renderer=pretend.call_recorder(lambda renderer: None), add_jinja2_search_path=pretend.call_recorder(lambda path, name: None), get_settings=lambda: configurator_settings, add_settings=pretend.call_recorder( lambda d: configurator_settings.update(d) ), add_tween=pretend.call_recorder(lambda tween_factory: None), add_static_view=pretend.call_recorder( lambda name, path, cachebust: None ), scan=pretend.call_recorder(lambda ignore: None), ) configurator_cls = pretend.call_recorder(lambda settings: configurator_obj) monkeypatch.setattr(config, "Configurator", configurator_cls) cachebuster_obj = pretend.stub() cachebuster_cls = pretend.call_recorder(lambda m, cache: cachebuster_obj) monkeypatch.setattr(config, "WarehouseCacheBuster", cachebuster_cls) transaction_manager = pretend.stub() transaction = pretend.stub( TransactionManager=pretend.call_recorder(lambda: transaction_manager), ) monkeypatch.setattr(config, "transaction", transaction) result = config.configure(settings=settings) expected_settings = { "warehouse.env": environment, "warehouse.commit": None, "site.name": "Warehouse", } if environment == config.Environment.development: expected_settings.update({ "enforce_https": False, "pyramid.reload_templates": True, "pyramid.reload_assets": True, "pyramid.prevent_http_cache": True, "debugtoolbar.hosts": ["0.0.0.0/0"], "debugtoolbar.panels": [ "pyramid_debugtoolbar.panels.versions.VersionDebugPanel", "pyramid_debugtoolbar.panels.settings.SettingsDebugPanel", "pyramid_debugtoolbar.panels.headers.HeaderDebugPanel", ( "pyramid_debugtoolbar.panels.request_vars." "RequestVarsDebugPanel" ), "pyramid_debugtoolbar.panels.renderings.RenderingsDebugPanel", "pyramid_debugtoolbar.panels.logger.LoggingPanel", ( "pyramid_debugtoolbar.panels.performance." "PerformanceDebugPanel" ), "pyramid_debugtoolbar.panels.routes.RoutesDebugPanel", "pyramid_debugtoolbar.panels.sqla.SQLADebugPanel", "pyramid_debugtoolbar.panels.tweens.TweensDebugPanel", ( "pyramid_debugtoolbar.panels.introspection." "IntrospectionDebugPanel" ), ], }) if settings is not None: expected_settings.update(settings) assert configurator_cls.calls == [pretend.call(settings=expected_settings)] assert result is configurator_obj assert configurator_obj.add_wsgi_middleware.calls == [ pretend.call(ProxyFixer, token="insecure token"), pretend.call(VhmRootRemover), ] assert configurator_obj.include.calls == ( [ pretend.call(x) for x in [ ( "pyramid_debugtoolbar" if environment == config.Environment.development else None ), ] if x is not None ] + [ pretend.call(".logging"), pretend.call("pyramid_jinja2"), pretend.call("pyramid_tm"), pretend.call("pyramid_services"), pretend.call("pyramid_rpc.xmlrpc"), pretend.call(".legacy.action_routing"), pretend.call(".i18n"), pretend.call(".db"), pretend.call(".search"), pretend.call(".aws"), pretend.call(".celery"), pretend.call(".sessions"), pretend.call(".cache.http"), pretend.call(".cache.origin"), pretend.call(".csrf"), pretend.call(".accounts"), pretend.call(".packaging"), pretend.call(".redirects"), pretend.call(".routes"), pretend.call(".raven"), ] ) assert configurator_obj.add_jinja2_renderer.calls == [ pretend.call(".html"), pretend.call(".txt"), pretend.call(".xml"), ] assert configurator_obj.add_jinja2_search_path.calls == [ pretend.call("warehouse:templates", name=".html"), pretend.call("warehouse:templates", name=".txt"), pretend.call("warehouse:templates", name=".xml"), ] assert configurator_obj.add_settings.calls == [ pretend.call({"jinja2.newstyle": True}), pretend.call({ "tm.attempts": 3, "tm.manager_hook": mock.ANY, "tm.activate_hook": config.activate_hook, "tm.annotate_user": False, }), pretend.call({ "csp": { "default-src": ["'none'"], "frame-ancestors": ["'none'"], "img-src": [ "'self'", "http://camo.example.com/", "https://secure.gravatar.com", ], "referrer": ["origin-when-cross-origin"], "reflected-xss": ["block"], "report-uri": [None], "script-src": ["'self'"], "style-src": ["'self'"], }, }), ] add_settings_dict = configurator_obj.add_settings.calls[1].args[0] assert add_settings_dict["tm.manager_hook"](pretend.stub()) is \ transaction_manager assert configurator_obj.add_directive.calls == [ pretend.call("find_service_factory", config.find_service_factory), ] assert configurator_obj.add_request_method.calls == [ pretend.call(config.find_service_factory), ] assert configurator_obj.add_tween.calls == [ pretend.call("warehouse.config.content_security_policy_tween_factory"), pretend.call("warehouse.config.require_https_tween_factory"), ] assert configurator_obj.add_static_view.calls == [ pretend.call( name="static", path="warehouse:static", cachebust=cachebuster_obj, ), ] assert cachebuster_cls.calls == [ pretend.call("warehouse:static/manifest.json", cache=True), ] assert configurator_obj.scan.calls == [ pretend.call(ignore=["warehouse.migrations.env", "warehouse.wsgi"]), ] assert configurator_obj.add_renderer.calls == [ pretend.call("json", json_renderer_obj), pretend.call("xmlrpc", xmlrpc_renderer_obj), ] assert json_renderer_cls.calls == [ pretend.call(sort_keys=True, separators=(",", ":")), ] assert xmlrpc_renderer_cls.calls == [pretend.call(allow_none=True)]
def test_configure(monkeypatch, settings): fs_obj = pretend.stub() opener = pretend.call_recorder(lambda path, create_dir: fs_obj) monkeypatch.setattr(fs.opener, "fsopendir", opener) json_renderer_obj = pretend.stub() json_renderer_cls = pretend.call_recorder(lambda **kw: json_renderer_obj) monkeypatch.setattr(renderers, "JSON", json_renderer_cls) class FakeRegistry(dict): def __init__(self): self.settings = { "camo.url": "http://camo.example.com/", "pyramid.reload_assets": False, "dirs.packages": "/srv/data/pypi/packages/", } configurator_settings = {} configurator_obj = pretend.stub( registry=FakeRegistry(), include=pretend.call_recorder(lambda include: None), add_renderer=pretend.call_recorder(lambda name, renderer: None), add_jinja2_renderer=pretend.call_recorder(lambda renderer: None), add_jinja2_search_path=pretend.call_recorder(lambda path, name: None), get_settings=lambda: configurator_settings, add_settings=pretend.call_recorder( lambda d: configurator_settings.update(d) ), add_tween=pretend.call_recorder(lambda tween_factory: None), add_notfound_view=pretend.call_recorder(lambda append_slash: None), add_static_view=pretend.call_recorder( lambda name, path, cachebust: None ), scan=pretend.call_recorder(lambda ignore: None), ) configurator_cls = pretend.call_recorder(lambda settings: configurator_obj) monkeypatch.setattr(config, "Configurator", configurator_cls) cachebuster_obj = pretend.stub() cachebuster_cls = pretend.call_recorder(lambda m, cache: cachebuster_obj) monkeypatch.setattr(config, "WarehouseCacheBuster", cachebuster_cls) config_defaults = pretend.call_recorder(lambda config, files: None) monkeypatch.setattr(config, "config_defaults", config_defaults) transaction_manager = pretend.stub() transaction = pretend.stub( TransactionManager=pretend.call_recorder(lambda: transaction_manager), ) monkeypatch.setattr(config, "transaction", transaction) result = config.configure(settings=settings) if settings is None: expected_settings = {} else: expected_settings = settings.copy() assert configurator_cls.calls == [pretend.call(settings=expected_settings)] assert config_defaults.calls == [ pretend.call(configurator_obj, ["warehouse:etc"]), ] assert result is configurator_obj assert configurator_obj.include.calls == [ pretend.call("tzf.pyramid_yml"), pretend.call(".logging"), pretend.call("pyramid_jinja2"), pretend.call("pyramid_tm"), pretend.call("pyramid_services"), pretend.call(".legacy.action_routing"), pretend.call(".i18n"), pretend.call(".db"), pretend.call(".sessions"), pretend.call(".cache.http"), pretend.call(".cache.origin"), pretend.call(".csrf"), pretend.call(".accounts"), pretend.call(".packaging"), pretend.call(".redirects"), pretend.call(".routes"), ] assert configurator_obj.add_jinja2_renderer.calls == [ pretend.call(".html"), ] assert configurator_obj.add_jinja2_search_path.calls == [ pretend.call("warehouse:templates", name=".html"), ] assert configurator_obj.add_settings.calls == [ pretend.call({ "tm.manager_hook": mock.ANY, "tm.activate_hook": config.activate_hook, "tm.annotate_user": False, }), pretend.call({ "csp": { "default-src": ["'none'"], "frame-ancestors": ["'none'"], "img-src": [ "'self'", "http://camo.example.com/", "https://secure.gravatar.com", ], "referrer": ["cross-origin"], "reflected-xss": ["block"], "script-src": ["'self'"], "style-src": ["'self'"], }, }), ] add_settings_dict = configurator_obj.add_settings.calls[0].args[0] assert add_settings_dict["tm.manager_hook"](pretend.stub()) is \ transaction_manager assert configurator_obj.add_tween.calls == [ pretend.call("warehouse.config.content_security_policy_tween_factory"), ] assert configurator_obj.registry["filesystems"] == {"packages": fs_obj} assert configurator_obj.add_static_view.calls == [ pretend.call( name="static", path="warehouse:static", cachebust=cachebuster_obj, ), ] assert cachebuster_cls.calls == [ pretend.call("warehouse:static/manifest.json", cache=True), ] assert configurator_obj.scan.calls == [ pretend.call(ignore=["warehouse.migrations.env"]), ] assert opener.calls == [ pretend.call("/srv/data/pypi/packages/", create_dir=True), ] assert configurator_obj.add_notfound_view.calls == [ pretend.call(append_slash=HTTPMovedPermanently), ] assert configurator_obj.add_renderer.calls == [ pretend.call("json", json_renderer_obj), ] assert json_renderer_cls.calls == [ pretend.call(sort_keys=True, separators=(",", ":")), ]
def test_configure(monkeypatch, settings, environment, other_settings): json_renderer_obj = pretend.stub() json_renderer_cls = pretend.call_recorder(lambda **kw: json_renderer_obj) monkeypatch.setattr(renderers, "JSON", json_renderer_cls) xmlrpc_renderer_obj = pretend.stub() xmlrpc_renderer_cls = pretend.call_recorder( lambda **kw: xmlrpc_renderer_obj) monkeypatch.setattr(config, "XMLRPCRenderer", xmlrpc_renderer_cls) if environment == config.Environment.development: monkeypatch.setenv("WAREHOUSE_ENV", "development") class FakeRegistry(dict): def __init__(self): self.settings = { "warehouse.token": "insecure token", "warehouse.env": environment, "camo.url": "http://camo.example.com/", "pyramid.reload_assets": False, "dirs.packages": "/srv/data/pypi/packages/", } configurator_settings = other_settings.copy() configurator_obj = pretend.stub( registry=FakeRegistry(), include=pretend.call_recorder(lambda include: None), add_directive=pretend.call_recorder(lambda name, fn: None), add_wsgi_middleware=pretend.call_recorder(lambda m, *a, **kw: None), add_renderer=pretend.call_recorder(lambda name, renderer: None), add_request_method=pretend.call_recorder(lambda fn: None), add_jinja2_renderer=pretend.call_recorder(lambda renderer: None), add_jinja2_search_path=pretend.call_recorder(lambda path, name: None), get_settings=lambda: configurator_settings, add_settings=pretend.call_recorder( lambda d: configurator_settings.update(d)), add_tween=pretend.call_recorder(lambda tween_factory, **kw: None), add_static_view=pretend.call_recorder(lambda name, path, **kw: None), scan=pretend.call_recorder(lambda ignore: None), ) configurator_cls = pretend.call_recorder(lambda settings: configurator_obj) monkeypatch.setattr(config, "Configurator", configurator_cls) cachebuster_obj = pretend.stub() cachebuster_cls = pretend.call_recorder(lambda p, reload: cachebuster_obj) monkeypatch.setattr(config, "ManifestCacheBuster", cachebuster_cls) transaction_manager = pretend.stub() transaction = pretend.stub(TransactionManager=pretend.call_recorder( lambda: transaction_manager), ) monkeypatch.setattr(config, "transaction", transaction) result = config.configure(settings=settings) expected_settings = { "warehouse.env": environment, "warehouse.commit": None, "site.name": "Warehouse", } if environment == config.Environment.development: expected_settings.update({ "enforce_https": False, "pyramid.reload_templates": True, "pyramid.reload_assets": True, "pyramid.prevent_http_cache": True, "debugtoolbar.hosts": ["0.0.0.0/0"], "debugtoolbar.panels": [ "pyramid_debugtoolbar.panels.versions.VersionDebugPanel", "pyramid_debugtoolbar.panels.settings.SettingsDebugPanel", "pyramid_debugtoolbar.panels.headers.HeaderDebugPanel", ("pyramid_debugtoolbar.panels.request_vars." "RequestVarsDebugPanel"), "pyramid_debugtoolbar.panels.renderings.RenderingsDebugPanel", "pyramid_debugtoolbar.panels.logger.LoggingPanel", ("pyramid_debugtoolbar.panels.performance." "PerformanceDebugPanel"), "pyramid_debugtoolbar.panels.routes.RoutesDebugPanel", "pyramid_debugtoolbar.panels.sqla.SQLADebugPanel", "pyramid_debugtoolbar.panels.tweens.TweensDebugPanel", ("pyramid_debugtoolbar.panels.introspection." "IntrospectionDebugPanel"), ], }) if settings is not None: expected_settings.update(settings) assert configurator_cls.calls == [pretend.call(settings=expected_settings)] assert result is configurator_obj assert configurator_obj.add_wsgi_middleware.calls == [ pretend.call(ProxyFixer, token="insecure token"), pretend.call(VhmRootRemover), ] assert configurator_obj.include.calls == ([ pretend.call(x) for x in [ ("pyramid_debugtoolbar" if environment == config.Environment.development else None), ] if x is not None ] + [ pretend.call(".logging"), pretend.call("pyramid_jinja2"), pretend.call("pyramid_tm"), pretend.call("pyramid_services"), pretend.call("pyramid_rpc.xmlrpc"), pretend.call(".legacy.action_routing"), pretend.call(".i18n"), pretend.call(".db"), pretend.call(".search"), pretend.call(".aws"), pretend.call(".celery"), pretend.call(".sessions"), pretend.call(".cache.http"), pretend.call(".cache.origin"), pretend.call(".csrf"), pretend.call(".accounts"), pretend.call(".packaging"), pretend.call(".redirects"), pretend.call(".routes"), pretend.call(".raven"), ] + [ pretend.call(x) for x in [ configurator_settings.get("warehouse.theme"), ] if x ]) assert configurator_obj.add_jinja2_renderer.calls == [ pretend.call(".html"), pretend.call(".txt"), pretend.call(".xml"), ] assert configurator_obj.add_jinja2_search_path.calls == [ pretend.call("warehouse:templates", name=".html"), pretend.call("warehouse:templates", name=".txt"), pretend.call("warehouse:templates", name=".xml"), ] assert configurator_obj.add_settings.calls == [ pretend.call({"jinja2.newstyle": True}), pretend.call({ "tm.attempts": 3, "tm.manager_hook": mock.ANY, "tm.activate_hook": config.activate_hook, "tm.annotate_user": False, }), pretend.call({ "csp": { "connect-src": ["'self'"], "default-src": ["'none'"], "font-src": ["'self'", "fonts.gstatic.com"], "frame-ancestors": ["'none'"], "img-src": [ "'self'", "http://camo.example.com/", "https://secure.gravatar.com", ], "referrer": ["origin-when-cross-origin"], "reflected-xss": ["block"], "report-uri": [None], "script-src": ["'self'"], "style-src": ["'self'", "fonts.googleapis.com"], }, }), ] add_settings_dict = configurator_obj.add_settings.calls[1].args[0] assert add_settings_dict["tm.manager_hook"](pretend.stub()) is \ transaction_manager assert configurator_obj.add_directive.calls == [ pretend.call("find_service_factory", config.find_service_factory), ] assert configurator_obj.add_request_method.calls == [ pretend.call(config.find_service_factory), ] assert configurator_obj.add_tween.calls == [ pretend.call("warehouse.config.content_security_policy_tween_factory"), pretend.call("warehouse.config.require_https_tween_factory"), pretend.call( "warehouse.utils.compression.compression_tween_factory", over=[ "warehouse.cache.http.conditional_http_tween_factory", "pyramid_debugtoolbar.toolbar_tween_factory", "warehouse.raven.raven_tween_factory", EXCVIEW, ], ), ] assert configurator_obj.add_static_view.calls == [ pretend.call( name="static", path="warehouse:static/dist/", cache_max_age=1, cachebust=cachebuster_obj, ), pretend.call(name="locales", path="warehouse:locales/"), ] assert cachebuster_cls.calls == [ pretend.call("warehouse:static/dist/manifest.json", reload=False), ] assert configurator_obj.scan.calls == [ pretend.call(ignore=["warehouse.migrations.env", "warehouse.wsgi"]), ] assert configurator_obj.add_renderer.calls == [ pretend.call("json", json_renderer_obj), pretend.call("xmlrpc", xmlrpc_renderer_obj), ] assert json_renderer_cls.calls == [ pretend.call(sort_keys=True, separators=(",", ":")), ] assert xmlrpc_renderer_cls.calls == [pretend.call(allow_none=True)]
def test_configure(monkeypatch, settings, environment, other_settings): json_renderer_obj = pretend.stub() json_renderer_cls = pretend.call_recorder(lambda **kw: json_renderer_obj) monkeypatch.setattr(renderers, "JSON", json_renderer_cls) xmlrpc_renderer_obj = pretend.stub() xmlrpc_renderer_cls = pretend.call_recorder( lambda **kw: xmlrpc_renderer_obj ) monkeypatch.setattr(config, "XMLRPCRenderer", xmlrpc_renderer_cls) if environment == config.Environment.development: monkeypatch.setenv("WAREHOUSE_ENV", "development") class FakeRegistry(dict): def __init__(self): self.settings = { "warehouse.token": "insecure token", "warehouse.env": environment, "camo.url": "http://camo.example.com/", "pyramid.reload_assets": False, "dirs.packages": "/srv/data/pypi/packages/", } configurator_settings = other_settings.copy() configurator_obj = pretend.stub( registry=FakeRegistry(), include=pretend.call_recorder(lambda include: None), add_directive=pretend.call_recorder(lambda name, fn, **k: None), add_wsgi_middleware=pretend.call_recorder(lambda m, *a, **kw: None), add_renderer=pretend.call_recorder(lambda name, renderer: None), add_request_method=pretend.call_recorder(lambda fn: None), add_jinja2_renderer=pretend.call_recorder(lambda renderer: None), add_jinja2_search_path=pretend.call_recorder(lambda path, name: None), get_settings=lambda: configurator_settings, add_settings=pretend.call_recorder( lambda d: configurator_settings.update(d) ), add_tween=pretend.call_recorder(lambda tween_factory, **kw: None), add_static_view=pretend.call_recorder(lambda *a, **kw: None), add_cache_buster=pretend.call_recorder(lambda spec, buster: None), scan=pretend.call_recorder(lambda ignore: None), commit=pretend.call_recorder(lambda: None), ) configurator_cls = pretend.call_recorder(lambda settings: configurator_obj) monkeypatch.setattr(config, "Configurator", configurator_cls) cachebuster_obj = pretend.stub() cachebuster_cls = pretend.call_recorder(lambda p, **kw: cachebuster_obj) monkeypatch.setattr(config, "ManifestCacheBuster", cachebuster_cls) transaction_manager = pretend.stub() transaction = pretend.stub( TransactionManager=pretend.call_recorder(lambda: transaction_manager), ) monkeypatch.setattr(config, "transaction", transaction) result = config.configure(settings=settings) expected_settings = { "warehouse.env": environment, "warehouse.commit": None, "site.name": "Warehouse", "mail.ssl": True } if environment == config.Environment.development: expected_settings.update({ "enforce_https": False, "pyramid.reload_templates": True, "pyramid.reload_assets": True, "pyramid.prevent_http_cache": True, "debugtoolbar.hosts": ["0.0.0.0/0"], "debugtoolbar.panels": [ "pyramid_debugtoolbar.panels.versions.VersionDebugPanel", "pyramid_debugtoolbar.panels.settings.SettingsDebugPanel", "pyramid_debugtoolbar.panels.headers.HeaderDebugPanel", ( "pyramid_debugtoolbar.panels.request_vars." "RequestVarsDebugPanel" ), "pyramid_debugtoolbar.panels.renderings.RenderingsDebugPanel", "pyramid_debugtoolbar.panels.logger.LoggingPanel", ( "pyramid_debugtoolbar.panels.performance." "PerformanceDebugPanel" ), "pyramid_debugtoolbar.panels.routes.RoutesDebugPanel", "pyramid_debugtoolbar.panels.sqla.SQLADebugPanel", "pyramid_debugtoolbar.panels.tweens.TweensDebugPanel", ( "pyramid_debugtoolbar.panels.introspection." "IntrospectionDebugPanel" ), ], }) if settings is not None: expected_settings.update(settings) assert configurator_cls.calls == [pretend.call(settings=expected_settings)] assert result is configurator_obj assert configurator_obj.add_wsgi_middleware.calls == [ pretend.call(ProxyFixer, token="insecure token", num_proxies=1), pretend.call(VhmRootRemover), pretend.call(HostRewrite), ] assert configurator_obj.include.calls == ( [pretend.call(".csrf")] + [ pretend.call(x) for x in [ ( "pyramid_debugtoolbar" if environment == config.Environment.development else None ), ] if x is not None ] + [ pretend.call(".logging"), pretend.call("pyramid_jinja2"), ] + [ pretend.call(x) for x in [ ( "pyramid_mailer.debug" if environment == config.Environment.development else "pyramid_mailer" ), ] ] + [ pretend.call("pyramid_tm"), pretend.call("pyramid_services"), pretend.call("pyramid_rpc.xmlrpc"), pretend.call(".legacy.action_routing"), pretend.call(".domain"), pretend.call(".i18n"), pretend.call(".db"), pretend.call(".search"), pretend.call(".aws"), pretend.call(".celery"), pretend.call(".sessions"), pretend.call(".cache.http"), pretend.call(".cache.origin"), pretend.call(".accounts"), pretend.call(".packaging"), pretend.call(".redirects"), pretend.call(".routes"), pretend.call(".forklift"), pretend.call(".raven"), pretend.call(".csp"), pretend.call(".recaptcha"), pretend.call(".http"), ] + [ pretend.call(x) for x in [ configurator_settings.get("warehouse.theme"), ] if x ] ) assert configurator_obj.add_jinja2_renderer.calls == [ pretend.call(".html"), pretend.call(".txt"), pretend.call(".xml"), ] assert configurator_obj.add_jinja2_search_path.calls == [ pretend.call("warehouse:templates", name=".html"), pretend.call("warehouse:templates", name=".txt"), pretend.call("warehouse:templates", name=".xml"), ] assert configurator_obj.add_settings.calls == [ pretend.call({"jinja2.newstyle": True}), pretend.call({ "tm.attempts": 3, "tm.manager_hook": mock.ANY, "tm.activate_hook": config.activate_hook, "tm.annotate_user": False, }), pretend.call({ "http": { "verify": "/etc/ssl/certs/", }, }), ] add_settings_dict = configurator_obj.add_settings.calls[1].args[0] assert add_settings_dict["tm.manager_hook"](pretend.stub()) is \ transaction_manager assert configurator_obj.add_tween.calls == [ pretend.call("warehouse.config.require_https_tween_factory"), pretend.call( "warehouse.utils.compression.compression_tween_factory", over=[ "warehouse.cache.http.conditional_http_tween_factory", "pyramid_debugtoolbar.toolbar_tween_factory", "warehouse.raven.raven_tween_factory", EXCVIEW, ], ), ] assert configurator_obj.add_static_view.calls == [ pretend.call( "static", "warehouse:static/dist/", cache_max_age=315360000, ), pretend.call("locales", "warehouse:locales/"), ] assert configurator_obj.add_cache_buster.calls == [ pretend.call("warehouse:static/dist/", cachebuster_obj), ] assert cachebuster_cls.calls == [ pretend.call( "warehouse:static/dist/manifest.json", reload=False, strict=True, ), ] assert configurator_obj.add_directive.calls == [ pretend.call( "add_template_view", config.template_view, action_wrap=False, ), ] assert configurator_obj.scan.calls == [ pretend.call(ignore=["warehouse.migrations.env", "warehouse.wsgi"]), ] assert configurator_obj.commit.calls == [pretend.call()] assert configurator_obj.add_renderer.calls == [ pretend.call("json", json_renderer_obj), pretend.call("xmlrpc", xmlrpc_renderer_obj), ] assert json_renderer_cls.calls == [ pretend.call(sort_keys=True, separators=(",", ":")), ] assert xmlrpc_renderer_cls.calls == [pretend.call(allow_none=True)]
def _configure_celery(*args, **kwargs): configure()
# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from warehouse.config import configure application = configure().make_wsgi_app()
def test_configure(monkeypatch, settings, environment, other_settings): json_renderer_obj = pretend.stub() json_renderer_cls = pretend.call_recorder(lambda **kw: json_renderer_obj) monkeypatch.setattr(renderers, "JSON", json_renderer_cls) xmlrpc_renderer_obj = pretend.stub() xmlrpc_renderer_cls = pretend.call_recorder( lambda **kw: xmlrpc_renderer_obj) monkeypatch.setattr(config, "XMLRPCRenderer", xmlrpc_renderer_cls) if environment == config.Environment.development: monkeypatch.setenv("WAREHOUSE_ENV", "development") class FakeRegistry(dict): def __init__(self): self.settings = { "warehouse.token": "insecure token", "warehouse.env": environment, "camo.url": "http://camo.example.com/", "pyramid.reload_assets": False, "dirs.packages": "/srv/data/pypi/packages/", "warehouse.xmlrpc.client.ratelimit_string": "3600 per hour", } configurator_settings = other_settings.copy() configurator_obj = pretend.stub( registry=FakeRegistry(), set_root_factory=pretend.call_recorder(lambda rf: None), include=pretend.call_recorder(lambda include: None), add_directive=pretend.call_recorder(lambda name, fn, **k: None), add_wsgi_middleware=pretend.call_recorder(lambda m, *a, **kw: None), add_renderer=pretend.call_recorder(lambda name, renderer: None), add_request_method=pretend.call_recorder(lambda fn: None), add_jinja2_renderer=pretend.call_recorder(lambda renderer: None), add_jinja2_search_path=pretend.call_recorder(lambda path, name: None), get_settings=lambda: configurator_settings, add_settings=pretend.call_recorder( lambda d: configurator_settings.update(d)), add_tween=pretend.call_recorder(lambda tween_factory, **kw: None), add_static_view=pretend.call_recorder(lambda *a, **kw: None), add_cache_buster=pretend.call_recorder(lambda spec, buster: None), whitenoise_serve_static=pretend.call_recorder(lambda *a, **kw: None), whitenoise_add_files=pretend.call_recorder(lambda *a, **kw: None), whitenoise_add_manifest=pretend.call_recorder(lambda *a, **kw: None), scan=pretend.call_recorder(lambda ignore: None), commit=pretend.call_recorder(lambda: None), ) configurator_cls = pretend.call_recorder(lambda settings: configurator_obj) monkeypatch.setattr(config, "Configurator", configurator_cls) cachebuster_obj = pretend.stub() cachebuster_cls = pretend.call_recorder(lambda p, **kw: cachebuster_obj) monkeypatch.setattr(config, "ManifestCacheBuster", cachebuster_cls) transaction_manager = pretend.stub() transaction = pretend.stub( TransactionManager=pretend.call_recorder(lambda: transaction_manager)) monkeypatch.setattr(config, "transaction", transaction) result = config.configure(settings=settings) expected_settings = { "warehouse.env": environment, "warehouse.commit": "null", "site.name": "Warehouse", "token.two_factor.max_age": 300, "token.default.max_age": 21600, "warehouse.xmlrpc.client.ratelimit_string": "3600 per hour", } if environment == config.Environment.development: expected_settings.update({ "enforce_https": False, "pyramid.reload_templates": True, "pyramid.reload_assets": True, "pyramid.prevent_http_cache": True, "debugtoolbar.hosts": ["0.0.0.0/0"], "debugtoolbar.panels": [ "pyramid_debugtoolbar.panels.versions.VersionDebugPanel", "pyramid_debugtoolbar.panels.settings.SettingsDebugPanel", "pyramid_debugtoolbar.panels.headers.HeaderDebugPanel", ("pyramid_debugtoolbar.panels.request_vars." "RequestVarsDebugPanel"), "pyramid_debugtoolbar.panels.renderings.RenderingsDebugPanel", "pyramid_debugtoolbar.panels.logger.LoggingPanel", ("pyramid_debugtoolbar.panels.performance." "PerformanceDebugPanel"), "pyramid_debugtoolbar.panels.routes.RoutesDebugPanel", "pyramid_debugtoolbar.panels.sqla.SQLADebugPanel", "pyramid_debugtoolbar.panels.tweens.TweensDebugPanel", ("pyramid_debugtoolbar.panels.introspection." "IntrospectionDebugPanel"), ], }) if settings is not None: expected_settings.update(settings) assert configurator_cls.calls == [pretend.call(settings=expected_settings)] assert result is configurator_obj assert configurator_obj.set_root_factory.calls == [ pretend.call(config.RootFactory) ] assert configurator_obj.add_wsgi_middleware.calls == [ pretend.call(ProxyFixer, token="insecure token", num_proxies=1), pretend.call(VhmRootRemover), pretend.call(HostRewrite), ] assert configurator_obj.include.calls == ([ pretend.call("pyramid_services"), pretend.call(".metrics"), pretend.call(".csrf"), ] + [ pretend.call(x) for x in [("pyramid_debugtoolbar" if environment == config.Environment.development else None)] if x is not None ] + [ pretend.call(".logging"), pretend.call("pyramid_jinja2"), pretend.call(".filters"), pretend.call("pyramid_mailer"), pretend.call("pyramid_retry"), pretend.call("pyramid_tm"), pretend.call(".legacy.api.xmlrpc"), pretend.call(".legacy.api.xmlrpc.cache"), pretend.call("pyramid_rpc.xmlrpc"), pretend.call(".legacy.action_routing"), pretend.call(".predicates"), pretend.call(".i18n"), pretend.call(".db"), pretend.call(".tasks"), pretend.call(".rate_limiting"), pretend.call(".static"), pretend.call(".policy"), pretend.call(".search"), pretend.call(".aws"), pretend.call(".gcloud"), pretend.call(".sessions"), pretend.call(".cache.http"), pretend.call(".cache.origin"), pretend.call(".email"), pretend.call(".accounts"), pretend.call(".macaroons"), pretend.call(".malware"), pretend.call(".manage"), pretend.call(".packaging"), pretend.call(".redirects"), pretend.call(".routes"), pretend.call(".admin"), pretend.call(".forklift"), pretend.call(".sentry"), pretend.call(".csp"), pretend.call(".referrer_policy"), pretend.call(".http"), ] + [ pretend.call(x) for x in [configurator_settings.get("warehouse.theme")] if x ] + [pretend.call(".sanity")]) assert configurator_obj.add_jinja2_renderer.calls == [ pretend.call(".html"), pretend.call(".txt"), pretend.call(".xml"), ] assert configurator_obj.add_jinja2_search_path.calls == [ pretend.call("warehouse:templates", name=".html"), pretend.call("warehouse:templates", name=".txt"), pretend.call("warehouse:templates", name=".xml"), ] assert configurator_obj.add_settings.calls == [ pretend.call({"jinja2.newstyle": True}), pretend.call({"jinja2.i18n.domain": "messages"}), pretend.call({"retry.attempts": 3}), pretend.call({ "tm.manager_hook": mock.ANY, "tm.activate_hook": config.activate_hook, "tm.commit_veto": config.commit_veto, "tm.annotate_user": False, }), pretend.call({"http": { "verify": "/etc/ssl/certs/" }}), ] add_settings_dict = configurator_obj.add_settings.calls[3].args[0] assert add_settings_dict["tm.manager_hook"]( pretend.stub()) is transaction_manager assert configurator_obj.add_tween.calls == [ pretend.call("warehouse.config.require_https_tween_factory"), pretend.call( "warehouse.utils.compression.compression_tween_factory", over=[ "warehouse.cache.http.conditional_http_tween_factory", "pyramid_debugtoolbar.toolbar_tween_factory", EXCVIEW, ], ), ] assert configurator_obj.add_static_view.calls == [ pretend.call("static", "warehouse:static/dist/", cache_max_age=315360000) ] assert configurator_obj.add_cache_buster.calls == [ pretend.call("warehouse:static/dist/", cachebuster_obj) ] assert cachebuster_cls.calls == [ pretend.call("warehouse:static/dist/manifest.json", reload=False, strict=True) ] assert configurator_obj.whitenoise_serve_static.calls == [ pretend.call(autorefresh=False, max_age=315360000) ] assert configurator_obj.whitenoise_add_files.calls == [ pretend.call("warehouse:static/dist/", prefix="/static/") ] assert configurator_obj.whitenoise_add_manifest.calls == [ pretend.call("warehouse:static/dist/manifest.json", prefix="/static/") ] assert configurator_obj.add_directive.calls == [ pretend.call("add_template_view", config.template_view, action_wrap=False) ] assert configurator_obj.scan.calls == [ pretend.call(ignore=[ "warehouse.migrations.env", "warehouse.celery", "warehouse.wsgi" ]) ] assert configurator_obj.commit.calls == [pretend.call()] assert configurator_obj.add_renderer.calls == [ pretend.call("json", json_renderer_obj), pretend.call("xmlrpc", xmlrpc_renderer_obj), ] assert json_renderer_cls.calls == [ pretend.call(sort_keys=True, separators=(",", ":")) ] assert xmlrpc_renderer_cls.calls == [pretend.call(allow_none=True)]
# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from warehouse.config import configure app = configure().make_celery_app()
def test_configure(monkeypatch, settings): fs_obj = pretend.stub() opener = pretend.call_recorder(lambda path, create_dir: fs_obj) monkeypatch.setattr(fs.opener, "fsopendir", opener) json_renderer_obj = pretend.stub() json_renderer_cls = pretend.call_recorder(lambda **kw: json_renderer_obj) monkeypatch.setattr(renderers, "JSON", json_renderer_cls) class FakeRegistry(dict): def __init__(self): self.settings = { "camo.url": "http://camo.example.com/", "pyramid.reload_assets": False, "dirs.packages": "/srv/data/pypi/packages/", } configurator_settings = {} configurator_obj = pretend.stub( registry=FakeRegistry(), include=pretend.call_recorder(lambda include: None), add_renderer=pretend.call_recorder(lambda name, renderer: None), add_jinja2_renderer=pretend.call_recorder(lambda renderer: None), add_jinja2_search_path=pretend.call_recorder(lambda path, name: None), get_settings=lambda: configurator_settings, add_settings=pretend.call_recorder( lambda d: configurator_settings.update(d)), add_tween=pretend.call_recorder(lambda tween_factory: None), add_notfound_view=pretend.call_recorder(lambda append_slash: None), add_static_view=pretend.call_recorder( lambda name, path, cachebust: None), scan=pretend.call_recorder(lambda ignore: None), ) configurator_cls = pretend.call_recorder(lambda settings: configurator_obj) monkeypatch.setattr(config, "Configurator", configurator_cls) cachebuster_obj = pretend.stub() cachebuster_cls = pretend.call_recorder(lambda m, cache: cachebuster_obj) monkeypatch.setattr(config, "WarehouseCacheBuster", cachebuster_cls) config_defaults = pretend.call_recorder(lambda config, files: None) monkeypatch.setattr(config, "config_defaults", config_defaults) transaction_manager = pretend.stub() transaction = pretend.stub(TransactionManager=pretend.call_recorder( lambda: transaction_manager), ) monkeypatch.setattr(config, "transaction", transaction) result = config.configure(settings=settings) if settings is None: expected_settings = {} else: expected_settings = settings.copy() assert configurator_cls.calls == [pretend.call(settings=expected_settings)] assert config_defaults.calls == [ pretend.call(configurator_obj, ["warehouse:etc"]), ] assert result is configurator_obj assert configurator_obj.include.calls == [ pretend.call("tzf.pyramid_yml"), pretend.call(".logging"), pretend.call("pyramid_jinja2"), pretend.call("pyramid_tm"), pretend.call("pyramid_services"), pretend.call(".i18n"), pretend.call(".db"), pretend.call(".sessions"), pretend.call(".cache.http"), pretend.call(".cache.origin"), pretend.call(".csrf"), pretend.call(".accounts"), pretend.call(".packaging"), pretend.call(".redirects"), pretend.call(".routes"), ] assert configurator_obj.add_jinja2_renderer.calls == [ pretend.call(".html"), ] assert configurator_obj.add_jinja2_search_path.calls == [ pretend.call("warehouse:templates", name=".html"), ] assert configurator_obj.add_settings.calls == [ pretend.call({ "tm.manager_hook": mock.ANY, "tm.activate_hook": config.activate_hook, }), pretend.call({ "csp": { "default-src": ["'none'"], "frame-ancestors": ["'none'"], "img-src": [ "'self'", "http://camo.example.com/", "https://secure.gravatar.com", ], "referrer": ["cross-origin"], "reflected-xss": ["block"], "script-src": ["'self'"], "style-src": ["'self'"], }, }), ] add_settings_dict = configurator_obj.add_settings.calls[0].args[0] assert add_settings_dict["tm.manager_hook"](pretend.stub()) is \ transaction_manager assert configurator_obj.add_tween.calls == [ pretend.call("warehouse.config.content_security_policy_tween_factory"), ] assert configurator_obj.registry["filesystems"] == {"packages": fs_obj} assert configurator_obj.add_static_view.calls == [ pretend.call( name="static", path="warehouse:static", cachebust=cachebuster_obj, ), ] assert cachebuster_cls.calls == [ pretend.call("warehouse:static/manifest.json", cache=True), ] assert configurator_obj.scan.calls == [ pretend.call(ignore=["warehouse.migrations.env"]), ] assert opener.calls == [ pretend.call("/srv/data/pypi/packages/", create_dir=True), ] assert configurator_obj.add_notfound_view.calls == [ pretend.call(append_slash=HTTPMovedPermanently), ] assert configurator_obj.add_renderer.calls == [ pretend.call("json", json_renderer_obj), ] assert json_renderer_cls.calls == [ pretend.call(sort_keys=True, separators=(",", ":")), ]
def __getattr__(self, name): if self.__config is None: from warehouse.config import configure self.__config = configure(*self.__args, **self.__kwargs) return getattr(self.__config, name)