From d6fbb8003451688a78f93bca84909a7c43e2825a Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 21 Nov 2020 12:15:51 +0100 Subject: [PATCH] agent: update user's attributes type in authentic (#48743) --- .../management/commands/hobo_deploy.py | 2 +- tests_authentic/test_hobo_deploy.py | 43 +++++++++++++------ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/hobo/agent/authentic2/management/commands/hobo_deploy.py b/hobo/agent/authentic2/management/commands/hobo_deploy.py index 27d805e..600b621 100644 --- a/hobo/agent/authentic2/management/commands/hobo_deploy.py +++ b/hobo/agent/authentic2/management/commands/hobo_deploy.py @@ -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', diff --git a/tests_authentic/test_hobo_deploy.py b/tests_authentic/test_hobo_deploy.py index 4c0214b..0a17c3f 100644 --- a/tests_authentic/test_hobo_deploy.py +++ b/tests_authentic/test_hobo_deploy.py @@ -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=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): -- 2.29.2