-
Notifications
You must be signed in to change notification settings - Fork 606
/
Copy pathtest_evaluation_plane_handler.py
executable file
·179 lines (160 loc) · 5.83 KB
/
test_evaluation_plane_handler.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
import base64
import os
import tempfile
from argparse import Namespace
from tabpy.tabpy_server.app.app import TabPyApp
from tabpy.tabpy_server.handlers.util import hash_password
from tornado.testing import AsyncHTTPTestCase
from unittest.mock import patch
class TestEvaluationPlainHandlerWithAuth(AsyncHTTPTestCase):
@classmethod
def setUpClass(cls):
cls.patcher = patch(
"tabpy.tabpy_server.app.app.TabPyApp._parse_cli_arguments",
return_value=Namespace(config=None),
)
cls.patcher.start()
prefix = "__TestEvaluationPlainHandlerWithAuth_"
# create password file
cls.pwd_file = tempfile.NamedTemporaryFile(
mode="w+t", prefix=prefix, suffix=".txt", delete=False
)
username = "username"
password = "password"
cls.pwd_file.write(f"{username} {hash_password(username, password)}\n")
cls.pwd_file.close()
# create state.ini dir and file
cls.state_dir = tempfile.mkdtemp(prefix=prefix)
cls.state_file = open(os.path.join(cls.state_dir, "state.ini"), "w+")
cls.state_file.write(
"[Service Info]\n"
"Name = TabPy Serve\n"
"Description = \n"
"Creation Time = 0\n"
"Access-Control-Allow-Origin = \n"
"Access-Control-Allow-Headers = \n"
"Access-Control-Allow-Methods = \n"
"\n"
"[Query Objects Service Versions]\n"
"\n"
"[Query Objects Docstrings]\n"
"\n"
"[Meta]\n"
"Revision Number = 1\n"
)
cls.state_file.close()
# create config file
cls.config_file = tempfile.NamedTemporaryFile(
mode="w+t", prefix=prefix, suffix=".conf", delete=False
)
cls.config_file.write(
"[TabPy]\n"
f"TABPY_PWD_FILE = {cls.pwd_file.name}\n"
f"TABPY_STATE_PATH = {cls.state_dir}"
)
cls.config_file.close()
cls.script = (
'{"data":{"_arg1":[2,3],"_arg2":[3,-1]},'
'"script":"res=[]\\nfor i in range(len(_arg1)):\\n '
'res.append(_arg1[i] * _arg2[i])\\nreturn res"}'
)
cls.script_not_present = (
'{"data":{"_arg1":[2,3],"_arg2":[3,-1]},'
'"":"res=[]\\nfor i in range(len(_arg1)):\\n '
'res.append(_arg1[i] * _arg2[i])\\nreturn res"}'
)
cls.args_not_present = (
'{"script":"res=[]\\nfor i in range(len(_arg1)):\\n '
'res.append(_arg1[i] * _arg2[i])\\nreturn res"}'
)
cls.args_not_sequential = (
'{"data":{"_arg1":[2,3],"_arg3":[3,-1]},'
'"script":"res=[]\\nfor i in range(len(_arg1)):\\n '
'res.append(_arg1[i] * _arg3[i])\\nreturn res"}'
)
@classmethod
def tearDownClass(cls):
cls.patcher.stop()
os.remove(cls.pwd_file.name)
os.remove(cls.state_file.name)
os.remove(cls.config_file.name)
os.rmdir(cls.state_dir)
def get_app(self):
self.app = TabPyApp(self.config_file.name)
return self.app._create_tornado_web_app()
def test_no_creds_required_auth_fails(self):
response = self.fetch("/evaluate", method="POST", body=self.script)
self.assertEqual(401, response.code)
def test_invalid_creds_fails(self):
response = self.fetch(
"/evaluate",
method="POST",
body=self.script,
headers={
"Authorization": "Basic {}".format(
base64.b64encode("user:wrong_password".encode("utf-8")).decode(
"utf-8"
)
)
},
)
self.assertEqual(401, response.code)
def test_valid_creds_pass(self):
response = self.fetch(
"/evaluate",
method="POST",
body=self.script,
headers={
"Authorization": "Basic {}".format(
base64.b64encode("username:password".encode("utf-8")).decode(
"utf-8"
)
)
},
)
self.assertEqual(200, response.code)
def test_null_request(self):
response = self.fetch("")
self.assertEqual(404, response.code)
def test_script_not_present(self):
response = self.fetch(
"/evaluate",
method="POST",
body=self.script_not_present,
headers={
"Authorization": "Basic {}".format(
base64.b64encode("username:password".encode("utf-8")).decode(
"utf-8"
)
)
},
)
self.assertEqual(400, response.code)
def test_arguments_not_present(self):
response = self.fetch(
"/evaluate",
method="POST",
body=self.args_not_present,
headers={
"Authorization": "Basic {}".format(
base64.b64encode("username:password".encode("utf-8")).decode(
"utf-8"
)
)
},
)
self.assertEqual(500, response.code)
def test_arguments_not_sequential(self):
response = self.fetch(
"/evaluate",
method="POST",
body=self.args_not_sequential,
headers={
"Authorization": "Basic {}".format(
base64.b64encode("username:password".encode("utf-8")).decode(
"utf-8"
)
)
},
)
self.assertEqual(400, response.code)