Compare commits
5 Commits
v0.6.1
...
a5188c9aa1
Author | SHA1 | Date | |
---|---|---|---|
a5188c9aa1 | |||
b62b98bd51 | |||
877f02fe82 | |||
bc888195ad | |||
8e69733cf6 |
@ -2,6 +2,18 @@ Change Log
|
||||
==========
|
||||
|
||||
|
||||
Version 0.6.2
|
||||
-------------
|
||||
|
||||
Released 2023/6/10
|
||||
|
||||
* Changed logging from STDERR to the Flask logger.
|
||||
* Test case updates:
|
||||
* Added missing documentation.
|
||||
* Changed properties from public to private.
|
||||
* Disabled logging.
|
||||
|
||||
|
||||
Version 0.6.1
|
||||
-------------
|
||||
|
||||
|
@ -22,5 +22,5 @@ from flask_digest_auth.algo import make_password_hash, calc_response
|
||||
from flask_digest_auth.auth import DigestAuth
|
||||
from flask_digest_auth.test import Client
|
||||
|
||||
VERSION: str = "0.6.1"
|
||||
VERSION: str = "0.6.2"
|
||||
"""The package version."""
|
||||
|
@ -140,7 +140,7 @@ class DigestAuth:
|
||||
return view(*args, **kwargs)
|
||||
except UnauthorizedException as e:
|
||||
if len(e.args) > 0:
|
||||
sys.stderr.write(e.args[0] + "\n")
|
||||
current_app.logger.warning(e.args[0])
|
||||
response: Response = Response()
|
||||
response.status = 401
|
||||
response.headers["WWW-Authenticate"] \
|
||||
|
@ -18,6 +18,7 @@
|
||||
"""The test case for the HTTP digest authentication.
|
||||
|
||||
"""
|
||||
import logging
|
||||
from secrets import token_urlsafe
|
||||
from typing import Any, Optional, Dict
|
||||
|
||||
@ -28,8 +29,11 @@ from werkzeug.datastructures import WWWAuthenticate, Authorization
|
||||
from flask_digest_auth import DigestAuth, make_password_hash, Client
|
||||
|
||||
_REALM: str = "testrealm@host.com"
|
||||
"""The realm."""
|
||||
_USERNAME: str = "Mufasa"
|
||||
"""The username."""
|
||||
_PASSWORD: str = "Circle Of Life"
|
||||
"""The password."""
|
||||
|
||||
|
||||
class User:
|
||||
@ -42,9 +46,11 @@ class User:
|
||||
:param password: The clear-text password.
|
||||
"""
|
||||
self.username: str = username
|
||||
self.password_hash: str = make_password_hash(
|
||||
_REALM, username, password)
|
||||
"""The username."""
|
||||
self.password_hash: str = make_password_hash(_REALM, username, password)
|
||||
"""The password hash."""
|
||||
self.visits: int = 0
|
||||
"""The number of visits."""
|
||||
|
||||
|
||||
class AuthenticationTestCase(TestCase):
|
||||
@ -55,6 +61,7 @@ class AuthenticationTestCase(TestCase):
|
||||
|
||||
:return: The Flask application.
|
||||
"""
|
||||
logging.getLogger("test_auth").addHandler(logging.NullHandler())
|
||||
app: Flask = Flask(__name__)
|
||||
app.config.from_mapping({
|
||||
"TESTING": True,
|
||||
@ -65,8 +72,9 @@ class AuthenticationTestCase(TestCase):
|
||||
|
||||
auth: DigestAuth = DigestAuth()
|
||||
auth.init_app(app)
|
||||
self.user: User = User(_USERNAME, _PASSWORD)
|
||||
user_db: Dict[str, User] = {_USERNAME: self.user}
|
||||
self.__user: User = User(_USERNAME, _PASSWORD)
|
||||
"""The user account."""
|
||||
user_db: Dict[str, User] = {_USERNAME: self.__user}
|
||||
|
||||
@auth.register_get_password
|
||||
def get_password_hash(username: str) -> Optional[str]:
|
||||
@ -142,7 +150,7 @@ class AuthenticationTestCase(TestCase):
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.data.decode("UTF-8"),
|
||||
f"Hello, {_USERNAME}! #2")
|
||||
self.assertEqual(self.user.visits, 1)
|
||||
self.assertEqual(self.__user.visits, 1)
|
||||
|
||||
def test_stale_opaque(self) -> None:
|
||||
"""Tests the stale and opaque value.
|
||||
@ -219,4 +227,4 @@ class AuthenticationTestCase(TestCase):
|
||||
|
||||
response = self.client.get(admin_uri)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(self.user.visits, 2)
|
||||
self.assertEqual(self.__user.visits, 2)
|
||||
|
@ -18,6 +18,7 @@
|
||||
"""The test case for the Flask-Login integration.
|
||||
|
||||
"""
|
||||
import logging
|
||||
from secrets import token_urlsafe
|
||||
from typing import Optional, Dict
|
||||
|
||||
@ -28,8 +29,11 @@ from werkzeug.datastructures import WWWAuthenticate, Authorization
|
||||
from flask_digest_auth import DigestAuth, make_password_hash, Client
|
||||
|
||||
_REALM: str = "testrealm@host.com"
|
||||
"""The realm."""
|
||||
_USERNAME: str = "Mufasa"
|
||||
"""The username."""
|
||||
_PASSWORD: str = "Circle Of Life"
|
||||
"""The password."""
|
||||
|
||||
|
||||
class User:
|
||||
@ -42,11 +46,15 @@ class User:
|
||||
:param password: The clear-text password.
|
||||
"""
|
||||
self.username: str = username
|
||||
self.password_hash: str = make_password_hash(
|
||||
_REALM, username, password)
|
||||
"""The username."""
|
||||
self.password_hash: str = make_password_hash(_REALM, username, password)
|
||||
"""The password hash."""
|
||||
self.visits: int = 0
|
||||
"""The number of visits."""
|
||||
self.is_active: bool = True
|
||||
"""True if the account is active, or False otherwise."""
|
||||
self.is_anonymous: bool = False
|
||||
"""True if the account is anonymous, or False otherwise."""
|
||||
|
||||
def get_id(self) -> str:
|
||||
"""Returns the username.
|
||||
@ -75,6 +83,7 @@ class FlaskLoginTestCase(TestCase):
|
||||
|
||||
:return: The Flask application.
|
||||
"""
|
||||
logging.getLogger("test_flask_login").addHandler(logging.NullHandler())
|
||||
app: Flask = Flask(__name__)
|
||||
app.config.from_mapping({
|
||||
"TESTING": True,
|
||||
@ -83,11 +92,12 @@ class FlaskLoginTestCase(TestCase):
|
||||
})
|
||||
app.test_client_class = Client
|
||||
|
||||
self.has_flask_login: bool = True
|
||||
self.__has_flask_login: bool = True
|
||||
"""Whether the Flask-Login package is installed."""
|
||||
try:
|
||||
import flask_login
|
||||
except ModuleNotFoundError:
|
||||
self.has_flask_login = False
|
||||
self.__has_flask_login = False
|
||||
return app
|
||||
|
||||
login_manager: flask_login.LoginManager = flask_login.LoginManager()
|
||||
@ -96,8 +106,9 @@ class FlaskLoginTestCase(TestCase):
|
||||
auth: DigestAuth = DigestAuth()
|
||||
auth.init_app(app)
|
||||
|
||||
self.user: User = User(_USERNAME, _PASSWORD)
|
||||
user_db: Dict[str, User] = {_USERNAME: self.user}
|
||||
self.__user: User = User(_USERNAME, _PASSWORD)
|
||||
"""The user account."""
|
||||
user_db: Dict[str, User] = {_USERNAME: self.__user}
|
||||
|
||||
@auth.register_get_password
|
||||
def get_password_hash(username: str) -> Optional[str]:
|
||||
@ -162,7 +173,7 @@ class FlaskLoginTestCase(TestCase):
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
if not self.has_flask_login:
|
||||
if not self.__has_flask_login:
|
||||
self.skipTest("Skipped without Flask-Login.")
|
||||
|
||||
response: Response = self.client.get(self.app.url_for("admin-1"))
|
||||
@ -176,14 +187,14 @@ class FlaskLoginTestCase(TestCase):
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.data.decode("UTF-8"),
|
||||
f"Hello, {_USERNAME}! #2")
|
||||
self.assertEqual(self.user.visits, 1)
|
||||
self.assertEqual(self.__user.visits, 1)
|
||||
|
||||
def test_stale_opaque(self) -> None:
|
||||
"""Tests the stale and opaque value.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
if not self.has_flask_login:
|
||||
if not self.__has_flask_login:
|
||||
self.skipTest("Skipped without Flask-Login.")
|
||||
|
||||
admin_uri: str = self.app.url_for("admin-1")
|
||||
@ -231,7 +242,7 @@ class FlaskLoginTestCase(TestCase):
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
if not self.has_flask_login:
|
||||
if not self.__has_flask_login:
|
||||
self.skipTest("Skipped without Flask-Login.")
|
||||
|
||||
admin_uri: str = self.app.url_for("admin-1")
|
||||
@ -265,33 +276,33 @@ class FlaskLoginTestCase(TestCase):
|
||||
|
||||
response = self.client.get(admin_uri)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(self.user.visits, 2)
|
||||
self.assertEqual(self.__user.visits, 2)
|
||||
|
||||
def test_disabled(self) -> None:
|
||||
"""Tests the disabled user.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
if not self.has_flask_login:
|
||||
if not self.__has_flask_login:
|
||||
self.skipTest("Skipped without Flask-Login.")
|
||||
|
||||
response: Response
|
||||
|
||||
self.user.is_active = False
|
||||
self.__user.is_active = False
|
||||
response = self.client.get(self.app.url_for("admin-1"))
|
||||
self.assertEqual(response.status_code, 401)
|
||||
response = self.client.get(self.app.url_for("admin-1"),
|
||||
digest_auth=(_USERNAME, _PASSWORD))
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
self.user.is_active = True
|
||||
self.__user.is_active = True
|
||||
response = self.client.get(self.app.url_for("admin-1"),
|
||||
digest_auth=(_USERNAME, _PASSWORD))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
response = self.client.get(self.app.url_for("admin-1"))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
self.user.is_active = False
|
||||
self.__user.is_active = False
|
||||
response = self.client.get(self.app.url_for("admin-1"))
|
||||
self.assertEqual(response.status_code, 401)
|
||||
response = self.client.get(self.app.url_for("admin-1"),
|
||||
|
Reference in New Issue
Block a user