Compare commits

..

No commits in common. "a5188c9aa1e7ba35690cc68d43d9ff7c23e942ca" and "bc888195ad6c1ad813770bd97df668ff178a801e" have entirely different histories.

4 changed files with 22 additions and 49 deletions

View File

@ -2,18 +2,6 @@ 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 Version 0.6.1
------------- -------------

View File

@ -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.auth import DigestAuth
from flask_digest_auth.test import Client from flask_digest_auth.test import Client
VERSION: str = "0.6.2" VERSION: str = "0.6.1"
"""The package version.""" """The package version."""

View File

@ -29,11 +29,8 @@ from werkzeug.datastructures import WWWAuthenticate, Authorization
from flask_digest_auth import DigestAuth, make_password_hash, Client from flask_digest_auth import DigestAuth, make_password_hash, Client
_REALM: str = "testrealm@host.com" _REALM: str = "testrealm@host.com"
"""The realm."""
_USERNAME: str = "Mufasa" _USERNAME: str = "Mufasa"
"""The username."""
_PASSWORD: str = "Circle Of Life" _PASSWORD: str = "Circle Of Life"
"""The password."""
class User: class User:
@ -46,11 +43,9 @@ class User:
:param password: The clear-text password. :param password: The clear-text password.
""" """
self.username: str = username self.username: str = username
"""The username.""" self.password_hash: str = make_password_hash(
self.password_hash: str = make_password_hash(_REALM, username, password) _REALM, username, password)
"""The password hash."""
self.visits: int = 0 self.visits: int = 0
"""The number of visits."""
class AuthenticationTestCase(TestCase): class AuthenticationTestCase(TestCase):
@ -72,9 +67,8 @@ class AuthenticationTestCase(TestCase):
auth: DigestAuth = DigestAuth() auth: DigestAuth = DigestAuth()
auth.init_app(app) auth.init_app(app)
self.__user: User = User(_USERNAME, _PASSWORD) self.user: User = User(_USERNAME, _PASSWORD)
"""The user account.""" user_db: Dict[str, User] = {_USERNAME: self.user}
user_db: Dict[str, User] = {_USERNAME: self.__user}
@auth.register_get_password @auth.register_get_password
def get_password_hash(username: str) -> Optional[str]: def get_password_hash(username: str) -> Optional[str]:
@ -150,7 +144,7 @@ class AuthenticationTestCase(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.decode("UTF-8"), self.assertEqual(response.data.decode("UTF-8"),
f"Hello, {_USERNAME}! #2") f"Hello, {_USERNAME}! #2")
self.assertEqual(self.__user.visits, 1) self.assertEqual(self.user.visits, 1)
def test_stale_opaque(self) -> None: def test_stale_opaque(self) -> None:
"""Tests the stale and opaque value. """Tests the stale and opaque value.
@ -227,4 +221,4 @@ class AuthenticationTestCase(TestCase):
response = self.client.get(admin_uri) response = self.client.get(admin_uri)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(self.__user.visits, 2) self.assertEqual(self.user.visits, 2)

View File

