Skip to content

Commit

Permalink
test: add DataSourceParser & Exporter unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
narasux committed Sep 19, 2023
1 parent 70edb8e commit 9c2c997
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
Empty file.
Empty file.
17 changes: 17 additions & 0 deletions src/bk-user/bkuser/plugins/local/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) 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.
"""
from hashlib import sha256


def gen_code(username_or_org: str) -> str:
# 本地数据源数据没有提供用户及部门 code 的方式,
# 因此使用 sha256 计算以避免冲突,也便于后续插入 DB 时进行比较
return sha256(username_or_org.encode("utf-8")).hexdigest()
31 changes: 31 additions & 0 deletions src/bk-user/tests/plugins/local/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) 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.
"""
import pytest
from bkuser.plugins.local.utils import gen_code


@pytest.mark.parametrize(
("raw", "excepted"),
[
("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
("zhangsan", "82721d844c04f22502244a87061313e08d49c6e8d3fbee47e12201880a5ce6cb"),
("lisi", "45ade5c9806fd3585a4ce199adbdf058301a01e625bf514252913e641191edd9"),
("wangwu", "4ada86d5cca8cf4ac00399a0a16738ee8c944df3767b4af7151ca8b148cfe9e3"),
("公司", "e06ff957ed48e868a41b7e7e4460ce371e398108db542cf1cd1d61795b83e647"),
("公司/部门A", "2da9c820170b44354632bd3fe26ad09f4836b5977d2f6a5ff20afe7b143ac1e1"),
("公司/部门A/中心AA", "63986fb4ef27820413deb3f7c57cc36aef2ea898f03d8355e854d73b5c14e09c"),
("公司/部门A/中心AA/小组AAA ", "e75be6462a8ff8b9b843b3c2e419db455b4477023f98941508bc19cfa3982ec0"),
],
)
def test_gen_code(raw, excepted):
# 重要:如果该单元测试挂了,说明修改了本地数据源用户 & 部门的 Code 的生成规则
# 改行为会导致新同步的数据,无法与 DB 中的数据匹配上,将会触发数据重建!!!
assert gen_code(raw) == excepted

0 comments on commit 9c2c997

Please sign in to comment.