Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#252 from IMBlues/development
Browse files Browse the repository at this point in the history
fix: count field type from str to int in api v3 & SaaS 页面支持 base64 密码传入
  • Loading branch information
IMBlues authored Jan 17, 2022
2 parents b8b4bee + 77fde2e commit 33f2fdb
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 59 deletions.
1 change: 0 additions & 1 deletion src/api/bkuser_core/categories/plugins/tof

This file was deleted.

4 changes: 2 additions & 2 deletions src/api/bkuser_core/profiles/v3/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from bkuser_core.apis.v3.serializers import StringArrayField
from bkuser_core.departments.serializers import SimpleDepartmentSerializer
from bkuser_core.profiles.v2.serializers import LeaderSerializer
from rest_framework.fields import BooleanField, CharField, JSONField
from rest_framework.fields import BooleanField, CharField, IntegerField, JSONField
from rest_framework.serializers import Serializer


Expand Down Expand Up @@ -64,7 +64,7 @@ class ResultProfileSerializer(ProfileSerializer):


class PaginatedProfileSerializer(Serializer):
count = CharField(required=False, help_text="总数")
count = IntegerField(required=False, help_text="总数")
next = CharField(required=False, help_text="下一页游标")
previous = CharField(required=False, help_text="上一页游标")
results = ResultProfileSerializer(many=True, help_text="结果")
3 changes: 1 addition & 2 deletions src/saas/bkuser_shell/apis/viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from django.utils.translation import get_language
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response as RawResponse
from rest_framework.viewsets import GenericViewSet

from bkuser_global.utils import force_str_2_bool
Expand Down Expand Up @@ -140,7 +139,7 @@ def call_through_api(self, request):
_preload_content=False,
)

resp = RawResponse(
resp = Response(
data=json.loads(urllib3_resp.data),
status=urllib3_resp.status,
content_type=urllib3_resp.headers.get("Content-Type"),
Expand Down
22 changes: 21 additions & 1 deletion src/saas/bkuser_shell/common/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,28 @@
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 rest_framework.serializers import Serializer
import base64

from django.utils.encoding import force_bytes, force_str
from rest_framework.serializers import CharField, Serializer


class EmptySerializer(Serializer):
"""空"""


def is_base64(value: str) -> bool:
"""判断字符串是否为 base64 编码"""
try:
return base64.b64encode(base64.b64decode(value)) == force_bytes(value)
except Exception: # pylint: disable=broad-except
return False


class Base64OrPlainField(CharField):
"""兼容 base64 和纯文本字段"""

def to_internal_value(self, data) -> str:
if is_base64(data):
return force_str(base64.b64decode(data))
return super().to_internal_value(data)
7 changes: 4 additions & 3 deletions src/saas/bkuser_shell/password/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 bkuser_shell.common.serializers import Base64OrPlainField
from rest_framework import serializers


Expand All @@ -17,9 +18,9 @@ class ResetPasswordEmailSerializer(serializers.Serializer):

class ResetByTokenSerialzier(serializers.Serializer):
token = serializers.CharField(required=True, max_length=254)
password = serializers.CharField(required=True, max_length=254)
password = Base64OrPlainField(required=True, max_length=254)


class ModifyPassWordSerialzier(serializers.Serializer):
old_password = serializers.CharField(required=True, max_length=254)
new_password = serializers.CharField(required=True, max_length=254)
old_password = Base64OrPlainField(required=True, max_length=254)
new_password = Base64OrPlainField(required=True, max_length=254)
5 changes: 1 addition & 4 deletions src/saas/bkuser_shell/tests/apis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ def get_api_factory(force_params: dict = None):
"""获取一个通用的 ApiFactory"""
force_params = force_params or {}
normal_params = {
"HTTP_FORCE_RAW_RESPONSE": True,
"HTTP_RAW_USERNAME": True,
"Content-Type": "application/json",
"HTTP_AUTHORIZATION": "iBearer HVp5CNn4th87w5MLT8x1FJw6Rcc5cF3SRT7NlcFILgij",
"HTTP_X_BKUSER_OPERATOR": "tester",
# TODO: 单元测试需要跳过身份验证
}
normal_params.update(force_params)

Expand Down
46 changes: 0 additions & 46 deletions src/saas/bkuser_shell/tests/apis/version_log/test_version_log.py

This file was deleted.

10 changes: 10 additions & 0 deletions src/saas/bkuser_shell/tests/common/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- 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.
"""
27 changes: 27 additions & 0 deletions src/saas/bkuser_shell/tests/common/test_serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- 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 saas.bkuser_shell.common.serializers import is_base64


class TestIsBase64:
@pytest.mark.parametrize(
"case,expected",
[
("aGVsbHdvcmxk", True),
("helloworld", False),
("aGVsbHdvcmxkZGFzZGY=", True),
("aGVsbHdvcmxkZGFzZGY", False),
],
)
def test_is_base64(self, case, expected):
assert is_base64(case) == expected

0 comments on commit 33f2fdb

Please sign in to comment.