Skip to content

Commit

Permalink
Merge pull request #129 from sgotti/configstore_disable_root_projectg…
Browse files Browse the repository at this point in the history
…roup_deletion

configstore: disable root project group deletion
  • Loading branch information
sgotti authored Oct 1, 2019
2 parents d76b77b + e2a0fed commit b6d038d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 11 deletions.
6 changes: 6 additions & 0 deletions internal/services/configstore/action/projectgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ func (h *ActionHandler) DeleteProjectGroup(ctx context.Context, projectGroupRef
return util.NewErrBadRequest(errors.Errorf("project group %q doesn't exist", projectGroupRef))
}

// cannot delete root project group
if projectGroup.Parent.Type == types.ConfigTypeOrg ||
projectGroup.Parent.Type == types.ConfigTypeUser {
return util.NewErrBadRequest(errors.Errorf("cannot delete root project group"))
}

// changegroup is the project group id.
cgNames := []string{util.EncodeSha256Hex(projectGroup.ID)}
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, cgNames)
Expand Down
74 changes: 63 additions & 11 deletions internal/services/configstore/configstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func TestProjectGroupsAndProjectsCreate(t *testing.T) {

ctx := context.Background()

cs, tetcd := setupConfigstore(ctx,t, dir)
cs, tetcd := setupConfigstore(ctx, t, dir)
defer shutdownEtcd(tetcd)

t.Logf("starting cs")
Expand Down Expand Up @@ -764,7 +764,7 @@ func TestProjectUpdate(t *testing.T) {

ctx := context.Background()

cs, tetcd := setupConfigstore(ctx,t, dir)
cs, tetcd := setupConfigstore(ctx, t, dir)
defer shutdownEtcd(tetcd)

t.Logf("starting cs")
Expand Down Expand Up @@ -840,7 +840,63 @@ func TestProjectGroupDelete(t *testing.T) {

ctx := context.Background()

cs, tetcd := setupConfigstore(ctx,t, dir)
cs, tetcd := setupConfigstore(ctx, t, dir)
defer shutdownEtcd(tetcd)

t.Logf("starting cs")
go func() {
_ = cs.Run(ctx)
}()

// TODO(sgotti) change the sleep with a real check that all is ready
time.Sleep(2 * time.Second)

org, err := cs.ah.CreateOrg(ctx, &types.Organization{Name: "org01", Visibility: types.VisibilityPublic})
if err != nil {
t.Fatalf("unexpected err: %v", err)
}

// TODO(sgotti) change the sleep with a real check that user is in readdb
time.Sleep(2 * time.Second)

// create a projectgroup in org root project group
pg01, err := cs.ah.CreateProjectGroup(ctx, &types.ProjectGroup{Name: "projectgroup01", Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: path.Join("org", org.Name)}, Visibility: types.VisibilityPublic})
if err != nil {
t.Fatalf("unexpected err: %v", err)
}

// create a child projectgroup in org root project group
_, err = cs.ah.CreateProjectGroup(ctx, &types.ProjectGroup{Name: "subprojectgroup01", Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: pg01.ID}, Visibility: types.VisibilityPublic})
if err != nil {
t.Fatalf("unexpected err: %v", err)
}

t.Run("delete root project group", func(t *testing.T) {
expectedErr := "cannot delete root project group"
err := cs.ah.DeleteProjectGroup(ctx, path.Join("org", org.Name))
if err.Error() != expectedErr {
t.Fatalf("expected err %v, got err: %v", expectedErr, err)
}
})

t.Run("delete project group", func(t *testing.T) {
err := cs.ah.DeleteProjectGroup(ctx, pg01.ID)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
})
}

func TestProjectGroupDeleteDontSeeOldChildObjects(t *testing.T) {
dir, err := ioutil.TempDir("", "agola")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)

ctx := context.Background()

cs, tetcd := setupConfigstore(ctx, t, dir)
defer shutdownEtcd(tetcd)

t.Logf("starting cs")
Expand All @@ -851,10 +907,6 @@ func TestProjectGroupDelete(t *testing.T) {
// TODO(sgotti) change the sleep with a real check that all is ready
time.Sleep(2 * time.Second)

//user, err := cs.ah.CreateUser(ctx, &action.CreateUserRequest{UserName: "user01"})
//if err != nil {
// t.Fatalf("unexpected err: %v", err)
//}
org, err := cs.ah.CreateOrg(ctx, &types.Organization{Name: "org01", Visibility: types.VisibilityPublic})
if err != nil {
t.Fatalf("unexpected err: %v", err)
Expand All @@ -869,7 +921,7 @@ func TestProjectGroupDelete(t *testing.T) {
t.Fatalf("unexpected err: %v", err)
}

//create a child projectgroup in org root project group
// create a child projectgroup in org root project group
spg01, err := cs.ah.CreateProjectGroup(ctx, &types.ProjectGroup{Name: "subprojectgroup01", Parent: types.Parent{Type: types.ConfigTypeProjectGroup, ID: pg01.ID}, Visibility: types.VisibilityPublic})
if err != nil {
t.Fatalf("unexpected err: %v", err)
Expand All @@ -892,7 +944,7 @@ func TestProjectGroupDelete(t *testing.T) {
t.Fatalf("unexpected err: %v", err)
}

// delete root projectgroup
// delete projectgroup
if err = cs.ah.DeleteProjectGroup(ctx, pg01.ID); err != nil {
t.Fatalf("unexpected err: %v", err)
}
Expand Down Expand Up @@ -979,7 +1031,7 @@ func TestOrgMembers(t *testing.T) {

ctx := context.Background()

cs, tetcd := setupConfigstore(ctx,t, dir)
cs, tetcd := setupConfigstore(ctx, t, dir)
defer shutdownEtcd(tetcd)

t.Logf("starting cs")
Expand Down Expand Up @@ -1215,7 +1267,7 @@ func TestRemoteSource(t *testing.T) {
}
ctx := context.Background()

cs, tetcd := setupConfigstore(ctx,t, dir)
cs, tetcd := setupConfigstore(ctx, t, dir)
defer shutdownEtcd(tetcd)

t.Logf("starting cs")
Expand Down

0 comments on commit b6d038d

Please sign in to comment.