2020-07-29 21:03:00 +02:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
import os
|
2020-08-05 11:10:14 +02:00
|
|
|
|
import subprocess
|
2020-08-05 14:28:55 +02:00
|
|
|
|
import importlib
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from click.testing import CliRunner
|
2020-08-05 11:10:14 +02:00
|
|
|
|
from configparser import ConfigParser
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
from halfapi import __version__
|
2020-07-29 21:03:00 +02:00
|
|
|
|
from halfapi.cli import cli
|
2020-08-05 14:28:55 +02:00
|
|
|
|
Cli = cli.cli
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
PROJNAME = os.environ.get('PROJ','tmp_api')
|
|
|
|
|
|
2020-07-29 21:03:00 +02:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def runner():
|
|
|
|
|
return CliRunner()
|
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def dropdb():
|
|
|
|
|
p = subprocess.Popen(['dropdb', f'halfapi_{PROJNAME}'])
|
|
|
|
|
p.wait()
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
p = subprocess.Popen(['dropdb', f'halfapi_{PROJNAME}'])
|
|
|
|
|
p.wait()
|
|
|
|
|
|
2020-08-05 12:27:56 +02:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def createdb():
|
|
|
|
|
p = subprocess.Popen(['createdb', f'halfapi_{PROJNAME}'])
|
|
|
|
|
p.wait()
|
|
|
|
|
return
|
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
|
|
|
|
|
@pytest.mark.incremental
|
|
|
|
|
class TestCli():
|
2020-08-05 12:27:56 +02:00
|
|
|
|
def test_options(self, runner, dropdb, createdb):
|
2020-08-05 11:10:14 +02:00
|
|
|
|
# Wrong command
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['foobar'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 2
|
|
|
|
|
|
|
|
|
|
# Test existing commands
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['--help'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 0
|
|
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['--version'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 0
|
|
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['init', '--help'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_init_project_fail(self, runner, dropdb):
|
|
|
|
|
# Missing argument (project)
|
|
|
|
|
testproject = 'testproject'
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['init'])
|
2020-07-29 21:03:00 +02:00
|
|
|
|
assert r.exit_code == 2
|
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
|
# Fail : Wrong project name
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['init', 'test*-project'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 1
|
|
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
|
# Fail : Already existing folder
|
|
|
|
|
os.mkdir(testproject)
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['init', testproject])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 1
|
|
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
|
# Fail : Already existing nod
|
|
|
|
|
os.mknod(testproject)
|
2020-08-05 14:28:55 +02:00
|
|
|
|
r = runner.invoke(Cli, ['init', testproject])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 1
|
|
|
|
|
|
2020-08-05 14:28:55 +02:00
|
|
|
|
def test_init_project(self, runner, dropdb, createdb):
|
2020-08-05 11:10:14 +02:00
|
|
|
|
cp = ConfigParser()
|
|
|
|
|
with runner.isolated_filesystem():
|
|
|
|
|
env = {
|
|
|
|
|
'HALFORM_CONF_DIR': os.environ.get('HALFORM_CONF_DIR', os.getcwd()),
|
|
|
|
|
'HALFAPI_CONF_DIR': os.environ.get('HALFAPI_CONF_DIR', os.getcwd()),
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['init', PROJNAME], env=env)
|
2020-08-05 11:10:14 +02:00
|
|
|
|
try:
|
|
|
|
|
assert os.path.isdir(PROJNAME)
|
|
|
|
|
assert os.path.isdir(os.path.join(PROJNAME, '.halfapi'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# .halfapi/config check
|
|
|
|
|
assert os.path.isfile(os.path.join(PROJNAME, '.halfapi', 'config'))
|
|
|
|
|
cp.read(os.path.join(PROJNAME, '.halfapi', 'config'))
|
|
|
|
|
assert cp.has_section('project')
|
|
|
|
|
assert cp.has_option('project', 'name')
|
|
|
|
|
assert cp.get('project', 'name') == PROJNAME
|
|
|
|
|
assert cp.get('project', 'halfapi_version') == __version__
|
|
|
|
|
|
|
|
|
|
# .halfapi/domains check
|
|
|
|
|
assert os.path.isfile(os.path.join(PROJNAME, '.halfapi', 'domains'))
|
|
|
|
|
cp.read(os.path.join(PROJNAME, '.halfapi', 'domains'))
|
|
|
|
|
assert cp.has_section('domains')
|
|
|
|
|
except AssertionError:
|
|
|
|
|
subprocess.run(['tree', '-a', os.getcwd()])
|
|
|
|
|
|
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
assert res.exception is None
|
|
|
|
|
|
2020-08-05 14:28:55 +02:00
|
|
|
|
def test_run_commands(self, runner, dropdb, createdb):
|
|
|
|
|
def reloadcli():
|
|
|
|
|
importlib.reload(cli)
|
|
|
|
|
return cli.cli
|
|
|
|
|
|
2020-08-05 13:59:46 +02:00
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['init', PROJNAME])
|
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
os.chdir(PROJNAME)
|
|
|
|
|
Cli2 = reloadcli()
|
|
|
|
|
res = runner.invoke(Cli2, ['run', '--help'])
|
|
|
|
|
assert res.exception is None
|
2020-08-05 13:59:46 +02:00
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['init', PROJNAME])
|
|
|
|
|
os.chdir(PROJNAME)
|
|
|
|
|
res = runner.invoke(Cli, ['run', 'foobar'])
|
2020-08-05 13:59:46 +02:00
|
|
|
|
assert res.exit_code == 2
|
|
|
|
|
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
2020-08-05 14:28:55 +02:00
|
|
|
|
def test_domain_commands(self, runner, dropdb, createdb):
|
2020-08-05 11:10:14 +02:00
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['init', PROJNAME])
|
|
|
|
|
os.chdir(PROJNAME)
|
|
|
|
|
res = runner.invoke(Cli, ['domain', 'foobar'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert res.exit_code == 2
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['init', PROJNAME])
|
|
|
|
|
os.chdir(PROJNAME)
|
|
|
|
|
res = runner.invoke(Cli, ['domain', '--help'])
|
2020-08-05 13:59:46 +02:00
|
|
|
|
assert r.exit_code == 0
|
|
|
|
|
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['init', PROJNAME])
|
|
|
|
|
os.chdir(PROJNAME)
|
|
|
|
|
res = runner.invoke(Cli, ['domain', 'create', '--help'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 0
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['init', PROJNAME])
|
|
|
|
|
os.chdir(PROJNAME)
|
|
|
|
|
res = runner.invoke(Cli, ['domain', 'read', '--help'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 0
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['init', PROJNAME])
|
|
|
|
|
os.chdir(PROJNAME)
|
|
|
|
|
res = runner.invoke(Cli, ['domain', 'update', '--help'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 0
|
2020-07-29 21:03:00 +02:00
|
|
|
|
|
2020-08-05 11:10:14 +02:00
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['init', PROJNAME])
|
|
|
|
|
os.chdir(PROJNAME)
|
|
|
|
|
res = runner.invoke(Cli, ['domain', 'delete', '--help'])
|
2020-08-05 11:10:14 +02:00
|
|
|
|
assert r.exit_code == 0
|
2020-08-05 12:27:56 +02:00
|
|
|
|
|
|
|
|
|
def test_domain_create(self, runner, dropdb):
|
|
|
|
|
with runner.isolated_filesystem():
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['init', PROJNAME])
|
2020-08-05 12:27:56 +02:00
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
|
|
os.chdir(PROJNAME)
|
|
|
|
|
|
|
|
|
|
DOMNAME='tmp_domain'
|
2020-08-05 14:28:55 +02:00
|
|
|
|
res = runner.invoke(Cli, ['domain', 'create', DOMNAME])
|
2020-08-05 12:27:56 +02:00
|
|
|
|
srcdir = os.path.join('domains', 'src', DOMNAME)
|
|
|
|
|
assert os.path.isdir(srcdir)
|
|
|
|
|
moddir = os.path.join(srcdir, DOMNAME)
|
|
|
|
|
assert os.path.isdir(moddir)
|
|
|
|
|
setup = os.path.join(srcdir, 'setup.py')
|
|
|
|
|
assert os.path.isfile(setup)
|
|
|
|
|
initfile = os.path.join(moddir, '__init__.py')
|
|
|
|
|
assert os.path.isfile(initfile)
|
|
|
|
|
aclfile = os.path.join(moddir, 'acl.py')
|
|
|
|
|
assert os.path.isfile(aclfile)
|
|
|
|
|
aclsdir = os.path.join(moddir, 'acls')
|
|
|
|
|
assert os.path.isdir(aclsdir)
|
|
|
|
|
routersdir = os.path.join(moddir, 'routers')
|
|
|
|
|
assert os.path.isdir(routersdir)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
dom_mod = importlib.import_module(DOMNAME, srcdir)
|
|
|
|
|
assert hasattr(dom_mod, 'ROUTERS')
|
|
|
|
|
except ImportError:
|
|
|
|
|
assert False
|