def test_update_edit_raw_valid(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    assert not read()
    res = test.post(url_for('jsondash.create'),
                    data=dict(id='a-b-c-d-e',
                              layout='freeform',
                              name='newname',
                              date='',
                              modules=[]),
                    follow_redirects=True)
    assert len(read()) == 1
    view_id = read()[0]['id']
    data = {
        'date': '',
        'layout': 'freeform',
        'id': 'a-b-c-d-e',
        'name': 'foo',
        'modules': []
    }
    view = json.dumps(data)
    readfunc = read(override=dict(data))
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    res = test.post(url_for('jsondash.update', c_id=view_id),
                    data={
                        'config': view,
                        'edit-raw': 'on'
                    },
                    follow_redirects=True)
    dom = pq(res.data)
    flash_msg = 'Updated view "{}"'.format(view_id)
    assert dom.find('.alert-info').text() == flash_msg
    assert len(read()) == 1
def test_set_global_flag_when_creating_dashboard(ctx, client, monkeypatch):
    app, test = client
    app.config['JSONDASH_GLOBALDASH'] = True
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    data = dict(name='global-check', is_global='on')
    test.post('/charts/create', data=data).data
    assert len(read()) == 1
    assert read()[0]['name'] == 'global-check'
    assert read()[0]['created_by'] == app.config.get('JSONDASH_GLOBAL_USER')
예제 #3
0
def test_set_global_flag_when_creating_dashboard(ctx, client, monkeypatch):
    app, test = client
    app.config['JSONDASH_GLOBALDASH'] = True
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    data = dict(name='global-check', is_global='on')
    test.post('/charts/create', data=data).data
    assert len(read()) == 1
    assert read()[0]['name'] == 'global-check'
    assert read()[0]['created_by'] == app.config.get('JSONDASH_GLOBAL_USER')
def test_update_edit_raw_invalidschema_missing_x(field, monkeypatch, ctx,
                                                 client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    module = dict(
        name='foo',
        width=1,
        height=1,
        guid='a-b-c-d-e',
        dataSource='...',
        family='C3',
        type='line',
    )
    view = dict(id='a-b-c-d-e', name='foo', modules=[module])
    del module[field]
    readfunc = read(override=dict(view))
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    res = test.post(url_for('jsondash.update', c_id=view['id']),
                    data={
                        'edit-raw': 'on',
                        'config': json.dumps(view)
                    },
                    follow_redirects=True)
    dom = pq(res.data)
    err_msg = "{'" + field + "': ['required field']"
    assert err_msg in dom.find('.alert-danger').text()
예제 #5
0
def test_update_valid(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
    assert not read()
    res = test.post(url_for('jsondash.create'),
                    data=dict(name='newname', modules=[]),
                    follow_redirects=True)
    assert len(read()) == 1
    view_id = read()[0]['id']
    res = test.post(url_for('jsondash.update', c_id=view_id),
                    data=dict(name='newname'),
                    follow_redirects=True)
    dom = pq(res.data)
    flash_msg = 'Updated view "{}"'.format(view_id)
    assert dom.find('.alert-info').text() == flash_msg
    assert len(read()) == 1
예제 #6
0
def test_get_view_valid_id_invalid_modules(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
    view = dict(id='123')
    readfunc = read(override=dict(view))
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    res = test.get(url_for('jsondash.view', c_id='123'), follow_redirects=True)
    assert 'Invalid configuration - missing modules.' in str(res.data)
예제 #7
0
def test_get_view_valid_modules_valid_dash_title(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
    view = get_json_config('inputs.json')
    readfunc = read(override=dict(view))
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    res = test.get(url_for('jsondash.view', c_id=view['id']))
    dom = pq(res.data)
    assert dom.find('.dashboard-title').text() == view['name']
예제 #8
0
def test_get_view_valid_id_invalid_config(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
    view = dict(modules=[dict(foo='bar')])
    readfunc = read(override=dict(view))
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    with pytest.raises(ValueError):
        res = test.get(url_for('jsondash.view', c_id='123'))
        assert 'Invalid config!' in str(res.data)
예제 #9
0
def test_view_valid_dashboard_count_and_inputs(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
    view = get_json_config('inputs.json')
    readfunc = read(override=dict(view))
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    res = test.get(url_for('jsondash.view', c_id=view['id']))
    dom = pq(res.data)
    assert len(dom.find('.item')) == len(view['modules'])
    assert len(dom.find('.charts-input-icon')) == 1
예제 #10
0
def test_create_dashboards_check_index_count(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
    for i in range(10):
        data = dict(name=i, modules=[])
        res = test.post(url_for('jsondash.create'), data=data)
    res = test.get(url_for('jsondash.dashboard'))
    assert len(read()) == 10
    heading = pq(res.data).find('h1.lead').text()
    assert 'Showing 10 dashboards with 0 charts' in heading
예제 #11
0
def test_get_dashboard_contains_all_chart_types_list(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    view = get_json_config('inputs.json')
    readfunc = read(override=dict(view))
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    res = test.get(url_for('jsondash.view', c_id=view['id']))
    for family, config in settings.CHARTS_CONFIG.items():
        for chart in config['charts']:
            _, label = chart
            assert label in str(res.data)
예제 #12
0
def test_get_view_valid_id_ensure_id_popped(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    view = get_json_config('inputs.json')
    view = dict(view)
    view.update(_id='foo')
    readfunc = read(override=view)
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    res = test.get(url_for('jsondash.view', c_id=view['id']))
    dom = pq(res.data)
    assert len(dom.find('.item')) == len(view['modules'])
예제 #13
0
def test_dashboards_override_perpage_pagination(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    for i in range(10):
        data = dict(name=i, modules=[])
        res = test.post(url_for('jsondash.create'), data=data)
    res = test.get(url_for('jsondash.dashboard') + '?per_page=2')
    # Ensure 10 exist, but only 5 are shown
    assert len(read()) == 10
    dom = pq(res.data)
    assert len(dom.find('.pagination').find('li:not(.active)')) == 5
예제 #14
0
def test_update_invalid_config(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
    view = get_json_config('inputs.json')
    readfunc = read(override=dict(view))
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    res = test.post(url_for('jsondash.update', c_id=view['id']),
                    data={'edit-raw': 'on'},
                    follow_redirects=True)
    dom = pq(res.data)
    assert dom.find('.alert-danger').text() == 'Error: Invalid JSON config.'
예제 #15
0
def test_no_demo_mode(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    data = dict(name='newname', modules=[])
    test.post(url_for('jsondash.create'), data=data, follow_redirects=True)
    view_id = read()[0]['id']
    url = url_for('jsondash.view', c_id=view_id)
    res = test.get(url)
    dom = pq(res.data)
    assert dom.find('.chart-header small > .btn')
    assert dom.find('.chart-header small > .btn').text().strip() == 'Back'
    assert dom.find('.chart-header .dropdown-toggle')
예제 #16
0
def test_demo_mode(monkeypatch, ctx, client):
    # Test that certain UI elements are removed when in demo mode.
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    data = dict(name='newname', modules=[])
    test.post(url_for('jsondash.create'), data=data, follow_redirects=True)
    view_id = read()[0]['id']
    url = url_for('jsondash.view', c_id=view_id) + '?jsondash_demo_mode=1'
    res = test.get(url)
    dom = pq(res.data)
    assert not dom.find('.chart-header > small .btn')
    assert not dom.find('.chart-header .dropdown-toggle')
def test_update_invalid_swap_freeform_to_grid_no_row(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    assert not read()
    dash_data = dict(
        name='newname',
        mode='freeform',
        module_foo=json.dumps(
            dict(dataSource='...',
                 width=1,
                 height=1,
                 name='foo',
                 family='C3',
                 type='line')),
        module_bar=json.dumps(
            dict(dataSource='...',
                 width=1,
                 height=1,
                 name='foo',
                 family='C3',
                 type='line')),
    )
    res = test.post(url_for('jsondash.create'),
                    data=dash_data,
                    follow_redirects=True)
    assert res.status_code == 200
    assert len(read()) == 1
    view_id = read()[0]['id']
    dash_data.update(mode='grid')
    res = test.post(url_for('jsondash.update', c_id=view_id),
                    data=dash_data,
                    follow_redirects=True)
    dom = pq(res.data)
    flash_msg = ('Cannot use grid layout without specifying row(s)! '
                 'Edit JSON manually to override this.')
    assert flash_msg in dom.find('.alert-danger').text()
    assert len(read()) == 1
예제 #18
0
def test_view_valid_dashboard_inputs_form(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
    view = get_json_config('inputs.json')
    readfunc = read(override=dict(view))
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    res = test.get(url_for('jsondash.view', c_id=view['id']))
    dom = pq(res.data)
    charts_with_inputs = [m for m in view['modules'] if 'inputs' in m]
    num_config_inputs = len(charts_with_inputs[0]['inputs']['options'])
    assert num_config_inputs == 5  # Sanity check
    assert len(charts_with_inputs) == 1
    assert len(dom.find('.chart-inputs')) == 1
    assert dom.find('.chart-inputs').hasClass('collapse')
    # There are 7 input fields generated for this particular json file.
    assert len(dom.find('.chart-inputs form input')) == 7
예제 #19
0
def test_create_dashboards_check_paginator_html(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    for i in range(100):
        data = dict(name=i, modules=[])
        res = test.post(url_for('jsondash.create'), data=data)
    res = test.get(url_for('jsondash.dashboard'))
    dom = pq(res.data)
    assert len(read()) == 100
    assert dom.find('.paginator-status').text() == 'Showing 0-25 of 100 results'
    res = test.get(url_for('jsondash.dashboard') + '?page=2')
    dom = pq(res.data)
    assert dom.find('.paginator-status').text() == 'Showing 25-50 of 100 results'
    res = test.get(url_for('jsondash.dashboard') + '?page=3')
    dom = pq(res.data)
    assert dom.find('.paginator-status').text() == 'Showing 50-75 of 100 results'
    res = test.get(url_for('jsondash.dashboard') + '?page=4')
    dom = pq(res.data)
    assert dom.find('.paginator-status').text() == 'Showing 75-100 of 100 results'
def test_dump_fixtures_delete_bad_path_show_errors_no_exception(monkeypatch):
    records = [
        model_factories.make_fake_dashboard(name=i, max_charts=1)
        for i in range(1)
    ]

    def delete_all():
        global records
        records = []

    monkeypatch.setattr(_db, 'read', lambda *args, **kwargs: records)
    monkeypatch.setattr(_db, 'delete_all', lambda *a, **kw: [])
    runner = CliRunner()
    args = ['--dump', '/fakepath/', '--delete']
    result = runner.invoke(model_factories.insert_dashboards, args)
    assert 'Saving db as fixtures to:' in result.output
    assert result.exit_code == 0
    assert len(read()) == 0
    err_msg = "The following records could not be dumped: ['//fakepath/"
    assert err_msg in result.output
def test_dump_fixtures_delete(monkeypatch, tmpdir):
    records = [
        model_factories.make_fake_dashboard(name=i, max_charts=1)
        for i in range(10)
    ]

    def delete_all():
        global records
        records = []

    monkeypatch.setattr(_db, 'read', lambda *args, **kwargs: records)
    monkeypatch.setattr(_db, 'delete_all', lambda *a, **kw: [])
    runner = CliRunner()
    tmp = tmpdir.mkdir('dumped_fixtures_test')
    args = ['--dump', tmp.strpath, '--delete']
    result = runner.invoke(model_factories.insert_dashboards, args)
    assert 'Saving db as fixtures to:' in result.output
    assert result.exit_code == 0
    assert len(os.listdir(tmp.strpath)) == 10
    assert len(read()) == 0
예제 #22
0
def test_create_dashboards_check_paginator_html(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_valid)
    for i in range(100):
        data = dict(name=i, modules=[])
        res = test.post(url_for('jsondash.create'), data=data)
    res = test.get(url_for('jsondash.dashboard'))
    dom = pq(res.data)
    assert len(read()) == 100
    assert dom.find('.paginator-status').text() == 'Showing 0-25 of 100 results'
    res = test.get(url_for('jsondash.dashboard') + '?page=2')
    dom = pq(res.data)
    assert dom.find(
        '.paginator-status').text() == 'Showing 25-50 of 100 results'
    res = test.get(url_for('jsondash.dashboard') + '?page=3')
    dom = pq(res.data)
    assert dom.find(
        '.paginator-status').text() == 'Showing 50-75 of 100 results'
    res = test.get(url_for('jsondash.dashboard') + '?page=4')
    dom = pq(res.data)
    assert dom.find(
        '.paginator-status').text() == 'Showing 75-100 of 100 results'
예제 #23
0
def test_view_valid_dashboard_inputs_form_counts(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
    view = get_json_config('inputs.json')
    readfunc = read(override=dict(view))
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    res = test.get(url_for('jsondash.view', c_id=view['id']))
    dom = pq(res.data)
    charts_with_inputs = [m for m in view['modules'] if 'inputs' in m]
    input_options = charts_with_inputs[0]['inputs']['options']
    radio_opts = [o for o in input_options if o['type'] == 'radio'][0]
    radio_opts = radio_opts['options']
    assert len(dom.find('.chart-inputs form .input-radio')) == len(radio_opts)
    select = [o for o in input_options if o['type'] == 'select']
    assert len(dom.find('.chart-inputs form select')) == len(select)
    options = select[0]['options']
    assert len(dom.find('.chart-inputs form select option')) == len(options)
    numbers = [inp for inp in input_options if inp['type'] == 'number']
    assert len(dom.find('.chart-inputs [type="number"]')) == len(numbers)
    text = [inp for inp in input_options if inp['type'] == 'text']
    assert len(dom.find('.chart-inputs [type="text"]')) == len(text)
    checkbox = [inp for inp in input_options if inp['type'] == 'checkbox']
    assert len(dom.find('.chart-inputs [type="checkbox"]')) == len(checkbox)
예제 #24
0
def test_clone_valid(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
    assert len(read()) == 0
    res = test.post(url_for('jsondash.create'),
                    data=dict(name='mydash', modules=[]),
                    follow_redirects=True)
    dom = pq(res.data)
    new_id = read()[0]['id']
    assert read()[0]['name'] == 'mydash'
    flash_msg = 'Created new dashboard "mydash"'
    assert dom.find('.alert-info').text() == flash_msg
    assert len(read()) == 1
    assert read()[0]['name'] == 'mydash'
    res = test.post(url_for('jsondash.clone', c_id=new_id),
                    follow_redirects=True)
    dom = pq(res.data)
    flash_msg = 'Created new dashboard clone "Clone of mydash"'
    assert flash_msg in dom.find('.alert').text()
    assert len(read()) == 2
예제 #25
0
def test_delete_valid(monkeypatch, ctx, client):
    app, test = client
    monkeypatch.setattr(charts_builder, 'auth', auth_ok)
    view = dict(name='mydash', modules=[])
    readfunc = read(override=dict(view))
    monkeypatch.setattr(charts_builder.adapter, 'read', readfunc)
    assert not read()
    # Create first one.
    res = test.post(url_for('jsondash.create'),
                    data=view,
                    follow_redirects=True)
    assert len(read()) == 1
    view_id = read()[0]['id']
    dom = pq(res.data)
    flash_msg = 'Created new dashboard "mydash"'
    assert dom.find('.alert-info').text() == flash_msg
    assert len(read()) == 1
    res = test.post(url_for('jsondash.delete', c_id=view_id),
                    follow_redirects=True)
    dom = pq(res.data)
    flash_msg = 'Deleted dashboard "{}"'.format(view_id)
    assert dom.find('.alert-info').text() == flash_msg
    assert len(read()) == 0