Beispiel #1
0
def test_generate_validation_report():
    # Empty
    this_conf, validation_results = config.read_mediagoblin_config(
        CARROT_CONF_EMPTY, FAKE_CONFIG_SPEC)
    report = config.generate_validation_report(this_conf, validation_results)
    assert report is None

    # Good
    this_conf, validation_results = config.read_mediagoblin_config(
        CARROT_CONF_GOOD, FAKE_CONFIG_SPEC)
    report = config.generate_validation_report(this_conf, validation_results)
    assert report is None

    # Bad
    this_conf, validation_results = config.read_mediagoblin_config(
        CARROT_CONF_BAD, FAKE_CONFIG_SPEC)
    report = config.generate_validation_report(this_conf, validation_results)

    assert report.startswith("""\
There were validation problems loading this config file:
--------------------------------------------------------""")

    expected_warnings = [
        'carrotapp:carrotcake = the value "slobber" is of the wrong type.',
        'carrotapp:num_carrots = the value "GROSS" is of the wrong type.',
        'celery:EAT_CELERY_WITH_CARROTS = the value "pants" is of the wrong type.']
    warnings = report.splitlines()[2:]

    assert len(warnings) == 3
    for warning in expected_warnings:
        assert warning in warnings
def test_generate_validation_report():
    # Empty
    this_conf, validation_results = config.read_mediagoblin_config(CARROT_CONF_EMPTY, FAKE_CONFIG_SPEC)
    report = config.generate_validation_report(this_conf, validation_results)
    assert report is None

    # Good
    this_conf, validation_results = config.read_mediagoblin_config(CARROT_CONF_GOOD, FAKE_CONFIG_SPEC)
    report = config.generate_validation_report(this_conf, validation_results)
    assert report is None

    # Bad
    this_conf, validation_results = config.read_mediagoblin_config(CARROT_CONF_BAD, FAKE_CONFIG_SPEC)
    report = config.generate_validation_report(this_conf, validation_results)

    assert report.startswith(
        """\
There were validation problems loading this config file:
--------------------------------------------------------"""
    )

    expected_warnings = [
        'carrotapp:carrotcake = the value "slobber" is of the wrong type.',
        'carrotapp:num_carrots = the value "GROSS" is of the wrong type.',
        'celery:EAT_CELERY_WITH_CARROTS = the value "pants" is of the wrong type.',
    ]
    warnings = report.splitlines()[2:]

    assert len(warnings) == 3
    for warning in expected_warnings:
        assert warning in warnings
Beispiel #3
0
def test_read_mediagoblin_config():
    # An empty file
    this_conf, validation_results = config.read_mediagoblin_config(
        CARROT_CONF_EMPTY, FAKE_CONFIG_SPEC)

    assert this_conf['carrotapp']['carrotcake'] == False
    assert this_conf['carrotapp']['num_carrots'] == 1
    assert 'encouragement_phrase' not in this_conf['carrotapp']
    assert this_conf['celery']['EAT_CELERY_WITH_CARROTS'] == True

    # A good file
    this_conf, validation_results = config.read_mediagoblin_config(
        CARROT_CONF_GOOD, FAKE_CONFIG_SPEC)

    assert this_conf['carrotapp']['carrotcake'] == True
    assert this_conf['carrotapp']['num_carrots'] == 88
    assert this_conf['carrotapp']['encouragement_phrase'] == \
        "I'd love it if you eat your carrots!"
    assert this_conf['carrotapp']['blah_blah'] == u"blæh!"
    assert this_conf['celery']['EAT_CELERY_WITH_CARROTS'] == False

    # A bad file
    this_conf, validation_results = config.read_mediagoblin_config(
        CARROT_CONF_BAD, FAKE_CONFIG_SPEC)

    # These should still open but will have errors that we'll test for
    # in test_generate_validation_report()
    assert this_conf['carrotapp']['carrotcake'] == 'slobber'
    assert this_conf['carrotapp']['num_carrots'] == 'GROSS'
    assert this_conf['carrotapp']['encouragement_phrase'] == \
        "586956856856"
    assert this_conf['carrotapp']['blah_blah'] == "blah!"
    assert this_conf['celery']['EAT_CELERY_WITH_CARROTS'] == "pants"
