Skip to content

Commit

Permalink
fix(api): import workflow with perms on node (#5451)
Browse files Browse the repository at this point in the history
Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored Sep 28, 2020
1 parent 453d24e commit 283a7fe
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
8 changes: 4 additions & 4 deletions engine/api/workflow/workflow_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ func Import(ctx context.Context, db gorpmapper.SqlExecutorWithTx, store cache.St
DisableHookManagement: w.DerivationBranch != "",
}

if err := Update(ctx, db, store, proj, w, uptOptions); err != nil {
return sdk.WrapError(err, "Unable to update workflow")
}

if err := importWorkflowGroups(db, w); err != nil {
return err
}

if err := Update(ctx, db, store, proj, w, uptOptions); err != nil {
return sdk.WrapError(err, "Unable to update workflow")
}

if msgChan != nil {
msgChan <- sdk.NewMessage(sdk.MsgWorkflowImportedUpdated, w.Name)
}
Expand Down
69 changes: 69 additions & 0 deletions engine/api/workflow_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,75 @@ metadata:
assert.Equal(t, "git.branch,git.author,git.hash", m["Workflow.Metadata.default_tags"])
}

func Test_postWorkflowImportWithPermissionHandler(t *testing.T) {
api, db, _ := newTestAPI(t)

u, pass := assets.InsertAdminUser(t, db)
g1 := assets.InsertTestGroup(t, db, "b-"+sdk.RandomString(10))
g2 := assets.InsertTestGroup(t, db, "c-"+sdk.RandomString(10))

proj := assets.InsertTestProject(t, db, api.Cache, sdk.RandomString(10), "a-"+sdk.RandomString(10))
require.NoError(t, group.InsertLinkGroupProject(context.TODO(), db, &group.LinkGroupProject{
GroupID: g1.ID,
ProjectID: proj.ID,
Role: sdk.PermissionReadExecute,
}))
require.NoError(t, group.InsertLinkGroupProject(context.TODO(), db, &group.LinkGroupProject{
GroupID: g2.ID,
ProjectID: proj.ID,
Role: sdk.PermissionReadExecute,
}))

pip := sdk.Pipeline{
ProjectID: proj.ID,
ProjectKey: proj.Key,
Name: "pip1",
}
require.NoError(t, pipeline.InsertPipeline(db, &pip))

//Prepare request
vars := map[string]string{
"permProjectKey": proj.Key,
}
uri := api.Router.GetRoute("POST", api.postWorkflowImportHandler, vars)
test.NotEmpty(t, uri)
req := assets.NewAuthentifiedRequest(t, u, pass, "POST", uri, nil)

body := `name: test2
version: v2.0
workflow:
test:
pipeline: pip1
permissions:
` + g1.Name + `: 7
permissions:
` + g1.Name + `: 5
` + g2.Name + `: 7`
req.Body = ioutil.NopCloser(strings.NewReader(body))
req.Header.Set("Content-Type", "application/x-yaml")

//Do the request
rec := httptest.NewRecorder()
api.Router.Mux.ServeHTTP(rec, req)
assert.Equal(t, 200, rec.Code)

//Check result
t.Logf(">>%s", rec.Body.String())

w, err := workflow.Load(context.TODO(), db, api.Cache, *proj, "test2", workflow.LoadOptions{})
test.NoError(t, err)

assert.NotNil(t, w)

m, _ := dump.ToStringMap(w)
t.Logf("%+v", m)
assert.Equal(t, "test2", m["Workflow.Name"])
assert.Equal(t, "test", m["Workflow.WorkflowData.Node.Name"])
assert.Equal(t, "pip1", m["Workflow.WorkflowData.Node.Context.PipelineName"])
assert.Equal(t, "7", m["Workflow.WorkflowData.Node.Groups.Groups0.Permission"])
assert.Equal(t, g1.Name, m["Workflow.WorkflowData.Node.Groups.Groups0.Group.Name"])
}

func Test_postWorkflowImportHandlerWithExistingIcon(t *testing.T) {
api, db, _ := newTestAPI(t)

Expand Down

0 comments on commit 283a7fe

Please sign in to comment.