Debian Codegraph @

dirsrvtests/tests/suites/clu/schema_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2023 Red Hat, Inc.
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 import logging
10 import pytest
11 import os
12 from ldap.schema.models import AttributeType, ObjectClass
13 from test389.topologies import topology_st as topo
14 from lib389.schema import Schema, OBJECT_MODEL_PARAMS
15
16 pytestmark = pytest.mark.tier0
17 log = logging.getLogger(__name__)
18
19 @pytest.fixture(scope="function")
20 def create_attribute(request, topo):
21 schema = Schema(topo.standalone)
22 attr_name = request.param['name']
23 oid = request.param['oid']
24 desc = request.param.get('desc', 'Test attribute')
25 syntax = request.param.get('syntax', '1.3.6.1.4.1.1466.115.121.1.15')
26 x_origin = request.param.get('x_origin', None)
27 sup = request.param.get('sup', None)
28 substr = request.param.get('substr', None)
29
30 parameters = OBJECT_MODEL_PARAMS[AttributeType].copy()
31 parameters.update({
32 'names': (attr_name,),
33 'oid': oid,
34 'desc': desc,
35 'syntax': syntax,
36 })
37 if x_origin:
38 parameters['x_origin'] = x_origin
39 if sup:
40 parameters['sup'] = sup
41 if substr:
42 parameters['substr'] = substr
43
44 schema.add_attributetype(parameters)
45
46 def fin():
47 schema.remove_attributetype(attr_name)
48
49 request.addfinalizer(fin)
50 return attr_name, schema
51
52
53 def test_origins_with_extra_parenthesis(topo):
54 """Test the custom schema with extra parenthesis in X-ORIGIN can be parsed
55 into JSON
56
57 :id: 4230f83b-0dc3-4bc4-a7a8-5ab0826a4f05
58 :setup: Standalone Instance
59 :steps:
60 1. Add attribute with X-ORIGIN that contains extra parenthesis
61 2. Querying for that attribute with JSON flag
62 :expectedresults:
63 1. Success
64 2. Success
65 """
66
67 ATTR_NAME = 'testAttribute'
68 X_ORG_VAL = 'test (TEST)'
69 schema = Schema(topo.standalone)
70
71 # Add new attribute
72 parameters = OBJECT_MODEL_PARAMS[AttributeType].copy()
73 parameters.update({
74 'names': (ATTR_NAME,),
75 'oid': '1.1.1.1.1.1.1.22222',
76 'desc': 'Test extra parenthesis in X-ORIGIN',
77 'syntax': '1.3.6.1.4.1.1466.115.121.1.15',
78 'x_origin': (X_ORG_VAL,),
79 })
80 schema.add_attributetype(parameters)
81
82 # Search for attribute with JSON option
83 attr_result = schema.query_attributetype(ATTR_NAME, json=True)
84
85 # Verify the x-origin value is correct
86 assert attr_result['at']['x_origin'][0] == X_ORG_VAL
87
88 # Clean up
89 schema.remove_attributetype(ATTR_NAME)
90
91
92 schema_params = [
93 {'name': 'attr1', 'oid': '99999.1', 'x_origin': None},
94 {'name': 'attr2', 'oid': '99999.2', 'x_origin': 'test-str'},
95 {'name': 'attr3', 'oid': '99999.3', 'x_origin': ['test-list']},
96 {'name': 'attr4', 'oid': '99999.4', 'x_origin': ('test-tuple',)},
97 ]
98 @pytest.mark.parametrize("create_attribute", schema_params, indirect=True)
99 def test_origins(create_attribute):
100 """Test the various possibilities of x-origin
101
102 :id: 3229f6f8-67c1-4558-9be5-71434283086a
103 :parametrized: yes
104 :setup: Standalone Instance
105 :steps:
106 1. Add an attribute with different x-origin values/types
107 :expectedresults:
108 1. Success
109 """
110 attr_name, schema = create_attribute
111 attr_result = schema.query_attributetype(attr_name, json=True)
112 assert attr_result['at']['names'][0] == attr_name
113
114
115 @pytest.mark.parametrize("create_attribute", [{'name': 'test-mr', 'oid': '99999999', 'desc': 'Test matching rule', 'substr': 'numericstringsubstringsmatch'}], indirect=True)
116 def test_mrs(create_attribute):
117 """Test an attribute can be added with a matching rule
118
119 :id: e4eb06e0-7f80-41fe-8868-08c2bafc7590
120 :parametrized: yes
121 :setup: Standalone Instance
122 :steps:
123 1. Add an attribute with a matching rule
124 :expectedresults:
125 1. Success
126 """
127 attr_name, schema = create_attribute
128 attr_result = schema.query_attributetype(attr_name, json=True)
129 assert attr_result['at']['names'][0] == attr_name
130
131
132 @pytest.mark.parametrize("create_attribute", [{'name': 'testEditAttr', 'oid': '1.2.3.4.5.6.7.8888', 'desc': 'Test edit attribute type'}], indirect=True)
133 def test_edit_attributetype(create_attribute):
134 """Test editing an attribute type in the schema
135
136 :id: 07c98f6a-89f8-44e5-9cc1-353d1f7bccf4
137 :parametrized: yes
138 :setup: Standalone Instance
139 :steps:
140 1. Add a new attribute type
141 2. Edit the attribute type
142 3. Verify the changes
143 :expectedresults:
144 1. Success
145 2. Success
146 3. Changes are reflected correctly
147 """
148 attr_name, schema = create_attribute
149
150 # Edit the attribute type
151 edit_parameters = {
152 'desc': 'Updated description for test edit attribute type',
153 'syntax': '1.3.6.1.4.1.1466.115.121.1.26', # IA5String
154 }
155 schema.edit_attributetype(attr_name, edit_parameters)
156
157 # Verify the changes
158 edited_attr = schema.query_attributetype(attr_name, json=True)
159 assert edited_attr['at']['desc'][0] == 'Updated description for test edit attribute type'
160 assert edited_attr['at']['syntax'][0] == '1.3.6.1.4.1.1466.115.121.1.26'
161
162
163 def test_edit_objectclass(topo):
164 """Test editing an object class in the schema
165
166 :id: b160cd33-c232-4f45-bc7c-1dd8ad20f857
167 :setup: Standalone Instance
168 :steps:
169 1. Add a new object class
170 2. Edit the object class
171 3. Verify the changes
172 :expectedresults:
173 1. Success
174 2. Success
175 3. Changes are reflected correctly
176 """
177
178 schema = Schema(topo.standalone)
179
180 # Add new object class
181 oc_name = 'testEditOC'
182 parameters = OBJECT_MODEL_PARAMS[ObjectClass].copy()
183 parameters.update({
184 'names': (oc_name,),
185 'oid': '1.2.3.4.5.6.7.9999',
186 'desc': 'Test edit object class',
187 'must': ('cn',),
188 'may': ('description',),
189 })
190 schema.add_objectclass(parameters)
191
192 # Edit the object class
193 edit_parameters = {
194 'desc': 'Updated description for test edit object class',
195 'must': ('cn', 'sn'),
196 'may': ('description', 'telephoneNumber'),
197 }
198 schema.edit_objectclass(oc_name, edit_parameters)
199
200 # Verify the changes
201 edited_oc = schema.query_objectclass(oc_name, json=True)
202 assert edited_oc['oc']['desc'][0] == 'Updated description for test edit object class'
203 assert set(edited_oc['oc']['must']) == set(['cn', 'sn'])
204 assert set(edited_oc['oc']['may']) == set(['description', 'telephoneNumber'])
205
206 # Clean up
207 schema.remove_objectclass(oc_name)
208
209
210 @pytest.mark.parametrize("create_attribute", [{'name': 'testEditAttrSup', 'oid': '1.2.3.4.5.6.7.7777', 'desc': 'Test edit attribute type with superior', 'sup': ('name',)}], indirect=True)
211 def test_edit_attributetype_remove_superior(create_attribute):
212 """Test editing an attribute type to remove a parameter from it
213
214 :id: bd6ae89f-9617-4620-adc2-465884ca568b
215 :parametrized: yes
216 :setup: Standalone Instance
217 :steps:
218 1. Add a new attribute type with a superior
219 2. Edit the attribute type to remove the superior
220 3. Verify the changes and the removal of inherited matching rules
221 :expectedresults:
222 1. Success
223 2. Success
224 3. Superior is removed and inherited matching rules are cleared
225 """
226 attr_name, schema = create_attribute
227
228 # Verify the attribute was created with a superior
229 initial_attr = schema.query_attributetype(attr_name, json=True)
230 assert 'sup' in initial_attr['at'] and initial_attr['at']['sup'][0] == 'name', "Superior not set correctly"
231
232 # Edit the attribute type to remove the superior
233 edit_parameters = {
234 'sup': None,
235 }
236 schema.edit_attributetype(attr_name, edit_parameters)
237
238 # Verify the changes
239 edited_attr = schema.query_attributetype(attr_name, json=True)
240 assert 'sup' not in edited_attr['at'] or not edited_attr['at']['sup'], "Superior not removed"
241 assert 'equality' not in edited_attr['at'] or not edited_attr['at']['equality'], "Equality matching rule not cleared"
242 assert 'ordering' not in edited_attr['at'] or not edited_attr['at']['ordering'], "Ordering matching rule not cleared"
243 assert 'substr' not in edited_attr['at'] or not edited_attr['at']['substr'], "Substring matching rule not cleared"
244
245
246 @pytest.mark.parametrize("create_attribute", [{'name': 'testCustomAttr', 'oid': '1.2.3.4.5.6.7.8888', 'desc': 'Initial description for custom attribute'}], indirect=True)
247 def test_edit_attribute_keep_custom_values(create_attribute):
248 """Test editing a custom schema attribute keeps all custom values
249
250 :id: 5b1e2e8b-28c2-4f77-9c03-07eff20f763d
251 :parametrized: yes
252 :setup: Standalone Instance
253 :steps:
254 1. Create a custom attribute
255 2. Edit the attribute and change the description
256 3. Verify that both the description and OID are kept
257 4. Edit the attribute again and change the OID
258 5. Verify that both the description and OID are kept
259 :expectedresults:
260 1. Success
261 2. Success
262 3. Both custom description and OID are preserved
263 4. Success
264 5. Both custom description and OID are preserved
265 """
266 attr_name, schema = create_attribute
267 initial_oid = '1.2.3.4.5.6.7.8888'
268 initial_desc = 'Initial description for custom attribute'
269
270 # Verify the attribute was created correctly
271 attr = schema.query_attributetype(attr_name, json=True)
272 assert attr['at']['oid'][0] == initial_oid
273 assert attr['at']['desc'][0] == initial_desc
274
275 # Edit the attribute and change the description
276 new_desc = 'Updated description for custom attribute'
277 edit_parameters = {
278 'desc': new_desc,
279 }
280 schema.edit_attributetype(attr_name, edit_parameters)
281
282 # Verify that both the description and OID are kept
283 edited_attr = schema.query_attributetype(attr_name, json=True)
284 assert edited_attr['at']['oid'][0] == initial_oid, f"OID changed unexpectedly to {edited_attr['at']['oid'][0]}"
285 assert edited_attr['at']['desc'][0] == new_desc, f"Description not updated, current value: {edited_attr['at']['desc'][0]}"
286
287 # Edit the attribute again and change the OID
288 new_oid = '1.2.3.4.5.6.7.9999'
289 edit_parameters = {
290 'oid': new_oid,
291 }
292 schema.edit_attributetype(attr_name, edit_parameters)
293
294 # Verify that both the description and OID are kept
295 re_edited_attr = schema.query_attributetype(attr_name, json=True)
296 assert re_edited_attr['at']['oid'][0] == new_oid, f"OID not updated, current value: {re_edited_attr['at']['oid'][0]}"
297 assert re_edited_attr['at']['desc'][0] == new_desc, f"Description changed unexpectedly to {re_edited_attr['at']['desc'][0]}"
298
299 # Additional check: Ensure the attribute name wasn't used as the OID
300 assert re_edited_attr['at']['oid'][0] != f"{attr_name}-oid", "OID was incorrectly set to '<ATTRIBUTE_NAME>-oid'"
301
302
303 if __name__ == '__main__':
304 # Run isolated
305 # -s for DEBUG mode
306 CURRENT_FILE = os.path.realpath(__file__)
307 pytest.main(["-s", CURRENT_FILE])