|
| 1 | +import pytest, os, re, yaml, random |
| 2 | +from tornado.options import options |
| 3 | +from tornado.testing import AsyncTestCase, AsyncHTTPTestCase |
| 4 | +from webssh.main import make_app, make_handlers |
| 5 | +from webssh.settings import get_app_settings |
| 6 | +from tests.utils import make_tests_data_path |
| 7 | +from yaml.loader import SafeLoader |
| 8 | + |
| 9 | +class TestYAMLLoading(object): |
| 10 | + def test_profile_samples(self): |
| 11 | + if 'PROFILES' in os.environ: del os.environ['PROFILES'] |
| 12 | + assert 'profiles' not in get_app_settings(options) |
| 13 | + |
| 14 | + os.environ['PROFILES']=make_tests_data_path('profiles-sample.yaml') |
| 15 | + assert 'profiles' in get_app_settings(options) |
| 16 | + profiles=get_app_settings(options)['profiles']['profiles'] |
| 17 | + assert profiles[0]['name']=='sample1' |
| 18 | + assert profiles[0]['description']=='Long description' |
| 19 | + assert profiles[0]['host']=='localhost' |
| 20 | + assert profiles[0]['port']==22 |
| 21 | + assert profiles[0]['username']=='robey' |
| 22 | + |
| 23 | + assert profiles[1]['name']=='sample2' |
| 24 | + assert profiles[1]['description']=='Long description' |
| 25 | + assert profiles[1]['host']=='localhost' |
| 26 | + assert profiles[1]['port']==22 |
| 27 | + assert profiles[1]['username']=='robey' |
| 28 | + assert profiles[1]['private-key']==open(make_tests_data_path('user_rsa_key'), 'r').read() |
| 29 | + del os.environ['PROFILES'] |
| 30 | + |
| 31 | +class _TestBasic_(object): |
| 32 | + running = [True] |
| 33 | + sshserver_port = 2200 |
| 34 | + body = 'hostname={host}&port={port}&profile={profile}&username={username}&password={password}' |
| 35 | + headers = {'Cookie': '_xsrf=yummy'} |
| 36 | + |
| 37 | + def _getApp_(self, **kwargs): |
| 38 | + loop = self.io_loop |
| 39 | + options.debug = False |
| 40 | + options.policy = random.choice(['warning', 'autoadd']) |
| 41 | + options.hostfile = '' |
| 42 | + options.syshostfile = '' |
| 43 | + options.tdstream = '' |
| 44 | + options.delay = 0.1 |
| 45 | + #options.profiles=make_tests_data_path('tests/data/profiles-sample.yaml') |
| 46 | + app = make_app(make_handlers(loop, options), get_app_settings(options)) |
| 47 | + return app |
| 48 | + |
| 49 | +class TestWebGUIWithProfiles(AsyncHTTPTestCase, _TestBasic_): |
| 50 | + def get_app(self): |
| 51 | + try: |
| 52 | + os.environ['PROFILES']=make_tests_data_path('profiles-sample.yaml') |
| 53 | + return self._getApp_() |
| 54 | + finally: |
| 55 | + del os.environ['PROFILES'] |
| 56 | + |
| 57 | + |
| 58 | + def test_get_app_settings(self): |
| 59 | + try: |
| 60 | + os.environ['PROFILES']=make_tests_data_path('profiles-sample.yaml') |
| 61 | + settings=get_app_settings(options) |
| 62 | + assert 'profiles' in settings |
| 63 | + profiles=settings['profiles']['profiles'] |
| 64 | + assert profiles[0]['name']=='sample1' |
| 65 | + assert profiles[0]['description']=='Long description' |
| 66 | + assert profiles[0]['host']=='localhost' |
| 67 | + assert profiles[0]['port']==22 |
| 68 | + assert profiles[0]['username']=='robey' |
| 69 | + |
| 70 | + assert profiles[1]['name']=='sample2' |
| 71 | + assert profiles[1]['description']=='Long description' |
| 72 | + assert profiles[1]['host']=='localhost' |
| 73 | + assert profiles[1]['port']==22 |
| 74 | + assert profiles[1]['username']=='robey' |
| 75 | + assert profiles[1]['private-key']==open(make_tests_data_path('user_rsa_key'), 'r').read() |
| 76 | + finally: |
| 77 | + del os.environ['PROFILES'] |
| 78 | + |
| 79 | + def test_without_profiles(self): |
| 80 | + rep = self.fetch('/') |
| 81 | + assert rep.code==200, 'Testing server response status code: {0}'.format(rep.code) |
| 82 | + assert str(rep.body).index('<!-- PROFILES -->')>=0, 'Expected the "profiles.html" but "index.html"' |
| 83 | + |
| 84 | +class TestWebGUIWithoutProfiles(AsyncHTTPTestCase, _TestBasic_): |
| 85 | + def get_app(self): |
| 86 | + if 'PROFILES' in os.environ: del os.environ['PROFILES'] |
| 87 | + return self._getApp_() |
| 88 | + |
| 89 | + def test_get_app_settings(self): |
| 90 | + if 'PROFILES' in os.environ: del os.environ['PROFILES'] |
| 91 | + settings=get_app_settings(options) |
| 92 | + assert 'profiles' not in settings |
| 93 | + |
| 94 | + def test_with_profiles(self): |
| 95 | + rep = self.fetch('/') |
| 96 | + assert rep.code==200, 'Testing server response status code: {0}'.format(rep.code) |
| 97 | + with pytest.raises(ValueError): |
| 98 | + str(rep.body).index('<!-- PROFILES -->') |
| 99 | + assert False, 'Expected the origin "index.html" but "profiles.html"' |
0 commit comments