Skip to content

Commit

Permalink
configstore: fixed DeleteOrg action to accept orgRef name and id
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandro-sorint committed Sep 23, 2022
1 parent 37c4758 commit cff0f9a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/services/configstore/action/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (h *ActionHandler) DeleteOrg(ctx context.Context, orgRef string) error {
err := h.d.Do(ctx, func(tx *sql.Tx) error {
var err error
// check org existance
org, err = h.d.GetOrgByName(tx, orgRef)
org, err = h.d.GetOrg(tx, orgRef)
if err != nil {
return errors.WithStack(err)
}
Expand Down
76 changes: 76 additions & 0 deletions internal/services/configstore/configstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1191,3 +1191,79 @@ func TestRemoteSource(t *testing.T) {
})
}
}

func TestDeleteOrg(t *testing.T) {
dir := t.TempDir()
log := testutil.NewLogger(t)

tests := []struct {
name string
f func(ctx context.Context, t *testing.T, cs *Configstore)
}{
{
name: "test delete org by id",
f: func(ctx context.Context, t *testing.T, cs *Configstore) {
orgs, err := getOrgs(ctx, cs)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}

if err := cs.ah.DeleteOrg(ctx, orgs[0].ID); err != nil {
t.Fatalf("unexpected err: %v", err)
}

orgs, err = getOrgs(ctx, cs)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if len(orgs) != 0 {
t.Fatalf("expected 0 orgs, got %d orgs", len(orgs))
}
},
},
{
name: "test delete org by name",
f: func(ctx context.Context, t *testing.T, cs *Configstore) {
orgs, err := getOrgs(ctx, cs)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}

if err := cs.ah.DeleteOrg(ctx, orgs[0].Name); err != nil {
t.Fatalf("unexpected err: %v", err)
}

orgs, err = getOrgs(ctx, cs)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if len(orgs) != 0 {
t.Fatalf("expected 0 orgs, got %d orgs", len(orgs))
}
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir, err := ioutil.TempDir(dir, "agola")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
ctx := context.Background()

cs := setupConfigstore(ctx, t, log, dir)

t.Logf("starting cs")

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

go func() { _ = cs.Run(ctx) }()

tt.f(ctx, t, cs)
})
}
}

0 comments on commit cff0f9a

Please sign in to comment.