-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtest_settings.py
132 lines (100 loc) · 5.58 KB
/
test_settings.py
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
"""Tests for our settings stuff."""
from datetime import timedelta
from os import close, environ, remove, write
from tempfile import mkstemp
from flask.ext.stormpath.errors import ConfigurationError
from flask.ext.stormpath.settings import check_settings, init_settings
from .helpers import StormpathTestCase
class TestInitSettings(StormpathTestCase):
"""Ensure we can properly initialize Flask app settings."""
def test_works(self):
init_settings(self.app.config)
# Ensure a couple of settings exist that we didn't explicitly specify
# anywhere.
self.assertEqual(self.app.config['STORMPATH_ENABLE_FACEBOOK'], False)
self.assertEqual(self.app.config['STORMPATH_ENABLE_GIVEN_NAME'], True)
class TestCheckSettings(StormpathTestCase):
"""Ensure our settings checker is working properly."""
def setUp(self):
"""Create an apiKey.properties file for testing."""
super(TestCheckSettings, self).setUp()
# Generate our file locally.
self.fd, self.file = mkstemp()
api_key_id = 'apiKey.id = %s\n' % environ.get('STORMPATH_API_KEY_ID')
api_key_secret = 'apiKey.secret = %s\n' % environ.get(
'STORMPATH_API_KEY_SECRET')
write(self.fd, api_key_id.encode('utf-8') + b'\n')
write(self.fd, api_key_secret.encode('utf-8') + b'\n')
def test_requires_api_credentials(self):
# We'll remove our default API credentials, and ensure we get an
# exception raised.
self.app.config['STORMPATH_API_KEY_ID'] = None
self.app.config['STORMPATH_API_KEY_SECRET'] = None
self.app.config['STORMPATH_API_KEY_FILE'] = None
self.assertRaises(ConfigurationError, check_settings, self.app.config)
# Now we'll check to see that if we specify an API key ID and secret
# things work.
self.app.config['STORMPATH_API_KEY_ID'] = environ.get('STORMPATH_API_KEY_ID')
self.app.config['STORMPATH_API_KEY_SECRET'] = environ.get('STORMPATH_API_KEY_SECRET')
check_settings(self.app.config)
# Now we'll check to see that if we specify an API key file things work.
self.app.config['STORMPATH_API_KEY_ID'] = None
self.app.config['STORMPATH_API_KEY_SECRET'] = None
self.app.config['STORMPATH_API_KEY_FILE'] = self.file
check_settings(self.app.config)
def test_requires_application(self):
# We'll remove our default Application, and ensure we get an exception
# raised.
self.app.config['STORMPATH_APPLICATION'] = None
self.assertRaises(ConfigurationError, check_settings, self.app.config)
def test_google_settings(self):
# Ensure that if the user has Google login enabled, they've specified
# the correct settings.
self.app.config['STORMPATH_ENABLE_GOOGLE'] = True
self.assertRaises(ConfigurationError, check_settings, self.app.config)
# Ensure that things don't work if not all social configs are specified.
self.app.config['STORMPATH_SOCIAL'] = {}
self.assertRaises(ConfigurationError, check_settings, self.app.config)
self.app.config['STORMPATH_SOCIAL'] = {'GOOGLE': {}}
self.assertRaises(ConfigurationError, check_settings, self.app.config)
self.app.config['STORMPATH_SOCIAL']['GOOGLE']['client_id'] = 'xxx'
self.assertRaises(ConfigurationError, check_settings, self.app.config)
# Now that we've configured things properly, it should work.
self.app.config['STORMPATH_SOCIAL']['GOOGLE']['client_secret'] = 'xxx'
check_settings(self.app.config)
def test_facebook_settings(self):
# Ensure that if the user has Facebook login enabled, they've specified
# the correct settings.
self.app.config['STORMPATH_ENABLE_FACEBOOK'] = True
self.assertRaises(ConfigurationError, check_settings, self.app.config)
# Ensure that things don't work if not all social configs are specified.
self.app.config['STORMPATH_SOCIAL'] = {}
self.assertRaises(ConfigurationError, check_settings, self.app.config)
self.app.config['STORMPATH_SOCIAL'] = {'FACEBOOK': {}}
self.assertRaises(ConfigurationError, check_settings, self.app.config)
self.app.config['STORMPATH_SOCIAL']['FACEBOOK']['app_id'] = 'xxx'
self.assertRaises(ConfigurationError, check_settings, self.app.config)
# Now that we've configured things properly, it should work.
self.app.config['STORMPATH_SOCIAL']['FACEBOOK']['app_secret'] = 'xxx'
check_settings(self.app.config)
def test_cookie_settings(self):
# Ensure that if a user specifies a cookie domain which isn't a string,
# an error is raised.
self.app.config['STORMPATH_COOKIE_DOMAIN'] = 1
self.assertRaises(ConfigurationError, check_settings, self.app.config)
# Now that we've configured things properly, it should work.
self.app.config['STORMPATH_COOKIE_DOMAIN'] = 'test'
check_settings(self.app.config)
# Ensure that if a user specifies a cookie duration which isn't a
# timedelta object, an error is raised.
self.app.config['STORMPATH_COOKIE_DURATION'] = 1
self.assertRaises(ConfigurationError, check_settings, self.app.config)
# Now that we've configured things properly, it should work.
self.app.config['STORMPATH_COOKIE_DURATION'] = timedelta(minutes=1)
check_settings(self.app.config)
def tearDown(self):
"""Remove our apiKey.properties file."""
super(TestCheckSettings, self).tearDown()
# Remove our file.
close(self.fd)
remove(self.file)