@@ -32,17 +32,14 @@ final class AEngineHandlerTest extends \OCA\Libresign\Tests\Unit\TestCase {
3232
3333 public function setUp (): void {
3434 $ this ->config = \OCP \Server::get (IConfig::class);
35- $ this ->appConfig = $ this ->getMockAppConfig ();
35+ $ this ->appConfig = $ this ->getMockAppConfigWithReset ();
3636 $ this ->appDataFactory = \OCP \Server::get (IAppDataFactory::class);
3737 $ this ->dateTimeFormatter = \OCP \Server::get (IDateTimeFormatter::class);
3838 $ this ->tempManager = \OCP \Server::get (ITempManager::class);
3939 $ this ->certificatePolicyService = \OCP \Server::get (CertificatePolicyService::class);
4040 $ this ->urlGenerator = \OCP \Server::get (IURLGenerator::class);
4141 $ this ->caIdentifierService = \OCP \Server::get (CaIdentifierService::class);
4242 $ this ->logger = \OCP \Server::get (LoggerInterface::class);
43-
44- $ this ->appConfig ->deleteKey (Application::APP_ID , 'certificate_engine ' );
45- $ this ->appConfig ->deleteKey (Application::APP_ID , 'identify_methods ' );
4643 }
4744
4845 private function getInstance (): OpenSslHandler {
@@ -232,4 +229,181 @@ public static function dataProviderIdentifyMethodsOtherEngines(): array {
232229 'First time cfssl ' => ['' , 'cfssl ' , null , 'no previous config ' ],
233230 ];
234231 }
232+
233+ #[DataProvider('dataProviderToArray ' )]
234+ public function testToArrayReturnsExpectedStructure (
235+ bool $ isSetupOk ,
236+ array $ certificateData ,
237+ array $ expectedKeys ,
238+ string $ description ,
239+ ): void {
240+ $ instance = $ this ->getInstance ();
241+
242+ foreach ($ certificateData as $ setter => $ value ) {
243+ $ method = 'set ' . ucfirst ($ setter );
244+ if (method_exists ($ instance , $ method )) {
245+ $ instance ->$ method ($ value );
246+ }
247+ }
248+
249+ if (!$ isSetupOk ) {
250+ $ this ->appConfig ->deleteKey (Application::APP_ID , 'config_path ' );
251+ }
252+
253+ $ result = $ instance ->toArray ();
254+
255+ foreach ($ expectedKeys as $ key ) {
256+ $ this ->assertArrayHasKey ($ key , $ result , "toArray() should contain ' $ key': $ description " );
257+ }
258+
259+ $ this ->assertIsBool ($ result ['generated ' ], "generated should be boolean: $ description " );
260+ }
261+
262+ #[DataProvider('dataProviderToArrayConfigPath ' )]
263+ public function testToArrayFiltersConfigPathWhenNotGenerated (
264+ bool $ certificateGenerated ,
265+ string $ expectedConfigPath ,
266+ string $ description ,
267+ ): void {
268+ $ instance = $ this ->getInstance ();
269+
270+ $ tempPath = $ this ->tempManager ->getTemporaryFolder ('test-config ' );
271+ $ instance ->setConfigPath ($ tempPath );
272+
273+ if ($ certificateGenerated ) {
274+ file_put_contents ($ tempPath . DIRECTORY_SEPARATOR . 'ca.pem ' , 'fake-cert ' );
275+ file_put_contents ($ tempPath . DIRECTORY_SEPARATOR . 'ca-key.pem ' , 'fake-key ' );
276+ }
277+
278+ $ result = $ instance ->toArray ();
279+
280+ if ($ expectedConfigPath === '' ) {
281+ $ this ->assertEmpty ($ result ['configPath ' ], "configPath should be empty: $ description " );
282+ } else {
283+ $ this ->assertNotEmpty ($ result ['configPath ' ], "configPath should not be empty: $ description " );
284+ }
285+
286+ $ this ->assertEquals ($ certificateGenerated , $ result ['generated ' ], "generated flag: $ description " );
287+ }
288+
289+ #[DataProvider('dataProviderToArrayCaIdFiltering ' )]
290+ public function testToArrayFiltersCaIdFromOrganizationalUnitWhenNotGenerated (
291+ bool $ certificateGenerated ,
292+ array $ organizationalUnits ,
293+ array $ expectedOuValues ,
294+ string $ description ,
295+ ): void {
296+ $ instance = $ this ->getInstance ();
297+
298+ $ tempPath = $ this ->tempManager ->getTemporaryFolder ('test-config ' );
299+ $ instance ->setConfigPath ($ tempPath );
300+ $ instance ->setOrganizationalUnit ($ organizationalUnits );
301+ $ instance ->setCountry ('BR ' );
302+
303+ if ($ certificateGenerated ) {
304+ file_put_contents ($ tempPath . DIRECTORY_SEPARATOR . 'ca.pem ' , 'fake-cert ' );
305+ file_put_contents ($ tempPath . DIRECTORY_SEPARATOR . 'ca-key.pem ' , 'fake-key ' );
306+ }
307+
308+ $ result = $ instance ->toArray ();
309+
310+ $ ouFound = null ;
311+ foreach ($ result ['rootCert ' ]['names ' ] as $ name ) {
312+ if ($ name ['id ' ] === 'OU ' ) {
313+ $ ouFound = $ name ['value ' ];
314+ break ;
315+ }
316+ }
317+
318+ if (!empty ($ expectedOuValues )) {
319+ $ this ->assertNotNull ($ ouFound , "OU should be present in names array: $ description " );
320+ $ this ->assertEquals (
321+ $ expectedOuValues ,
322+ $ ouFound ,
323+ "OrganizationalUnit filtering: $ description "
324+ );
325+ } else {
326+ $ this ->assertNull ($ ouFound , "OU should not be present when filtered to empty: $ description " );
327+ }
328+ }
329+
330+ public static function dataProviderToArray (): array {
331+ return [
332+ 'Basic structure with minimal data ' => [
333+ false ,
334+ ['commonName ' => 'Test CA ' ],
335+ ['configPath ' , 'generated ' , 'rootCert ' , 'policySection ' ],
336+ 'minimal certificate data ' ,
337+ ],
338+ 'Complete certificate data ' => [
339+ false ,
340+ [
341+ 'commonName ' => 'LibreSign CA ' ,
342+ 'country ' => 'BR ' ,
343+ 'state ' => 'Santa Catarina ' ,
344+ 'locality ' => 'Florianópolis ' ,
345+ 'organization ' => 'LibreCode ' ,
346+ 'organizationalUnit ' => ['Engineering ' , 'Security ' ],
347+ ],
348+ ['configPath ' , 'generated ' , 'rootCert ' , 'policySection ' ],
349+ 'full certificate data ' ,
350+ ],
351+ ];
352+ }
353+
354+ public static function dataProviderToArrayConfigPath (): array {
355+ return [
356+ 'Certificate not generated ' => [
357+ false ,
358+ '' ,
359+ 'configPath should be empty when certificate not generated ' ,
360+ ],
361+ 'Certificate generated ' => [
362+ true ,
363+ '/path/to/config ' ,
364+ 'configPath should be set when certificate is generated ' ,
365+ ],
366+ ];
367+ }
368+
369+ public static function dataProviderToArrayCaIdFiltering (): array {
370+ return [
371+ 'OU without CA ID - not generated ' => [
372+ false ,
373+ ['Engineering ' , 'Security ' ],
374+ ['Engineering ' , 'Security ' ],
375+ 'normal OU values should pass through when not generated ' ,
376+ ],
377+ 'OU without CA ID - generated ' => [
378+ true ,
379+ ['Engineering ' , 'Security ' ],
380+ ['Engineering ' , 'Security ' ],
381+ 'normal OU values should pass through when generated ' ,
382+ ],
383+ 'OU with CA ID - not generated (filtered) ' => [
384+ false ,
385+ ['libresign-ca-id:abc123_g:1_e:openssl ' , 'Engineering ' , 'Security ' ],
386+ ['Engineering ' , 'Security ' ],
387+ 'CA ID should be filtered when certificate not generated ' ,
388+ ],
389+ 'OU with CA ID - generated (kept) ' => [
390+ true ,
391+ ['libresign-ca-id:abc123_g:1_e:openssl ' , 'Engineering ' , 'Security ' ],
392+ ['libresign-ca-id:abc123_g:1_e:openssl ' , 'Engineering ' , 'Security ' ],
393+ 'CA ID should be kept when certificate is generated ' ,
394+ ],
395+ 'OU with only CA ID - not generated ' => [
396+ false ,
397+ ['libresign-ca-id:abc123_g:1_e:openssl ' ],
398+ [],
399+ 'OU should be empty when only CA ID and not generated ' ,
400+ ],
401+ 'OU with only CA ID - generated ' => [
402+ true ,
403+ ['libresign-ca-id:abc123_g:1_e:openssl ' ],
404+ ['libresign-ca-id:abc123_g:1_e:openssl ' ],
405+ 'OU with only CA ID should be kept when generated ' ,
406+ ],
407+ ];
408+ }
235409}
0 commit comments