Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
fix remove group (closes #29)
Browse files Browse the repository at this point in the history
  • Loading branch information
gerson24 committed Sep 23, 2016
1 parent b46dd28 commit ba3a526
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
14 changes: 11 additions & 3 deletions database/postgresql/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ func (g PostgresRepo) UpdateGroup(group api.Group) (*api.Group, error) {
}

func (g PostgresRepo) RemoveGroup(id string) error {

transaction := g.Dbmap.Begin()

// Delete group
transaction.Where("id like ?", id).Delete(&Group{})

// Error handling
if err := transaction.Error; err != nil {
transaction.Rollback()
return &database.Error{
Expand All @@ -164,8 +164,16 @@ func (g PostgresRepo) RemoveGroup(id string) error {

// Delete all group relations
transaction.Where("group_id like ?", id).Delete(&GroupUserRelation{})
if err := transaction.Error; err != nil {
transaction.Rollback()
return &database.Error{
Code: database.INTERNAL_ERROR,
Message: err.Error(),
}

// Error handling
}
// Delete all policy relations
transaction.Where("group_id like ?", id).Delete(&GroupPolicyRelation{})
if err := transaction.Error; err != nil {
transaction.Rollback()
return &database.Error{
Expand Down
69 changes: 57 additions & 12 deletions database/postgresql/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,16 +695,20 @@ func TestPostgresRepo_UpdateGroup(t *testing.T) {
}

func TestPostgresRepo_RemoveGroup(t *testing.T) {
type relation struct {
userID string
groupID string
groupNotFound bool
type userRelation struct {
userID string
groupID string
}
type policyRelation struct {
policyID string
groupID string
}
now := time.Now().UTC()
testcases := map[string]struct {
// Previous data
previousGroups []Group
relations []relation
previousGroups []Group
userRelations []userRelation
policyRelations []policyRelation
// Postgres Repo Args
groupToDelete string
}{
Expand All @@ -729,23 +733,34 @@ func TestPostgresRepo_RemoveGroup(t *testing.T) {
Org: "Org",
},
},
relations: []relation{
userRelations: []userRelation{
{
userID: "UserID",
groupID: "GroupID",
},
{
userID: "UserID",
userID: "UserID1",
groupID: "GroupID2",
},
},
policyRelations: []policyRelation{
{
policyID: "policyID",
groupID: "GroupID",
},
{
policyID: "policyID1",
groupID: "GroupID2",
},
},
groupToDelete: "GroupID",
},
}

for n, test := range testcases {
cleanGroupTable()
cleanGroupUserRelationTable()
cleanGroupPolicyRelationTable()

// Insert previous data
if test.previousGroups != nil {
Expand All @@ -756,14 +771,22 @@ func TestPostgresRepo_RemoveGroup(t *testing.T) {
}
}
}
if test.relations != nil {
for _, rel := range test.relations {
if test.userRelations != nil {
for _, rel := range test.userRelations {
if err := insertGroupUserRelation(rel.userID, rel.groupID); err != nil {
t.Errorf("Test %v failed. Unexpected error inserting previous group user relations: %v", n, err)
continue
}
}
}
if test.policyRelations != nil {
for _, rel := range test.policyRelations {
if err := insertGroupPolicyRelation(rel.groupID, rel.policyID); err != nil {
t.Errorf("Test %v failed. Unexpected error inserting previous group policy relations: %v", n, err)
continue
}
}
}
// Call to repository to remove group
err := repoDB.RemoveGroup(test.groupToDelete)

Expand Down Expand Up @@ -803,13 +826,35 @@ func TestPostgresRepo_RemoveGroup(t *testing.T) {
// Check total group user relations
totalRelations, err := getGroupUserRelations("", "")
if err != nil {
t.Errorf("Test %v failed. Unexpected error counting total relations: %v", n, err)
t.Errorf("Test %v failed. Unexpected error counting total group user relations: %v", n, err)
continue
}
if relations != 0 {
if totalRelations != 1 {
t.Errorf("Test %v failed. Received different total group user relation number: %v", n, totalRelations)
continue
}

// Check group policy relations
relations, err = getGroupPolicyRelationCount("", test.groupToDelete)
if err != nil {
t.Errorf("Test %v failed. Unexpected error counting group policy relations: %v", n, err)
continue
}
if relations != 0 {
t.Errorf("Test %v failed. Received different group policy relation number: %v", n, relations)
continue
}

// Check total group policy relations
totalRelations, err = getGroupPolicyRelationCount("", "")
if err != nil {
t.Errorf("Test %v failed. Unexpected error counting total group policy relations: %v", n, err)
continue
}
if totalRelations != 1 {
t.Errorf("Test %v failed. Received different total group policy relation number: %v", n, totalRelations)
continue
}
}
}

Expand Down

0 comments on commit ba3a526

Please sign in to comment.