| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2019 William Brown <[email protected]> |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# License: GPL (version 3 or any later version). |
| 6 |
# See LICENSE for details. |
| 7 |
# --- END COPYRIGHT BLOCK --- |
| 8 |
# |
| 9 |
|
| 10 |
import ldap |
| 11 |
import pytest |
| 12 |
|
| 13 |
from lib389._constants import DEFAULT_SUFFIX |
| 14 |
from test389.topologies import topology_st |
| 15 |
|
| 16 |
from lib389.idm.group import Groups, Group |
| 17 |
|
| 18 |
pytestmark = pytest.mark.tier1 |
| 19 |
|
| 20 |
################################################################################# |
| 21 |
# This is a series of test cases to assert that various DN construction scenarios |
| 22 |
# work as expected in lib389. |
| 23 |
# |
| 24 |
# DSLdapObjects are designed to allow explicit control, or to "safely assume" |
| 25 |
# so that ldap concepts aren't as confusing. |
| 26 |
# You can thus construct an object with a DN that is: |
| 27 |
# * defined by you expliticly |
| 28 |
# * derived from properties of the object automatically |
| 29 |
# |
| 30 |
# There are also two paths to construction: from the pluralised factory style |
| 31 |
# builder, or from the singular. The factory style has very few extra parts |
| 32 |
# but it's worth testing anyway. |
| 33 |
# |
| 34 |
# In no case do we derive a multi value rdn due to their complexity. |
| 35 |
# |
| 36 |
|
| 37 |
def test_mul_explicit_rdn(topology_st): |
| 38 |
"""Test that with multiple cn and an explicit rdn, we use the rdn |
| 39 |
|
| 40 |
:id: b39ef204-45c0-4a74-9b59-b4ac1199d78c |
| 41 |
|
| 42 |
:setup: standalone instance |
| 43 |
|
| 44 |
:steps: 1. Create with mulitple cn and rdn |
| 45 |
|
| 46 |
:expectedresults: 1. Create success |
| 47 |
""" |
| 48 |
# Create with an explicit rdn value, given to the properties/rdn |
| 49 |
gps = Groups(topology_st.standalone, DEFAULT_SUFFIX) |
| 50 |
gp = gps.create('cn=test_mul_explicit_rdn', |
| 51 |
properties={ |
| 52 |
'cn': ['test_mul_explicit_rdn', 'other_cn_test_mul_explicit_rdn'], |
| 53 |
}) |
| 54 |
assert gp.dn.lower() == f'cn=test_mul_explicit_rdn,ou=groups,{DEFAULT_SUFFIX}'.lower() |
| 55 |
gp.delete() |
| 56 |
|
| 57 |
def test_mul_derive_single_dn(topology_st): |
| 58 |
"""Test that with single cn we derive rdn correctly. |
| 59 |
|
| 60 |
:id: f34f271a-ca57-4aa0-905a-b5392ce06c79 |
| 61 |
|
| 62 |
:setup: standalone instance |
| 63 |
|
| 64 |
:steps: 1. Create with single cn |
| 65 |
|
| 66 |
:expectedresults: 1. Create success |
| 67 |
""" |
| 68 |
gps = Groups(topology_st.standalone, DEFAULT_SUFFIX) |
| 69 |
gp = gps.create(properties={ |
| 70 |
'cn': ['test_mul_derive_single_dn'], |
| 71 |
}) |
| 72 |
assert gp.dn.lower() == f'cn=test_mul_derive_single_dn,ou=groups,{DEFAULT_SUFFIX}'.lower() |
| 73 |
gp.delete() |
| 74 |
|
| 75 |
def test_mul_derive_mult_dn(topology_st): |
| 76 |
"""Test that with multiple cn we derive rdn correctly. |
| 77 |
|
| 78 |
:id: 1e1f5483-bfad-4f73-9dfb-aec54d08b268 |
| 79 |
|
| 80 |
:setup: standalone instance |
| 81 |
|
| 82 |
:steps: 1. Create with multiple cn |
| 83 |
|
| 84 |
:expectedresults: 1. Create success |
| 85 |
""" |
| 86 |
gps = Groups(topology_st.standalone, DEFAULT_SUFFIX) |
| 87 |
gp = gps.create(properties={ |
| 88 |
'cn': ['test_mul_derive_mult_dn', 'test_mul_derive_single_dn'], |
| 89 |
}) |
| 90 |
assert gp.dn.lower() == f'cn=test_mul_derive_mult_dn,ou=groups,{DEFAULT_SUFFIX}'.lower() |
| 91 |
gp.delete() |
| 92 |
|
| 93 |
def test_sin_explicit_dn(topology_st): |
| 94 |
"""Test explicit dn with create |
| 95 |
|
| 96 |
:id: 2d812225-243b-4f87-85ad-d403a4ae0267 |
| 97 |
|
| 98 |
:setup: standalone instance |
| 99 |
|
| 100 |
:steps: 1. Create with explicit dn |
| 101 |
|
| 102 |
:expectedresults: 1. Create success |
| 103 |
""" |
| 104 |
expect_dn = f'cn=test_sin_explicit_dn,ou=groups,{DEFAULT_SUFFIX}' |
| 105 |
gp = Group(topology_st.standalone, dn=expect_dn) |
| 106 |
gp.create(properties={ |
| 107 |
'cn': ['test_sin_explicit_dn'], |
| 108 |
}) |
| 109 |
assert gp.dn.lower() == expect_dn.lower() |
| 110 |
gp.delete() |
| 111 |
|
| 112 |
def test_sin_explicit_rdn(topology_st): |
| 113 |
"""Test explicit rdn with create. |
| 114 |
|
| 115 |
:id: a2c14e50-8086-4edb-9088-3f4a8e875c3a |
| 116 |
|
| 117 |
:setup: standalone instance |
| 118 |
|
| 119 |
:steps: 1. Create with explicit rdn |
| 120 |
|
| 121 |
:expectedresults: 1. Create success |
| 122 |
""" |
| 123 |
gp = Group(topology_st.standalone) |
| 124 |
gp.create(rdn='cn=test_sin_explicit_rdn', |
| 125 |
basedn=f'ou=groups,{DEFAULT_SUFFIX}', |
| 126 |
properties={ |
| 127 |
'cn': ['test_sin_explicit_rdn'], |
| 128 |
}) |
| 129 |
assert gp.dn.lower() == f'cn=test_sin_explicit_rdn,ou=groups,{DEFAULT_SUFFIX}'.lower() |
| 130 |
gp.delete() |
| 131 |
|
| 132 |
def test_sin_derive_single_dn(topology_st): |
| 133 |
"""Derive the dn from a single cn |
| 134 |
|
| 135 |
:id: d7597016-214c-4fbd-8b48-71eb16ea9ede |
| 136 |
|
| 137 |
:setup: standalone instance |
| 138 |
|
| 139 |
:steps: 1. Create with a single cn (no dn, no rdn) |
| 140 |
|
| 141 |
:expectedresults: 1. Create success |
| 142 |
""" |
| 143 |
gp = Group(topology_st.standalone) |
| 144 |
gp.create(basedn=f'ou=groups,{DEFAULT_SUFFIX}', |
| 145 |
properties={ |
| 146 |
'cn': ['test_sin_explicit_dn'], |
| 147 |
}) |
| 148 |
assert gp.dn.lower() == f'cn=test_sin_explicit_dn,ou=groups,{DEFAULT_SUFFIX}'.lower() |
| 149 |
gp.delete() |
| 150 |
|
| 151 |
def test_sin_derive_mult_dn(topology_st): |
| 152 |
"""Derive the dn from multiple cn |
| 153 |
|
| 154 |
:id: 0a1a7132-a08f-4b56-ae52-30c8ca59cfaf |
| 155 |
|
| 156 |
:setup: standalone instance |
| 157 |
|
| 158 |
:steps: 1. Create with multiple cn |
| 159 |
|
| 160 |
:expectedresults: 1. Create success |
| 161 |
""" |
| 162 |
gp = Group(topology_st.standalone) |
| 163 |
gp.create(basedn=f'ou=groups,{DEFAULT_SUFFIX}', |
| 164 |
properties={ |
| 165 |
'cn': ['test_sin_derive_mult_dn', 'other_test_sin_derive_mult_dn'], |
| 166 |
}) |
| 167 |
assert gp.dn.lower() == f'cn=test_sin_derive_mult_dn,ou=groups,{DEFAULT_SUFFIX}'.lower() |
| 168 |
gp.delete() |
| 169 |
|
| 170 |
def test_sin_invalid_no_basedn(topology_st): |
| 171 |
"""Test that with insufficent data, create fails. |
| 172 |
|
| 173 |
:id: a710b81c-cb74-4632-97b3-bdbcccd40954 |
| 174 |
|
| 175 |
:setup: standalone instance |
| 176 |
|
| 177 |
:steps: 1. Create with no basedn (no rdn derivation will work) |
| 178 |
|
| 179 |
:expectedresults: 1. Create fails |
| 180 |
""" |
| 181 |
gp = Group(topology_st.standalone) |
| 182 |
# No basedn, so we can't derive the full dn from this. |
| 183 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 184 |
gp.create(properties={ |
| 185 |
'cn': ['test_sin_invalid_no_basedn'], |
| 186 |
}) |
| 187 |
|
| 188 |
def test_sin_invalid_no_rdn(topology_st): |
| 189 |
"""Test that with no cn, rdn derivation fails. |
| 190 |
|
| 191 |
:id: c3bb28f8-db59-4d8a-8920-169879ef702b |
| 192 |
|
| 193 |
:setup: standalone instance |
| 194 |
|
| 195 |
:steps: 1. Create with no cn |
| 196 |
|
| 197 |
:expectedresults: 1. Create fails |
| 198 |
""" |
| 199 |
gp = Group(topology_st.standalone) |
| 200 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 201 |
# Note lack of rdn derivable type (cn) AND no rdn |
| 202 |
gp.create(basedn=f'ou=groups,{DEFAULT_SUFFIX}', |
| 203 |
properties={ |
| 204 |
'member': ['test_sin_explicit_dn'], |
| 205 |
}) |
| 206 |
|
| 207 |
def test_sin_non_present_rdn(topology_st): |
| 208 |
"""Test that with an rdn not present in attributes, create succeeds in some cases. |
| 209 |
|
| 210 |
:id: a5d9cb24-8907-4622-ac85-90407a66e00a |
| 211 |
|
| 212 |
:setup: standalone instance |
| 213 |
|
| 214 |
:steps: 1. Create with an rdn not in properties |
| 215 |
|
| 216 |
:expectedresults: 1. Create success |
| 217 |
""" |
| 218 |
# Test that creating something with an rdn not present in the properties works |
| 219 |
# NOTE: I think that this is 389-ds making this work, NOT lib389. |
| 220 |
gp1 = Group(topology_st.standalone) |
| 221 |
gp1.create(rdn='cn=test_sin_non_present_rdn', |
| 222 |
basedn=f'ou=groups,{DEFAULT_SUFFIX}', |
| 223 |
properties={ |
| 224 |
'cn': ['other_test_sin_non_present_rdn'], |
| 225 |
}) |
| 226 |
assert gp1.dn.lower() == f'cn=test_sin_non_present_rdn,ou=groups,{DEFAULT_SUFFIX}'.lower() |
| 227 |
gp1.delete() |
| 228 |
|
| 229 |
# Now, test where there is no cn. lib389 is blocking this today, but |
| 230 |
# 50259 will change this. |
| 231 |
gp2 = Group(topology_st.standalone) |
| 232 |
gp2.create(rdn='cn=test_sin_non_present_rdn', |
| 233 |
basedn=f'ou=groups,{DEFAULT_SUFFIX}', |
| 234 |
properties={}) |
| 235 |
assert gp2.dn.lower() == f'cn=test_sin_non_present_rdn,ou=groups,{DEFAULT_SUFFIX}'.lower() |
| 236 |
gp2.delete() |