@@ -14,6 +14,10 @@ import (
1414 "github.com/device-management-toolkit/console/pkg/logger"
1515)
1616
17+ func ptr (s string ) * string {
18+ return & s
19+ }
20+
1721type testUsecase struct {
1822 name string
1923 guid string
@@ -291,8 +295,8 @@ func TestUpdate(t *testing.T) {
291295 GUID : "device-guid-123" ,
292296 TenantID : "tenant-id-456" ,
293297 Password : "encrypted" ,
294- MPSPassword : "encrypted" ,
295- MEBXPassword : "encrypted" ,
298+ MPSPassword : nil ,
299+ MEBXPassword : nil ,
296300 Tags : "hello,test" ,
297301 }
298302
@@ -367,8 +371,8 @@ func TestInsert(t *testing.T) {
367371 device := & entity.Device {
368372 GUID : "device-guid-123" ,
369373 Password : "encrypted" ,
370- MPSPassword : "encrypted" ,
371- MEBXPassword : "encrypted" ,
374+ MPSPassword : nil ,
375+ MEBXPassword : nil ,
372376 TenantID : "tenant-id-456" ,
373377 }
374378
@@ -388,8 +392,8 @@ func TestInsert(t *testing.T) {
388392 device := & entity.Device {
389393 GUID : "device-guid-123" ,
390394 Password : "encrypted" ,
391- MPSPassword : "encrypted" ,
392- MEBXPassword : "encrypted" ,
395+ MPSPassword : nil ,
396+ MEBXPassword : nil ,
393397 TenantID : "tenant-id-456" ,
394398 }
395399
@@ -431,3 +435,169 @@ func TestInsert(t *testing.T) {
431435 })
432436 }
433437}
438+
439+ func TestUpdateWithPasswords (t * testing.T ) {
440+ t .Parallel ()
441+
442+ // Entity with encrypted passwords (what gets stored in DB)
443+ deviceWithPasswords := & entity.Device {
444+ GUID : "device-guid-123" ,
445+ TenantID : "tenant-id-456" ,
446+ Password : "encrypted" ,
447+ MPSPassword : ptr ("encrypted" ),
448+ MEBXPassword : ptr ("encrypted" ),
449+ Tags : "hello,test" ,
450+ }
451+
452+ // DTO with plaintext passwords (what comes from API)
453+ deviceDTOWithPasswords := & dto.Device {
454+ GUID : "device-guid-123" ,
455+ TenantID : "tenant-id-456" ,
456+ Tags : []string {"hello" , "test" },
457+ MPSPassword : "mpspass" ,
458+ MEBXPassword : "mebxpass" ,
459+ }
460+
461+ // Expected DTO result (passwords not returned without includeSecrets)
462+ expectedDTO := & dto.Device {
463+ GUID : "device-guid-123" ,
464+ TenantID : "tenant-id-456" ,
465+ Tags : []string {"hello" , "test" },
466+ MPSPassword : "encrypted" ,
467+ MEBXPassword : "encrypted" ,
468+ }
469+
470+ t .Run ("successful update with passwords" , func (t * testing.T ) {
471+ t .Parallel ()
472+
473+ useCase , repo , management := devicesTest (t )
474+
475+ repo .EXPECT ().
476+ Update (context .Background (), deviceWithPasswords ).
477+ Return (true , nil )
478+ repo .EXPECT ().
479+ GetByID (context .Background (), "device-guid-123" , "tenant-id-456" ).
480+ Return (deviceWithPasswords , nil )
481+ management .EXPECT ().
482+ DestroyWsmanClient (* expectedDTO )
483+
484+ result , err := useCase .Update (context .Background (), deviceDTOWithPasswords )
485+
486+ require .NoError (t , err )
487+ require .Equal (t , expectedDTO , result )
488+ })
489+ }
490+
491+ func TestInsertWithPasswords (t * testing.T ) {
492+ t .Parallel ()
493+
494+ t .Run ("successful insertion with passwords" , func (t * testing.T ) {
495+ t .Parallel ()
496+
497+ useCase , repo , _ := devicesTest (t )
498+
499+ // Entity with encrypted passwords
500+ deviceWithPasswords := & entity.Device {
501+ GUID : "device-guid-123" ,
502+ TenantID : "tenant-id-456" ,
503+ Password : "encrypted" ,
504+ MPSPassword : ptr ("encrypted" ),
505+ MEBXPassword : ptr ("encrypted" ),
506+ }
507+
508+ repo .EXPECT ().
509+ Insert (context .Background (), deviceWithPasswords ).
510+ Return ("unique-device-id" , nil )
511+ repo .EXPECT ().
512+ GetByID (context .Background (), "device-guid-123" , "tenant-id-456" ).
513+ Return (deviceWithPasswords , nil )
514+
515+ // DTO with plaintext passwords
516+ deviceDTO := & dto.Device {
517+ GUID : "device-guid-123" ,
518+ TenantID : "tenant-id-456" ,
519+ Tags : []string {"" },
520+ MPSPassword : "mpspass" ,
521+ MEBXPassword : "mebxpass" ,
522+ }
523+
524+ insertedDevice , err := useCase .Insert (context .Background (), deviceDTO )
525+
526+ require .NoError (t , err )
527+ require .Equal (t , deviceDTO .TenantID , insertedDevice .TenantID )
528+ require .Equal (t , "encrypted" , insertedDevice .MPSPassword )
529+ require .Equal (t , "encrypted" , insertedDevice .MEBXPassword )
530+ })
531+ }
532+
533+ func TestGetByIDWithSecrets (t * testing.T ) {
534+ t .Parallel ()
535+
536+ // Entity with encrypted passwords from DB
537+ deviceWithPasswords := & entity.Device {
538+ GUID : "device-guid-123" ,
539+ TenantID : "tenant-id-456" ,
540+ Password : "encrypted" ,
541+ MPSPassword : ptr ("encrypted" ),
542+ MEBXPassword : ptr ("encrypted" ),
543+ }
544+
545+ // Expected DTO with decrypted passwords
546+ expectedDTO := & dto.Device {
547+ GUID : "device-guid-123" ,
548+ TenantID : "tenant-id-456" ,
549+ Tags : nil ,
550+ Password : "decrypted" ,
551+ MPSPassword : "decrypted" ,
552+ MEBXPassword : "decrypted" ,
553+ }
554+
555+ t .Run ("successful retrieval with secrets" , func (t * testing.T ) {
556+ t .Parallel ()
557+
558+ useCase , repo , _ := devicesTest (t )
559+
560+ repo .EXPECT ().
561+ GetByID (context .Background (), "device-guid-123" , "tenant-id-456" ).
562+ Return (deviceWithPasswords , nil )
563+
564+ got , err := useCase .GetByID (context .Background (), "device-guid-123" , "tenant-id-456" , true )
565+
566+ require .NoError (t , err )
567+ require .Equal (t , expectedDTO , got )
568+ })
569+
570+ t .Run ("retrieval with secrets - nil passwords" , func (t * testing.T ) {
571+ t .Parallel ()
572+
573+ useCase , repo , _ := devicesTest (t )
574+
575+ // Entity with nil passwords
576+ deviceNilPasswords := & entity.Device {
577+ GUID : "device-guid-123" ,
578+ TenantID : "tenant-id-456" ,
579+ Password : "encrypted" ,
580+ MPSPassword : nil ,
581+ MEBXPassword : nil ,
582+ }
583+
584+ repo .EXPECT ().
585+ GetByID (context .Background (), "device-guid-123" , "tenant-id-456" ).
586+ Return (deviceNilPasswords , nil )
587+
588+ // Expected DTO - passwords should be empty strings when nil
589+ expectedDTONilPasswords := & dto.Device {
590+ GUID : "device-guid-123" ,
591+ TenantID : "tenant-id-456" ,
592+ Tags : nil ,
593+ Password : "decrypted" ,
594+ MPSPassword : "" ,
595+ MEBXPassword : "" ,
596+ }
597+
598+ got , err := useCase .GetByID (context .Background (), "device-guid-123" , "tenant-id-456" , true )
599+
600+ require .NoError (t , err )
601+ require .Equal (t , expectedDTONilPasswords , got )
602+ })
603+ }
0 commit comments