-
Notifications
You must be signed in to change notification settings - Fork 601
/
test_endpoint_handler.py
executable file
·119 lines (107 loc) · 3.83 KB
/
test_endpoint_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
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 TestEndpointHandlerWithAuth(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 = "__TestEndpointHandlerWithAuth_"
# 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)}")
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()
@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("/endpoints/anything")
self.assertEqual(401, response.code)
def test_invalid_creds_fails(self):
response = self.fetch(
"/endpoints/anything",
method="GET",
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(
"/endpoints/",
method="GET",
headers={
"Authorization": "Basic {}".format(
base64.b64encode("username:password".encode("utf-8")).decode(
"utf-8"
)
)
},
)
self.assertEqual(200, response.code)
def test_valid_creds_unknown_endpoint_fails(self):
response = self.fetch(
"/endpoints/unknown_endpoint",
method="GET",
headers={
"Authorization": "Basic {}".format(
base64.b64encode("username:password".encode("utf-8")).decode(
"utf-8"
)
)
},
)
self.assertEqual(404, response.code)