tiramisu/tests/test_config_domain.py

258 lines
11 KiB
Python
Raw Normal View History

2017-07-09 09:49:03 +02:00
from .autopath import do_autopath
2015-07-24 17:54:10 +02:00
do_autopath()
2019-06-21 23:04:04 +02:00
from .config import config_type, get_config, value_list, global_owner
2015-07-24 17:54:10 +02:00
2016-03-19 21:27:37 +01:00
import warnings, sys
2019-12-24 15:24:20 +01:00
import pytest
from tiramisu import Config, DomainnameOption, EmailOption, URLOption, OptionDescription
from tiramisu.error import ValueWarning
from tiramisu.i18n import _
2018-10-31 08:00:19 +01:00
from tiramisu.storage import list_sessions
def teardown_function(function):
assert list_sessions() == [], 'session list is not empty when leaving "{}"'.format(function.__name__)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_domainname(config_type):
d = DomainnameOption('d', '')
2013-10-01 10:13:17 +02:00
f = DomainnameOption('f', '', allow_without_dot=True)
2014-02-06 22:17:20 +01:00
g = DomainnameOption('g', '', allow_ip=True)
2019-12-08 09:09:48 +01:00
h = DomainnameOption('h', '', allow_cidr_network=True)
od = OptionDescription('a', '', [d, f, g, h])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
await cfg.property.read_write()
cfg = await get_config(cfg, config_type)
#
2019-12-24 15:24:20 +01:00
await cfg.option('d').value.set('toto.com')
with pytest.raises(ValueError):
await cfg.option('d').value.set('toto')
await cfg.option('d').value.set('toto3.com')
with pytest.raises(ValueError):
await cfg.option('d').value.set('toto_super.com')
await cfg.option('d').value.set('toto-.com')
with pytest.raises(ValueError):
await cfg.option('d').value.set('toto..com')
2013-10-01 10:13:17 +02:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('f').value.set('toto.com')
await cfg.option('f').value.set('toto')
await cfg.option('f').value.set('domainnametoolongthathavemorethanmaximumsizeforatruedomainnamea')
with pytest.raises(ValueError):
await cfg.option('f').value.set('domainnametoolongthathavemorethanmaximumsizeforatruedomainnamean')
await cfg.option('f').value.set('domainnametoolongthathavemorethanmaximumsizeforatruedomainnamea.nd')
await cfg.option('f').value.set('domainnametoolongthathavemorethanmaximumsizeforatruedomainnamea.nditsnoteasytogeneratesolongdomainnamewithoutrepeatdomainnameto.olongthathavemorethanmaximumsizeforatruedomainnameanditsnoteas.ytogeneratesolongdomainnamewithoutrepeatbutimnotabletodoitnowie')
with pytest.raises(ValueError):
await cfg.option('d').value.set('domainnametoolongthathavemorethanmaximumsizeforatruedomainnamea.nditsnoteasytogeneratesolongdomainnamewithoutrepeatdomainnameto.olongthathavemorethanmaximumsizeforatruedomainnameanditsnoteas.ytogeneratesolongdomainnamewithoutrepeatbutimnotabletodoitnowien')
await cfg.option('f').value.set('d')
await cfg.option('f').value.set('d.t')
2014-02-06 22:17:20 +01:00
#
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('f').value.set('192.168.1.1')
with pytest.raises(ValueError):
await cfg.option('f').value.set('192.168.1.0/24')
#
2019-12-24 15:24:20 +01:00
await cfg.option('g').value.set('toto.com')
await cfg.option('g').value.set('192.168.1.0')
await cfg.option('g').value.set('192.168.1.29')
with pytest.raises(ValueError):
await cfg.option('g').value.set('192.168.1.0/24')
#
2019-12-24 15:24:20 +01:00
await cfg.option('h').value.set('toto.com')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('h').value.set('192.168.1.0')
with pytest.raises(ValueError):
await cfg.option('h').value.set('192.168.1.29')
2019-12-08 09:09:48 +01:00
# it's a network address
2019-12-24 15:24:20 +01:00
await cfg.option('h').value.set('192.168.1.0/24')
2019-12-08 09:09:48 +01:00
# but not here
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('h').value.set('192.168.1.1/24')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_domainname_upper(config_type):
d = DomainnameOption('d', '')
od = OptionDescription('a', '', [d])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
await cfg.property.read_write()
cfg = await get_config(cfg, config_type)
await cfg.option('d').value.set('toto.com')
msg = _('some characters are uppercase')
has_error = False
try:
2019-12-24 15:24:20 +01:00
await cfg.option('d').value.set('TOTO.COM')
2016-03-19 21:27:37 +01:00
except ValueError as err:
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
assert msg in str(err)
has_error = True
assert has_error is True
has_error = False
try:
2019-12-24 15:24:20 +01:00
await cfg.option('d').value.set('toTo.com')
2016-03-19 21:27:37 +01:00
except ValueError as err:
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
assert msg in str(err)
has_error = True
assert has_error is True
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_domainname_warning(config_type):
d = DomainnameOption('d', '', warnings_only=True)
f = DomainnameOption('f', '', allow_without_dot=True, warnings_only=True)
g = DomainnameOption('g', '', allow_ip=True, warnings_only=True)
od = OptionDescription('a', '', [d, f, g])
warnings.simplefilter("always", ValueWarning)
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
await cfg.property.read_write()
cfg = await get_config(cfg, config_type)
await cfg.option('d').value.set('toto.com')
with pytest.raises(ValueError):
await cfg.option('d').value.set('toto')
await cfg.option('d').value.set('toto3.com')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
with warnings.catch_warnings(record=True) as w:
2019-12-24 15:24:20 +01:00
await cfg.option('d').value.set('toto_super.com')
2019-06-21 23:04:04 +02:00
assert len(w) == 1
2018-03-19 08:33:53 +01:00
with warnings.catch_warnings(record=True) as w:
2019-12-24 15:24:20 +01:00
await cfg.option('d').value.set('toto-.com')
2018-03-19 08:33:53 +01:00
assert len(w) == 0
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('d').value.set('toto..com')
#
2019-12-24 15:24:20 +01:00
await cfg.option('f').value.set('toto.com')
await cfg.option('f').value.set('toto')
await cfg.option('f').value.set('domainnametoolongthathavemorethanmaximumsizeforatruedomainnamea')
with pytest.raises(ValueError):
await cfg.option('f').value.set('domainnametoolongthathavemorethanmaximumsizeforatruedomainnamean')
await cfg.option('f').value.set('domainnametoolongthathavemorethanmaximumsizeforatruedomainnamea.nd')
await cfg.option('f').value.set('domainnametoolongthathavemorethanmaximumsizeforatruedomainnamea.nditsnoteasytogeneratesolongdomainnamewithoutrepeatdomainnameto.olongthathavemorethanmaximumsizeforatruedomainnameanditsnoteas.ytogeneratesolongdomainnamewithoutrepeatbutimnotabletodoitnowie')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('f').value.set('domainnametoolongthathavemorethanmaximumsizeforatruedomainname.nditsnoteasytogeneratesolongdomainnamewithoutrepeatdomainnamet.olongthathavemorethanmaximumsizeforatruedomainnameanditsnotea.ytogeneratesolongdomainnamewithoutrepeatbutimnotabletodoitnowie.xxxx')
await cfg.option('f').value.set('d')
await cfg.option('f').value.set('d.t')
#
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('f').value.set('192.168.1.1')
await cfg.option('g').value.set('toto.com')
await cfg.option('g').value.set('192.168.1.0')
await cfg.option('g').value.set('192.168.1.29')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_special_domain_name(config_type):
"""domain name option that starts with a number or not
"""
d = DomainnameOption('d', '')
2019-12-08 09:09:48 +01:00
e = DomainnameOption('e', '', type='netbios')
od = OptionDescription('a', '', [d, e])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
await cfg.property.read_write()
cfg = await get_config(cfg, config_type)
await cfg.option('d').value.set('1toto.com')
await cfg.option('d').value.set('123toto.com')
await cfg.option('e').value.set('toto')
await cfg.option('e').value.set('1toto')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_domainname_netbios(config_type):
2019-12-08 09:09:48 +01:00
d = DomainnameOption('d', '', type='netbios')
e = DomainnameOption('e', '', "toto", type='netbios')
od = OptionDescription('a', '', [d, e])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
await cfg.property.read_write()
cfg = await get_config(cfg, config_type)
with pytest.raises(ValueError):
await cfg.option('d').value.set('toto.com')
await cfg.option('d').value.set('toto')
with pytest.raises(ValueError):
await cfg.option('d').value.set('domainnametoolong')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_domainname_hostname(config_type):
2019-12-08 09:09:48 +01:00
d = DomainnameOption('d', '', type='hostname')
e = DomainnameOption('e', '', "toto", type='hostname')
od = OptionDescription('a', '', [d, e])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
await cfg.property.read_write()
cfg = await get_config(cfg, config_type)
with pytest.raises(ValueError):
await cfg.option('d').value.set('toto.com')
await cfg.option('d').value.set('toto')
await cfg.option('d').value.set('domainnametoolong')
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_email(config_type):
e = EmailOption('e', '')
od = OptionDescription('a', '', [e])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
await cfg.property.read_write()
cfg = await get_config(cfg, config_type)
await cfg.option('e').value.set('foo-bar.baz@example.com')
await cfg.option('e').value.set('root@foo.com')
await cfg.option('e').value.set('root@domain')
with pytest.raises(ValueError):
await cfg.option('e').value.set(1)
with pytest.raises(ValueError):
await cfg.option('e').value.set('root')
with pytest.raises(ValueError):
await cfg.option('e').value.set('root[]@domain')
@pytest.mark.asyncio
async def test_url(config_type):
u = URLOption('u', '')
od = OptionDescription('a', '', [u])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
await cfg.property.read_write()
cfg = await get_config(cfg, config_type)
await cfg.option('u').value.set('http://foo.com')
await cfg.option('u').value.set('https://foo.com')
await cfg.option('u').value.set('https://foo.com/')
with pytest.raises(ValueError):
await cfg.option('u').value.set(1)
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('u').value.set('ftp://foo.com')
with pytest.raises(ValueError):
await cfg.option('u').value.set('foo.com')
with pytest.raises(ValueError):
await cfg.option('u').value.set(':/foo.com')
with pytest.raises(ValueError):
await cfg.option('u').value.set('foo.com/http://')
await cfg.option('u').value.set('https://foo.com/index.html')
await cfg.option('u').value.set('https://foo.com/index.html?var=value&var2=val2')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('u').value.set('https://foo.com/index\\n.html')
await cfg.option('u').value.set('https://foo.com:8443')
await cfg.option('u').value.set('https://foo.com:8443/')
await cfg.option('u').value.set('https://foo.com:8443/index.html')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('u').value.set('https://foo.com:84438989')
await cfg.option('u').value.set('https://foo.com:8443/INDEX')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('u').value.set('https://FOO.COM:8443')