1 |
1 |
import __builtin__
|
|
2 |
import random
|
2 |
3 |
import json
|
3 |
4 |
|
4 |
5 |
from django.core import management
|
... | ... | |
6 |
7 |
|
7 |
8 |
from django_rbac.utils import get_role_model
|
8 |
9 |
|
|
10 |
Role = get_role_model()
|
|
11 |
|
|
12 |
|
|
13 |
@pytest.fixture
|
|
14 |
def json_fixture(tmpdir):
|
|
15 |
def f(content):
|
|
16 |
name = str(random.getrandbits(64))
|
|
17 |
outfile = tmpdir.join(name)
|
|
18 |
with outfile.open('w') as f:
|
|
19 |
f.write(json.dumps(content))
|
|
20 |
return outfile.strpath
|
|
21 |
return f
|
|
22 |
|
9 |
23 |
|
10 |
24 |
def dummy_export_site(*args):
|
11 |
25 |
return {'roles': [{'name': 'role1'}]}
|
... | ... | |
30 |
44 |
assert json.loads(f.read()) == dummy_export_site()
|
31 |
45 |
|
32 |
46 |
|
33 |
|
def test_import_site_cmd(db, tmpdir, monkeypatch):
|
34 |
|
export_file = tmpdir.join('roles-export.json')
|
35 |
|
with export_file.open('w'):
|
36 |
|
export_file.write(json.dumps({'roles': []}))
|
37 |
|
management.call_command('import_site', export_file.strpath)
|
|
47 |
def test_import_site_cmd(db, monkeypatch, json_fixture):
|
|
48 |
management.call_command('import_site', json_fixture({'roles': []}))
|
38 |
49 |
|
39 |
50 |
|
40 |
|
def test_import_site_cmd_infos_on_stdout(db, tmpdir, monkeypatch, capsys):
|
41 |
|
export_file = tmpdir.join('roles-export.json')
|
42 |
|
with export_file.open('w'):
|
43 |
|
export_file.write(json.dumps(
|
44 |
|
{'roles': [{
|
45 |
|
'uuid': 'dqfewrvesvews2532', 'slug': 'role-slug', 'name': 'role-name',
|
46 |
|
'ou': None, 'service': None}]}))
|
|
51 |
def test_import_site_cmd_infos_on_stdout(db, monkeypatch, capsys, json_fixture):
|
|
52 |
content = {
|
|
53 |
'roles': [
|
|
54 |
{
|
|
55 |
'uuid': 'dqfewrvesvews2532',
|
|
56 |
'slug': 'role-slug',
|
|
57 |
'name': 'role-name',
|
|
58 |
'ou': None,
|
|
59 |
'service': None
|
|
60 |
}
|
|
61 |
]
|
|
62 |
}
|
47 |
63 |
|
48 |
|
management.call_command('import_site', export_file.strpath)
|
|
64 |
management.call_command('import_site', json_fixture(content))
|
49 |
65 |
|
50 |
66 |
out, err = capsys.readouterr()
|
51 |
67 |
assert "Real run" in out
|
... | ... | |
53 |
69 |
assert "0 roles updated" in out
|
54 |
70 |
|
55 |
71 |
|
56 |
|
def test_import_site_transaction_rollback_on_error(db, tmpdir, monkeypatch, capsys):
|
57 |
|
export_file = tmpdir.join('roles-export.json')
|
58 |
|
with export_file.open('w'):
|
59 |
|
export_file.write(json.dumps({'roles': []}))
|
60 |
|
|
61 |
|
Role = get_role_model()
|
62 |
|
|
|
72 |
def test_import_site_transaction_rollback_on_error(db, monkeypatch, capsys, json_fixture):
|
63 |
73 |
def exception_import_site(*args):
|
64 |
74 |
Role.objects.create(slug='role-slug')
|
65 |
75 |
raise Exception()
|
... | ... | |
69 |
79 |
authentic2.management.commands.import_site, 'import_site', exception_import_site)
|
70 |
80 |
|
71 |
81 |
with pytest.raises(Exception):
|
72 |
|
management.call_command('import_site', export_file.strpath)
|
|
82 |
management.call_command('import_site', json_fixture({'roles': []}))
|
73 |
83 |
|
74 |
84 |
with pytest.raises(Role.DoesNotExist):
|
75 |
85 |
Role.objects.get(slug='role-slug')
|
76 |
86 |
|
77 |
87 |
|
78 |
|
def test_import_site_transaction_rollback_on_dry_run(db, tmpdir, monkeypatch, capsys):
|
79 |
|
export_file = tmpdir.join('roles-export.json')
|
80 |
|
with export_file.open('w'):
|
81 |
|
export_file.write(json.dumps(
|
82 |
|
{'roles': [{
|
83 |
|
'uuid': 'dqfewrvesvews2532', 'slug': 'role-slug', 'name': 'role-name',
|
84 |
|
'ou': None, 'service': None}]}))
|
85 |
|
|
86 |
|
Role = get_role_model()
|
|
88 |
def test_import_site_transaction_rollback_on_dry_run(db, monkeypatch, capsys, json_fixture):
|
|
89 |
content = {
|
|
90 |
'roles': [
|
|
91 |
{
|
|
92 |
'uuid': 'dqfewrvesvews2532',
|
|
93 |
'slug': 'role-slug',
|
|
94 |
'name': 'role-name',
|
|
95 |
'ou': None,
|
|
96 |
'service': None
|
|
97 |
}
|
|
98 |
]
|
|
99 |
}
|
87 |
100 |
|
88 |
|
management.call_command('import_site', '--dry-run', export_file.strpath)
|
|
101 |
management.call_command('import_site', '--dry-run', json_fixture(content))
|
89 |
102 |
|
90 |
103 |
with pytest.raises(Role.DoesNotExist):
|
91 |
104 |
Role.objects.get(slug='role-slug')
|
92 |
105 |
|
93 |
106 |
|
94 |
|
def test_import_site_cmd_unhandled_context_option(db, tmpdir, monkeypatch, capsys):
|
|
107 |
def test_import_site_cmd_unhandled_context_option(db, monkeypatch, capsys, json_fixture):
|
95 |
108 |
from authentic2.data_transfer import DataImportError
|
96 |
109 |
|
97 |
|
export_file = tmpdir.join('roles-export.json')
|
98 |
|
with export_file.open('w'):
|
99 |
|
export_file.write(json.dumps(
|
100 |
|
{'roles': [{
|
101 |
|
'uuid': 'dqfewrvesvews2532', 'slug': 'role-slug', 'name': 'role-name',
|
102 |
|
'ou': None, 'service': None}]}))
|
|
110 |
content = {
|
|
111 |
'roles': [
|
|
112 |
{
|
|
113 |
'uuid': 'dqfewrvesvews2532',
|
|
114 |
'slug': 'role-slug',
|
|
115 |
'name': 'role-name',
|
|
116 |
'ou': None,
|
|
117 |
'service': None
|
|
118 |
}
|
|
119 |
]
|
|
120 |
}
|
103 |
121 |
|
104 |
|
get_role_model().objects.create(uuid='dqfewrvesvews2532', slug='role-slug', name='role-name')
|
|
122 |
Role.objects.create(uuid='dqfewrvesvews2532', slug='role-slug', name='role-name')
|
105 |
123 |
|
106 |
124 |
with pytest.raises(DataImportError):
|
107 |
125 |
management.call_command(
|
108 |
|
'import_site', '-o', 'role-delete-orphans', export_file.strpath)
|
|
126 |
'import_site', '-o', 'role-delete-orphans', json_fixture(content))
|
109 |
127 |
|
110 |
128 |
|
111 |
129 |
def test_import_site_cmd_unknown_context_option(db, tmpdir, monkeypatch, capsys):
|
... | ... | |
115 |
133 |
management.call_command('import_site', '-o', 'unknown-option', export_file.strpath)
|
116 |
134 |
|
117 |
135 |
|
118 |
|
def test_import_site_confirm_prompt_yes(db, tmpdir, monkeypatch):
|
119 |
|
export_file = tmpdir.join('roles-export.json')
|
120 |
|
with export_file.open('w'):
|
121 |
|
export_file.write(json.dumps(
|
122 |
|
{'roles': [{
|
123 |
|
'uuid': 'dqfewrvesvews2532', 'slug': 'role-slug', 'name': 'role-name',
|
124 |
|
'ou': None, 'service': None}]}))
|
|
136 |
def test_import_site_confirm_prompt_yes(db, monkeypatch, json_fixture):
|
|
137 |
content = {
|
|
138 |
'roles': [
|
|
139 |
{
|
|
140 |
'uuid': 'dqfewrvesvews2532',
|
|
141 |
'slug': 'role-slug',
|
|
142 |
'name': 'role-name',
|
|
143 |
'ou': None,
|
|
144 |
'service': None
|
|
145 |
}
|
|
146 |
]
|
|
147 |
}
|
125 |
148 |
|
126 |
149 |
def yes_raw_input(*args, **kwargs):
|
127 |
150 |
return 'yes'
|
128 |
151 |
|
129 |
152 |
monkeypatch.setattr(__builtin__, 'raw_input', yes_raw_input)
|
130 |
153 |
|
131 |
|
management.call_command('import_site', export_file.strpath, stdin='yes')
|
132 |
|
assert get_role_model().objects.get(uuid='dqfewrvesvews2532')
|
|
154 |
management.call_command('import_site', json_fixture(content), stdin='yes')
|
|
155 |
assert Role.objects.get(uuid='dqfewrvesvews2532')
|
133 |
|
-
|