@ -29,11 +29,8 @@ from werkzeug.datastructures import WWWAuthenticate, Authorization
from flask_digest_auth import DigestAuth, make_password_hash, Client from flask_digest_auth import DigestAuth, make_password_hash, Client
_REALM: str = "testrealm@host.com" _REALM: str = "testrealm@host.com"
"""The realm."""
_USERNAME: str = "Mufasa" _USERNAME: str = "Mufasa"
"""The username."""
_PASSWORD: str = "Circle Of Life" _PASSWORD: str = "Circle Of Life"
"""The password."""
class User: class User:
@ -46,15 +43,11 @@ class User:
:param password: The clear-text password. :param password: The clear-text password.
""" """
self.username: str = username self.username: str = username
"""The username.""" self.password_hash: str = make_password_hash(
self.password_hash: str = make_password_hash(_REALM, username, password) _REALM, username, password)
"""The password hash."""
self.visits: int = 0 self.visits: int = 0
"""The number of visits."""
self.is_active: bool = True self.is_active: bool = True
"""True if the account is active, or False otherwise."""
self.is_anonymous: bool = False self.is_anonymous: bool = False
"""True if the account is anonymous, or False otherwise."""
def get_id(self) -> str: def get_id(self) -> str:
"""Returns the username. """Returns the username.
@ -92,12 +85,11 @@ class FlaskLoginTestCase(TestCase):
}) })
app.test_client_class = Client 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: try:
import flask_login import flask_login
except ModuleNotFoundError: except ModuleNotFoundError:
self.__has_flask_login = False self.has_flask_login = False
return app return app
login_manager: flask_login.LoginManager = flask_login.LoginManager() login_manager: flask_login.LoginManager = flask_login.LoginManager()
@ -106,9 +98,8 @@ class FlaskLoginTestCase(TestCase):
auth: DigestAuth = DigestAuth() auth: DigestAuth = DigestAuth()
auth.init_app(app) auth.init_app(app)
self.__user: User = User(_USERNAME, _PASSWORD) self.user: User = User(_USERNAME, _PASSWORD)
"""The user account.""" user_db: Dict[str, User] = {_USERNAME: self.user}
user_db: Dict[str, User] = {_USERNAME: self.__user}
@auth.register_get_password @auth.register_get_password
def get_password_hash(username: str) -> Optional[str]: def get_password_hash(username: str) -> Optional[str]:
@ -173,7 +164,7 @@ class FlaskLoginTestCase(TestCase):
:return: None. :return: None.
""" """
if not self.__has_flask_login: if not self.has_flask_login:
self.skipTest("Skipped without Flask-Login.") self.skipTest("Skipped without Flask-Login.")
response: Response = self.client.get(self.app.url_for("admin-1")) response: Response = self.client.get(self.app.url_for("admin-1"))
@ -187,14 +178,14 @@ class FlaskLoginTestCase(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.decode("UTF-8"), self.assertEqual(response.data.decode("UTF-8"),
f"Hello, {_USERNAME}! #2") f"Hello, {_USERNAME}! #2")
self.assertEqual(self.__user.visits, 1) self.assertEqual(self.user.visits, 1)
def test_stale_opaque(self) -> None: def test_stale_opaque(self) -> None:
"""Tests the stale and opaque value. """Tests the stale and opaque value.
:return: None. :return: None.
""" """
if not self.__has_flask_login: if not self.has_flask_login:
self.skipTest("Skipped without Flask-Login.") self.skipTest("Skipped without Flask-Login.")
admin_uri: str = self.app.url_for("admin-1") admin_uri: str = self.app.url_for("admin-1")
@ -242,7 +233,7 @@ class FlaskLoginTestCase(TestCase):
:return: None. :return: None.
""" """
if not self.__has_flask_login: if not self.has_flask_login:
self.skipTest("Skipped without Flask-Login.") self.skipTest("Skipped without Flask-Login.")
admin_uri: str = self.app.url_for("admin-1") admin_uri: str = self.app.url_for("admin-1")
@ -276,33 +267,33 @@ class FlaskLoginTestCase(TestCase):
response = self.client.get(admin_uri) response = self.client.get(admin_uri)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(self.__user.visits, 2) self.assertEqual(self.user.visits, 2)
def test_disabled(self) -> None: def test_disabled(self) -> None:
"""Tests the disabled user. """Tests the disabled user.
:return: None. :return: None.
""" """
if not self.__has_flask_login: if not self.has_flask_login:
self.skipTest("Skipped without Flask-Login.") self.skipTest("Skipped without Flask-Login.")
response: Response response: Response
self.__user.is_active = False self.user.is_active = False
response = self.client.get(self.app.url_for("admin-1")) response = self.client.get(self.app.url_for("admin-1"))
self.assertEqual(response.status_code, 401) self.assertEqual(response.status_code, 401)
response = self.client.get(self.app.url_for("admin-1"), response = self.client.get(self.app.url_for("admin-1"),
digest_auth=(_USERNAME, _PASSWORD)) digest_auth=(_USERNAME, _PASSWORD))
self.assertEqual(response.status_code, 401) 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"), response = self.client.get(self.app.url_for("admin-1"),
digest_auth=(_USERNAME, _PASSWORD)) digest_auth=(_USERNAME, _PASSWORD))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(self.app.url_for("admin-1")) response = self.client.get(self.app.url_for("admin-1"))
self.assertEqual(response.status_code, 200) 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")) response = self.client.get(self.app.url_for("admin-1"))
self.assertEqual(response.status_code, 401) self.assertEqual(response.status_code, 401)
response = self.client.get(self.app.url_for("admin-1"), response = self.client.get(self.app.url_for("admin-1"),