From fd81a4daeaea22be3309f46b8a62a1d3d9c3ad51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 29 Oct 2016 22:14:41 +0200 Subject: [PATCH] hobo: don't require all role attributes when deprovisionning (13799) --- tests/test_hobo_notify.py | 32 +++++++++++++++++++++++--------- wcs/ctl/hobo_notify.py | 47 +++++++++++++++++++++++++++-------------------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/tests/test_hobo_notify.py b/tests/test_hobo_notify.py index 9a43bea..75bce48 100644 --- a/tests/test_hobo_notify.py +++ b/tests/test_hobo_notify.py @@ -234,27 +234,41 @@ def test_process_notification_role_deprovision(): notification = { '@type': u'deprovision', 'audience': [u'test'], - 'full': True, + 'full': False, 'objects': { '@type': 'role', 'data': [ { '@type': 'role', - 'name': u'Service état civil', - 'slug': u'service-ett-civil', - 'description': u'Rôle du service état civil', 'uuid': u'xyz', - 'emails': [u'etat-civil@example.com'], - 'emails_to_members': True, }, ] } } + role = Role.select()[0] + role.remove_self() + assert role.name == 'Service étt civil' + assert role.slug == 'service-ett-civil' + role.id = 'xyz' + role.store() + + role = Role('foo') + role.slug = 'bar' + role.store() + + assert Role.count() == 2 + CmdHoboNotify.process_notification(notification) assert Role.count() == 1 - assert Role.select()[0].name == 'Service étt civil' - assert Role.select()[0].slug == 'service-ett-civil' + assert Role.select()[0].slug == 'bar' + + r = Role(name='Service étt civil') + r.slug = 'xyz' + r.store() + assert Role.count() == 2 CmdHoboNotify.process_notification(notification) - assert Role.count() == 0 + assert Role.count() == 1 + assert Role.select()[0].slug == 'bar' + PROFILE = { 'fields': [ diff --git a/wcs/ctl/hobo_notify.py b/wcs/ctl/hobo_notify.py index f8d87db..bd50c8d 100644 --- a/wcs/ctl/hobo_notify.py +++ b/wcs/ctl/hobo_notify.py @@ -95,38 +95,45 @@ class CmdHoboNotify(Command): @classmethod def check_valid_role(cls, o): return 'uuid' in o \ - or 'name' in o \ - or 'emails' in o \ - or 'emails_to_members' in o \ - or 'slug' in o + and 'name' in o \ + and 'emails' in o \ + and 'emails_to_members' in o \ + and 'slug' in o @classmethod def provision_role(cls, publisher, issuer, action, data, full=False): uuids = set() for o in data: - assert cls.check_valid_role(o) + assert 'uuid' in o uuid = o['uuid'].encode(publisher.site_charset) uuids.add(uuid) - slug = o['slug'].encode(publisher.site_charset) - details = o.get('details', '').encode(publisher.site_charset) or None - name = o['name'].encode(publisher.site_charset) - emails = [email.encode(publisher.site_charset) for email in o['emails']] - emails_to_members = o['emails_to_members'] + slug = None + if action == 'provision': + assert cls.check_valid_role(o) + slug = o['slug'].encode(publisher.site_charset) + details = o.get('details', '').encode(publisher.site_charset) or None + name = o['name'].encode(publisher.site_charset) + emails = [email.encode(publisher.site_charset) for email in o['emails']] + emails_to_members = o['emails_to_members'] # Find existing role + role = None try: role = Role.get(uuid) except KeyError: - try: - role = Role.get_on_index(uuid, 'slug') - except KeyError: + for slug_id in (slug, uuid): + if not slug_id: + continue try: - role = Role.get_on_index(slug, 'slug') + role = Role.get_on_index(slug_id, 'slug') + break except KeyError: - # New role - if action != 'provision': - continue - role = Role() - role.id = uuid + continue + else: + # New role + if action != 'provision': + continue + role = Role() + role.id = uuid if action == 'provision': # Provision/rename role.name = name @@ -139,7 +146,7 @@ class CmdHoboNotify(Command): # Deprovision role.remove_self() # All roles have been sent - if full: + if full and action == 'provision': for role in Role.select(): if role.slug not in uuids: role.remove_self() -- 2.10.1