Projet

Général

Profil

0001-auth_saml-rename-toggle-role-action-to-add-role-4685.patch

Valentin Deniaud, 20 octobre 2020 10:10

Télécharger (5,24 ko)

Voir les différences:

Subject: [PATCH] auth_saml: rename toggle-role action to add-role (#46857)

 src/authentic2_auth_saml/adapters.py |  5 +-
 tests/test_auth_saml.py              | 75 +++++++++++++++++-----------
 2 files changed, 51 insertions(+), 29 deletions(-)
src/authentic2_auth_saml/adapters.py
245 245
        except Exception as e:
246 246
            raise MappingError('condition evaluation failed', details={'error': six.text_type(e)})
247 247

  
248
    def action_toggle_role(self, user, idp, saml_attributes, mapping):
248
    def action_add_role(self, user, idp, saml_attributes, mapping):
249 249
        role = self.get_role(mapping)
250 250
        if self.evaluate_condition(user, saml_attributes, mapping):
251 251
            if role not in user.roles.all():
......
256 256
                logger.info('auth_saml: removing role "%s"', role, extra={'user': user})
257 257
                user.roles.remove(role)
258 258

  
259
    def action_toggle_role(self, *args, **kwargs):
260
        return self.action_add_role(*args, **kwargs)
261

  
259 262
    def auth_login(self, request, user):
260 263
        utils.login(request, user, 'saml')
tests/test_auth_saml.py
52 52
    assert response.pyquery('button[name="login-saml-1"]')
53 53

  
54 54

  
55
def test_provision_attributes(db, caplog, simple_role):
55
def test_provision_attributes(db, caplog):
56 56
    from authentic2_auth_saml.adapters import AuthenticAdapter
57 57

  
58 58
    adapter = AuthenticAdapter()
......
79 79
            {
80 80
                'attribute': 'first_name',
81 81
                'saml_attribute': 'first_name',
82
            },
83
            {
84
                'action': 'toggle-role',
85
                'role': {
86
                    'name': simple_role.name,
87
                    'ou': {
88
                        'name': simple_role.ou.name,
89
                    },
90
                },
91
                'condition': "roles == 'A'",
92 82
            }
93 83
        ]
94 84
    }
......
106 96
    assert user.email == 'john.doe@example.com'
107 97
    assert user.attributes.title == 'Mr.'
108 98
    assert user.first_name == 'John'
99
    user.delete()
100

  
101
    # on missing mandatory attribute, no user is created
102
    del saml_attributes['mail']
103
    assert adapter.lookup_user(idp, saml_attributes) is None
104

  
105
    # simulate no attribute value
106
    saml_attributes['first_name'] = []
107
    mapping = {
108
        'attribute': 'first_name',
109
        'saml_attribute': 'first_name',
110
    }
111
    with pytest.raises(MappingError, match='no value for first_name'):
112
        adapter.action_set_attribute(user, idp, saml_attributes, mapping)
113

  
114

  
115
@pytest.mark.parametrize('action_name', ['add-role', 'toggle-role'])
116
def test_provision_add_role(db, simple_role, action_name):
117
    from authentic2_auth_saml.adapters import AuthenticAdapter
118

  
119
    adapter = AuthenticAdapter()
120
    User = get_user_model()
121
    user = User.objects.create()
122
    idp = {
123
        'A2_ATTRIBUTE_MAPPING': [
124
            {
125
                'action': action_name,
126
                'role': {
127
                    'name': simple_role.name,
128
                    'ou': {
129
                        'name': simple_role.ou.name,
130
                    },
131
                },
132
                'condition': "roles == 'A'",
133
            }
134
        ]
135
    }
136

  
137
    saml_attributes = {
138
        'issuer': 'https://idp.com/',
139
        'name_id_content': 'xxx',
140
        'name_id_format': lasso.SAML2_NAME_IDENTIFIER_FORMAT_PERSISTENT,
141
    }
142
    user = adapter.lookup_user(idp, saml_attributes)
143
    user.refresh_from_db()
109 144
    assert simple_role not in user.roles.all()
110 145
    user.delete()
111 146

  
112 147
    # if a toggle-role is mandatory, failure to evaluate condition block user creation
113
    assert idp['A2_ATTRIBUTE_MAPPING'][-1]['action'] == 'toggle-role'
148
    assert idp['A2_ATTRIBUTE_MAPPING'][-1]['action'] == action_name
114 149
    idp['A2_ATTRIBUTE_MAPPING'][-1]['mandatory'] = True
115 150
    assert adapter.lookup_user(idp, saml_attributes) is None
116 151

  
......
130 165
    # condition failed, so role should be removed
131 166
    assert simple_role not in user.roles.all()
132 167

  
133
    user.delete()
134

  
135
    # on missing mandatory attribute, no user is created
136
    del saml_attributes['mail']
137
    assert adapter.lookup_user(idp, saml_attributes) is None
138

  
139
    # simulate no attribute value
140
    saml_attributes['first_name'] = []
141
    mapping = {
142
        'attribute': 'first_name',
143
        'saml_attribute': 'first_name',
144
    }
145
    with pytest.raises(MappingError, match='no value for first_name'):
146
        adapter.action_set_attribute(user, idp, saml_attributes, mapping)
147

  
148

  
149 168

  
150 169
def test_login_with_conditionnal_authenticators(db, app, settings, caplog):
151 170

  
152
-