Commit | Line | Data |
---|---|---|
d5234024 | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
d5234024 CAW |
3 | # |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | import pkg_resources | |
18 | ||
421129b6 | 19 | from mediagoblin.init import config |
d5234024 CAW |
20 | |
21 | ||
22 | CARROT_CONF_GOOD = pkg_resources.resource_filename( | |
23 | 'mediagoblin.tests', 'fake_carrot_conf_good.ini') | |
24 | CARROT_CONF_EMPTY = pkg_resources.resource_filename( | |
25 | 'mediagoblin.tests', 'fake_carrot_conf_empty.ini') | |
26 | CARROT_CONF_BAD = pkg_resources.resource_filename( | |
27 | 'mediagoblin.tests', 'fake_carrot_conf_bad.ini') | |
28 | FAKE_CONFIG_SPEC = pkg_resources.resource_filename( | |
29 | 'mediagoblin.tests', 'fake_config_spec.ini') | |
30 | ||
31 | ||
32 | def test_read_mediagoblin_config(): | |
33 | # An empty file | |
34 | this_conf, validation_results = config.read_mediagoblin_config( | |
35 | CARROT_CONF_EMPTY, FAKE_CONFIG_SPEC) | |
36 | ||
37 | assert this_conf['carrotapp']['carrotcake'] == False | |
38 | assert this_conf['carrotapp']['num_carrots'] == 1 | |
04453ccf | 39 | assert 'encouragement_phrase' not in this_conf['carrotapp'] |
e9f87f72 | 40 | assert this_conf['celery']['EAT_CELERY_WITH_CARROTS'] == True |
d5234024 CAW |
41 | |
42 | # A good file | |
43 | this_conf, validation_results = config.read_mediagoblin_config( | |
44 | CARROT_CONF_GOOD, FAKE_CONFIG_SPEC) | |
45 | ||
46 | assert this_conf['carrotapp']['carrotcake'] == True | |
47 | assert this_conf['carrotapp']['num_carrots'] == 88 | |
48 | assert this_conf['carrotapp']['encouragement_phrase'] == \ | |
49 | "I'd love it if you eat your carrots!" | |
50 | assert this_conf['carrotapp']['blah_blah'] == "blah!" | |
e9f87f72 | 51 | assert this_conf['celery']['EAT_CELERY_WITH_CARROTS'] == False |
d5234024 CAW |
52 | |
53 | # A bad file | |
54 | this_conf, validation_results = config.read_mediagoblin_config( | |
55 | CARROT_CONF_BAD, FAKE_CONFIG_SPEC) | |
56 | ||
57 | # These should still open but will have errors that we'll test for | |
58 | # in test_generate_validation_report() | |
59 | assert this_conf['carrotapp']['carrotcake'] == 'slobber' | |
60 | assert this_conf['carrotapp']['num_carrots'] == 'GROSS' | |
61 | assert this_conf['carrotapp']['encouragement_phrase'] == \ | |
62 | "586956856856" | |
63 | assert this_conf['carrotapp']['blah_blah'] == "blah!" | |
e9f87f72 | 64 | assert this_conf['celery']['EAT_CELERY_WITH_CARROTS'] == "pants" |
d5234024 CAW |
65 | |
66 | ||
67 | def test_generate_validation_report(): | |
68 | # Empty | |
69 | this_conf, validation_results = config.read_mediagoblin_config( | |
70 | CARROT_CONF_EMPTY, FAKE_CONFIG_SPEC) | |
71 | report = config.generate_validation_report(this_conf, validation_results) | |
72 | assert report is None | |
73 | ||
74 | # Good | |
75 | this_conf, validation_results = config.read_mediagoblin_config( | |
76 | CARROT_CONF_GOOD, FAKE_CONFIG_SPEC) | |
77 | report = config.generate_validation_report(this_conf, validation_results) | |
78 | assert report is None | |
79 | ||
80 | # Bad | |
81 | this_conf, validation_results = config.read_mediagoblin_config( | |
82 | CARROT_CONF_BAD, FAKE_CONFIG_SPEC) | |
83 | report = config.generate_validation_report(this_conf, validation_results) | |
84 | ||
85 | assert report.startswith("""\ | |
86 | There were validation problems loading this config file: | |
87 | --------------------------------------------------------""") | |
88 | ||
89 | expected_warnings = [ | |
90 | 'carrotapp:carrotcake = the value "slobber" is of the wrong type.', | |
91 | 'carrotapp:num_carrots = the value "GROSS" is of the wrong type.', | |
e9f87f72 | 92 | 'celery:EAT_CELERY_WITH_CARROTS = the value "pants" is of the wrong type.'] |
d5234024 CAW |
93 | warnings = report.splitlines()[2:] |
94 | ||
95 | assert len(warnings) == 3 | |
96 | for warning in expected_warnings: | |
97 | assert warning in warnings |