Exemplo n.º 1
0
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 == []
Exemplo n.º 2
0
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']
Exemplo n.º 3
0
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"}')
Exemplo n.º 4
0
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'}
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
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
Exemplo n.º 8
0
def test_post_index(client: Client):
    client.get_json('/')
    assert client.post_json('/', status=None)
    assert client.last_request.status_code == 405
Exemplo n.º 9
0
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'
Exemplo n.º 10
0
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
Exemplo n.º 11
0
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'
    }
Exemplo n.º 12
0
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,
    }
Exemplo n.º 13
0
def test_request_error(client: Client, mocker):
    fail_mock = mocker.patch('foxglove.testing.pytest.fail')
    client.post_json('/')
    fail_mock.called_once()
Exemplo n.º 14
0
def test_index(client: Client):
    assert client.post_json('/no-csrf/') is None
    assert client.last_request.status_code == 200
Exemplo n.º 15
0
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
Exemplo n.º 16
0
def fix_client(settings: Settings, db_conn, glove):
    app = settings.create_app()
    with Client(app) as client:
        yield client