Skip to content

Commit

Permalink
python tests
Browse files Browse the repository at this point in the history
Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
  • Loading branch information
rissson committed Nov 30, 2024
1 parent a853549 commit e225d97
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
1 change: 0 additions & 1 deletion kadmin/tests/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ macro_rules! gen_tests {
assert_eq!(policy.name(), &polname);
assert_eq!(policy.password_max_life(), None);
assert_eq!(policy.attributes(), 0);
policy.delete(&kadmin)?;
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions python-kadmin-rs/python/kadmin/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class KAdmin:
def add_policy(self, name: str, **kwargs) -> Policy: ...
def delete_policy(self, name: str) -> None: ...
def get_policy(self, name: str) -> Policy | None: ...
def policy_exists(self, name: str) -> bool: ...
def list_policies(self, query: str | None = None) -> List[str]: ...
@staticmethod
def with_password(
Expand Down
1 change: 1 addition & 0 deletions python-kadmin-rs/python/kadmin_local/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class KAdmin:
def add_policy(self, name: str, **kwargs) -> Policy: ...
def delete_policy(self, name: str) -> None: ...
def get_policy(self, name: str) -> Policy | None: ...
def policy_exists(self, name: str) -> bool: ...
def list_policies(self, query: str | None = None) -> List[str]: ...
@staticmethod
def with_local(
Expand Down
19 changes: 19 additions & 0 deletions python-kadmin-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ pub mod pykadmin {
}))
}

/// Check if a policy exists
///
/// :param name: policy name to check for
/// :type name: str
/// :return: `True` if the policy exists, `False` otherwise
/// :rtype: bool
fn policy_exists(&self, name: &str) -> Result<bool> {
Ok(self.0.policy_exists(name)?)
}

/// List policies
///
/// :param query: a shell-style glob expression that can contain the wild-card characters
Expand Down Expand Up @@ -563,6 +573,15 @@ pub mod pykadmin {
Ok(self.inner.delete(self.kadmin.deref())?)
}

/// The policy name
///
/// :getter: Get the policy name
/// :type: str
#[getter]
pub fn name(&self) -> &str {
self.inner.name()
}

/// Minimum lifetime of a password
///
/// :getter: Get the minimum lifetime of a password
Expand Down
68 changes: 68 additions & 0 deletions python-kadmin-rs/tests/test_policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from .utils import KerberosTestCase, random_string

import kadmin


class TestPolicy(KerberosTestCase):
def test_list_policies(self):
kadm = kadmin.KAdmin.with_password(
self.realm.admin_princ, self.realm.password("admin")
)
self.assertEqual(
kadm.list_policies("*"),
[],
)

def test_policy_exists(self):
kadm = kadmin.KAdmin.with_password(
self.realm.admin_princ, self.realm.password("admin")
)
polname = random_string(16)
kadm.add_policy(polname)
self.assertTrue(kadm.policy_exists(polname))
kadm.delete_policy(polname)

def test_create_policy(self):
kadm = kadmin.KAdmin.with_password(
self.realm.admin_princ, self.realm.password("admin")
)
polname = random_string(16)
policy = kadm.add_policy(polname)
self.assertEqual(policy.name, polname)
self.assertIsNone(policy.password_max_life)
self.assertEqual(policy.attributes, 0)
kadm.delete_policy(polname)

def test_delete_policy(self):
kadm = kadmin.KAdmin.with_password(
self.realm.admin_princ, self.realm.password("admin")
)
polname = random_string(16)
policy = kadm.add_policy(polname)
self.assertTrue(kadm.policy_exists(polname))
policy.delete()
self.assertFalse(kadm.policy_exists(polname))

def test_modify(self):
kadm = kadmin.KAdmin.with_password(
self.realm.admin_princ, self.realm.password("admin")
)
polname = random_string(16)
policy = kadm.add_policy(polname)
policy = policy.modify(password_min_length=42)
self.assertEqual(policy.password_min_length, 42)
policy = kadm.get_policy(polname)
self.assertEqual(policy.password_min_length, 42)
kadm.delete_policy(polname)

def test_setter(self):
kadm = kadmin.KAdmin.with_password(
self.realm.admin_princ, self.realm.password("admin")
)
polname = random_string(16)
policy = kadm.add_policy(polname)
policy.password_min_length = 42
self.assertEqual(policy.password_min_length, 42)
policy = kadm.get_policy(polname)
self.assertEqual(policy.password_min_length, 42)
kadm.delete_policy(polname)
2 changes: 1 addition & 1 deletion python-kadmin-rs/tests/test_principal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import kadmin


class TestInit(KerberosTestCase):
class TestPrincipal(KerberosTestCase):
def test_list_principals(self):
kadm = kadmin.KAdmin.with_password(
self.realm.admin_princ, self.realm.password("admin")
Expand Down
6 changes: 6 additions & 0 deletions python-kadmin-rs/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
from copy import deepcopy
from k5test import realm
from unittest import TestCase
from random import SystemRandom
import string

def random_string(length: int) -> str:
rand = SystemRandom()
return "".join(rand.choice(string.ascii_lowercase + string.digits) for _ in range(length))


class KerberosTestCase(TestCase):
Expand Down

0 comments on commit e225d97

Please sign in to comment.