data_transfer: save object in update_model (fixes #29545)

This commit is contained in:
Benjamin Dauvergne 2019-01-08 15:29:17 +01:00
parent ce72b9a24e
commit a44d45fc24
3 changed files with 42 additions and 3 deletions

View File

@ -9,6 +9,7 @@ from authentic2.a2_rbac.models import RoleAttribute
def update_model(obj, d):
for attr, value in d.items():
setattr(obj, attr, value)
obj.save()
def export_site():

View File

@ -146,24 +146,29 @@ def test_role_deserializer_update_ou(db):
ou1 = OU.objects.create(name='ou 1', slug='ou-1')
ou2 = OU.objects.create(name='ou 2', slug='ou-2')
uuid = get_hex_uuid()
assert Role.objects.exclude(slug__startswith='_').count() == 0
existing_role = Role.objects.create(uuid=uuid, slug='some-role', ou=ou1)
rd = RoleDeserializer({
'uuid': uuid, 'name': 'some-role', 'slug': 'some-role',
'ou': {'slug': 'ou-2'}, 'service': None}, ImportContext())
role, status = rd.deserialize()
assert role == existing_role
assert role.ou == ou2
existing_role.refresh_from_db()
assert existing_role.ou == ou2
assert Role.objects.exclude(slug__startswith='_').count() == 1
def test_role_deserializer_update_fields(db):
uuid = get_hex_uuid()
assert Role.objects.exclude(slug__startswith='_').count() == 0
existing_role = Role.objects.create(uuid=uuid, slug='some-role', name='some role')
rd = RoleDeserializer({
'uuid': uuid, 'slug': 'some-role', 'name': 'some role changed',
'ou': None, 'service': None}, ImportContext())
role, status = rd.deserialize()
existing_role.refresh_from_db()
assert role == existing_role
assert role.name == 'some role changed'
assert existing_role.name == 'some role changed'
assert Role.objects.exclude(slug__startswith='_').count() == 1
def test_role_deserializer_with_attributes(db):
@ -178,6 +183,7 @@ def test_role_deserializer_with_attributes(db):
'ou': None, 'service': None}, ImportContext())
role, status = rd.deserialize()
created, deleted = rd.attributes()
assert status == 'created'
assert role.attributes.count() == 2
assert len(created) == 2
@ -211,6 +217,7 @@ def test_role_deserializer_parenting_existing_parent(db):
child_role, status = rd.deserialize()
created, deleted = rd.parentings()
assert status == 'created'
assert len(created) == 1
parenting = created[0]
assert parenting.direct is True

View File

@ -153,3 +153,34 @@ def test_import_site_confirm_prompt_yes(db, monkeypatch, json_fixture):
management.call_command('import_site', json_fixture(content), stdin='yes')
assert Role.objects.get(uuid='dqfewrvesvews2532')
def test_import_site_update_roles(db, json_fixture):
r1 = Role.objects.create(name='Role1', slug='role1')
r2 = Role.objects.create(name='Role2', slug='role2')
content = {
'roles': [
{
'ou': None,
'service': None,
'slug': r1.slug,
'name': 'Role first update',
}
]
}
management.call_command('import_site', json_fixture(content))
r1.refresh_from_db()
assert r1.name == 'Role first update'
content['roles'][0]['uuid'] = r1.uuid
content['roles'][0]['slug'] = 'slug-updated'
content['roles'][0]['name'] = 'Role second update'
management.call_command('import_site', json_fixture(content))
r1.refresh_from_db()
assert r1.slug == 'slug-updated'
assert r1.name == 'Role second update'