156 lines
4.5 KiB
Python
156 lines
4.5 KiB
Python
import __builtin__
|
|
import random
|
|
import json
|
|
|
|
from django.core import management
|
|
import pytest
|
|
|
|
from django_rbac.utils import get_role_model
|
|
|
|
Role = get_role_model()
|
|
|
|
|
|
@pytest.fixture
|
|
def json_fixture(tmpdir):
|
|
def f(content):
|
|
name = str(random.getrandbits(64))
|
|
outfile = tmpdir.join(name)
|
|
with outfile.open('w') as f:
|
|
f.write(json.dumps(content))
|
|
return outfile.strpath
|
|
return f
|
|
|
|
|
|
def dummy_export_site(*args):
|
|
return {'roles': [{'name': 'role1'}]}
|
|
|
|
|
|
def test_export_role_cmd_stdout(db, capsys, monkeypatch):
|
|
import authentic2.management.commands.export_site
|
|
monkeypatch.setattr(
|
|
authentic2.management.commands.export_site, 'export_site', dummy_export_site)
|
|
management.call_command('export_site')
|
|
out, err = capsys.readouterr()
|
|
assert json.loads(out) == dummy_export_site()
|
|
|
|
|
|
def test_export_role_cmd_to_file(db, monkeypatch, tmpdir):
|
|
import authentic2.management.commands.export_site
|
|
monkeypatch.setattr(
|
|
authentic2.management.commands.export_site, 'export_site', dummy_export_site)
|
|
outfile = tmpdir.join('export.json')
|
|
management.call_command('export_site', '--output', outfile.strpath)
|
|
with outfile.open('r') as f:
|
|
assert json.loads(f.read()) == dummy_export_site()
|
|
|
|
|
|
def test_import_site_cmd(db, monkeypatch, json_fixture):
|
|
management.call_command('import_site', json_fixture({'roles': []}))
|
|
|
|
|
|
def test_import_site_cmd_infos_on_stdout(db, monkeypatch, capsys, json_fixture):
|
|
content = {
|
|
'roles': [
|
|
{
|
|
'uuid': 'dqfewrvesvews2532',
|
|
'slug': 'role-slug',
|
|
'name': 'role-name',
|
|
'ou': None,
|
|
'service': None
|
|
}
|
|
]
|
|
}
|
|
|
|
management.call_command('import_site', json_fixture(content))
|
|
|
|
out, err = capsys.readouterr()
|
|
assert "Real run" in out
|
|
assert "1 roles created" in out
|
|
assert "0 roles updated" in out
|
|
|
|
|
|
def test_import_site_transaction_rollback_on_error(db, monkeypatch, capsys, json_fixture):
|
|
def exception_import_site(*args):
|
|
Role.objects.create(slug='role-slug')
|
|
raise Exception()
|
|
|
|
import authentic2.management.commands.import_site
|
|
monkeypatch.setattr(
|
|
authentic2.management.commands.import_site, 'import_site', exception_import_site)
|
|
|
|
with pytest.raises(Exception):
|
|
management.call_command('import_site', json_fixture({'roles': []}))
|
|
|
|
with pytest.raises(Role.DoesNotExist):
|
|
Role.objects.get(slug='role-slug')
|
|
|
|
|
|
def test_import_site_transaction_rollback_on_dry_run(db, monkeypatch, capsys, json_fixture):
|
|
content = {
|
|
'roles': [
|
|
{
|
|
'uuid': 'dqfewrvesvews2532',
|
|
'slug': 'role-slug',
|
|
'name': 'role-name',
|
|
'ou': None,
|
|
'service': None
|
|
}
|
|
]
|
|
}
|
|
|
|
management.call_command('import_site', '--dry-run', json_fixture(content))
|
|
|
|
with pytest.raises(Role.DoesNotExist):
|
|
Role.objects.get(slug='role-slug')
|
|
|
|
|
|
def test_import_site_cmd_unhandled_context_option(db, monkeypatch, capsys, json_fixture):
|
|
from authentic2.data_transfer import DataImportError
|
|
|
|
content = {
|
|
'roles': [
|
|
{
|
|
'uuid': 'dqfewrvesvews2532',
|
|
'slug': 'role-slug',
|
|
'name': 'role-name',
|
|
'ou': None,
|
|
'service': None
|
|
}
|
|
]
|
|
}
|
|
|
|
Role.objects.create(uuid='dqfewrvesvews2532', slug='role-slug', name='role-name')
|
|
|
|
with pytest.raises(DataImportError):
|
|
management.call_command(
|
|
'import_site', '-o', 'role-delete-orphans', json_fixture(content))
|
|
|
|
|
|
def test_import_site_cmd_unknown_context_option(db, tmpdir, monkeypatch, capsys):
|
|
from django.core.management.base import CommandError
|
|
export_file = tmpdir.join('roles-export.json')
|
|
with pytest.raises(CommandError):
|
|
management.call_command('import_site', '-o', 'unknown-option', export_file.strpath)
|
|
|
|
|
|
def test_import_site_confirm_prompt_yes(db, monkeypatch, json_fixture):
|
|
content = {
|
|
'roles': [
|
|
{
|
|
'uuid': 'dqfewrvesvews2532',
|
|
'slug': 'role-slug',
|
|
'name': 'role-name',
|
|
'ou': None,
|
|
'service': None
|
|
}
|
|
]
|
|
}
|
|
|
|
def yes_raw_input(*args, **kwargs):
|
|
return 'yes'
|
|
|
|
monkeypatch.setattr(__builtin__, 'raw_input', yes_raw_input)
|
|
|
|
management.call_command('import_site', json_fixture(content), stdin='yes')
|
|
assert Role.objects.get(uuid='dqfewrvesvews2532')
|