@@ -379,6 +379,7 @@ func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImpo
379
379
}
380
380
381
381
func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImportWithRegen (t * testing.T ) {
382
+ // init project and application for test
382
383
api , db , _ , end := newTestAPI (t )
383
384
defer end ()
384
385
@@ -391,35 +392,61 @@ func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImpo
391
392
}
392
393
test .NoError (t , application .Insert (db , api .Cache , proj , app , u ))
393
394
394
- k := & sdk.ApplicationKey {
395
+ // create password, pgp and ssh keys
396
+ k1 := & sdk.ApplicationKey {
395
397
Key : sdk.Key {
396
- Name : "app-mykey " ,
398
+ Name : "app-key-1 " ,
397
399
Type : "pgp" ,
398
400
},
399
401
ApplicationID : app .ID ,
400
402
}
401
403
402
- kpgp , err := keys .GeneratePGPKeyPair (k .Name )
404
+ kpgp , err := keys .GeneratePGPKeyPair (k1 .Name )
403
405
test .NoError (t , err )
404
- k .Public = kpgp .Public
405
- k .Private = kpgp .Private
406
- k .KeyID = kpgp .KeyID
407
- if err := application .InsertKey (api .mustDB (), k ); err != nil {
408
- t .Fatal (err )
406
+ k1 .Public = kpgp .Public
407
+ k1 .Private = kpgp .Private
408
+ k1 .KeyID = kpgp .KeyID
409
+ test .NoError (t , application .InsertKey (api .mustDB (), k1 ))
410
+
411
+ // create password, pgp and ssh keys
412
+ k2 := & sdk.ApplicationKey {
413
+ Key : sdk.Key {
414
+ Name : "app-key-2" ,
415
+ Type : "ssh" ,
416
+ },
417
+ ApplicationID : app .ID ,
409
418
}
410
419
420
+ kssh , err := keys .GenerateSSHKey (k2 .Name )
421
+ test .NoError (t , err )
422
+ k2 .Public = kssh .Public
423
+ k2 .Private = kssh .Private
424
+ k2 .KeyID = kssh .KeyID
425
+ test .NoError (t , application .InsertKey (api .mustDB (), k2 ))
426
+
411
427
test .NoError (t , application .InsertVariable (api .mustDB (), api .Cache , app , sdk.Variable {
412
428
Name : "myPassword" ,
413
429
Type : sdk .SecretVariable ,
414
430
Value : "MySecretValue" ,
415
431
}, u ))
416
432
417
- //Export all the things
418
- vars := map [string ]string {
433
+ // check that keys secrets are well stored
434
+ app , err = application .LoadByName (db , api .Cache , proj .Key , "myNewApp" , nil ,
435
+ application .LoadOptions .WithClearKeys ,
436
+ application .LoadOptions .WithVariablesWithClearPassword ,
437
+ )
438
+ test .NoError (t , err )
439
+ test .Equal (t , 1 , len (app .Variable ))
440
+ test .Equal (t , "MySecretValue" , app .Variable [0 ].Value )
441
+ test .Equal (t , 2 , len (app .Keys ))
442
+ test .Equal (t , kpgp .Private , app .Keys [0 ].Private )
443
+ test .Equal (t , kssh .Private , app .Keys [1 ].Private )
444
+
445
+ // export the app then import it with regen false
446
+ uri := api .Router .GetRoute ("GET" , api .getApplicationExportHandler , map [string ]string {
419
447
"key" : proj .Key ,
420
448
"permApplicationName" : app .Name ,
421
- }
422
- uri := api .Router .GetRoute ("GET" , api .getApplicationExportHandler , vars )
449
+ })
423
450
test .NotEmpty (t , uri )
424
451
req := assets .NewAuthentifiedRequest (t , u , pass , "GET" , uri , nil )
425
452
@@ -434,44 +461,51 @@ func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImpo
434
461
435
462
eapp := & exportentities.Application {}
436
463
test .NoError (t , yaml .Unmarshal ([]byte (body ), eapp ))
464
+ test .Equal (t , 1 , len (eapp .Variables ))
465
+ test .Equal (t , 2 , len (eapp .Keys ))
437
466
438
467
False := false
439
- ek := eapp .Keys [k .Name ]
440
- ek .Regen = & False
441
- ek .Value = ""
442
- eapp .Keys [k .Name ] = ek
468
+ ek1 := eapp .Keys [k1 .Name ]
469
+ ek1 .Regen = & False
470
+ ek1 .Value = ""
471
+ eapp .Keys [k1 .Name ] = ek1
472
+
473
+ ek2 := eapp .Keys [k2 .Name ]
474
+ ek2 .Regen = & False
475
+ ek2 .Value = ""
476
+ eapp .Keys [k2 .Name ] = ek2
443
477
444
478
btes , err := yaml .Marshal (eapp )
445
479
body = string (btes )
446
480
447
481
t .Log (body )
448
482
449
- //Import the new application
450
- vars = map [string ]string {
483
+ // import the new application then check secrets values.
484
+ uri = api . Router . GetRoute ( "POST" , api . postApplicationImportHandler , map [string ]string {
451
485
"permProjectKey" : proj .Key ,
452
- }
453
- uri = api .Router .GetRoute ("POST" , api .postApplicationImportHandler , vars )
486
+ })
454
487
test .NotEmpty (t , uri )
455
488
uri += "?force=true"
456
489
req = assets .NewAuthentifiedRequest (t , u , pass , "POST" , uri , nil )
457
490
req .Body = ioutil .NopCloser (strings .NewReader (body ))
458
491
req .Header .Set ("Content-Type" , "application/x-yaml" )
459
492
460
- //Do the request
461
493
rec = httptest .NewRecorder ()
462
494
api .Router .Mux .ServeHTTP (rec , req )
463
495
assert .Equal (t , 200 , rec .Code )
464
496
465
- //Check result
466
497
t .Logf (">>%s" , rec .Body .String ())
467
498
468
- app , err = application .LoadByName (db , api .Cache , proj .Key , "myNewApp" , nil , application .LoadOptions .WithKeys , application .LoadOptions .WithVariablesWithClearPassword )
499
+ app , err = application .LoadByName (db , api .Cache , proj .Key , "myNewApp" , nil ,
500
+ application .LoadOptions .WithClearKeys ,
501
+ application .LoadOptions .WithVariablesWithClearPassword ,
502
+ )
469
503
test .NoError (t , err )
470
- //Check keys
471
- for _ , k := range app .Keys {
472
- assert . NotEmpty (t , k . Private )
473
- assert . NotEmpty (t , k . Public )
474
- }
504
+ test . Equal ( t , 1 , len ( app . Variable ))
505
+ test . Equal ( t , "MySecretValue" , app .Variable [ 0 ]. Value )
506
+ test . Equal (t , 2 , len ( app . Keys ) )
507
+ test . Equal (t , kpgp . Private , app . Keys [ 0 ]. Private )
508
+ test . Equal ( t , kssh . Private , app . Keys [ 1 ]. Private )
475
509
}
476
510
477
511
func Test_postApplicationImportHandler_NewAppFromYAMLWithEmptyKey (t * testing.T ) {
0 commit comments