1
|
|
|
import base64
|
2
|
|
|
import os
|
3
|
|
|
import tempfile
|
4
|
|
|
|
5
|
|
|
from tabpy.tabpy_server.app.app import TabPyApp
|
6
|
|
|
from tabpy.tabpy_server.handlers.util import hash_password
|
7
|
|
|
from tornado.testing import AsyncHTTPTestCase
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class TestEndpointsHandlerWithAuth(AsyncHTTPTestCase):
|
11
|
|
View Code Duplication |
@classmethod
|
|
|
|
|
12
|
|
|
def setUpClass(cls):
|
13
|
|
|
prefix = "__TestEndpointsHandlerWithAuth_"
|
14
|
|
|
# create password file
|
15
|
|
|
cls.pwd_file = tempfile.NamedTemporaryFile(
|
16
|
|
|
mode="w+t", prefix=prefix, suffix=".txt", delete=False
|
17
|
|
|
)
|
18
|
|
|
username = "username"
|
19
|
|
|
password = "password"
|
20
|
|
|
cls.pwd_file.write(f"{username} {hash_password(username, password)}")
|
21
|
|
|
cls.pwd_file.close()
|
22
|
|
|
|
23
|
|
|
# create state.ini dir and file
|
24
|
|
|
cls.state_dir = tempfile.mkdtemp(prefix=prefix)
|
25
|
|
|
cls.state_file = open(os.path.join(cls.state_dir, "state.ini"), "w+")
|
26
|
|
|
cls.state_file.write(
|
27
|
|
|
"[Service Info]\n"
|
28
|
|
|
"Name = TabPy Serve\n"
|
29
|
|
|
"Description = \n"
|
30
|
|
|
"Creation Time = 0\n"
|
31
|
|
|
"Access-Control-Allow-Origin = \n"
|
32
|
|
|
"Access-Control-Allow-Headers = \n"
|
33
|
|
|
"Access-Control-Allow-Methods = \n"
|
34
|
|
|
"\n"
|
35
|
|
|
"[Query Objects Service Versions]\n"
|
36
|
|
|
"\n"
|
37
|
|
|
"[Query Objects Docstrings]\n"
|
38
|
|
|
"\n"
|
39
|
|
|
"[Meta]\n"
|
40
|
|
|
"Revision Number = 1\n"
|
41
|
|
|
)
|
42
|
|
|
cls.state_file.close()
|
43
|
|
|
|
44
|
|
|
# create config file
|
45
|
|
|
cls.config_file = tempfile.NamedTemporaryFile(
|
46
|
|
|
mode="w+t", prefix=prefix, suffix=".conf", delete=False
|
47
|
|
|
)
|
48
|
|
|
cls.config_file.write(
|
49
|
|
|
"[TabPy]\n"
|
50
|
|
|
f"TABPY_PWD_FILE = {cls.pwd_file.name}\n"
|
51
|
|
|
f"TABPY_STATE_PATH = {cls.state_dir}"
|
52
|
|
|
)
|
53
|
|
|
cls.config_file.close()
|
54
|
|
|
|
55
|
|
|
@classmethod
|
56
|
|
|
def tearDownClass(cls):
|
57
|
|
|
os.remove(cls.pwd_file.name)
|
58
|
|
|
os.remove(cls.state_file.name)
|
59
|
|
|
os.remove(cls.config_file.name)
|
60
|
|
|
os.rmdir(cls.state_dir)
|
61
|
|
|
|
62
|
|
|
def get_app(self):
|
63
|
|
|
self.app = TabPyApp(self.config_file.name)
|
64
|
|
|
return self.app._create_tornado_web_app()
|
65
|
|
|
|
66
|
|
|
def test_no_creds_required_auth_fails(self):
|
67
|
|
|
response = self.fetch("/endpoints")
|
68
|
|
|
self.assertEqual(401, response.code)
|
69
|
|
|
|
70
|
|
|
def test_invalid_creds_fails(self):
|
71
|
|
|
response = self.fetch(
|
72
|
|
|
"/endpoints",
|
73
|
|
|
method="GET",
|
74
|
|
|
headers={
|
75
|
|
|
"Authorization": "Basic {}".format(
|
76
|
|
|
base64.b64encode("user:wrong_password".encode("utf-8")).decode(
|
77
|
|
|
"utf-8"
|
78
|
|
|
)
|
79
|
|
|
)
|
80
|
|
|
},
|
81
|
|
|
)
|
82
|
|
|
self.assertEqual(401, response.code)
|
83
|
|
|
|
84
|
|
|
def test_valid_creds_pass(self):
|
85
|
|
|
response = self.fetch(
|
86
|
|
|
"/endpoints",
|
87
|
|
|
method="GET",
|
88
|
|
|
headers={
|
89
|
|
|
"Authorization": "Basic {}".format(
|
90
|
|
|
base64.b64encode("username:password".encode("utf-8")).decode(
|
91
|
|
|
"utf-8"
|
92
|
|
|
)
|
93
|
|
|
)
|
94
|
|
|
},
|
95
|
|
|
)
|
96
|
|
|
self.assertEqual(200, response.code)
|
97
|
|
|
|