Skip to content

Commit

Permalink
increase test coverage, not done
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Nov 14, 2023
1 parent f2b9794 commit 4c09026
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions tests/unittests/web/auth/ldap_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mock import Mock, patch
from mock import Mock, MagicMock, patch

import pytest

Expand All @@ -7,7 +7,18 @@
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 +226,43 @@ 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_accounts_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_accounts_with_correct_password_return_account(self, *_):
result = ldap.authenticate('foo', 'ni!')
assert result == ACTIVE_ACCOUNT


#
# Pytest fixtures
#
Expand Down

0 comments on commit 4c09026

Please sign in to comment.