Skip to content

Commit

Permalink
feat: add subject template group unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu327 committed Oct 25, 2023
1 parent 952efb8 commit c937444
Show file tree
Hide file tree
Showing 8 changed files with 706 additions and 5 deletions.
44 changes: 44 additions & 0 deletions pkg/cacheimpls/system_subject_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,47 @@ func TestSystemSubjectPKCacheKey_Key(t *testing.T) {

assert.Equal(t, "test:1", key.Key())
}

func TestGetSubjectSystemGroup(t *testing.T) {
ctl := gomock.NewController(t)
defer ctl.Finish()

expiration := 5 * time.Minute

mockService := mock.NewMockGroupService(ctl)
mockService.EXPECT().ListEffectThinSubjectGroups("system", []int64{1}).Return(map[int64][]types.ThinSubjectGroup{
1: {
{
GroupPK: 2,
ExpiredAt: 2,
},
{
GroupPK: 3,
ExpiredAt: 3,
},
},
}, nil).AnyTimes()

patches := gomonkey.ApplyFunc(service.NewGroupService,
func() service.GroupService {
return mockService
})
defer patches.Reset()

mockCache := redis.NewMockCache("mockCache", expiration)

SubjectDepartmentCache = mockCache

groups, err := GetSubjectSystemGroup("system", 1)
assert.NoError(t, err)
assert.Equal(t, []types.ThinSubjectGroup{
{
GroupPK: 2,
ExpiredAt: 2,
},
{
GroupPK: 3,
ExpiredAt: 3,
},
}, groups)
}
2 changes: 1 addition & 1 deletion pkg/database/dao/group_system_auth_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (m *groupSystemAuthTypeManager) GetOneAuthSystemByGroup(groupPK int64) (str
FROM group_system_auth_type
WHERE group_pk = ?
LIMIT 1`
err := database.SqlxSelect(m.DB, &system, query, groupPK)
err := database.SqlxGet(m.DB, &system, query, groupPK)
return system, err
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/database/dao/group_system_auth_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,20 @@ func Test_groupSystemAuthTypeManager_UpdateWithTx(t *testing.T) {
assert.Equal(t, int64(1), rows)
})
}

func Test_groupSystemAuthTypeManager_GetOneAuthSystemByGroup(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^SELECT
system_id
FROM group_system_auth_type
WHERE group_pk =`
mockRows := sqlmock.NewRows([]string{"system_id"}).AddRow("1")
mock.ExpectQuery(mockQuery).WithArgs(int64(1)).WillReturnRows(mockRows)

manager := &groupSystemAuthTypeManager{DB: db}
systemID, err := manager.GetOneAuthSystemByGroup(int64(1))

assert.NoError(t, err, "query from db fail.")
assert.Equal(t, "1", systemID)
})
}
39 changes: 39 additions & 0 deletions pkg/database/dao/subject_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,42 @@ func Test_subjectRelationManager_ListPagingSubjectSystemGroupBeforeExpiredAt(t *
assert.Len(t, relations, 1)
})
}

