agent: update user's attributes type in authentic (#48743)

This commit is contained in:
Benjamin Dauvergne 2020-11-21 12:15:51 +01:00
parent e0607bfa58
commit 44ed90ad84
2 changed files with 31 additions and 14 deletions

View File

@ -75,7 +75,7 @@ class Command(hobo_deploy.Command):
# so it gets shared as SAML attribute.
fields.append(attribute['name'])
continue
attr, created = Attribute.all_objects.get_or_create(
attr, created = Attribute.all_objects.update_or_create(
name=attribute['name'],
defaults={'kind': attribute['kind']})
for key in ('label', 'description', 'asked_on_registration',

View File

@ -29,7 +29,7 @@ def skeleton_dir(request, settings):
return settings.HOBO_SKELETONS_DIR
def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir):
def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir, tmp_path):
from django.core.management import call_command
from django.conf import settings
from hobo.agent.authentic2.management.commands.hobo_deploy import Command as HoboDeployCommand
@ -122,12 +122,12 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir):
side_effect_iter = iter([meta1, meta2, RequestException(), meta3])
def side_effect(*args, **kwargs):
v = next(side_effect_iter)
if isinstance(v, Exception):
raise v
m = mock.Mock()
m.text = v
return m
for v in side_effect_iter:
if isinstance(v, Exception):
raise v
m = mock.Mock()
m.text = v
return m
requests_get.side_effect = side_effect
env = {
'users': [
@ -230,7 +230,7 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir):
'name': 'phone'
},
{
'kind': 'phone_number',
'kind': 'string',
'description': '',
'required': False,
'user_visible': True,
@ -308,14 +308,16 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir):
},
]
}
hobo_json_content = json.dumps(env)
hobo_json = tempfile.NamedTemporaryFile(mode='w')
hobo_json.write(hobo_json_content)
hobo_json.flush()
def hobo_json():
with tempfile.NamedTemporaryFile(mode='w', dir=str(tmp_path), delete=False) as hobo_json:
hobo_json_content = json.dumps(env)
hobo_json.write(hobo_json_content)
return hobo_json.name
with mock.patch('hobo.agent.authentic2.provisionning.notify_agents') as mock_notify, \
mock.patch('hobo.agent.authentic2.management.commands.hobo_deploy.sleep', wraps=time.sleep) as sleep_mock:
call_command('hobo_deploy', 'http://sso.example.net', hobo_json.name)
call_command('hobo_deploy', 'http://sso.example.net', hobo_json())
assert sleep_mock.call_count == 1 # there is one retry, as the third service's metadata is temporarily unavailable
# check role mass provisionning to new services
@ -469,6 +471,21 @@ def test_hobo_deploy(monkeypatch, tenant_base, mocker, skeleton_dir):
call_command('hobo_deploy', redeploy=True)
assert sleep_mock.call_count == 0
# test attribute kind update
with tenant_context(tenant):
assert Attribute.objects.filter(name='mobile', kind='string').count() == 1
field = env['profile']['fields'][8]
assert field['name'] == 'mobile'
field['kind'] = 'phone_number'
side_effect_iter = iter([meta1, meta2, RequestException(), meta3])
with mock.patch('hobo.agent.authentic2.provisionning.notify_agents') as mock_notify, \
mock.patch('hobo.agent.authentic2.management.commands.hobo_deploy.sleep', wraps=time.sleep) as sleep_mock:
call_command('hobo_deploy', 'http://sso.example.net', hobo_json(), ignore_timestamp=True)
with tenant_context(tenant):
assert Attribute.objects.filter(name='mobile', kind='string').count() == 0
assert Attribute.objects.filter(name='mobile', kind='phone_number').count() == 1
def test_import_template(db, tenant_base):
def listify(value):