Skip to content

Commit 6c77e52

Browse files
authored
chore: improve tests more coverage (#1713)
1 parent 565d3c3 commit 6c77e52

4 files changed

Lines changed: 200 additions & 109 deletions

File tree

flask_appbuilder/security/api.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
API_SECURITY_ACCESS_TOKEN_KEY,
55
API_SECURITY_PROVIDER_DB,
66
API_SECURITY_PROVIDER_LDAP,
7+
API_SECURITY_REFRESH_TOKEN_KEY,
78
API_SECURITY_VERSION,
89
)
910
from flask_appbuilder.security.schemas import login_post
@@ -107,8 +108,10 @@ def login(self) -> Response:
107108
resp[API_SECURITY_ACCESS_TOKEN_KEY] = create_access_token(
108109
identity=user.id, fresh=True
109110
)
110-
if "refresh" in login_payload:
111-
login_payload["refresh"] = create_refresh_token(identity=user.id)
111+
if "refresh" in login_payload and login_payload["refresh"]:
112+
resp[API_SECURITY_REFRESH_TOKEN_KEY] = create_refresh_token(
113+
identity=user.id
114+
)
112115
return self.response(200, **resp)
113116

114117
@expose("/refresh", methods=["POST"])

flask_appbuilder/security/schemas.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515

1616

1717
def validate_password(value: Union[bytes, bytearray, str]) -> None:
18-
if not value:
19-
raise ValidationError("Password is required")
20-
if len(value) == 1 and value.encode()[0] == 0:
18+
if value and sum(value.encode()) == 0:
2119
raise ValidationError("Password null is not allowed")
2220

2321

@@ -31,7 +29,7 @@ def validate_provider(value: Union[bytes, bytearray, str]) -> None:
3129
class LoginPost(Schema):
3230
username = fields.String(required=True, allow_none=False, validate=Length(min=1))
3331
password = fields.String(
34-
validate=validate_password, required=True, allow_none=False
32+
validate=[Length(min=1), validate_password], required=True, allow_none=False
3533
)
3634
provider = fields.String(
3735
validate=[

flask_appbuilder/tests/base.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from flask_appbuilder.const import (
99
API_SECURITY_PASSWORD_KEY,
1010
API_SECURITY_PROVIDER_KEY,
11+
API_SECURITY_REFRESH_KEY,
1112
API_SECURITY_USERNAME_KEY,
1213
API_SECURITY_VERSION,
1314
)
@@ -36,7 +37,7 @@ def auth_client_post(client, token, uri, json):
3637
)
3738

3839
@staticmethod
39-
def _login(client, username, password):
40+
def _login(client, username, password, refresh: bool = False):
4041
"""
4142
Login help method
4243
:param client: Flask test client
@@ -45,15 +46,13 @@ def _login(client, username, password):
4546
:return: Flask client response class
4647
"""
4748
return client.post(
48-
"api/{}/security/login".format(API_SECURITY_VERSION),
49-
data=json.dumps(
50-
{
51-
API_SECURITY_USERNAME_KEY: username,
52-
API_SECURITY_PASSWORD_KEY: password,
53-
API_SECURITY_PROVIDER_KEY: "db",
54-
}
55-
),
56-
content_type="application/json",
49+
f"api/{API_SECURITY_VERSION}/security/login",
50+
json={
51+
API_SECURITY_USERNAME_KEY: username,
52+
API_SECURITY_PASSWORD_KEY: password,
53+
API_SECURITY_PROVIDER_KEY: "db",
54+
API_SECURITY_REFRESH_KEY: refresh,
55+
},
5756
)
5857

5958
def login(self, client, username, password):

0 commit comments

Comments
 (0)