Skip to content

Commit 2c3b24c

Browse files
committed
added unit tests to check new path for evaluate handler disabled
1 parent f4a0cee commit 2c3b24c

File tree

1 file changed

+104
-11
lines changed

1 file changed

+104
-11
lines changed

tests/unit/server_tests/test_evaluation_plane_handler.py

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from tabpy.tabpy_server.handlers.util import hash_password
99

1010

11-
class TestEvaluationPlainHandlerWithAuth(AsyncHTTPTestCase):
11+
class TestEvaluationPlaneHandlerWithAuth(AsyncHTTPTestCase):
1212
@classmethod
1313
def setUpClass(cls):
14-
prefix = "__TestEvaluationPlainHandlerWithAuth_"
14+
prefix = "__TestEvaluationPlaneHandlerWithAuth_"
1515
# create password file
1616
cls.pwd_file = tempfile.NamedTemporaryFile(
1717
mode="w+t", prefix=prefix, suffix=".txt", delete=False
@@ -205,10 +205,10 @@ def test_script_returns_none(self):
205205
self.assertEqual(b'null', response.body)
206206

207207

208-
class TestEvaluationPlainHandlerWithoutAuth(AsyncHTTPTestCase):
208+
class TestEvaluationPlaneHandlerWithoutAuth(AsyncHTTPTestCase):
209209
@classmethod
210210
def setUpClass(cls):
211-
prefix = "__TestEvaluationPlainHandlerWithoutAuth_"
211+
prefix = "__TestEvaluationPlaneHandlerWithoutAuth_"
212212

213213
# create state.ini dir and file
214214
cls.state_dir = tempfile.mkdtemp(prefix=prefix)
@@ -287,11 +287,10 @@ def test_creds_no_auth_fails(self):
287287
)
288288
self.assertEqual(406, response.code)
289289

290-
291-
class TestEvaluationPlainHandlerDisabled(AsyncHTTPTestCase):
290+
class TestEvaluationPlaneHandlerDisabledWithoutAuth(AsyncHTTPTestCase):
292291
@classmethod
293292
def setUpClass(cls):
294-
prefix = "__TestEvaluationPlainHandlerDisabled_"
293+
prefix = "__TestEvaluationPlaneHandlerDisabledWithoutAuth_"
295294

296295
# create config file
297296
cls.config_file = tempfile.NamedTemporaryFile(
@@ -325,11 +324,105 @@ def test_evaluation_disabled_fails(self):
325324
)
326325
self.assertEqual(404, response.code)
327326

327+
class TestEvaluationPlaneHandlerDisabledWithAuth(AsyncHTTPTestCase):
328+
@classmethod
329+
def setUpClass(cls):
330+
prefix = "__TestEvaluationPlaneHandlerDisabledWithAuth_"
331+
332+
# create password file
333+
cls.pwd_file = tempfile.NamedTemporaryFile(
334+
mode="w+t", prefix=prefix, suffix=".txt", delete=False
335+
)
336+
username = "username"
337+
password = "password"
338+
cls.pwd_file.write(f"{username} {hash_password(username, password)}\n")
339+
cls.pwd_file.close()
340+
341+
# create state.ini dir and file
342+
cls.state_dir = tempfile.mkdtemp(prefix=prefix)
343+
cls.state_file = open(os.path.join(cls.state_dir, "state.ini"), "w+")
344+
cls.state_file.write(
345+
"[Service Info]\n"
346+
"Name = TabPy Serve\n"
347+
"Description = \n"
348+
"Creation Time = 0\n"
349+
"Access-Control-Allow-Origin = \n"
350+
"Access-Control-Allow-Headers = \n"
351+
"Access-Control-Allow-Methods = \n"
352+
"\n"
353+
"[Query Objects Service Versions]\n"
354+
"\n"
355+
"[Query Objects Docstrings]\n"
356+
"\n"
357+
"[Meta]\n"
358+
"Revision Number = 1\n"
359+
)
360+
cls.state_file.close()
361+
362+
# create config file
363+
cls.config_file = tempfile.NamedTemporaryFile(
364+
mode="w+t", prefix=prefix, suffix=".conf", delete=False
365+
)
366+
cls.config_file.write(
367+
"[TabPy]\n"
368+
f"TABPY_PWD_FILE = {cls.pwd_file.name}\n"
369+
f"TABPY_STATE_PATH = {cls.state_dir}\n"
370+
f"TABPY_EVALUATE_ENABLE = false"
371+
)
372+
cls.config_file.close()
373+
374+
cls.script = (
375+
'{"data":{"_arg1":[2,3],"_arg2":[3,-1]},'
376+
'"script":"res=[]\\nfor i in range(len(_arg1)):\\n '
377+
'res.append(_arg1[i] * _arg2[i])\\nreturn res"}'
378+
)
379+
380+
@classmethod
381+
def tearDownClass(cls):
382+
os.remove(cls.pwd_file.name)
383+
os.remove(cls.state_file.name)
384+
os.remove(cls.config_file.name)
385+
os.rmdir(cls.state_dir)
386+
387+
def get_app(self):
388+
self.app = TabPyApp(self.config_file.name)
389+
return self.app._create_tornado_web_app()
390+
391+
def test_evaluation_disabled_fails_with_invalid_creds(self):
392+
response = self.fetch(
393+
"/evaluate",
394+
method="POST",
395+
body=self.script,
396+
headers={
397+
"Authorization": "Basic {}".format(
398+
base64.b64encode("user:wrong_password".encode("utf-8")).decode(
399+
"utf-8"
400+
)
401+
)
402+
},
403+
)
404+
self.assertEqual(401, response.code)
405+
406+
def test_evaluation_disabled_fails_with_valid_creds(self):
407+
response = self.fetch(
408+
"/evaluate",
409+
method="POST",
410+
body=self.script,
411+
headers={
412+
"Authorization": "Basic {}".format(
413+
base64.b64encode("username:password".encode("utf-8")).decode(
414+
"utf-8"
415+
)
416+
)
417+
},
418+
)
419+
self.assertEqual(404, response.code)
420+
328421

329-
class TestEvaluationPlainHandlerEnabled(AsyncHTTPTestCase):
422+
class TestEvaluationPlaneHandlerEnabled(AsyncHTTPTestCase):
330423
@classmethod
331424
def setUpClass(cls):
332-
prefix = "__TestEvaluationPlainHandlerEnabled_"
425+
prefix = "__TestEvaluationPlaneHandlerEnabled_"
333426

334427
# create config file
335428
cls.config_file = tempfile.NamedTemporaryFile(
@@ -364,10 +457,10 @@ def test_evaluation_enabled(self):
364457
self.assertEqual(200, response.code)
365458

366459

367-
class TestEvaluationPlainHandlerDefault(AsyncHTTPTestCase):
460+
class TestEvaluationPlaneHandlerDefault(AsyncHTTPTestCase):
368461
@classmethod
369462
def setUpClass(cls):
370-
prefix = "__TestEvaluationPlainHandlerDefault_"
463+
prefix = "__TestEvaluationPlaneHandlerDefault_"
371464

372465
# create config file
373466
cls.config_file = tempfile.NamedTemporaryFile(

0 commit comments

Comments
 (0)