Debian Codegraph @

lemonldap-ng-manager/t/81-build-ext.t

raw
1 # Test llng-build-manager-files script with plugin extensions
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use File::Temp qw(tempdir);
7 use File::Copy;
8 use JSON;
9
10 my $count = 0;
11
12 # Create temporary directories
13 my $tmpdir = tempdir( CLEANUP => 1 );
14 my $pluginsdir = "$tmpdir/plugins";
15 mkdir $pluginsdir or die "Cannot create plugins dir: $!";
16
17 # Create a test JSON plugin
18 my $plugin_content = {
19 attributes => {
20 oidcRPMetaDataOptionsTestParam => {
21 type => 'bool',
22 default => 0,
23 documentation => 'Test parameter for plugin extension',
24 },
25 testTreeParam => {
26 type => 'text',
27 default => 'default_value',
28 documentation => 'Test tree parameter',
29 },
30
31 # Existing attribute with a new select option to append
32 authentication => {
33 type => 'select',
34 select =>
35 [ { k => 'MyCustomAuth', v => 'My Custom Auth Module' }, ],
36 },
37 },
38 tree => {
39 insert_into => 'generalParameters/plugins',
40 insert_after => 'stayConnected',
41 nodes => [ {
42 title => 'testPluginNode',
43 form => 'simpleInputContainer',
44 nodes => ['testTreeParam'],
45 }
46 ],
47 },
48 ctrees => {
49 oidcRPMetaDataNode => {
50 insert_into =>
51 'oidcRPMetaDataOptions/oidcRPMetaDataOptionsAdvanced',
52 insert_after => 'oidcRPMetaDataOptionsTokenXAuthorizedRP',
53 nodes => ['oidcRPMetaDataOptionsTestParam'],
54 },
55 },
56 constants => {
57 PE_TEST_PLUGIN_ERROR => 250,
58 },
59 lang => {
60 testTreeParam => {
61 en => 'Test tree parameter',
62 fr => 'Paramètre de test',
63 },
64 oidcRPMetaDataOptionsTestParam => {
65 en => 'Test OIDC parameter',
66 fr => 'Paramètre OIDC de test',
67 },
68 },
69 };
70
71 # Write plugin file
72 my $plugin_file = "$pluginsdir/test-plugin.json";
73 open my $fh, '>', $plugin_file or die "Cannot write plugin: $!";
74 print $fh JSON->new->pretty->encode($plugin_content);
75 close $fh;
76 ok( -f $plugin_file, 'Plugin file created' );
77 $count++;
78
79 # Create temporary language directory with copies of real language files
80 my $langdir = "$tmpdir/languages";
81 mkdir $langdir or die "Cannot create lang dir: $!";
82 my $real_langdir = 'site/htdocs/static/languages';
83 if ( -d $real_langdir ) {
84 opendir my $dh, $real_langdir or die "Cannot open $real_langdir: $!";
85 for my $f ( grep { /\.json$/ } readdir $dh ) {
86 copy( "$real_langdir/$f", "$langdir/$f" )
87 or die "Cannot copy $f: $!";
88 }
89 closedir $dh;
90 }
91
92 # Output files
93 my $struct_file = "$tmpdir/struct.json";
94 my $conftree_file = "$tmpdir/conftree.js";
95 my $attributes_file = "$tmpdir/Attributes.pm";
96 my $constants_file = "$tmpdir/Constants.pm";
97 my $default_file = "$tmpdir/DefaultValues.pm";
98 my $reconstants_file = "$tmpdir/ReConstants.pm";
99 my $reverse_tree_file = "$tmpdir/reverseTree.json";
100 my $portal_const_file = "$tmpdir/PortalConstants.pm";
101 my $handler_const = "$tmpdir/StatusConstants.pm";
102
103 # Run llng-build-manager-files
104 my $script = 'scripts/llng-build-manager-files';
105 ok( -f $script, 'Build script exists' );
106 $count++;
107
108 my $cmd = join( ' ',
109 'perl',
110 '-Ilib',
111 '-I../lemonldap-ng-common/lib',
112 '-I../lemonldap-ng-portal/lib',
113 '-I../lemonldap-ng-handler/lib',
114 $script,
115 "--plugins-dir=$pluginsdir",
116 "--struct-file=$struct_file",
117 "--conftree-file=$conftree_file",
118 "--manager-attributes-file=$attributes_file",
119 "--conf-constants-file=$constants_file",
120 "--default-values-file=$default_file",
121 "--manager-constants-file=$reconstants_file",
122 "--reverse-tree-file=$reverse_tree_file",
123 "--portal-constants-file=$portal_const_file",
124 "--handler-status-constants-file=$handler_const",
125 "--lang-dir=$langdir",
126 '2>&1' );
127
128 my $output = `$cmd`;
129 my $rc = $? >> 8;
130 ok( $rc == 0, "Script executed successfully (rc=$rc)" )
131 or diag("Output: $output");
132 $count++;
133
134 # Verify output files exist
135 ok( -f $struct_file, 'struct.json created' );
136 ok( -f $conftree_file, 'conftree.js created' );
137 ok( -f $attributes_file, 'Attributes.pm created' );
138 ok( -f $portal_const_file, 'PortalConstants.pm created' );
139 $count += 4;
140
141 # Test 1: Check that the new attribute is in Attributes.pm
142 {
143 open my $fh, '<', $attributes_file or die "Cannot read Attributes.pm: $!";
144 my $content = do { local $/; <$fh> };
145 close $fh;
146
147 like(
148 $content,
149 qr/oidcRPMetaDataOptionsTestParam/,
150 'oidcRPMetaDataOptionsTestParam found in Attributes.pm'
151 );
152 like( $content, qr/testTreeParam/, 'testTreeParam found in Attributes.pm' );
153 $count += 2;
154 }
155
156 # Test 2: Check that the new attribute is in conftree.js (for oidcRP ctree)
157 {
158 open my $fh, '<', $conftree_file or die "Cannot read conftree.js: $!";
159 my $content = do { local $/; <$fh> };
160 close $fh;
161
162 like(
163 $content,
164 qr/oidcRPMetaDataOptionsTestParam/,
165 'oidcRPMetaDataOptionsTestParam found in conftree.js'
166 );
167 $count++;
168
169 # Verify it's in the right position (after oidcRPMetaDataOptionsTokenXAuthorizedRP)
170 my $pos_token =
171 index( $content, 'oidcRPMetaDataOptionsTokenXAuthorizedRP' );
172 my $pos_test = index( $content, 'oidcRPMetaDataOptionsTestParam' );
173 ok(
174 $pos_token > 0 && $pos_test > $pos_token,
175 'oidcRPMetaDataOptionsTestParam is after oidcRPMetaDataOptionsTokenXAuthorizedRP'
176 );
177 $count++;
178 }
179
180 # Test 3: Check that the new tree node is in struct.json
181 {
182 open my $fh, '<', $struct_file or die "Cannot read struct.json: $!";
183 my $content = do { local $/; <$fh> };
184 close $fh;
185
186 like( $content, qr/testPluginNode/, 'testPluginNode found in struct.json' );
187 like( $content, qr/testTreeParam/, 'testTreeParam found in struct.json' );
188 $count += 2;
189 }
190
191 # Test 4: Check that the new constant is in PortalConstants.pm
192 {
193 open my $fh, '<', $portal_const_file
194 or die "Cannot read PortalConstants.pm: $!";
195 my $content = do { local $/; <$fh> };
196 close $fh;
197
198 like( $content, qr/PE_TEST_PLUGIN_ERROR/,
199 'PE_TEST_PLUGIN_ERROR found in PortalConstants.pm' );
200 like( $content, qr/250/, 'Constant value 250 found in PortalConstants.pm' );
201 $count += 2;
202 }
203
204 # Test 5: Check that the default value is in DefaultValues.pm
205 # Note: oidcRPMetaDataOptionsTestParam is a metadata parameter (per-RP),
206 # so it doesn't appear in DefaultValues.pm (only global parameters do)
207 {
208 open my $fh, '<', $default_file or die "Cannot read DefaultValues.pm: $!";
209 my $content = do { local $/; <$fh> };
210 close $fh;
211
212 like(
213 $content,
214 qr/testTreeParam.*default_value/s,
215 'testTreeParam default value found in DefaultValues.pm'
216 );
217 $count++;
218 }
219
220 # Test 5b: Check that select options were appended (not replaced) for
221 # existing attribute "authentication"
222 {
223 open my $fh, '<', $attributes_file or die "Cannot read Attributes.pm: $!";
224 my $content = do { local $/; <$fh> };
225 close $fh;
226
227 # Original core options must still be present
228 like( $content, qr/'k'\s*=>\s*'LDAP'/,
229 'authentication still has original "LDAP" option' );
230 like( $content, qr/'k'\s*=>\s*'DBI'/,
231 'authentication still has original "DBI" option' );
232
233 # New option from extension must have been appended
234 like( $content, qr/'k'\s*=>\s*'MyCustomAuth'/,
235 'authentication has new "MyCustomAuth" option appended by extension' );
236 $count += 3;
237 }
238
239 # Test 6: Check that translations were added to ALL language files
240 {
241 my @lang_files = glob("$langdir/*.json");
242 ok( scalar @lang_files > 0, 'Language files exist in temp dir' );
243 $count++;
244
245 # Expected translations per language code
246 # Languages with specific translations get them; others fall back to English
247 my %expected = (
248 en => {
249 testTreeParam => 'Test tree parameter',
250 oidcRPMetaDataOptionsTestParam => 'Test OIDC parameter',
251 },
252 fr => {
253 testTreeParam => "Param\x{e8}tre de test",
254 oidcRPMetaDataOptionsTestParam => "Param\x{e8}tre OIDC de test",
255 },
256 );
257 my $en_fallback = $expected{en};
258
259 for my $lang_file ( sort @lang_files ) {
260 ( my $lang_code = $lang_file ) =~ s{.*/(.+)\.json$}{$1};
261 open my $fh, '<', $lang_file or die "Cannot read $lang_file: $!";
262 my $content = do { local $/; <$fh> };
263 close $fh;
264 my $lang_data = JSON->new->utf8->decode($content);
265
266 # Use specific expected values if available, otherwise English fallback
267 my $expect = $expected{$lang_code} || $en_fallback;
268
269 for my $key ( sort keys %$expect ) {
270 is( $lang_data->{$key}, $expect->{$key},
271 "[$lang_code] translation for $key" );
272 $count++;
273 }
274 }
275 }
276
277 # Test 7: Test without plugins directory (should work without errors)
278 {
279 my $empty_struct = "$tmpdir/empty_struct.json";
280 my $cmd2 = join( ' ',
281 'perl',
282 '-Ilib',
283 '-I../lemonldap-ng-common/lib',
284 '-I../lemonldap-ng-portal/lib',
285 '-I../lemonldap-ng-handler/lib',
286 $script,
287 "--struct-file=$empty_struct",
288 "--conftree-file=$tmpdir/empty_conftree.js",
289 "--manager-attributes-file=$tmpdir/empty_Attributes.pm",
290 "--conf-constants-file=$tmpdir/empty_Constants.pm",
291 "--default-values-file=$tmpdir/empty_DefaultValues.pm",
292 "--manager-constants-file=$tmpdir/empty_ReConstants.pm",
293 "--reverse-tree-file=$tmpdir/empty_reverseTree.json",
294 "--portal-constants-file=$tmpdir/empty_PortalConstants.pm",
295 "--handler-status-constants-file=$tmpdir/empty_StatusConstants.pm",
296 '2>&1' );
297
298 my $output2 = `$cmd2`;
299 my $rc2 = $? >> 8;
300 ok( $rc2 == 0, 'Script works without --plugins-dir' )
301 or diag("Output: $output2");
302 ok( -f $empty_struct, 'struct.json created without plugins' );
303 $count += 2;
304 }
305
306 # Test 7: Test with non-existent plugins directory (should work without errors)
307 {
308 my $nodir_struct = "$tmpdir/nodir_struct.json";
309 my $cmd3 = join( ' ',
310 'perl',
311 '-Ilib',
312 '-I../lemonldap-ng-common/lib',
313 '-I../lemonldap-ng-portal/lib',
314 '-I../lemonldap-ng-handler/lib',
315 $script,
316 "--plugins-dir=/nonexistent/path",
317 "--struct-file=$nodir_struct",
318 "--conftree-file=$tmpdir/nodir_conftree.js",
319 "--manager-attributes-file=$tmpdir/nodir_Attributes.pm",
320 "--conf-constants-file=$tmpdir/nodir_Constants.pm",
321 "--default-values-file=$tmpdir/nodir_DefaultValues.pm",
322 "--manager-constants-file=$tmpdir/nodir_ReConstants.pm",
323 "--reverse-tree-file=$tmpdir/nodir_reverseTree.json",
324 "--portal-constants-file=$tmpdir/nodir_PortalConstants.pm",
325 "--handler-status-constants-file=$tmpdir/nodir_StatusConstants.pm",
326 '2>&1' );
327
328 my $output3 = `$cmd3`;
329 my $rc3 = $? >> 8;
330 ok( $rc3 == 0, 'Script works with non-existent plugins-dir' )
331 or diag("Output: $output3");
332 ok( -f $nodir_struct, 'struct.json created with non-existent plugins-dir' );
333 $count += 2;
334 }
335
336 done_testing($count);