Skip to content

Commit

Permalink
Increase test coverage of auth.ldap
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Mar 1, 2024
1 parent 34e15ee commit 83d0041
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions tests/unittests/web/auth/ldap_test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
from mock import Mock, patch
from mock import Mock, MagicMock, patch

import pytest

from nav.config import NAVConfigParser
from nav.models.profiles import Account
from nav.web import auth
from nav.web.auth import ldap


LDAP_ACCOUNT = auth.Account(login='knight', ext_sync='ldap', password='shrubbery')
LOCKED_ACCOUNT = auth.Account(
login='galahad',
ext_sync='ldap',
password='shrubbery',
locked=True,
)
ACTIVE_ACCOUNT = auth.Account(
login='arthur',
ext_sync='ldap',
password='shrubbery',
locked=False,
)


class LdapTestConfig(NAVConfigParser):
Expand Down Expand Up @@ -215,6 +227,51 @@ def test_no_admin_entitlement_option_should_make_no_admin_decision(user_zaphod):
assert u.is_admin() is None


class TestLdapAuthenticate:
@patch("nav.web.auth.ldap.available", new=False)
def test_if_ldap_not_available_return_None(self, *_):
result = ldap.authenticate('foo', 'bar')
assert result is None

@patch("nav.web.auth.ldap.available", new=True)
@patch("nav.web.auth.ldap.get_ldap_user", return_value=False)
@patch(
"nav.web.auth.Account.objects.get", new=MagicMock(return_value=LOCKED_ACCOUNT)
)
def test_locked_accounts_return_None(self, *_):
result = ldap.authenticate('foo', 'bar')
assert result is None

@patch("nav.web.auth.ldap.available", new=True)
@patch("nav.web.auth.ldap.get_ldap_user", return_value=False)
@patch(
"nav.web.auth.Account.objects.get", new=MagicMock(return_value=ACTIVE_ACCOUNT)
)
@patch("nav.web.auth.Account.check_password", return_value=False)
def test_active_account_with_wrong_password_return_None(self, *_):
result = ldap.authenticate('foo', 'ni!')
assert result is None

@patch("nav.web.auth.ldap.available", new=True)
@patch("nav.web.auth.ldap.get_ldap_user", return_value=False)
@patch(
"nav.web.auth.Account.objects.get", new=MagicMock(return_value=ACTIVE_ACCOUNT)
)
@patch("nav.web.auth.Account.check_password", return_value=True)
@patch("nav.web.auth.ldap.update_ldap_user", return_value=ACTIVE_ACCOUNT)
def test_active_account_with_correct_password_return_account(self, *_):
result = ldap.authenticate('foo', 'ni!')
assert result == ACTIVE_ACCOUNT

@patch("nav.web.auth.ldap.available", new=True)
@patch("nav.web.auth.ldap.get_ldap_user", return_value=True)
@patch("nav.web.auth.Account.objects.get", side_effect=Account.DoesNotExist())
@patch("nav.web.auth.ldap.autocreate_ldap_user", return_value=ACTIVE_ACCOUNT)
def test_nonexisting_accounts_are_created(self, *_):
result = ldap.authenticate('foo', 'ni!')
assert result == ACTIVE_ACCOUNT


#
# Pytest fixtures
#
Expand Down

0 comments on commit 83d0041

Please sign in to comment.