Support Unicode characters in configuration values
[mediagoblin.git] / mediagoblin / tests / test_config.py
1 # -*- coding: utf-8 -*-
2 #
3 # GNU MediaGoblin -- federated, autonomous media hosting
4 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
5 #
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.
10 #
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.
15 #
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/>.
18
19 import pkg_resources
20
21 from mediagoblin.init import config
22
23
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')
32
33
34 def test_read_mediagoblin_config():
35 # An empty file
36 this_conf, validation_results = config.read_mediagoblin_config(
37 CARROT_CONF_EMPTY, FAKE_CONFIG_SPEC)
38
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
43
44 # A good file
45 this_conf, validation_results = config.read_mediagoblin_config(
46 CARROT_CONF_GOOD, FAKE_CONFIG_SPEC)
47
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
54
55 # A bad file
56 this_conf, validation_results = config.read_mediagoblin_config(
57 CARROT_CONF_BAD, FAKE_CONFIG_SPEC)
58
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'] == \
64 "586956856856"
65 assert this_conf['carrotapp']['blah_blah'] == "blah!"
66 assert this_conf['celery']['EAT_CELERY_WITH_CARROTS'] == "pants"
67
68
69 def test_generate_validation_report():
70 # Empty
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)
74 assert report is None
75
76 # Good
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)
80 assert report is None
81
82 # Bad
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)
86
87 assert report.startswith("""\
88 There were validation problems loading this config file:
89 --------------------------------------------------------""")
90
91 expected_warnings = [
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:]
96
97 assert len(warnings) == 3
98 for warning in expected_warnings:
99 assert warning in warnings