mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-27 10:44:33 +00:00
Updated unit- and doctests
This commit is contained in:
parent
7a4486fadf
commit
7af92d9e65
@ -95,7 +95,31 @@ def convert_service_to_string(service):
|
|||||||
|
|
||||||
|
|
||||||
def get_augeas_servicefile_path(protocol):
|
def get_augeas_servicefile_path(protocol):
|
||||||
"""Get the augeas path where a service for a protocol should be stored"""
|
"""Get the augeas path where a service for a protocol should be stored
|
||||||
|
|
||||||
|
TODO: Use doctests instead of unittests until we can use python3.
|
||||||
|
|
||||||
|
>>> get_augeas_servicefile_path('http')
|
||||||
|
'/files/etc/pagekite.d/80_http.rc/service_on'
|
||||||
|
|
||||||
|
>>> get_augeas_servicefile_path('https')
|
||||||
|
'/files/etc/pagekite.d/443_https.rc/service_on'
|
||||||
|
|
||||||
|
>>> get_augeas_servicefile_path('http/80')
|
||||||
|
'/files/etc/pagekite.d/80_http.rc/service_on'
|
||||||
|
|
||||||
|
>>> get_augeas_servicefile_path('http/8080')
|
||||||
|
'/files/etc/pagekite.d/8080_http.rc/service_on'
|
||||||
|
|
||||||
|
>>> get_augeas_servicefile_path('raw/22')
|
||||||
|
'/files/etc/pagekite.d/22_raw.rc/service_on'
|
||||||
|
|
||||||
|
>>> get_augeas_servicefile_path('xmpp')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Unsupported protocol: xmpp
|
||||||
|
|
||||||
|
"""
|
||||||
if not protocol.startswith(("http", "https", "raw")):
|
if not protocol.startswith(("http", "https", "raw")):
|
||||||
raise ValueError('Unsupported protocol: %s' % protocol)
|
raise ValueError('Unsupported protocol: %s' % protocol)
|
||||||
|
|
||||||
@ -112,3 +136,8 @@ def get_augeas_servicefile_path(protocol):
|
|||||||
relpath = '%s_%s.rc' % (port, _protocol)
|
relpath = '%s_%s.rc' % (port, _protocol)
|
||||||
|
|
||||||
return os.path.join(CONF_PATH, relpath, 'service_on')
|
return os.path.join(CONF_PATH, relpath, 'service_on')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|||||||
0
plinth/modules/__init__.py
Normal file
0
plinth/modules/__init__.py
Normal file
@ -73,9 +73,14 @@ PREDEFINED_SERVICES = {
|
|||||||
|
|
||||||
def convert_to_service(service_string):
|
def convert_to_service(service_string):
|
||||||
""" Convert a service string into a service parameter dictionary
|
""" Convert a service string into a service parameter dictionary
|
||||||
>>> convert_to_service('https/443:@kitename:localhost:443:@kitesecret')
|
>>> input = 'https/443:@kitename:localhost:443:@kitesecret'
|
||||||
{'kitename': '@kitename', 'backend_host': 'localhost', \
|
>>> output = convert_to_service(input)
|
||||||
'secret': '@kitesecret', 'protocol': 'https/443', 'backend_port': '443'}
|
>>> expected_output = {'secret': '@kitesecret',
|
||||||
|
... 'backend_host': 'localhost', 'kitename': '@kitename',
|
||||||
|
... 'backend_port': '443', 'protocol': 'https/443'}
|
||||||
|
...
|
||||||
|
>>> output == expected_output
|
||||||
|
True
|
||||||
"""
|
"""
|
||||||
# The actions.py uses shlex.quote() to escape/quote malicious user input.
|
# The actions.py uses shlex.quote() to escape/quote malicious user input.
|
||||||
# That affects '*.@kitename', so the params string gets quoted.
|
# That affects '*.@kitename', so the params string gets quoted.
|
||||||
@ -193,3 +198,8 @@ def _run(arguments, superuser=True):
|
|||||||
return actions.superuser_run(command, arguments)
|
return actions.superuser_run(command, arguments)
|
||||||
else:
|
else:
|
||||||
return actions.run(command, arguments)
|
return actions.run(command, arguments)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -15,10 +16,8 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
import os
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from actions.pagekite_util import get_augeas_servicefile_path, CONF_PATH
|
|
||||||
from plinth.modules.pagekite.util import convert_to_service, \
|
from plinth.modules.pagekite.util import convert_to_service, \
|
||||||
convert_service_to_string
|
convert_service_to_string
|
||||||
|
|
||||||
@ -50,21 +49,6 @@ class TestPagekiteActions(unittest.TestCase):
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
def test_get_augeas_servicefile_path(self):
|
|
||||||
""" Test the generation of augeas-paths for pagekite services """
|
|
||||||
tests = (('http', '80_http.rc'),
|
|
||||||
('https', '443_https.rc'),
|
|
||||||
('http/80', '80_http.rc'),
|
|
||||||
('http/8080', '8080_http.rc'),
|
|
||||||
('raw/22', '22_raw.rc'))
|
|
||||||
for protocol, filename in tests:
|
|
||||||
expected_path = os.path.join(CONF_PATH, filename, 'service_on')
|
|
||||||
returned_path = get_augeas_servicefile_path(protocol)
|
|
||||||
self.assertEqual(expected_path, returned_path)
|
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
get_augeas_servicefile_path('xmpp')
|
|
||||||
|
|
||||||
def test_convert_service_to_string(self):
|
def test_convert_service_to_string(self):
|
||||||
""" Test deconstructing parameter dictionaries into strings """
|
""" Test deconstructing parameter dictionaries into strings """
|
||||||
for test in self._tests:
|
for test in self._tests:
|
||||||
Loading…
x
Reference in New Issue
Block a user