From 382c6a2bf85cb08102adb2ed0e0e31452c81c9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Samin?= Date: Mon, 20 Apr 2020 15:26:01 +0200 Subject: [PATCH] fix(api): getProjectsHandler_FilterByRepo (#5136) Signed-off-by: francois samin --- engine/api/project.go | 2 ++ engine/api/project_test.go | 70 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/engine/api/project.go b/engine/api/project.go index 4089053afe..907c651f64 100644 --- a/engine/api/project.go +++ b/engine/api/project.go @@ -65,6 +65,8 @@ func (api *API) getProjectsHandler_FilterByRepo(ctx context.Context, w http.Resp opts := []project.LoadOptionFunc{ project.LoadOptions.WithPermission, + project.LoadOptions.WithApplications, + project.LoadOptions.WithWorkflows, } opts = append(opts, filterByRepoFunc) diff --git a/engine/api/project_test.go b/engine/api/project_test.go index 878aec4d63..3ae99cdb0d 100644 --- a/engine/api/project_test.go +++ b/engine/api/project_test.go @@ -17,9 +17,11 @@ import ( "github.com/ovh/cds/engine/api/authentication" "github.com/ovh/cds/engine/api/authentication/builtin" "github.com/ovh/cds/engine/api/group" + "github.com/ovh/cds/engine/api/pipeline" "github.com/ovh/cds/engine/api/project" "github.com/ovh/cds/engine/api/test" "github.com/ovh/cds/engine/api/test/assets" + "github.com/ovh/cds/engine/api/workflow" "github.com/ovh/cds/sdk" "github.com/ovh/cds/sdk/cdsclient" ) @@ -428,3 +430,71 @@ func Test_putProjectLabelsHandler(t *testing.T) { assert.Equal(t, "this is a test", projReturned.Labels[2].Name) assert.NotZero(t, projReturned.Labels[2].Color) } + +func Test_getProjectsHandler_FilterByRepo(t *testing.T) { + api, tsURL, tsClose := newTestServer(t) + defer tsClose() + + admin, _ := assets.InsertAdminUser(t, api.mustDB()) + localConsumer, err := authentication.LoadConsumerByTypeAndUserID(context.TODO(), api.mustDB(), sdk.ConsumerLocal, admin.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser) + require.NoError(t, err) + + _, jws, err := builtin.NewConsumer(context.TODO(), api.mustDB(), sdk.RandomString(10), sdk.RandomString(10), localConsumer, admin.GetGroupIDs(), + sdk.NewAuthConsumerScopeDetails(sdk.AuthConsumerScopeProject)) + + u, _ := assets.InsertLambdaUser(t, api.mustDB()) + + pkey := sdk.RandomString(10) + proj := assets.InsertTestProject(t, api.mustDB(), api.Cache, pkey, pkey) + require.NoError(t, group.InsertLinkGroupUser(context.TODO(), api.mustDB(), &group.LinkGroupUser{ + GroupID: proj.ProjectGroups[0].Group.ID, + AuthentifiedUserID: u.ID, + Admin: true, + })) + + repofullName := sdk.RandomString(10) + + app := &sdk.Application{ + Name: sdk.RandomString(10), + RepositoryFullname: "ovh/" + repofullName, + } + require.NoError(t, application.Insert(api.mustDB(), *proj, app)) + + pip := sdk.Pipeline{ + ProjectID: proj.ID, + ProjectKey: proj.Key, + Name: "pip1", + } + test.NoError(t, pipeline.InsertPipeline(api.mustDB(), &pip)) + + wf := sdk.Workflow{ + Name: "workflow1", + ProjectID: proj.ID, + ProjectKey: proj.Key, + WorkflowData: sdk.WorkflowData{ + Node: sdk.Node{ + Name: "root", + Context: &sdk.NodeContext{ + PipelineID: pip.ID, + ApplicationID: app.ID, + }, + }, + }, + } + test.NoError(t, workflow.Insert(context.TODO(), api.mustDB(), api.Cache, *proj, &wf)) + + // Call with an admin + sdkclientAdmin := cdsclient.New(cdsclient.Config{ + Host: tsURL, + BuitinConsumerAuthenticationToken: jws, + }) + + projs, err := sdkclientAdmin.ProjectList(true, true, cdsclient.Filter{Name: "repo", Value: "ovh/" + repofullName}) + require.NoError(t, err) + require.True(t, len(projs) == 1) + require.True(t, len(projs[0].Workflows) == 1) + require.Equal(t, wf.Name, projs[0].Workflows[0].Name) + require.Equal(t, app.ID, projs[0].Workflows[0].WorkflowData.Node.Context.ApplicationID) + require.Equal(t, pip.ID, projs[0].Workflows[0].WorkflowData.Node.Context.PipelineID) + +}