import json
from datetime import datetime as dt

from flask import Flask
import pytest

from flask_jsondash import charts_builder


app = Flask('test_flask_jsondash')
app.debug = True
app.register_blueprint(charts_builder.charts)


def _username():
    return 'Username'


def _authtest():
    return False


@pytest.fixture
def client(request):
    app.config.update(
        JSONDASH_GLOBALDASH=False,
        JSONDASH_FILTERUSERS=False,
        JSONDASH_GLOBAL_USER='global-test',
    )
    app.config['JSONDASH'] = dict(
        metadata=dict(
            created_by=_username,
            username=_username,
        ),
        static=dict(
            js_path='js/vendor/',
            css_path='css/vendor/',
        ),
        auth=dict(
            edit_global=_authtest,
            clone=_authtest,
            delete=_authtest,
        )
    )
    return app.test_client()


def test_no_config_sanity_test(client):
    assert app.config.get('JSONDASH_GLOBALDASH') == False
    assert app.config.get('JSONDASH_FILTERUSERS') == False
    assert app.config.get('JSONDASH_GLOBAL_USER') == 'global-test'


def test_setting(client):
    with app.app_context():
        _get = charts_builder.setting
        assert _get('JSONDASH_GLOBALDASH') == False
        assert _get('JSONDASH_FILTERUSERS') == False
        assert _get('JSONDASH_GLOBAL_USER') == 'global-test'


def test_metadata(client):
    with app.app_context():
        assert charts_builder.metadata(key='created_by') == 'Username'
        assert charts_builder.metadata(key='username') == 'Username'

@@ -65,16 +65,29 @@
 rt Flask
+, current_app
 %0Aimport 
@@ -330,16 +330,32 @@
 .fixture
+(scope='module')
 %0Adef cli
@@ -362,15 +362,8 @@
 ent(
-request
 ):%0A 
@@ -1567,24 +1567,274 @@
 sername') == 'Username'%0A
+%0A%0Adef test_metadata_bad(client):%0A    with app.app_context():%0A        assert charts_builder.metadata(key='foo') is None%0A%0A%0Adef test_app_redirect(client):%0A    resp = client.get('/charts')%0A    assert 'You should be redirected automatically' in resp.data%0A

Add more tests.