forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_handlers.py
322 lines (264 loc) · 11.9 KB
/
test_handlers.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from flask_login import current_user
from funcy import project
from mock import patch
from redash import models, settings
from tests import BaseTestCase, authenticated_user
class AuthenticationTestMixin(object):
def test_returns_404_when_not_unauthenticated(self):
for path in self.paths:
rv = self.client.get(path)
self.assertEqual(404, rv.status_code)
def test_returns_content_when_authenticated(self):
for path in self.paths:
rv = self.make_request("get", path, is_json=False)
self.assertEqual(200, rv.status_code)
class TestAuthentication(BaseTestCase):
def test_responds_with_success_for_signed_in_user(self):
with self.client as c:
with c.session_transaction() as sess:
sess["_user_id"] = self.factory.user.get_id()
rv = self.client.get("/default/")
self.assertEqual(200, rv.status_code)
def test_redirects_for_nonsigned_in_user(self):
rv = self.client.get("/default/")
self.assertEqual(302, rv.status_code)
def test_redirects_for_invalid_session_identifier(self):
with self.client as c:
with c.session_transaction() as sess:
sess["_user_id"] = 100
rv = self.client.get("/default/")
self.assertEqual(302, rv.status_code)
class PingTest(BaseTestCase):
def test_ping(self):
rv = self.client.get("/ping")
self.assertEqual(200, rv.status_code)
self.assertEqual(b"PONG.", rv.data)
class IndexTest(BaseTestCase):
def setUp(self):
self.paths = [
"/default/",
"/default/dashboard/example",
"/default/queries/1",
"/default/admin/status",
]
super(IndexTest, self).setUp()
def test_redirect_to_login_when_not_authenticated(self):
for path in self.paths:
rv = self.client.get(path)
self.assertEqual(302, rv.status_code)
def test_returns_content_when_authenticated(self):
for path in self.paths:
rv = self.make_request("get", path, org=False, is_json=False)
self.assertEqual(200, rv.status_code)
class StatusTest(BaseTestCase):
def test_returns_data_for_super_admin(self):
admin = self.factory.create_admin()
models.db.session.commit()
rv = self.make_request("get", "/status.json", org=False, user=admin, is_json=False)
self.assertEqual(rv.status_code, 200)
def test_returns_403_for_non_admin(self):
rv = self.make_request("get", "/status.json", org=False, is_json=False)
self.assertEqual(rv.status_code, 403)
def test_redirects_non_authenticated_user(self):
rv = self.client.get("/status.json")
self.assertEqual(rv.status_code, 302)
class JobAPITest(BaseTestCase, AuthenticationTestMixin):
def setUp(self):
self.paths = []
super(JobAPITest, self).setUp()
class TestLogin(BaseTestCase):
def setUp(self):
super(TestLogin, self).setUp()
self.factory.org.set_setting("auth_password_login_enabled", True)
def test_get_login_form(self):
rv = self.client.get("/default/login")
self.assertEqual(rv.status_code, 200)
def test_get_login_form_remote_auth(self):
"""Make sure the remote auth link can be rendered correctly on the
login page when the remote user login feature is enabled"""
old_remote_user_enabled = settings.REMOTE_USER_LOGIN_ENABLED
old_ldap_login_enabled = settings.LDAP_LOGIN_ENABLED
try:
settings.REMOTE_USER_LOGIN_ENABLED = True
settings.LDAP_LOGIN_ENABLED = True
rv = self.client.get("/default/login")
self.assertEqual(rv.status_code, 200)
self.assertIn("/{}/remote_user/login".format(self.factory.org.slug), rv.data.decode())
self.assertIn("/{}/ldap/login".format(self.factory.org.slug), rv.data.decode())
finally:
settings.REMOTE_USER_LOGIN_ENABLED = old_remote_user_enabled
settings.LDAP_LOGIN_ENABLED = old_ldap_login_enabled
def test_submit_non_existing_user(self):
with patch("redash.handlers.authentication.login_user") as login_user_mock:
rv = self.client.post("/default/login", data={"email": "arik", "password": "password"})
self.assertEqual(rv.status_code, 200)
self.assertFalse(login_user_mock.called)
def test_submit_correct_user_and_password(self):
user = self.factory.user
user.hash_password("password")
self.db.session.add(user)
self.db.session.commit()
with patch("redash.handlers.authentication.login_user") as login_user_mock:
rv = self.client.post("/default/login", data={"email": user.email, "password": "password"})
self.assertEqual(rv.status_code, 302)
login_user_mock.assert_called_with(user, remember=False)
def test_submit_case_insensitive_user_and_password(self):
user = self.factory.user
user.hash_password("password")
self.db.session.add(user)
self.db.session.commit()
with patch("redash.handlers.authentication.login_user") as login_user_mock:
rv = self.client.post(
"/default/login",
data={"email": user.email.upper(), "password": "password"},
)
self.assertEqual(rv.status_code, 302)
login_user_mock.assert_called_with(user, remember=False)
def test_submit_correct_user_and_password_and_remember_me(self):
user = self.factory.user
user.hash_password("password")
self.db.session.add(user)
self.db.session.commit()
with patch("redash.handlers.authentication.login_user") as login_user_mock:
rv = self.client.post(
"/default/login",
data={"email": user.email, "password": "password", "remember": True},
)
self.assertEqual(rv.status_code, 302)
login_user_mock.assert_called_with(user, remember=True)
def test_submit_correct_user_and_password_with_next(self):
user = self.factory.user
user.hash_password("password")
self.db.session.add(user)
self.db.session.commit()
with patch("redash.handlers.authentication.login_user") as login_user_mock:
rv = self.client.post(
"/default/login?next=/test",
data={"email": user.email, "password": "password"},
)
self.assertEqual(rv.status_code, 302)
self.assertEqual(rv.location, "/test")
login_user_mock.assert_called_with(user, remember=False)
def test_submit_incorrect_user(self):
with patch("redash.handlers.authentication.login_user") as login_user_mock:
rv = self.client.post("/default/login", data={"email": "non-existing", "password": "password"})
self.assertEqual(rv.status_code, 200)
self.assertFalse(login_user_mock.called)
def test_submit_incorrect_password(self):
user = self.factory.user
user.hash_password("password")
self.db.session.add(user)
self.db.session.commit()
with patch("redash.handlers.authentication.login_user") as login_user_mock:
rv = self.client.post(
"/default/login",
data={"email": user.email, "password": "badbadpassword"},
)
self.assertEqual(rv.status_code, 200)
self.assertFalse(login_user_mock.called)
def test_submit_empty_password(self):
user = self.factory.user
with patch("redash.handlers.authentication.login_user") as login_user_mock:
rv = self.client.post("/default/login", data={"email": user.email, "password": ""})
self.assertEqual(rv.status_code, 200)
self.assertFalse(login_user_mock.called)
def test_user_already_loggedin(self):
with authenticated_user(self.client), patch("redash.handlers.authentication.login_user") as login_user_mock:
rv = self.client.get("/default/login")
self.assertEqual(rv.status_code, 302)
self.assertFalse(login_user_mock.called)
def test_correct_user_and_password_when_password_login_disabled(self):
user = self.factory.user
user.hash_password("password")
self.db.session.add(user)
self.db.session.commit()
self.factory.org.set_setting("auth_password_login_enabled", False)
with patch("redash.handlers.authentication.login_user"):
rv = self.client.post("/default/login", data={"email": user.email, "password": "password"})
self.assertEqual(rv.status_code, 200)
self.assertIn("Password login is not enabled for your organization", str(rv.data))
class TestLogout(BaseTestCase):
def test_logout_when_not_loggedin(self):
with self.app.test_client() as c:
rv = c.get("/default/logout")
self.assertEqual(rv.status_code, 302)
self.assertFalse(current_user.is_authenticated)
def test_logout_when_loggedin(self):
with self.app.test_client() as c, authenticated_user(c, user=self.factory.user):
rv = c.get("/default/")
self.assertTrue(current_user.is_authenticated)
rv = c.get("/default/logout")
self.assertEqual(rv.status_code, 302)
self.assertFalse(current_user.is_authenticated)
class TestQuerySnippet(BaseTestCase):
def test_create(self):
res = self.make_request(
"post",
"/api/query_snippets",
data={"trigger": "x", "description": "y", "snippet": "z"},
user=self.factory.user,
)
self.assertEqual(
project(res.json, ["id", "trigger", "description", "snippet"]),
{"id": 1, "trigger": "x", "description": "y", "snippet": "z"},
)
qs = models.QuerySnippet.query.one()
self.assertEqual(qs.trigger, "x")
self.assertEqual(qs.description, "y")
self.assertEqual(qs.snippet, "z")
def test_edit(self):
qs = models.QuerySnippet(
trigger="a",
description="b",
snippet="c",
user=self.factory.user,
org=self.factory.org,
)
models.db.session.add(qs)
models.db.session.commit()
res = self.make_request(
"post",
"/api/query_snippets/1",
data={"trigger": "x", "description": "y", "snippet": "z"},
user=self.factory.user,
)
self.assertEqual(
project(res.json, ["id", "trigger", "description", "snippet"]),
{"id": 1, "trigger": "x", "description": "y", "snippet": "z"},
)
self.assertEqual(qs.trigger, "x")
self.assertEqual(qs.description, "y")
self.assertEqual(qs.snippet, "z")
def test_list(self):
qs = models.QuerySnippet(
trigger="x",
description="y",
snippet="z",
user=self.factory.user,
org=self.factory.org,
)
models.db.session.add(qs)
models.db.session.commit()
res = self.make_request("get", "/api/query_snippets", user=self.factory.user)
self.assertEqual(res.status_code, 200)
data = res.json
self.assertEqual(len(data), 1)
self.assertEqual(
project(data[0], ["id", "trigger", "description", "snippet"]),
{"id": 1, "trigger": "x", "description": "y", "snippet": "z"},
)
self.assertEqual(qs.trigger, "x")
self.assertEqual(qs.description, "y")
self.assertEqual(qs.snippet, "z")
def test_delete(self):
qs = models.QuerySnippet(
trigger="a",
description="b",
snippet="c",
user=self.factory.user,
org=self.factory.org,
)
models.db.session.add(qs)
models.db.session.commit()
self.make_request("delete", "/api/query_snippets/1", user=self.factory.user)
self.assertEqual(models.QuerySnippet.query.count(), 0)