Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve tests more coverage #1713

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions flask_appbuilder/security/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
API_SECURITY_ACCESS_TOKEN_KEY,
API_SECURITY_PROVIDER_DB,
API_SECURITY_PROVIDER_LDAP,
API_SECURITY_REFRESH_TOKEN_KEY,
API_SECURITY_VERSION,
)
from flask_appbuilder.security.schemas import login_post
Expand Down Expand Up @@ -107,8 +108,10 @@ def login(self) -> Response:
resp[API_SECURITY_ACCESS_TOKEN_KEY] = create_access_token(
identity=user.id, fresh=True
)
if "refresh" in login_payload:
login_payload["refresh"] = create_refresh_token(identity=user.id)
if "refresh" in login_payload and login_payload["refresh"]:
resp[API_SECURITY_REFRESH_TOKEN_KEY] = create_refresh_token(
identity=user.id
)
return self.response(200, **resp)

@expose("/refresh", methods=["POST"])
Expand Down
6 changes: 2 additions & 4 deletions flask_appbuilder/security/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@


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


Expand All @@ -31,7 +29,7 @@ def validate_provider(value: Union[bytes, bytearray, str]) -> None:
class LoginPost(Schema):
username = fields.String(required=True, allow_none=False, validate=Length(min=1))
password = fields.String(
validate=validate_password, required=True, allow_none=False
validate=[Length(min=1), validate_password], required=True, allow_none=False
)
provider = fields.String(
validate=[
Expand Down
19 changes: 9 additions & 10 deletions flask_appbuilder/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from flask_appbuilder.const import (
API_SECURITY_PASSWORD_KEY,
API_SECURITY_PROVIDER_KEY,
API_SECURITY_REFRESH_KEY,
API_SECURITY_USERNAME_KEY,
API_SECURITY_VERSION,
)
Expand Down Expand Up @@ -36,7 +37,7 @@ def auth_client_post(client, token, uri, json):
)

@staticmethod
def _login(client, username, password):
def _login(client, username, password, refresh: bool = False):
"""
Login help method
:param client: Flask test client
Expand All @@ -45,15 +46,13 @@ def _login(client, username, password):
:return: Flask client response class
"""
return client.post(
"api/{}/security/login".format(API_SECURITY_VERSION),
data=json.dumps(
{
API_SECURITY_USERNAME_KEY: username,
API_SECURITY_PASSWORD_KEY: password,
API_SECURITY_PROVIDER_KEY: "db",
}
),
content_type="application/json",
f"api/{API_SECURITY_VERSION}/security/login",
json={
API_SECURITY_USERNAME_KEY: username,
API_SECURITY_PASSWORD_KEY: password,
API_SECURITY_PROVIDER_KEY: "db",
API_SECURITY_REFRESH_KEY: refresh,
},
)

def login(self, client, username, password):
Expand Down
Loading