def test_no_token(client: Client, settings, dummy_server: DummyServer): settings.recaptcha_url = f'{dummy_server.server_name}/recaptcha_url/' assert client.get_json('/') == {'app': 'foxglove-demo'} assert client.post_json('/captcha-check/', {}, status=400) == { 'message': 'No recaptcha value' } assert dummy_server.log == []
def test_success(client: Client, settings, dummy_server: DummyServer, caplog): caplog.set_level(logging.INFO) settings.recaptcha_url = f'{dummy_server.server_name}/recaptcha_url/' assert client.get_json('/') == {'app': 'foxglove-demo'} assert client.post_json('/captcha-check/', {'recaptcha_token': '__ok__'}) == { 'status': 'ok' } assert dummy_server.log == [ 'POST /recaptcha_url/ > 200 (recaptcha __ok__)' ] logs = [ r.message for r in caplog.records if r.name == 'foxglove.recaptcha' ] assert logs == ['recaptcha success']
def test_bad_token(client: Client, settings, dummy_server: DummyServer, caplog): caplog.set_level(logging.INFO) settings.recaptcha_url = f'{dummy_server.server_name}/recaptcha_url/' assert client.get_json('/') == {'app': 'foxglove-demo'} assert client.post_json('/captcha-check/', {'recaptcha_token': 'bad'}, status=400) == { 'message': 'Invalid recaptcha value' } assert dummy_server.log == ['POST /recaptcha_url/ > 200 (recaptcha bad)'] logs = [ r.message for r in caplog.records if r.name == 'foxglove.recaptcha' ] assert len(logs) == 1 assert logs[0] == ( 'recaptcha failure, path=/captcha-check/ request_host=testserver ip=testclient ' 'response={"success": false, "hostname": "testserver"}')
def test_errors_raise_unexpected(client: Client, caplog): assert client.get_json('/error/', status=400) == {'message': 'raised HttpBadRequest'} assert len(caplog.records) == 1, caplog.text assert '"GET /error/", unexpected response: 400' in caplog.text r = caplog.records[0] assert r.user == {'ip_address': 'testclient'} assert r.request['url'] == 'http://testserver/error/' assert r.extra['route_endpoint'] == 'error' assert r.extra['response_body'] == {'message': 'raised HttpBadRequest'}
def test_errors_expected_sentry(client_sentry: Client, caplog, mocker): m = mocker.patch('foxglove.middleware.capture_event') assert client_sentry.get_json('/error/', status=400) == {'message': 'raised HttpBadRequest'} assert len(caplog.records) == 1, caplog.text msg = '"GET /error/", unexpected response: 400' assert msg in caplog.text m.assert_called_once() assert m.call_args.args[0]['message'] == msg assert m.call_args.args[0]['fingerprint'] == ('/error/', 'GET', '400') assert m.call_args.args[1] is None
def test_errors_request_return_unexpected(client: Client, caplog, mocker): m = mocker.patch('foxglove.middleware.capture_event') assert client.get_json('/error/', params={'error': 'return'}, status=400) == {'error': 'return'} assert len(caplog.records) == 1, caplog.text assert '"GET /error/?error=return", unexpected response: 400' in caplog.text r = caplog.records[0] assert r.user == {'ip_address': 'testclient'} assert r.request['url'] == 'http://testserver/error/?error=return' assert r.extra['route_endpoint'] == 'error' assert r.extra['response_body'] == {'error': 'return'} assert m.call_count == 0
def test_errors_exception_sentry(client_sentry: Client, caplog, mocker): m = mocker.patch('foxglove.middleware.capture_event') r = client_sentry.get('/error/', params={'error': 'RuntimeError'}) assert r.status_code == 500, r.text assert len(caplog.records) == 1, caplog.text msg = '"GET /error/?error=RuntimeError", RuntimeError(\'raised RuntimeError\')' assert msg in caplog.text m.assert_called_once() assert m.call_args.args[0]['message'] == msg assert m.call_args.args[0]['fingerprint'] == ('/error/', 'GET', "RuntimeError('raised RuntimeError')") assert m.call_args.args[1] is not None
def test_post_index(client: Client): client.get_json('/') assert client.post_json('/', status=None) assert client.last_request.status_code == 405
def test_errors_exception(client: Client, caplog): r = client.get('/error/', params={'error': 'RuntimeError'}) assert r.status_code == 500, r.text assert len(caplog.records) == 1, caplog.text assert '"GET /error/?error=RuntimeError", RuntimeError(' in caplog.text assert caplog.records[0].request['url'] == 'http://testserver/error/?error=RuntimeError'
def test_index(client: Client): assert client.last_request is None assert client.get_json('/') == {'app': 'foxglove-demo'} assert client.last_request.status_code == 200
def test_no_session_id(client: Client): assert client.post_json('/create-user/', {'first_name': 'Samuel', 'last_name': 'Colvin'}, status=403) == { 'message': 'Permission Denied, no session set, updates not permitted' }
def test_create_user(client: Client): client.get_json('/') assert client.post_json('/create-user/', {'first_name': 'Samuel', 'last_name': 'Colvin'}, status=201) == { 'id': 123, 'v': 16, }
def test_request_error(client: Client, mocker): fail_mock = mocker.patch('foxglove.testing.pytest.fail') client.post_json('/') fail_mock.called_once()
def test_index(client: Client): assert client.post_json('/no-csrf/') is None assert client.last_request.status_code == 200
def fix_client_sentry(settings: Settings, db_conn, glove): settings.sentry_dsn = 'https://[email protected]/789' glove._settings = settings app = settings.create_app() with Client(app) as client: yield client
def fix_client(settings: Settings, db_conn, glove): app = settings.create_app() with Client(app) as client: yield client