def test_read_mediagoblin_config():
    # An empty file
    this_conf, validation_results = config.read_mediagoblin_config(CARROT_CONF_EMPTY, FAKE_CONFIG_SPEC)

    assert this_conf["carrotapp"]["carrotcake"] == False
    assert this_conf["carrotapp"]["num_carrots"] == 1
    assert "encouragement_phrase" not in this_conf["carrotapp"]
    assert this_conf["celery"]["EAT_CELERY_WITH_CARROTS"] == True

    # A good file
    this_conf, validation_results = config.read_mediagoblin_config(CARROT_CONF_GOOD, FAKE_CONFIG_SPEC)

    assert this_conf["carrotapp"]["carrotcake"] == True
    assert this_conf["carrotapp"]["num_carrots"] == 88
    assert this_conf["carrotapp"]["encouragement_phrase"] == "I'd love it if you eat your carrots!"
    assert this_conf["carrotapp"]["blah_blah"] == "blah!"
    assert this_conf["celery"]["EAT_CELERY_WITH_CARROTS"] == False

    # A bad file
    this_conf, validation_results = config.read_mediagoblin_config(CARROT_CONF_BAD, FAKE_CONFIG_SPEC)

    # These should still open but will have errors that we'll test for
    # in test_generate_validation_report()
    assert this_conf["carrotapp"]["carrotcake"] == "slobber"
    assert this_conf["carrotapp"]["num_carrots"] == "GROSS"
    assert this_conf["carrotapp"]["encouragement_phrase"] == "586956856856"
    assert this_conf["carrotapp"]["blah_blah"] == "blah!"
    assert this_conf["celery"]["EAT_CELERY_WITH_CARROTS"] == "pants"
Beispiel #5
0
def test_read_mediagoblin_config():
    # An empty file
    this_conf, validation_results = config.read_mediagoblin_config(
        CARROT_CONF_EMPTY, FAKE_CONFIG_SPEC)

    assert this_conf['carrotapp']['carrotcake'] == False
    assert this_conf['carrotapp']['num_carrots'] == 1
    assert not this_conf['carrotapp'].has_key('encouragement_phrase')
    assert this_conf['celery']['EAT_CELERY_WITH_CARROTS'] == True

    # A good file
    this_conf, validation_results = config.read_mediagoblin_config(
        CARROT_CONF_GOOD, FAKE_CONFIG_SPEC)

    assert this_conf['carrotapp']['carrotcake'] == True
    assert this_conf['carrotapp']['num_carrots'] == 88
    assert this_conf['carrotapp']['encouragement_phrase'] == \
        "I'd love it if you eat your carrots!"
    assert this_conf['carrotapp']['blah_blah'] == "blah!"
    assert this_conf['celery']['EAT_CELERY_WITH_CARROTS'] == False

    # A bad file
    this_conf, validation_results = config.read_mediagoblin_config(
        CARROT_CONF_BAD, FAKE_CONFIG_SPEC)

    # These should still open but will have errors that we'll test for
    # in test_generate_validation_report()
    assert this_conf['carrotapp']['carrotcake'] == 'slobber'
    assert this_conf['carrotapp']['num_carrots'] == 'GROSS'
    assert this_conf['carrotapp']['encouragement_phrase'] == \
        "586956856856"
    assert this_conf['carrotapp']['blah_blah'] == "blah!"
    assert this_conf['celery']['EAT_CELERY_WITH_CARROTS'] == "pants"
