Skip to content

Commit

Permalink
fix(api): getProjectsHandler_FilterByRepo (#5136)
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <francois.samin@corp.ovh.com>
  • Loading branch information
fsamin authored Apr 20, 2020
1 parent a5a1195 commit 382c6a2
Showing 2 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions engine/api/project.go
Original file line number Diff line number Diff line change
@@ -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)

70 changes: 70 additions & 0 deletions engine/api/project_test.go
Original file line number Diff line number Diff line change
@@ -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)

}

0 comments on commit 382c6a2

Please sign in to comment.