tests: simplify import_site tests (#29545)

This commit is contained in:
Benjamin Dauvergne 2019-01-09 19:00:21 +01:00
parent c60ed8b5ee
commit 75e0b37880
1 changed files with 72 additions and 49 deletions

View File

@ -1,4 +1,5 @@
import __builtin__ import __builtin__
import random
import json import json
from django.core import management from django.core import management
@ -6,6 +7,19 @@ import pytest
from django_rbac.utils import get_role_model 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): def dummy_export_site(*args):
return {'roles': [{'name': 'role1'}]} return {'roles': [{'name': 'role1'}]}
@ -30,22 +44,24 @@ def test_export_role_cmd_to_file(db, monkeypatch, tmpdir):
assert json.loads(f.read()) == dummy_export_site() assert json.loads(f.read()) == dummy_export_site()
def test_import_site_cmd(db, tmpdir, monkeypatch): def test_import_site_cmd(db, monkeypatch, json_fixture):
export_file = tmpdir.join('roles-export.json') management.call_command('import_site', json_fixture({'roles': []}))
with export_file.open('w'):
export_file.write(json.dumps({'roles': []}))
management.call_command('import_site', export_file.strpath)
def test_import_site_cmd_infos_on_stdout(db, tmpdir, monkeypatch, capsys): def test_import_site_cmd_infos_on_stdout(db, monkeypatch, capsys, json_fixture):
export_file = tmpdir.join('roles-export.json') content = {
with export_file.open('w'): 'roles': [
export_file.write(json.dumps( {
{'roles': [{ 'uuid': 'dqfewrvesvews2532',
'uuid': 'dqfewrvesvews2532', 'slug': 'role-slug', 'name': 'role-name', 'slug': 'role-slug',
'ou': None, 'service': None}]})) 'name': 'role-name',
'ou': None,
'service': None
}
]
}
management.call_command('import_site', export_file.strpath) management.call_command('import_site', json_fixture(content))
out, err = capsys.readouterr() out, err = capsys.readouterr()
assert "Real run" in out assert "Real run" in out
@ -53,13 +69,7 @@ def test_import_site_cmd_infos_on_stdout(db, tmpdir, monkeypatch, capsys):
assert "0 roles updated" in out assert "0 roles updated" in out
def test_import_site_transaction_rollback_on_error(db, tmpdir, monkeypatch, capsys): def test_import_site_transaction_rollback_on_error(db, monkeypatch, capsys, json_fixture):
export_file = tmpdir.join('roles-export.json')
with export_file.open('w'):
export_file.write(json.dumps({'roles': []}))
Role = get_role_model()
def exception_import_site(*args): def exception_import_site(*args):
Role.objects.create(slug='role-slug') Role.objects.create(slug='role-slug')
raise Exception() raise Exception()
@ -69,43 +79,51 @@ def test_import_site_transaction_rollback_on_error(db, tmpdir, monkeypatch, caps
authentic2.management.commands.import_site, 'import_site', exception_import_site) authentic2.management.commands.import_site, 'import_site', exception_import_site)
with pytest.raises(Exception): with pytest.raises(Exception):
management.call_command('import_site', export_file.strpath) management.call_command('import_site', json_fixture({'roles': []}))
with pytest.raises(Role.DoesNotExist): with pytest.raises(Role.DoesNotExist):
Role.objects.get(slug='role-slug') Role.objects.get(slug='role-slug')
def test_import_site_transaction_rollback_on_dry_run(db, tmpdir, monkeypatch, capsys): def test_import_site_transaction_rollback_on_dry_run(db, monkeypatch, capsys, json_fixture):
export_file = tmpdir.join('roles-export.json') content = {
with export_file.open('w'): 'roles': [
export_file.write(json.dumps( {
{'roles': [{ 'uuid': 'dqfewrvesvews2532',
'uuid': 'dqfewrvesvews2532', 'slug': 'role-slug', 'name': 'role-name', 'slug': 'role-slug',
'ou': None, 'service': None}]})) 'name': 'role-name',
'ou': None,
'service': None
}
]
}
Role = get_role_model() management.call_command('import_site', '--dry-run', json_fixture(content))
management.call_command('import_site', '--dry-run', export_file.strpath)
with pytest.raises(Role.DoesNotExist): with pytest.raises(Role.DoesNotExist):
Role.objects.get(slug='role-slug') Role.objects.get(slug='role-slug')
def test_import_site_cmd_unhandled_context_option(db, tmpdir, monkeypatch, capsys): def test_import_site_cmd_unhandled_context_option(db, monkeypatch, capsys, json_fixture):
from authentic2.data_transfer import DataImportError from authentic2.data_transfer import DataImportError
export_file = tmpdir.join('roles-export.json') content = {
with export_file.open('w'): 'roles': [
export_file.write(json.dumps( {
{'roles': [{ 'uuid': 'dqfewrvesvews2532',
'uuid': 'dqfewrvesvews2532', 'slug': 'role-slug', 'name': 'role-name', 'slug': 'role-slug',
'ou': None, 'service': None}]})) 'name': 'role-name',
'ou': None,
'service': None
}
]
}
get_role_model().objects.create(uuid='dqfewrvesvews2532', slug='role-slug', name='role-name') Role.objects.create(uuid='dqfewrvesvews2532', slug='role-slug', name='role-name')
with pytest.raises(DataImportError): with pytest.raises(DataImportError):
management.call_command( management.call_command(
'import_site', '-o', 'role-delete-orphans', export_file.strpath) 'import_site', '-o', 'role-delete-orphans', json_fixture(content))
def test_import_site_cmd_unknown_context_option(db, tmpdir, monkeypatch, capsys): def test_import_site_cmd_unknown_context_option(db, tmpdir, monkeypatch, capsys):
@ -115,18 +133,23 @@ def test_import_site_cmd_unknown_context_option(db, tmpdir, monkeypatch, capsys)
management.call_command('import_site', '-o', 'unknown-option', export_file.strpath) management.call_command('import_site', '-o', 'unknown-option', export_file.strpath)
def test_import_site_confirm_prompt_yes(db, tmpdir, monkeypatch): def test_import_site_confirm_prompt_yes(db, monkeypatch, json_fixture):
export_file = tmpdir.join('roles-export.json') content = {
with export_file.open('w'): 'roles': [
export_file.write(json.dumps( {
{'roles': [{ 'uuid': 'dqfewrvesvews2532',
'uuid': 'dqfewrvesvews2532', 'slug': 'role-slug', 'name': 'role-name', 'slug': 'role-slug',
'ou': None, 'service': None}]})) 'name': 'role-name',
'ou': None,
'service': None
}
]
}
def yes_raw_input(*args, **kwargs): def yes_raw_input(*args, **kwargs):
return 'yes' return 'yes'
monkeypatch.setattr(__builtin__, 'raw_input', yes_raw_input) monkeypatch.setattr(__builtin__, 'raw_input', yes_raw_input)
management.call_command('import_site', export_file.strpath, stdin='yes') management.call_command('import_site', json_fixture(content), stdin='yes')
assert get_role_model().objects.get(uuid='dqfewrvesvews2532') assert Role.objects.get(uuid='dqfewrvesvews2532')