func Test_subjectRelationManager_BulkUpdateExpiredAtWithTx(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^UPDATE subject_relation`
mock.ExpectBegin()
mock.ExpectPrepare(mockQuery)
mock.ExpectExec(mockQuery).WithArgs(int64(3), int64(2), int64(1)).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

tx, err := db.Beginx()
assert.NoError(t, err)
manager := &subjectGroupManager{DB: db}
err = manager.BulkUpdateExpiredAtWithTx(tx, []SubjectRelation{{
SubjectPK: int64(2),
GroupPK: int64(1),
ExpiredAt: int64(3),
}})

assert.NoError(t, err, "query from db fail.")
})
}

func Test_subjectRelationManager_HasRelation(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^SELECT pk FROM subject_relation WHERE subject_pk = (.*) AND parent_pk = (.*) LIMIT 1`
mockRows := sqlmock.NewRows(
[]string{
"pk",
},
).AddRow(int64(1))
mock.ExpectQuery(mockQuery).WithArgs(int64(1), int64(2)).WillReturnRows(mockRows)

manager := &subjectGroupManager{DB: db}
exist, err := manager.HasRelation(int64(1), int64(2))

assert.NoError(t, err, "query from db fail.")
assert.True(t, exist)
})
}
9 changes: 6 additions & 3 deletions pkg/database/dao/subject_template_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (m *subjectTemplateGroupManager) BulkUpdateExpiredAtWithTx(
relations []SubjectRelation,
) error {
sql := `UPDATE subject_template_group
SET expired_at = :expired_at
WHERE subject_pk = :subject_pk AND group_pk = :group_pk`
SET expired_at = :policy_expired_at
WHERE subject_pk = :subject_pk AND group_pk = :parent_pk`
return database.SqlxBulkUpdateWithTx(tx, sql, relations)
}

Expand All @@ -85,7 +85,10 @@ func (m *subjectTemplateGroupManager) BulkDeleteWithTx(tx *sqlx.Tx, relations []
return nil
}

sql := `DELETE FROM subject_template_group WHERE subject_pk = ? AND group_pk = ? AND template_id = ?`
sql := `DELETE FROM subject_template_group
WHERE subject_pk = :subject_pk
AND group_pk = :group_pk
AND template_id = :template_id`
return database.SqlxBulkUpdateWithTx(tx, sql, relations)
}

Expand Down
123 changes: 123 additions & 0 deletions pkg/database/dao/subject_template_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-权限中心(BlueKing-IAM) available.
* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package dao

import (
"testing"

"iam/pkg/database"

sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
)

func Test_subjectTemplateGroupManager_BulkCreateWithTx(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mock.ExpectBegin()
mock.ExpectExec(`^INSERT INTO subject_template_group`).WithArgs(
int64(2), int64(1), int64(1), int64(3),
).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

relations := []SubjectTemplateGroup{{
SubjectPK: 2,
TemplateID: 1,
GroupPK: 1,
ExpiredAt: 3,
}}

tx, err := db.Beginx()
assert.NoError(t, err)

manager := &subjectTemplateGroupManager{DB: db}
err = manager.BulkCreateWithTx(tx, relations)

tx.Commit()

assert.NoError(t, err)
})
}

func Test_subjectTemplateGroupManager_BulkUpdateExpiredAtWithTx(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^UPDATE subject_template_group`
mock.ExpectBegin()
mock.ExpectPrepare(mockQuery)
mock.ExpectExec(mockQuery).WithArgs(int64(3), int64(2), int64(1)).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

tx, err := db.Beginx()
assert.NoError(t, err)
manager := &subjectTemplateGroupManager{DB: db}
err = manager.BulkUpdateExpiredAtWithTx(tx, []SubjectRelation{{
SubjectPK: int64(2),
GroupPK: int64(1),
ExpiredAt: int64(3),
}})

assert.NoError(t, err, "query from db fail.")
})
}

func Test_subjectTemplateGroupManager_BulkDeleteWithTx(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^DELETE FROM subject_template_group WHERE subject_pk = (.*) AND group_pk = (.*) AND template_id = (.*)`
mock.ExpectBegin()
mock.ExpectPrepare(mockQuery)
mock.ExpectExec(mockQuery).WithArgs(int64(2), int64(1), int64(1)).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

tx, err := db.Beginx()
assert.NoError(t, err)
manager := &subjectTemplateGroupManager{DB: db}
err = manager.BulkDeleteWithTx(tx, []SubjectTemplateGroup{{
SubjectPK: int64(2),
TemplateID: int64(1),
GroupPK: int64(1),
ExpiredAt: int64(3),
}})

assert.NoError(t, err, "query from db fail.")
})
}

func Test_subjectTemplateGroupManager_HasRelationExceptTemplate(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^SELECT pk FROM subject_template_group WHERE subject_pk =`
mockRows := sqlmock.NewRows(
[]string{
"pk",
},
).AddRow(int64(1))
mock.ExpectQuery(mockQuery).WithArgs(int64(1), int64(2), int64(3)).WillReturnRows(mockRows)

manager := &subjectTemplateGroupManager{DB: db}
exist, err := manager.HasRelationExceptTemplate(int64(1), int64(2), int64(3))

assert.NoError(t, err, "query from db fail.")
assert.True(t, exist)
})
}

func Test_subjectTemplateGroupManager_GetExpiredAtBySubjectGroup(t *testing.T) {
database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) {
mockQuery := `^SELECT expired_at FROM subject_template_group WHERE subject_pk =`
mockRows := sqlmock.NewRows([]string{"policy_expired_at"}).AddRow(int64(1))
mock.ExpectQuery(mockQuery).WithArgs(int64(1), int64(10)).WillReturnRows(mockRows)

manager := &subjectTemplateGroupManager{DB: db}
expiredAt, err := manager.GetExpiredAtBySubjectGroup(int64(1), int64(10))

assert.NoError(t, err, "query from db fail.")
assert.Equal(t, expiredAt, int64(1))
})
}
2 changes: 1 addition & 1 deletion pkg/service/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ func (l *groupService) BulkDeleteSubjectTemplateGroupWithTx(

err := l.subjectTemplateGroupManager.BulkDeleteWithTx(tx, daoRelations)
if err != nil {
return errorWrapf(err, "subjectTemplateGroupManager.BulkDelete relations=`%+v` fail", daoRelations)
return errorWrapf(err, "subjectTemplateGroupManager.BulkDeleteWithTx relations=`%+v` fail", daoRelations)
}

groupSystemIDCache := make(map[int64][]string)
Expand Down
Loading

0 comments on commit c937444

Please sign in to comment.