Compare commits
3 Commits
7e71115844
...
v0.2.1
Author | SHA1 | Date | |
---|---|---|---|
495e9a9785 | |||
cbbd2248f0 | |||
2028cb1362 |
40
README.rst
40
README.rst
@ -359,10 +359,13 @@ Writing Tests
|
|||||||
=============
|
=============
|
||||||
|
|
||||||
You can write tests with our test client that handles HTTP Digest
|
You can write tests with our test client that handles HTTP Digest
|
||||||
Authentication. Example for a unittest testcase:
|
Authentication.
|
||||||
|
|
||||||
|
Example for a unittest_ test case:
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
from flask import Flask
|
||||||
from flask_digest_auth import Client
|
from flask_digest_auth import Client
|
||||||
from flask_testing import TestCase
|
from flask_testing import TestCase
|
||||||
from my_app import create_app
|
from my_app import create_app
|
||||||
@ -385,6 +388,41 @@ Authentication. Example for a unittest testcase:
|
|||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Example for a pytest_ test:
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from flask import Flask
|
||||||
|
from flask_digest_auth import Client
|
||||||
|
from my_app import create_app
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def app():
|
||||||
|
app: Flask = create_app({
|
||||||
|
"SECRET_KEY": token_urlsafe(32),
|
||||||
|
"TESTING": True
|
||||||
|
})
|
||||||
|
app.test_client_class = Client
|
||||||
|
yield app
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def client(app):
|
||||||
|
return app.test_client()
|
||||||
|
|
||||||
|
def test_admin(app: Flask, client: Client):
|
||||||
|
with app.app_context():
|
||||||
|
response = self.client.get("/admin")
|
||||||
|
assert response.status_code == 401
|
||||||
|
response = self.client.get(
|
||||||
|
"/admin", digest_auth=("my_name", "my_pass"))
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
.. _unittest: https://docs.python.org/3/library/unittest.html
|
||||||
|
.. _pytest: https://pytest.org
|
||||||
|
|
||||||
|
|
||||||
Copyright
|
Copyright
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
[metadata]
|
[metadata]
|
||||||
name = flask-digest-auth
|
name = flask-digest-auth
|
||||||
version = 0.2.0
|
version = 0.2.1
|
||||||
author = imacat
|
author = imacat
|
||||||
author_email = imacat@mail.imacat.idv.tw
|
author_email = imacat@mail.imacat.idv.tw
|
||||||
description = The Flask HTTP Digest Authentication project.
|
description = The Flask HTTP Digest Authentication project.
|
||||||
|
@ -35,14 +35,15 @@ _PASSWORD: str = "Circle Of Life"
|
|||||||
class User:
|
class User:
|
||||||
"""A dummy user"""
|
"""A dummy user"""
|
||||||
|
|
||||||
def __init__(self, username: str, password_hash: str):
|
def __init__(self, username: str, password: str):
|
||||||
"""Constructs a dummy user.
|
"""Constructs a dummy user.
|
||||||
|
|
||||||
:param username: The username.
|
:param username: The username.
|
||||||
:param password_hash: The password hash.
|
:param password: The clear-text password.
|
||||||
"""
|
"""
|
||||||
self.username: str = username
|
self.username: str = username
|
||||||
self.password_hash: str = password_hash
|
self.password_hash: str = make_password_hash(
|
||||||
|
_REALM, username, password)
|
||||||
self.visits: int = 0
|
self.visits: int = 0
|
||||||
|
|
||||||
|
|
||||||
@ -63,8 +64,7 @@ class AuthenticationTestCase(TestCase):
|
|||||||
|
|
||||||
auth: DigestAuth = DigestAuth(realm=_REALM)
|
auth: DigestAuth = DigestAuth(realm=_REALM)
|
||||||
auth.init_app(app)
|
auth.init_app(app)
|
||||||
self.user: User = User(
|
self.user: User = User(_USERNAME, _PASSWORD)
|
||||||
_USERNAME, make_password_hash(_REALM, _USERNAME, _PASSWORD))
|
|
||||||
user_db: t.Dict[str, User] = {_USERNAME: self.user}
|
user_db: t.Dict[str, User] = {_USERNAME: self.user}
|
||||||
|
|
||||||
@auth.register_get_password
|
@auth.register_get_password
|
||||||
|
@ -35,14 +35,15 @@ _PASSWORD: str = "Circle Of Life"
|
|||||||
class User:
|
class User:
|
||||||
"""A dummy user."""
|
"""A dummy user."""
|
||||||
|
|
||||||
def __init__(self, username: str, password_hash: str):
|
def __init__(self, username: str, password: str):
|
||||||
"""Constructs a dummy user.
|
"""Constructs a dummy user.
|
||||||
|
|
||||||
:param username: The username.
|
:param username: The username.
|
||||||
:param password_hash: The password hash.
|
:param password: The clear-text password.
|
||||||
"""
|
"""
|
||||||
self.username: str = username
|
self.username: str = username
|
||||||
self.password_hash: str = password_hash
|
self.password_hash: str = make_password_hash(
|
||||||
|
_REALM, username, password)
|
||||||
self.visits: int = 0
|
self.visits: int = 0
|
||||||
self.is_authenticated: bool = True
|
self.is_authenticated: bool = True
|
||||||
self.is_active: bool = True
|
self.is_active: bool = True
|
||||||
@ -85,8 +86,7 @@ class FlaskLoginTestCase(TestCase):
|
|||||||
auth: DigestAuth = DigestAuth(realm=_REALM)
|
auth: DigestAuth = DigestAuth(realm=_REALM)
|
||||||
auth.init_app(app)
|
auth.init_app(app)
|
||||||
|
|
||||||
self.user: User = User(
|
self.user: User = User(_USERNAME, _PASSWORD)
|
||||||
_USERNAME, make_password_hash(_REALM, _USERNAME, _PASSWORD))
|
|
||||||
user_db: t.Dict[str, User] = {_USERNAME: self.user}
|
user_db: t.Dict[str, User] = {_USERNAME: self.user}
|
||||||
|
|
||||||
@auth.register_get_password
|
@auth.register_get_password
|
||||||
|
Reference in New Issue
Block a user