def test_setup_celery_from_config():
    def _wipe_testmodule_clean(module):
        vars_to_wipe = [
            var for var in dir(module)
            if not var.startswith('__') and not var.endswith('__')]
        for var in vars_to_wipe:
            delattr(module, var)

    global_config, validation_result = read_mediagoblin_config(
        TEST_CELERY_CONF_NOSPECIALDB)
    app_config = global_config['mediagoblin']

    celery_setup.setup_celery_from_config(
        app_config, global_config,
        'mediagoblin.tests.fake_celery_module', set_environ=False)

    from mediagoblin.tests import fake_celery_module
    assert fake_celery_module.SOME_VARIABLE == 'floop'
    assert fake_celery_module.MAIL_PORT == 2000
    assert isinstance(fake_celery_module.MAIL_PORT, int)
    assert fake_celery_module.CELERYD_ETA_SCHEDULER_PRECISION == 1.3
    assert isinstance(fake_celery_module.CELERYD_ETA_SCHEDULER_PRECISION, float)
    assert fake_celery_module.CELERY_RESULT_PERSISTENT is True
    assert fake_celery_module.CELERY_IMPORTS == [
        'foo.bar.baz', 'this.is.an.import', 'mediagoblin.processing.task', \
        'mediagoblin.notifications.task', 'mediagoblin.submit.task']
    assert fake_celery_module.CELERY_RESULT_BACKEND == 'database'
    assert fake_celery_module.CELERY_RESULT_DBURI == (
        'sqlite:///' +
        pkg_resources.resource_filename('mediagoblin.tests', 'celery.db'))

    assert fake_celery_module.BROKER_TRANSPORT == 'sqlalchemy'
    assert fake_celery_module.BROKER_URL == (
        'sqlite:///' +
        pkg_resources.resource_filename('mediagoblin.tests', 'kombu.db'))
Beispiel #7
0
def setup_global_and_app_config(config_path):
    global_config, validation_result = read_mediagoblin_config(config_path)
    app_config = global_config["mediagoblin"]
    # report errors if necessary
    validation_report = generate_validation_report(global_config, validation_result)
    if validation_report:
        raise ImproperlyConfigured(validation_report)

    setup_globals(app_config=app_config, global_config=global_config)

    return global_config, app_config
Beispiel #8
0
def setup_global_and_app_config(config_path):
    global_config, validation_result = read_mediagoblin_config(config_path)
    app_config = global_config['mediagoblin']
    # report errors if necessary
    validation_report = generate_validation_report(global_config,
                                                   validation_result)
    if validation_report:
        raise ImproperlyConfigured(validation_report)

    setup_globals(app_config=app_config, global_config=global_config)

    return global_config, app_config
Beispiel #9
0
def get_app(request, paste_config=None, mgoblin_config=None):
    """Create a MediaGoblin app for testing.

    Args:
     - request: Not an http request, but a pytest fixture request.  We
       use this to make temporary directories that pytest
       automatically cleans up as needed.
     - paste_config: particular paste config used by this application.
     - mgoblin_config: particular mediagoblin config used by this
       application.
    """
    paste_config = paste_config or TEST_SERVER_CONFIG
    mgoblin_config = mgoblin_config or TEST_APP_CONFIG

    # This is the directory we're copying the paste/mgoblin config stuff into
    run_dir = request.config._tmpdirhandler.mktemp('mgoblin_app',
                                                   numbered=True)
    user_dev_dir = run_dir.mkdir('user_dev').strpath

    new_paste_config = run_dir.join('paste.ini').strpath
    new_mgoblin_config = run_dir.join('mediagoblin.ini').strpath
    shutil.copyfile(paste_config, new_paste_config)
    shutil.copyfile(mgoblin_config, new_mgoblin_config)

    Session.rollback()
    Session.remove()

    # install user_dev directories
    for directory in USER_DEV_DIRECTORIES_TO_SETUP:
        full_dir = os.path.join(user_dev_dir, directory)
        os.makedirs(full_dir)

    # Get app config
    global_config, validation_result = read_mediagoblin_config(
        new_mgoblin_config)
    app_config = global_config['mediagoblin']

    # Run database setup/migrations
    # @@: The *only* test that doesn't pass if we remove this is in
    #   test_persona.py... why?
    run_dbupdate(app_config, global_config)

    # setup app and return
    test_app = loadapp('config:' + new_paste_config)

    # Insert the TestingMeddleware, which can do some
    # sanity checks on every request/response.
    # Doing it this way is probably not the cleanest way.
    # We'll fix it, when we have plugins!
    mg_globals.app.meddleware.insert(0, TestingMeddleware(mg_globals.app))

    app = TestApp(test_app)
    return app
