1 # -*- coding: utf-8 -*-
3 # GNU MediaGoblin -- federated, autonomous media hosting
4 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 from mediagoblin
.init
import config
24 CARROT_CONF_GOOD
= pkg_resources
.resource_filename(
25 'mediagoblin.tests', 'fake_carrot_conf_good.ini')
26 CARROT_CONF_EMPTY
= pkg_resources
.resource_filename(
27 'mediagoblin.tests', 'fake_carrot_conf_empty.ini')
28 CARROT_CONF_BAD
= pkg_resources
.resource_filename(
29 'mediagoblin.tests', 'fake_carrot_conf_bad.ini')
30 FAKE_CONFIG_SPEC
= pkg_resources
.resource_filename(
31 'mediagoblin.tests', 'fake_config_spec.ini')
34 def test_read_mediagoblin_config():
36 this_conf
, validation_results
= config
.read_mediagoblin_config(
37 CARROT_CONF_EMPTY
, FAKE_CONFIG_SPEC
)
39 assert this_conf
['carrotapp']['carrotcake'] == False
40 assert this_conf
['carrotapp']['num_carrots'] == 1
41 assert 'encouragement_phrase' not in this_conf
['carrotapp']
42 assert this_conf
['celery']['EAT_CELERY_WITH_CARROTS'] == True
45 this_conf
, validation_results
= config
.read_mediagoblin_config(
46 CARROT_CONF_GOOD
, FAKE_CONFIG_SPEC
)
48 assert this_conf
['carrotapp']['carrotcake'] == True
49 assert this_conf
['carrotapp']['num_carrots'] == 88
50 assert this_conf
['carrotapp']['encouragement_phrase'] == \
51 "I'd love it if you eat your carrots!"
52 assert this_conf
['carrotapp']['blah_blah'] == u
"blæh!"
53 assert this_conf
['celery']['EAT_CELERY_WITH_CARROTS'] == False
56 this_conf
, validation_results
= config
.read_mediagoblin_config(
57 CARROT_CONF_BAD
, FAKE_CONFIG_SPEC
)
59 # These should still open but will have errors that we'll test for
60 # in test_generate_validation_report()
61 assert this_conf
['carrotapp']['carrotcake'] == 'slobber'
62 assert this_conf
['carrotapp']['num_carrots'] == 'GROSS'
63 assert this_conf
['carrotapp']['encouragement_phrase'] == \
65 assert this_conf
['carrotapp']['blah_blah'] == "blah!"
66 assert this_conf
['celery']['EAT_CELERY_WITH_CARROTS'] == "pants"
69 def test_generate_validation_report():
71 this_conf
, validation_results
= config
.read_mediagoblin_config(
72 CARROT_CONF_EMPTY
, FAKE_CONFIG_SPEC
)
73 report
= config
.generate_validation_report(this_conf
, validation_results
)
77 this_conf
, validation_results
= config
.read_mediagoblin_config(
78 CARROT_CONF_GOOD
, FAKE_CONFIG_SPEC
)
79 report
= config
.generate_validation_report(this_conf
, validation_results
)
83 this_conf
, validation_results
= config
.read_mediagoblin_config(
84 CARROT_CONF_BAD
, FAKE_CONFIG_SPEC
)
85 report
= config
.generate_validation_report(this_conf
, validation_results
)
87 assert report
.startswith("""\
88 There were validation problems loading this config file:
89 --------------------------------------------------------""")
92 'carrotapp:carrotcake = the value "slobber" is of the wrong type.',
93 'carrotapp:num_carrots = the value "GROSS" is of the wrong type.',
94 'celery:EAT_CELERY_WITH_CARROTS = the value "pants" is of the wrong type.']
95 warnings
= report
.splitlines()[2:]
97 assert len(warnings
) == 3
98 for warning
in expected_warnings
:
99 assert warning
in warnings