@@ -17,9 +17,11 @@ import (
1717 "github.com/ovh/cds/engine/api/authentication"
1818 "github.com/ovh/cds/engine/api/authentication/builtin"
1919 "github.com/ovh/cds/engine/api/group"
20+ "github.com/ovh/cds/engine/api/pipeline"
2021 "github.com/ovh/cds/engine/api/project"
2122 "github.com/ovh/cds/engine/api/test"
2223 "github.com/ovh/cds/engine/api/test/assets"
24+ "github.com/ovh/cds/engine/api/workflow"
2325 "github.com/ovh/cds/sdk"
2426 "github.com/ovh/cds/sdk/cdsclient"
2527)
@@ -428,3 +430,71 @@ func Test_putProjectLabelsHandler(t *testing.T) {
428430 assert .Equal (t , "this is a test" , projReturned .Labels [2 ].Name )
429431 assert .NotZero (t , projReturned .Labels [2 ].Color )
430432}
433+
434+ func Test_getProjectsHandler_FilterByRepo (t * testing.T ) {
435+ api , tsURL , tsClose := newTestServer (t )
436+ defer tsClose ()
437+
438+ admin , _ := assets .InsertAdminUser (t , api .mustDB ())
439+ localConsumer , err := authentication .LoadConsumerByTypeAndUserID (context .TODO (), api .mustDB (), sdk .ConsumerLocal , admin .ID , authentication .LoadConsumerOptions .WithAuthentifiedUser )
440+ require .NoError (t , err )
441+
442+ _ , jws , err := builtin .NewConsumer (context .TODO (), api .mustDB (), sdk .RandomString (10 ), sdk .RandomString (10 ), localConsumer , admin .GetGroupIDs (),
443+ sdk .NewAuthConsumerScopeDetails (sdk .AuthConsumerScopeProject ))
444+
445+ u , _ := assets .InsertLambdaUser (t , api .mustDB ())
446+
447+ pkey := sdk .RandomString (10 )
448+ proj := assets .InsertTestProject (t , api .mustDB (), api .Cache , pkey , pkey )
449+ require .NoError (t , group .InsertLinkGroupUser (context .TODO (), api .mustDB (), & group.LinkGroupUser {
450+ GroupID : proj .ProjectGroups [0 ].Group .ID ,
451+ AuthentifiedUserID : u .ID ,
452+ Admin : true ,
453+ }))
454+
455+ repofullName := sdk .RandomString (10 )
456+
457+ app := & sdk.Application {
458+ Name : sdk .RandomString (10 ),
459+ RepositoryFullname : "ovh/" + repofullName ,
460+ }
461+ require .NoError (t , application .Insert (api .mustDB (), * proj , app ))
462+
463+ pip := sdk.Pipeline {
464+ ProjectID : proj .ID ,
465+ ProjectKey : proj .Key ,
466+ Name : "pip1" ,
467+ }
468+ test .NoError (t , pipeline .InsertPipeline (api .mustDB (), & pip ))
469+
470+ wf := sdk.Workflow {
471+ Name : "workflow1" ,
472+ ProjectID : proj .ID ,
473+ ProjectKey : proj .Key ,
474+ WorkflowData : sdk.WorkflowData {
475+ Node : sdk.Node {
476+ Name : "root" ,
477+ Context : & sdk.NodeContext {
478+ PipelineID : pip .ID ,
479+ ApplicationID : app .ID ,
480+ },
481+ },
482+ },
483+ }
484+ test .NoError (t , workflow .Insert (context .TODO (), api .mustDB (), api .Cache , * proj , & wf ))
485+
486+ // Call with an admin
487+ sdkclientAdmin := cdsclient .New (cdsclient.Config {
488+ Host : tsURL ,
489+ BuitinConsumerAuthenticationToken : jws ,
490+ })
491+
492+ projs , err := sdkclientAdmin .ProjectList (true , true , cdsclient.Filter {Name : "repo" , Value : "ovh/" + repofullName })
493+ require .NoError (t , err )
494+ require .True (t , len (projs ) == 1 )
495+ require .True (t , len (projs [0 ].Workflows ) == 1 )
496+ require .Equal (t , wf .Name , projs [0 ].Workflows [0 ].Name )
497+ require .Equal (t , app .ID , projs [0 ].Workflows [0 ].WorkflowData .Node .Context .ApplicationID )
498+ require .Equal (t , pip .ID , projs [0 ].Workflows [0 ].WorkflowData .Node .Context .PipelineID )
499+
500+ }
0 commit comments