-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtest_stormpath.py
231 lines (225 loc) · 8.12 KB
/
test_stormpath.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""
Our Flask-Stormpath tests.
Why are these in a single file instead of a directory? Honestly, it's because
this extension is so simple, it didn't warrant a proper directory / module. So
we'll just follow Flask conventions and have single file stuff going on.
"""
from os import environ, remove
from unittest import TestCase
from uuid import uuid4
from flask import Flask, request
from flask.ext.stormpath import (
StormpathManager,
User,
groups_required,
login_user,
logout_user,
)
from stormpath.client import Client
#class TestStormpathManager(TestCase):
# """Our StormpathManager test suite."""
#
# def setUp(self):
# self.client = Client(
# id = environ.get('STORMPATH_API_KEY_ID'),
# secret = environ.get('STORMPATH_API_KEY_SECRET'),
# )
# self.application_name = 'flask-stormpath-tests-%s' % uuid4().hex
#
# # Try to delete our test application / directory first, this way if we
# # mess something up while developing test code (like I have many times
# # now), we won't get issues and have to manually remove these resources.
# try:
# self.client.applications.search(self.application_name)[0].delete()
# self.client.directories.search(self.application_name)[0].delete()
# except:
# pass
#
# self.application = self.client.applications.create({
# 'name': self.application_name,
# 'description': 'This application is ONLY used for testing the Flask-Stormpath library. Please do not use this for anything serious.',
# }, create_directory=True)
# self.user = self.application.accounts.create({
# 'given_name': 'Randall',
# 'surname': 'Degges',
# 'email': 'randall@stormpath.com',
# 'username': 'randall',
# 'password': 'woot1LoveCookies!',
# })
# self.user.__class__ = User
#
# keyfile = open('apiKey.properties', 'wb')
# keyfile.write('apiKey.id = %s\n' % environ.get('STORMPATH_API_KEY_ID'))
# keyfile.write('apiKey.secret = %s\n' % environ.get('STORMPATH_API_KEY_SECRET'))
# keyfile.close()
#
# self.app = Flask(__name__)
# self.app.config['SECRET_KEY'] = 'woot'
# self.app.config['STORMPATH_API_KEY_FILE'] = 'apiKey.properties'
# self.app.config['STORMPATH_APPLICATION'] = self.application_name
# StormpathManager(self.app)
#
# def test_init(self):
# sm = StormpathManager()
# self.assertEqual(sm.app, None)
#
# sm = StormpathManager(self.app)
# self.assertEqual(sm.app, self.app)
#
# def test_init_app(self):
# StormpathManager(self.app)
# self.assertEqual(self.app.login_manager.session_protection, 'strong')
#
# def test_client(self):
# with self.app.app_context():
# self.assertIsInstance(self.app.stormpath_manager.client, Client)
#
# def test_login_view(self):
# def test_view():
# return 'hi'
#
# with self.app.app_context():
# self.app.stormpath_manager.login_view = test_view
#
# def tearDown(self):
# remove('apiKey.properties')
# self.application.delete()
# self.client.directories.search(self.application_name)[0].delete()
#
#
#class TestGroupsRequired(TestCase):
# """Our groups_required decorator test suite."""
#
# def setUp(self):
# self.client = Client(
# id = environ.get('STORMPATH_API_KEY_ID'),
# secret = environ.get('STORMPATH_API_KEY_SECRET'),
# )
# self.application_name = 'flask-stormpath-tests-%s' % uuid4().hex
#
# # Try to delete our test application / directory first, this way if we
# # mess something up while developing test code (like I have many times
# # now), we won't get issues and have to manually remove these resources.
# try:
# self.client.applications.search(self.application_name)[0].delete()
# self.client.directories.search(self.application_name)[0].delete()
# except:
# pass
#
# self.application = self.client.applications.create({
# 'name': self.application_name,
# 'description': 'This application is ONLY used for testing the Flask-Stormpath library. Please do not use this for anything serious.',
# }, create_directory=True)
#
# self.user = self.application.accounts.create({
# 'given_name': 'Randall',
# 'surname': 'Degges',
# 'email': 'randall@stormpath.com',
# 'username': 'randall',
# 'password': 'woot1LoveCookies!',
# })
# self.user.__class__ = User
#
# self.admin_group = self.application.groups.create({
# 'name': 'admins',
# })
# self.developers_group = self.application.groups.create({
# 'name': 'developers',
# })
#
# self.app = Flask(__name__)
# self.app.config['SECRET_KEY'] = 'woot'
# 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')
# self.app.config['STORMPATH_APPLICATION'] = self.application_name
#
# StormpathManager(self.app)
#
# def test_unauthenticated(self):
# with self.app.test_client() as c:
# @self.app.route('/')
# @groups_required(['admins'])
# def test_view():
# return 'hi'
#
# self.assertEqual(c.get('/').status_code, 401)
#
# def test_unauthorized(self):
# with self.app.test_client() as c:
#
# @self.app.route('/login', methods=['POST'])
# def login():
# user = User.from_login(
# request.form.get('email'),
# request.form.get('password'),
# )
# login_user(user, remember=True)
# return 'logged in'
#
# @self.app.route('/')
# @groups_required(['admins', 'developers'])
# def test_view():
# return 'hi'
#
# resp = c.post('/login', data=dict(
# email = self.user.email,
# password = 'woot1LoveCookies!',
# ))
# self.assertEqual(resp.status_code, 200)
#
# self.assertEqual(c.get('/').status_code, 401)
#
# self.user.add_group('developers')
# self.assertEqual(c.get('/').status_code, 401)
#
# def test_authorized(self):
# with self.app.test_client() as c:
#
# @self.app.route('/login', methods=['POST'])
# def login():
# user = User.from_login(
# request.form.get('email'),
# request.form.get('password'),
# )
# login_user(user, remember=True)
# return 'logged in'
#
# @self.app.route('/logout')
# def logout():
# logout_user()
#
# @self.app.route('/')
# @groups_required([
# self.admin_group.href,
# self.developers_group
# ], all=False)
# def test_view():
# return 'hi'
#
# @self.app.route('/another_test')
# @groups_required(['admins', 'developers'], all=True)
# def another_test_view():
# return 'hi'
#
# resp = c.post('/login', data=dict(
# email = self.user.email,
# password = 'woot1LoveCookies!',
# ))
# self.assertEqual(resp.status_code, 200)
#
# self.assertEqual(c.get('/').status_code, 401)
# self.assertEqual(c.get('/another_test').status_code, 401)
#
# # Add the user to the admins group.
# self.user.add_group('admins')
#
# self.assertEqual(c.get('/').status_code, 200)
# self.assertEqual(c.get('/another_test').status_code, 401)
#
# # Add the user to the developers group.
# self.user.add_group('developers')
# self.assertEqual(c.get('/another_test').status_code, 200)
#
# def tearDown(self):
# self.application.delete()
# self.client.directories.search(self.application_name)[0].delete()