Files
webssh/tests/test_auth_app.py
T
2026-06-13 12:11:36 +08:00

65 lines
2.1 KiB
Python

import json
import shutil
import tempfile
from tornado.options import options
from tornado.testing import AsyncHTTPTestCase
from webssh.main import make_app, make_handlers
from webssh.settings import get_app_settings
from webssh.utils import to_str
class TestAppAuth(AsyncHTTPTestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
super(TestAppAuth, self).setUp()
def tearDown(self):
super(TestAppAuth, self).tearDown()
shutil.rmtree(self.tmpdir)
def get_app(self):
loop = self.io_loop
options.auth = True
options.auth_username = 'admin'
options.auth_password = ''
options.auth_password_hash = ''
options.auth_session_days = 7
options.data_dir = self.tmpdir
options.debug = False
options.xsrf = False
options.policy = 'warning'
options.hostfile = ''
options.syshostfile = ''
options.tdstream = ''
options.origin = 'same'
handlers = make_handlers(loop, options)
return make_app(handlers, get_app_settings(options))
def get_cookie_header(self, response):
cookie = response.headers.get_list('Set-Cookie')[0]
return {'Cookie': cookie.split(';', 1)[0]}
def test_auth_setup_and_saved_connections_endpoint(self):
response = self.fetch('/', follow_redirects=False)
self.assertEqual(response.code, 302)
self.assertIn('/login', response.headers['Location'])
response = self.fetch('/login')
self.assertIn('创建管理员账号'.encode('utf-8'), response.body)
body = 'username=admin&password=secret&confirm=secret'
response = self.fetch('/login', method='POST', body=body,
follow_redirects=False)
self.assertEqual(response.code, 302)
headers = self.get_cookie_header(response)
response = self.fetch('/', headers=headers)
self.assertIn('已保存连接'.encode('utf-8'), response.body)
response = self.fetch('/connections', headers=headers)
data = json.loads(to_str(response.body))
self.assertEqual(data, {'connections': []})