2017-09-04 05:41:43 +02:00
|
|
|
import pytest
|
2019-05-16 16:57:02 +02:00
|
|
|
from lemur.auth.ldap import * # noqa
|
2017-09-04 05:41:43 +02:00
|
|
|
from mock import patch, MagicMock
|
|
|
|
|
|
|
|
|
|
|
|
class LdapPrincipalTester(LdapPrincipal):
|
|
|
|
def __init__(self, args):
|
|
|
|
super().__init__(args)
|
2019-05-16 16:57:02 +02:00
|
|
|
self.ldap_server = "ldap://localhost"
|
2017-09-04 05:41:43 +02:00
|
|
|
|
|
|
|
def bind_test(self):
|
2019-05-16 16:57:02 +02:00
|
|
|
groups = [
|
|
|
|
(
|
|
|
|
"user",
|
|
|
|
{
|
|
|
|
"memberOf": [
|
|
|
|
"CN=Lemur Access,OU=Groups,DC=example,DC=com".encode("utf-8"),
|
|
|
|
"CN=Pen Pushers,OU=Groups,DC=example,DC=com".encode("utf-8"),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
]
|
2017-09-04 05:41:43 +02:00
|
|
|
self.ldap_client = MagicMock()
|
|
|
|
self.ldap_client.search_s.return_value = groups
|
|
|
|
self._bind()
|
|
|
|
|
|
|
|
def authorize_test_groups_to_roles_admin(self):
|
2019-05-16 16:57:02 +02:00
|
|
|
self.ldap_groups = "".join(
|
|
|
|
[
|
|
|
|
"CN=Pen Pushers,OU=Groups,DC=example,DC=com",
|
|
|
|
"CN=Lemur Admins,OU=Groups,DC=example,DC=com",
|
|
|
|
"CN=Lemur Read Only,OU=Groups,DC=example,DC=com",
|
|
|
|
]
|
|
|
|
)
|
2017-09-04 05:41:43 +02:00
|
|
|
self.ldap_required_group = None
|
2019-05-16 16:57:02 +02:00
|
|
|
self.ldap_groups_to_roles = {
|
|
|
|
"Lemur Admins": "admin",
|
|
|
|
"Lemur Read Only": "read-only",
|
|
|
|
}
|
2017-09-04 05:41:43 +02:00
|
|
|
return self._authorize()
|
|
|
|
|
|
|
|
def authorize_test_required_group(self, group):
|
2019-05-16 16:57:02 +02:00
|
|
|
self.ldap_groups = "".join(
|
|
|
|
[
|
|
|
|
"CN=Lemur Access,OU=Groups,DC=example,DC=com",
|
|
|
|
"CN=Pen Pushers,OU=Groups,DC=example,DC=com",
|
|
|
|
]
|
|
|
|
)
|
2017-09-04 05:41:43 +02:00
|
|
|
self.ldap_required_group = group
|
|
|
|
return self._authorize()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def principal(session):
|
2019-05-16 16:57:02 +02:00
|
|
|
args = {"username": "user", "password": "p4ssw0rd"}
|
2017-09-04 05:41:43 +02:00
|
|
|
yield LdapPrincipalTester(args)
|
|
|
|
|
|
|
|
|
|
|
|
class TestLdapPrincipal:
|
2019-05-16 16:57:02 +02:00
|
|
|
@patch("ldap.initialize")
|
2017-09-04 05:41:43 +02:00
|
|
|
def test_bind(self, app, principal):
|
|
|
|
self.test_ldap_user = principal
|
|
|
|
self.test_ldap_user.bind_test()
|
2019-05-16 16:57:02 +02:00
|
|
|
group = "Pen Pushers"
|
2017-09-04 05:41:43 +02:00
|
|
|
assert group in self.test_ldap_user.ldap_groups
|
2019-05-16 16:57:02 +02:00
|
|
|
assert self.test_ldap_user.ldap_principal == "user@example.com"
|
2017-09-04 05:41:43 +02:00
|
|
|
|
|
|
|
def test_authorize_groups_to_roles_admin(self, app, principal):
|
|
|
|
self.test_ldap_user = principal
|
|
|
|
roles = self.test_ldap_user.authorize_test_groups_to_roles_admin()
|
|
|
|
assert any(x.name == "admin" for x in roles)
|
|
|
|
|
|
|
|
def test_authorize_required_group_missing(self, app, principal):
|
|
|
|
self.test_ldap_user = principal
|
2019-05-16 16:57:02 +02:00
|
|
|
roles = self.test_ldap_user.authorize_test_required_group("Not Allowed")
|
2017-09-04 05:41:43 +02:00
|
|
|
assert not roles
|
|
|
|
|
|
|
|
def test_authorize_required_group_access(self, session, principal):
|
|
|
|
self.test_ldap_user = principal
|
2019-05-16 16:57:02 +02:00
|
|
|
roles = self.test_ldap_user.authorize_test_required_group("Lemur Access")
|
2017-09-04 05:41:43 +02:00
|
|
|
assert len(roles) >= 1
|
|
|
|
assert any(x.name == "user@example.com" for x in roles)
|