def server(db_apis, request): proc = ServerMonitor(rdb_url=ERT_STORAGE.rdb_url, blob_url=ERT_STORAGE.blob_url, lockfile=False) proc.start() request.addfinalizer(lambda: proc.shutdown()) yield proc
def do_patch(resource_method_to_authorized={}): # Resource/Method to authorized: { RESOURCE: { METHOD: True/False } } def make_mock_response(method, url, *args, **kwargs): method = method.upper() mocked_response = mock.MagicMock(requests.Response) if url != f"{arborist_base_url}/auth/request": mocked_response.status_code = 404 mocked_response.text = "NOT FOUND" elif method != "POST": mocked_response.status_code = 405 mocked_response.text = "METHOD NOT ALLOWED" else: authz_res, authz_met = None, None authz_requests = kwargs["json"]["requests"] if authz_requests: authz_res = kwargs["json"]["requests"][0]["resource"] authz_met = kwargs["json"]["requests"][0]["action"][ "method"] authorized = resource_method_to_authorized.get( authz_res, {}).get(authz_met, False) mocked_response.status_code = 200 mocked_response.json.return_value = {"auth": authorized} return mocked_response mocked_method = mock.MagicMock(side_effect=make_mock_response) patch_method = patch( "gen3authz.client.arborist.client.httpx.Client.request", mocked_method) patch_method.start() request.addfinalizer(patch_method.stop)
def client(request): test_client = app.test_client() def teardown(): pass request.addfinalizer(teardown) return test_client
def session(db, request): session = db.create_scoped_session() db.session = session def teardown(): session.remove() request.addfinalizer(teardown) return session
def client(original, request): client = original check_auth = auth.check_auth get_response = services.player.get_response def teardown(): auth.check_auth = check_auth services.player.get_response = get_response request.addfinalizer(teardown) return client
def funcprep(request, app): # function level bring up and teardown logger.debug("%s funcprep start", "*"*80) def teardown(): logger.debug("%s funcprep tear down", "-"*80) # kill proxy_server Rest_TestProxy.delete(_filters={}) request.addfinalizer(teardown) logger.debug("********** funcprep completed") return
def original(request): db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp() flaskr.app.config['TESTING'] = True client = flaskr.app.test_client() with flaskr.app.app_context(): flaskr.init_db() def teardown(): os.close(db_fd) os.unlink(flaskr.app.config['DATABASE']) request.addfinalizer(teardown) return client
def client(request): db_fd, coinmart.app.config['DATABASE'] = tempfile.mkstemp() coinmart.app.config['TESTING'] = True client = coinmart.app.test_client() with coinmart.app.app_context(): coinmart.init_db() def teardown(): os.close(db_fd) os.unlink(coinmart.app.config['DATABASE']) request.addfinalizer(teardown) return client
def app(request, app): # module level setup executed before any 'user' test in current file logger.debug("%s module-prep start", "-"*80) app.config["LOGIN_ENABLED"] = False t_app = create_app("config.py") t_app.db = get_db() t_app.client = t_app.test_client() t_app.config["LOGIN_ENABLED"] = False t_app.config["PROXY_URL"] = "http://127.0.0.1:%s" % PROXY_PORT t_app.config["TMP_DIR"] = os.path.realpath("%s/test" % t_app.config["TMP_DIR"]) # create test directory if not present if not os.path.isdir(t_app.config["TMP_DIR"]): os.makedirs(t_app.config["TMP_DIR"]) # copy testdata files to /tmp/ directory before tests start testfiles = [ "%s/download.txt" % testdata, ] logger.debug("copying testdata files to tmp directory") for tf in testfiles: dst = re.sub("//", "/", "%s/%s" % (t_app.config["TMP_DIR"], tf.split("/")[-1])) logger.debug("copying testfile from %s to %s", tf, dst) if not os.path.exists(dst): shutil.copy(tf, dst) logger.debug("setting up proxy server") proxy_server = Process(target=run_proxy_app, args=(t_app,)) proxy_server.start() # give the server a second to start logger.debug("waiting for proxy server to start") time.sleep(2) # ensure uni exists uni = Universe() assert uni.save() # teardown called after all tests in session have completed def teardown(): logger.debug("%s module-prep teardown", "-"*80) if proxy_server.is_alive(): logger.debug("terminating proxy server (%s)", proxy_server.pid) kill_group(proxy_server.pid) terminate_process(proxy_server) shutil.rmtree(t_app.config["TMP_DIR"]) request.addfinalizer(teardown) logger.debug("(proxy) module level app setup completed") return t_app
def ldap_app(request): """app ficture for testing LDAP connections.""" config = { "TESTING": True, "DEBUG": True, "SERVER_NAME": "fakey.server.name", "LDAP_HOST": "ldap://test_ldap_server", "WTF_CSRF_ENABLED": False, "MONGO_DBNAME": "testdb", } app = create_app(config=config) ctx = app.app_context() ctx.push() def teardown(): ctx.pop() request.addfinalizer(teardown) return app
def _use_mock_authz(allowed_permissions=None): """ Args: allowed_permissions (list of (string, list) tuples), (optional): Only authorize the listed (method, resources) tuples. By default, authorize all requests. """ if allowed_permissions is None: mock_authz = lambda *x: x else: assert isinstance(allowed_permissions, list) def mock_authz(method, resources): for resource in resources: if (method, resource) not in allowed_permissions: raise AuthError( "Mock indexd.auth.authz: ({},{}) is not one of the allowed permissions: {}" .format(method, resource, allowed_permissions)) patched_authz = patch("flask.current_app.auth.authz", mock_authz) patched_authz.start() request.addfinalizer(patched_authz.stop)
def http_nlg(request): http_server = WSGIServer(application=nlg_app()) http_server.start() request.addfinalizer(http_server.stop) return http_server.url
def http_nlg(request): http_server = WSGIServer(application=nlg_app()) http_server.start() request.addfinalizer(http_server.stop) return http_server.url