def test_iter_group(mocker, settings, group_dn, subgroups, expected): app = Flask('test') multipass = Multipass(app) with app.app_context(): idp = LDAPIdentityProvider(multipass, 'LDAP test idp', settings) group = LDAPGroup(idp, 'LDAP test group', group_dn) visited_groups = [] iter_group = group._iter_group() # should not throw StopIteration as the initial group dn must be returned first current_dn = next(iter_group) with pytest.raises(StopIteration): while current_dn: visited_groups.append(current_dn) current_dn = iter_group.send(subgroups.get(current_dn, [])) assert len(visited_groups) == len(expected) assert set(visited_groups) == expected