1
|
|
|
import base64
|
2
|
|
|
import os
|
3
|
|
|
import sys
|
4
|
|
|
import tempfile
|
5
|
|
|
|
6
|
|
|
from tabpy.tabpy_server.app.app import TabPyApp
|
7
|
|
|
from tabpy.tabpy_server.app.app import _init_asyncio_patch
|
8
|
|
|
from tabpy.tabpy_server.handlers.util import hash_password
|
9
|
|
|
from tornado.testing import AsyncHTTPTestCase
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class TestEndpointHandlerWithAuth(AsyncHTTPTestCase):
|
13
|
|
View Code Duplication |
@classmethod
|
|
|
|
|
14
|
|
|
def setUpClass(cls):
|
15
|
|
|
_init_asyncio_patch()
|
16
|
|
|
prefix = "__TestEndpointHandlerWithAuth_"
|
17
|
|
|
# create password file
|
18
|
|
|
cls.pwd_file = tempfile.NamedTemporaryFile(
|
19
|
|
|
mode="w+t", prefix=prefix, suffix=".txt", delete=False
|
20
|
|
|
)
|
21
|
|
|
username = "username"
|
22
|
|
|
password = "password"
|
23
|
|
|
cls.pwd_file.write(f"{username} {hash_password(username, password)}")
|
24
|
|
|
cls.pwd_file.close()
|
25
|
|
|
|
26
|
|
|
# create state.ini dir and file
|
27
|
|
|
cls.state_dir = tempfile.mkdtemp(prefix=prefix)
|
28
|
|
|
cls.state_file = open(os.path.join(cls.state_dir, "state.ini"), "w+")
|
29
|
|
|
cls.state_file.write(
|
30
|
|
|
"[Service Info]\n"
|
31
|
|
|
"Name = TabPy Serve\n"
|
32
|
|
|
"Description = \n"
|
33
|
|
|
"Creation Time = 0\n"
|
34
|
|
|
"Access-Control-Allow-Origin = \n"
|
35
|
|
|
"Access-Control-Allow-Headers = \n"
|
36
|
|
|
"Access-Control-Allow-Methods = \n"
|
37
|
|
|
"\n"
|
38
|
|
|
"[Query Objects Service Versions]\n"
|
39
|
|
|
"\n"
|
40
|
|
|
"[Query Objects Docstrings]\n"
|
41
|
|
|
"\n"
|
42
|
|
|
"[Meta]\n"
|
43
|
|
|
"Revision Number = 1\n"
|
44
|
|
|
)
|
45
|
|
|
cls.state_file.close()
|
46
|
|
|
|
47
|
|
|
# create config file
|
48
|
|
|
cls.config_file = tempfile.NamedTemporaryFile(
|
49
|
|
|
mode="w+t", prefix=prefix, suffix=".conf", delete=False
|
50
|
|
|
)
|
51
|
|
|
cls.config_file.write(
|
52
|
|
|
"[TabPy]\n"
|
53
|
|
|
f"TABPY_PWD_FILE = {cls.pwd_file.name}\n"
|
54
|
|
|
f"TABPY_STATE_PATH = {cls.state_dir}"
|
55
|
|
|
)
|
56
|
|
|
cls.config_file.close()
|
57
|
|
|
|
58
|
|
|
@classmethod
|
59
|
|
|
def tearDownClass(cls):
|
60
|
|
|
os.remove(cls.pwd_file.name)
|
61
|
|
|
os.remove(cls.state_file.name)
|
62
|
|
|
os.remove(cls.config_file.name)
|
63
|
|
|
os.rmdir(cls.state_dir)
|
64
|
|
|
|
65
|
|
|
def get_app(self):
|
66
|
|
|
self.app = TabPyApp(self.config_file.name)
|
67
|
|
|
return self.app._create_tornado_web_app()
|
68
|
|
|
|
69
|
|
|
def test_no_creds_required_auth_fails(self):
|
70
|
|
|
response = self.fetch("/endpoints/anything")
|
71
|
|
|
self.assertEqual(401, response.code)
|
72
|
|
|
|
73
|
|
|
def test_invalid_creds_fails(self):
|
74
|
|
|
response = self.fetch(
|
75
|
|
|
"/endpoints/anything",
|
76
|
|
|
method="GET",
|
77
|
|
|
headers={
|
78
|
|
|
"Authorization": "Basic {}".format(
|
79
|
|
|
base64.b64encode("user:wrong_password".encode("utf-8")).decode(
|
80
|
|
|
"utf-8"
|
81
|
|
|
)
|
82
|
|
|
)
|
83
|
|
|
},
|
84
|
|
|
)
|
85
|
|
|
self.assertEqual(401, response.code)
|
86
|
|
|
|
87
|
|
|
def test_valid_creds_pass(self):
|
88
|
|
|
response = self.fetch(
|
89
|
|
|
"/endpoints/",
|
90
|
|
|
method="GET",
|
91
|
|
|
headers={
|
92
|
|
|
"Authorization": "Basic {}".format(
|
93
|
|
|
base64.b64encode("username:password".encode("utf-8")).decode(
|
94
|
|
|
"utf-8"
|
95
|
|
|
)
|
96
|
|
|
)
|
97
|
|
|
},
|
98
|
|
|
)
|
99
|
|
|
self.assertEqual(200, response.code)
|
100
|
|
|
|
101
|
|
|
def test_valid_creds_unknown_endpoint_fails(self):
|
102
|
|
|
response = self.fetch(
|
103
|
|
|
"/endpoints/unknown_endpoint",
|
104
|
|
|
method="GET",
|
105
|
|
|
headers={
|
106
|
|
|
"Authorization": "Basic {}".format(
|
107
|
|
|
base64.b64encode("username:password".encode("utf-8")).decode(
|
108
|
|
|
"utf-8"
|
109
|
|
|
)
|
110
|
|
|
)
|
111
|
|
|
},
|
112
|
|
|
)
|
113
|
|
|
self.assertEqual(404, response.code)
|
114
|
|
|
|
115
|
|
|
|
116
|
|
View Code Duplication |
class TestEndpointHandlerWithoutAuth(AsyncHTTPTestCase):
|
|
|
|
|
117
|
|
|
@classmethod
|
118
|
|
|
def setUpClass(cls):
|
119
|
|
|
_init_asyncio_patch()
|
120
|
|
|
prefix = "__TestEndpointHandlerWithoutAuth_"
|
121
|
|
|
|
122
|
|
|
# create state.ini dir and file
|
123
|
|
|
cls.state_dir = tempfile.mkdtemp(prefix=prefix)
|
124
|
|
|
cls.state_file = open(os.path.join(cls.state_dir, "state.ini"), "w+")
|
125
|
|
|
cls.state_file.write(
|
126
|
|
|
"[Service Info]\n"
|
127
|
|
|
"Name = TabPy Serve\n"
|
128
|
|
|
"Description = \n"
|
129
|
|
|
"Creation Time = 0\n"
|
130
|
|
|
"Access-Control-Allow-Origin = \n"
|
131
|
|
|
"Access-Control-Allow-Headers = \n"
|
132
|
|
|
"Access-Control-Allow-Methods = \n"
|
133
|
|
|
"\n"
|
134
|
|
|
"[Query Objects Service Versions]\n"
|
135
|
|
|
"\n"
|
136
|
|
|
"[Query Objects Docstrings]\n"
|
137
|
|
|
"\n"
|
138
|
|
|
"[Meta]\n"
|
139
|
|
|
"Revision Number = 1\n"
|
140
|
|
|
)
|
141
|
|
|
cls.state_file.close()
|
142
|
|
|
|
143
|
|
|
@classmethod
|
144
|
|
|
def tearDownClass(cls):
|
145
|
|
|
os.remove(cls.state_file.name)
|
146
|
|
|
os.rmdir(cls.state_dir)
|
147
|
|
|
|
148
|
|
|
def get_app(self):
|
149
|
|
|
self.app = TabPyApp(None)
|
150
|
|
|
return self.app._create_tornado_web_app()
|
151
|
|
|
|
152
|
|
|
def test_creds_no_auth_fails(self):
|
153
|
|
|
response = self.fetch(
|
154
|
|
|
"/endpoints/",
|
155
|
|
|
method="GET",
|
156
|
|
|
headers={
|
157
|
|
|
"Authorization": "Basic {}".format(
|
158
|
|
|
base64.b64encode("username:password".encode("utf-8")).decode(
|
159
|
|
|
"utf-8"
|
160
|
|
|
)
|
161
|
|
|
)
|
162
|
|
|
},
|
163
|
|
|
)
|
164
|
|
|
self.assertEqual(406, response.code)
|
165
|
|
|
|