Beispiel #10
0
def get_app(request, paste_config=None, mgoblin_config=None):
    """Create a MediaGoblin app for testing.

    Args:
     - request: Not an http request, but a pytest fixture request.  We
       use this to make temporary directories that pytest
       automatically cleans up as needed.
     - paste_config: particular paste config used by this application.
     - mgoblin_config: particular mediagoblin config used by this
       application.
    """
    paste_config = paste_config or TEST_SERVER_CONFIG
    mgoblin_config = mgoblin_config or TEST_APP_CONFIG

    # This is the directory we're copying the paste/mgoblin config stuff into
    run_dir = request.config._tmpdirhandler.mktemp(
        'mgoblin_app', numbered=True)
    user_dev_dir = run_dir.mkdir('user_dev').strpath

    new_paste_config = run_dir.join('paste.ini').strpath
    new_mgoblin_config = run_dir.join('mediagoblin.ini').strpath
    shutil.copyfile(paste_config, new_paste_config)
    shutil.copyfile(mgoblin_config, new_mgoblin_config)

    Session.rollback()
    Session.remove()

    # install user_dev directories
    for directory in USER_DEV_DIRECTORIES_TO_SETUP:
        full_dir = os.path.join(user_dev_dir, directory)
        os.makedirs(full_dir)

    # Get app config
    global_config, validation_result = read_mediagoblin_config(new_mgoblin_config)
    app_config = global_config['mediagoblin']

    # Run database setup/migrations
    # @@: The *only* test that doesn't pass if we remove this is in
    #   test_persona.py... why?
    run_dbupdate(app_config, global_config)

    # setup app and return
    test_app = loadapp(
        'config:' + new_paste_config)

    # Insert the TestingMeddleware, which can do some
    # sanity checks on every request/response.
    # Doing it this way is probably not the cleanest way.
    # We'll fix it, when we have plugins!
    mg_globals.app.meddleware.insert(0, TestingMeddleware(mg_globals.app))

    app = TestApp(test_app)
    return app
Beispiel #11
0
def get_test_app(dump_old_app=True):
    suicide_if_bad_celery_environ()

    # Make sure we've turned on testing
    testing._activate_testing()

    # Leave this imported as it sets up celery.
    from mediagoblin.init.celery import from_tests

    global MGOBLIN_APP

    # Just return the old app if that exists and it's okay to set up
    # and return
    if MGOBLIN_APP and not dump_old_app:
        return MGOBLIN_APP

    Session.rollback()
    Session.remove()

    # Remove and reinstall user_dev directories
    if os.path.exists(TEST_USER_DEV):
        shutil.rmtree(TEST_USER_DEV)

    for directory in USER_DEV_DIRECTORIES_TO_SETUP:
        full_dir = os.path.join(TEST_USER_DEV, directory)
        os.makedirs(full_dir)

    # Get app config
    global_config, validation_result = read_mediagoblin_config(TEST_APP_CONFIG)
    app_config = global_config['mediagoblin']

    # Run database setup/migrations
    run_dbupdate(app_config, global_config)

    # setup app and return
    test_app = loadapp(
        'config:' + TEST_SERVER_CONFIG)

    # Re-setup celery
    setup_celery_app(app_config, global_config)

    # Insert the TestingMeddleware, which can do some
    # sanity checks on every request/response.
    # Doing it this way is probably not the cleanest way.
    # We'll fix it, when we have plugins!
    mg_globals.app.meddleware.insert(0, TestingMeddleware(mg_globals.app))

    app = TestApp(test_app)
    MGOBLIN_APP = app

    return app
