Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/websdk/flask/testsuite/config.py
blob: ad1721fdf15a14497b0dc93650ed2832da3b242d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
"""
    flask.testsuite.config
    ~~~~~~~~~~~~~~~~~~~~~~

    Configuration and instances.

    :copyright: (c) 2011 by Armin Ronacher.
    :license: BSD, see LICENSE for more details.
"""
import os
import sys
import flask
import unittest
from flask.testsuite import FlaskTestCase


# config keys used for the ConfigTestCase
TEST_KEY = 'foo'
SECRET_KEY = 'devkey'


class ConfigTestCase(FlaskTestCase):

    def common_object_test(self, app):
        self.assert_equal(app.secret_key, 'devkey')
        self.assert_equal(app.config['TEST_KEY'], 'foo')
        self.assert_('ConfigTestCase' not in app.config)

    def test_config_from_file(self):
        app = flask.Flask(__name__)
        app.config.from_pyfile(__file__.rsplit('.', 1)[0] + '.py')
        self.common_object_test(app)

    def test_config_from_object(self):
        app = flask.Flask(__name__)
        app.config.from_object(__name__)
        self.common_object_test(app)

    def test_config_from_class(self):
        class Base(object):
            TEST_KEY = 'foo'
        class Test(Base):
            SECRET_KEY = 'devkey'
        app = flask.Flask(__name__)
        app.config.from_object(Test)
        self.common_object_test(app)

    def test_config_from_envvar(self):
        env = os.environ
        try:
            os.environ = {}
            app = flask.Flask(__name__)
            try:
                app.config.from_envvar('FOO_SETTINGS')
            except RuntimeError, e:
                self.assert_("'FOO_SETTINGS' is not set" in str(e))
            else:
                self.assert_(0, 'expected exception')
            self.assert_(not app.config.from_envvar('FOO_SETTINGS', silent=True))

            os.environ = {'FOO_SETTINGS': __file__.rsplit('.', 1)[0] + '.py'}
            self.assert_(app.config.from_envvar('FOO_SETTINGS'))
            self.common_object_test(app)
        finally:
            os.environ = env

    def test_config_missing(self):
        app = flask.Flask(__name__)
        try:
            app.config.from_pyfile('missing.cfg')
        except IOError, e:
            msg = str(e)
            self.assert_(msg.startswith('[Errno 2] Unable to load configuration '
                                        'file (No such file or directory):'))
            self.assert_(msg.endswith("missing.cfg'"))
        else:
            self.assert_(0, 'expected config')
        self.assert_(not app.config.from_pyfile('missing.cfg', silent=True))

    def test_session_lifetime(self):
        app = flask.Flask(__name__)
        app.config['PERMANENT_SESSION_LIFETIME'] = 42
        self.assert_equal(app.permanent_session_lifetime.seconds, 42)


class InstanceTestCase(FlaskTestCase):

    def test_explicit_instance_paths(self):
        here = os.path.abspath(os.path.dirname(__file__))
        try:
            flask.Flask(__name__, instance_path='instance')
        except ValueError, e:
            self.assert_('must be absolute' in str(e))
        else:
            self.fail('Expected value error')

        app = flask.Flask(__name__, instance_path=here)
        self.assert_equal(app.instance_path, here)

    def test_uninstalled_module_paths(self):
        from config_module_app import app
        here = os.path.abspath(os.path.dirname(__file__))
        self.assert_equal(app.instance_path, os.path.join(here, 'test_apps', 'instance'))

    def test_uninstalled_package_paths(self):
        from config_package_app import app
        here = os.path.abspath(os.path.dirname(__file__))
        self.assert_equal(app.instance_path, os.path.join(here, 'test_apps', 'instance'))

    def test_installed_module_paths(self):
        import types
        expected_prefix = os.path.abspath('foo')
        mod = types.ModuleType('myapp')
        mod.__file__ = os.path.join(expected_prefix, 'lib', 'python2.5',
                                    'site-packages', 'myapp.py')
        sys.modules['myapp'] = mod
        try:
            mod.app = flask.Flask(mod.__name__)
            self.assert_equal(mod.app.instance_path,
                             os.path.join(expected_prefix, 'var',
                                          'myapp-instance'))
        finally:
            sys.modules['myapp'] = None

    def test_installed_package_paths(self):
        import types
        expected_prefix = os.path.abspath('foo')
        package_path = os.path.join(expected_prefix, 'lib', 'python2.5',
                                    'site-packages', 'myapp')
        mod = types.ModuleType('myapp')
        mod.__path__ = [package_path]
        mod.__file__ = os.path.join(package_path, '__init__.py')
        sys.modules['myapp'] = mod
        try:
            mod.app = flask.Flask(mod.__name__)
            self.assert_equal(mod.app.instance_path,
                             os.path.join(expected_prefix, 'var',
                                          'myapp-instance'))
        finally:
            sys.modules['myapp'] = None

    def test_prefix_installed_paths(self):
        import types
        expected_prefix = os.path.abspath(sys.prefix)
        package_path = os.path.join(expected_prefix, 'lib', 'python2.5',
                                    'site-packages', 'myapp')
        mod = types.ModuleType('myapp')
        mod.__path__ = [package_path]
        mod.__file__ = os.path.join(package_path, '__init__.py')
        sys.modules['myapp'] = mod
        try:
            mod.app = flask.Flask(mod.__name__)
            self.assert_equal(mod.app.instance_path,
                             os.path.join(expected_prefix, 'var',
                                          'myapp-instance'))
        finally:
            sys.modules['myapp'] = None

    def test_egg_installed_paths(self):
        import types
        expected_prefix = os.path.abspath(sys.prefix)
        package_path = os.path.join(expected_prefix, 'lib', 'python2.5',
                                    'site-packages', 'MyApp.egg', 'myapp')
        mod = types.ModuleType('myapp')
        mod.__path__ = [package_path]
        mod.__file__ = os.path.join(package_path, '__init__.py')
        sys.modules['myapp'] = mod
        try:
            mod.app = flask.Flask(mod.__name__)
            self.assert_equal(mod.app.instance_path,
                             os.path.join(expected_prefix, 'var',
                                          'myapp-instance'))
        finally:
            sys.modules['myapp'] = None


def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(ConfigTestCase))
    suite.addTest(unittest.makeSuite(InstanceTestCase))
    return suite