def request_environ_set(app, data): def handler(sender, **kwargs): for (k, v) in data.items(): request.environ[k] = v with request_started.connected_to(handler, app): yield
def test_create_failed_message(self, mock_publish): """Assert test results can be created.""" test_run = { "kernel_version": "5.1.2", "build_release": "300.fc30", "arch": "aarch64", "fedora_version": 29, "tests": [{ "name": "Boot test", "passed": True, "waived": False, "details": "Something something booted successfully", }], } db.Session.add(db.Release(version=29)) db.Session.commit() mock_publish.side_effect = fm_exceptions.PublishException() with mock.patch.object(authentication.oidc, "validate_token", return_value=True): def handler(sender, **kwargs): g.user = User(None, None, "jcline") with request_started.connected_to(handler, self.flask_app): result = self.flask_client.post("/api/v1/results/", json=test_run) assert result.status_code == 201 assert db.TestRun.query.count() == 1 assert db.Test.query.count() == 1
def factory(user): def push(sender): if user: login_user(user) else: logout_user() with request_started.connected_to(push): yield
def user_set(app, user): print 'ready handler' def handler(sender, **kwargs): print 'run handler' print 'request: ', request g.user = user with request_started.connected_to(handler, app): print 'with start' yield print 'with end'
def login_user(app, user): """ A context manager to log a user in for testing purposes. For example: >>> with login_user(self.flask_app, user): ... self.flask_app.test_client().get('/protected/view') The above example will cause the request to ``/protected/view`` to occur with the provided user being authenticated. Args: app (flask.Flask): An instance of the Flask application. user (kerneltest.app.User): The user to log in. """ def handler(sender, **kwargs): g.user = user with request_started.connected_to(handler, app): yield
def login_user(app, user): """ A context manager to log a user in for testing purposes. For example: >>> with login_user(self.flask_app, user): ... self.flask_app.test_client().get('/protected/view') The above example will cause the request to ``/protected/view`` to occur with the provided user being authenticated. Args: app (flask.Flask): An instance of the Flask application. user (models.User): The user to log in. Note that this user must be committed to the database as it needs a ``user.id`` value. """ def handler(sender, **kwargs): flask_login.login_user(user) with request_started.connected_to(handler, app): yield
def mock_ip(app, ip): def handler(sender, **kwargs): request.remote_addr = ip with request_started.connected_to(handler, app): yield