Beispiel #12
0
def test_plugin_config():
    """
    Make sure plugins can set up their own config
    """
    config, validation_result = read_mediagoblin_config(
        pkg_resources.resource_filename('mediagoblin.tests',
                                        'appconfig_plugin_specs.ini'))

    pluginspec_section = config['plugins'][
        'mediagoblin.tests.testplugins.pluginspec']
    assert pluginspec_section['some_string'] == 'not blork'
    assert pluginspec_section['dont_change_me'] == 'still the default'

    # Make sure validation works... this should be an error
    assert isinstance(
        validation_result['plugins']
        ['mediagoblin.tests.testplugins.pluginspec']['some_int'], VdtTypeError)

    # the callables thing shouldn't really have anything though.
    assert len(
        config['plugins']['mediagoblin.tests.testplugins.callables1']) == 0
def test_setup_celery_from_config():
    def _wipe_testmodule_clean(module):
        vars_to_wipe = [
            var for var in dir(module)
            if not var.startswith('__') and not var.endswith('__')
        ]
        for var in vars_to_wipe:
            delattr(module, var)

    global_config, validation_result = read_mediagoblin_config(
        TEST_CELERY_CONF_NOSPECIALDB)
    app_config = global_config['mediagoblin']

    celery_setup.setup_celery_from_config(
        app_config,
        global_config,
        'mediagoblin.tests.fake_celery_module',
        set_environ=False)

    from mediagoblin.tests import fake_celery_module
    assert fake_celery_module.SOME_VARIABLE == 'floop'
    assert fake_celery_module.MAIL_PORT == 2000
    assert isinstance(fake_celery_module.MAIL_PORT, int)
    assert fake_celery_module.CELERYD_ETA_SCHEDULER_PRECISION == 1.3
    assert isinstance(fake_celery_module.CELERYD_ETA_SCHEDULER_PRECISION,
                      float)
    assert fake_celery_module.CELERY_RESULT_PERSISTENT is True
    assert fake_celery_module.CELERY_IMPORTS == [
        'foo.bar.baz', 'this.is.an.import', 'mediagoblin.processing.task',
        'mediagoblin.notifications.task'
    ]
    assert fake_celery_module.CELERY_RESULT_BACKEND == 'database'
    assert fake_celery_module.CELERY_RESULT_DBURI == (
        'sqlite:///' +
        pkg_resources.resource_filename('mediagoblin.tests', 'celery.db'))

    assert fake_celery_module.BROKER_TRANSPORT == 'sqlalchemy'
    assert fake_celery_module.BROKER_URL == (
        'sqlite:///' +
        pkg_resources.resource_filename('mediagoblin.tests', 'kombu.db'))
Beispiel #14
0
def test_plugin_config():
    """
    Make sure plugins can set up their own config
    """
    config, validation_result = read_mediagoblin_config(
        pkg_resources.resource_filename(
            'mediagoblin.tests', 'appconfig_plugin_specs.ini'))

    pluginspec_section = config['plugins'][
        'mediagoblin.tests.testplugins.pluginspec']
    assert pluginspec_section['some_string'] == 'not blork'
    assert pluginspec_section['dont_change_me'] == 'still the default'

    # Make sure validation works... this should be an error
    assert isinstance(
        validation_result[
            'plugins'][
                'mediagoblin.tests.testplugins.pluginspec'][
                    'some_int'],
        VdtTypeError)

    # the callables thing shouldn't really have anything though.
    assert len(config['plugins'][
        'mediagoblin.tests.testplugins.callables1']) == 0