# authentic2 - versatile identity manager
# Copyright (C) 2010-2019 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from authentic2.backends import is_user_authenticable
from authentic2.utils.misc import authenticate


def test_user_filters(settings, db, simple_user, user_ou1, ou1):
    assert authenticate(username=simple_user.username, password=simple_user.username)
    assert is_user_authenticable(simple_user)
    assert is_user_authenticable(user_ou1)
    assert authenticate(username=user_ou1.username, password=user_ou1.username)
    settings.A2_USER_FILTER = {'ou__slug': 'ou1'}
    assert not authenticate(username=simple_user.username, password=simple_user.username)
    assert authenticate(username=user_ou1.username, password=user_ou1.username)
    assert not is_user_authenticable(simple_user)
    assert is_user_authenticable(user_ou1)
    settings.A2_USER_EXCLUDE = {'ou__slug': 'ou1'}
    assert not authenticate(username=simple_user.username, password=simple_user.username)
    assert not authenticate(username=user_ou1.username, password=user_ou1.username)
    assert not is_user_authenticable(simple_user)
    assert not is_user_authenticable(user_ou1)
    settings.A2_USER_FILTER = {}
    assert authenticate(username=simple_user.username, password=simple_user.username)
    assert not authenticate(username=user_ou1.username, password=user_ou1.username)
    assert is_user_authenticable(simple_user)
    assert not is_user_authenticable(user_ou1)
