Revised the AuthenticationTestCase and FlaskLoginTestCase test cases for simplicity and readability.
This commit is contained in:
parent
78514a8f17
commit
ccec1365bf
@ -71,19 +71,19 @@ class AuthenticationTestCase(TestCase):
|
|||||||
return SimpleNamespace(username=username) if username in user_db \
|
return SimpleNamespace(username=username) if username in user_db \
|
||||||
else None
|
else None
|
||||||
|
|
||||||
@app.get("/login-required-1/auth", endpoint="auth-1")
|
@app.get("/admin-1/auth", endpoint="admin-1")
|
||||||
@auth.login_required
|
@auth.login_required
|
||||||
def login_required_1() -> str:
|
def admin_1() -> str:
|
||||||
"""The first dummy view.
|
"""The first administration section.
|
||||||
|
|
||||||
:return: The response.
|
:return: The response.
|
||||||
"""
|
"""
|
||||||
return f"Hello, {g.user.username}! #1"
|
return f"Hello, {g.user.username}! #1"
|
||||||
|
|
||||||
@app.get("/login-required-2/auth", endpoint="auth-2")
|
@app.get("/admin-2/auth", endpoint="admin-2")
|
||||||
@auth.login_required
|
@auth.login_required
|
||||||
def login_required_2() -> str:
|
def admin_2() -> str:
|
||||||
"""The second dummy view.
|
"""The second administration section.
|
||||||
|
|
||||||
:return: The response.
|
:return: The response.
|
||||||
"""
|
"""
|
||||||
@ -96,14 +96,14 @@ class AuthenticationTestCase(TestCase):
|
|||||||
|
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
response: Response = self.client.get(self.app.url_for("auth-1"))
|
response: 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(
|
response = self.client.get(
|
||||||
self.app.url_for("auth-1"), digest_auth=(_USERNAME, _PASSWORD))
|
self.app.url_for("admin-1"), digest_auth=(_USERNAME, _PASSWORD))
|
||||||
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}! #1")
|
f"Hello, {_USERNAME}! #1")
|
||||||
response: Response = self.client.get(self.app.url_for("auth-2"))
|
response: Response = self.client.get(self.app.url_for("admin-2"))
|
||||||
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")
|
||||||
@ -113,29 +113,33 @@ class AuthenticationTestCase(TestCase):
|
|||||||
|
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
uri: str = self.app.url_for("auth-1")
|
admin_uri: str = self.app.url_for("admin-1")
|
||||||
response: Response = self.client.get(uri)
|
response: Response
|
||||||
|
www_authenticate: WWWAuthenticate
|
||||||
|
auth_data: Authorization
|
||||||
|
|
||||||
|
response = self.client.get(admin_uri)
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, 401)
|
||||||
www_authenticate: WWWAuthenticate = response.www_authenticate
|
www_authenticate = response.www_authenticate
|
||||||
self.assertEqual(www_authenticate.type, "digest")
|
self.assertEqual(www_authenticate.type, "digest")
|
||||||
self.assertEqual(www_authenticate.stale, None)
|
self.assertEqual(www_authenticate.stale, None)
|
||||||
|
|
||||||
www_authenticate.nonce = "bad"
|
www_authenticate.nonce = "bad"
|
||||||
auth_data: Authorization = Client.make_authorization(
|
auth_data = Client.make_authorization(
|
||||||
www_authenticate, uri, _USERNAME, _PASSWORD)
|
www_authenticate, admin_uri, _USERNAME, _PASSWORD)
|
||||||
response = self.client.get(uri, auth=auth_data)
|
response = self.client.get(admin_uri, auth=auth_data)
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, 401)
|
||||||
www_authenticate = response.www_authenticate
|
www_authenticate = response.www_authenticate
|
||||||
self.assertEqual(www_authenticate.stale, True)
|
self.assertEqual(www_authenticate.stale, True)
|
||||||
|
|
||||||
auth_data = Client.make_authorization(
|
auth_data = Client.make_authorization(
|
||||||
www_authenticate, uri, _USERNAME, _PASSWORD + "2")
|
www_authenticate, admin_uri, _USERNAME, _PASSWORD + "2")
|
||||||
response = self.client.get(uri, auth=auth_data)
|
response = self.client.get(admin_uri, auth=auth_data)
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, 401)
|
||||||
www_authenticate = response.www_authenticate
|
www_authenticate = response.www_authenticate
|
||||||
self.assertEqual(www_authenticate.stale, False)
|
self.assertEqual(www_authenticate.stale, False)
|
||||||
|
|
||||||
auth_data = Client.make_authorization(
|
auth_data = Client.make_authorization(
|
||||||
www_authenticate, uri, _USERNAME, _PASSWORD)
|
www_authenticate, admin_uri, _USERNAME, _PASSWORD)
|
||||||
response = self.client.get(uri, auth=auth_data)
|
response = self.client.get(admin_uri, auth=auth_data)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
@ -97,19 +97,19 @@ class FlaskLoginTestCase(TestCase):
|
|||||||
"""
|
"""
|
||||||
return User(user_id) if user_id in user_db else None
|
return User(user_id) if user_id in user_db else None
|
||||||
|
|
||||||
@app.get("/login-required-1/auth", endpoint="auth-1")
|
@app.get("/admin-1/auth", endpoint="admin-1")
|
||||||
@flask_login.login_required
|
@flask_login.login_required
|
||||||
def login_required_1() -> str:
|
def admin_1() -> str:
|
||||||
"""The first dummy view.
|
"""The first administration section.
|
||||||
|
|
||||||
:return: The response.
|
:return: The response.
|
||||||
"""
|
"""
|
||||||
return f"Hello, {flask_login.current_user.username}! #1"
|
return f"Hello, {flask_login.current_user.username}! #1"
|
||||||
|
|
||||||
@app.get("/login-required-2/auth", endpoint="auth-2")
|
@app.get("/admin-2/auth", endpoint="admin-2")
|
||||||
@flask_login.login_required
|
@flask_login.login_required
|
||||||
def login_required_2() -> str:
|
def admin_2() -> str:
|
||||||
"""The second dummy view.
|
"""The second administration section.
|
||||||
|
|
||||||
:return: The response.
|
:return: The response.
|
||||||
"""
|
"""
|
||||||
@ -125,14 +125,14 @@ class FlaskLoginTestCase(TestCase):
|
|||||||
if not self.has_flask_login:
|
if not self.has_flask_login:
|
||||||
self.skipTest("Skipped testing Flask-Login integration without it.")
|
self.skipTest("Skipped testing Flask-Login integration without it.")
|
||||||
|
|
||||||
response: Response = self.client.get(self.app.url_for("auth-1"))
|
response: 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(
|
response = self.client.get(
|
||||||
self.app.url_for("auth-1"), digest_auth=(_USERNAME, _PASSWORD))
|
self.app.url_for("admin-1"), digest_auth=(_USERNAME, _PASSWORD))
|
||||||
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}! #1")
|
f"Hello, {_USERNAME}! #1")
|
||||||
response: Response = self.client.get(self.app.url_for("auth-2"))
|
response: Response = self.client.get(self.app.url_for("admin-2"))
|
||||||
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")
|
||||||
@ -142,19 +142,26 @@ class FlaskLoginTestCase(TestCase):
|
|||||||
|
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
uri: str = self.app.url_for("auth-1")
|
if not self.has_flask_login:
|
||||||
response: Response = self.client.get(uri)
|
self.skipTest("Skipped testing Flask-Login integration without it.")
|
||||||
|
|
||||||
|
admin_uri: str = self.app.url_for("admin-1")
|
||||||
|
response: Response
|
||||||
|
www_authenticate: WWWAuthenticate
|
||||||
|
auth_data: Authorization
|
||||||
|
|
||||||
|
response = self.client.get(admin_uri)
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, 401)
|
||||||
www_authenticate: WWWAuthenticate = response.www_authenticate
|
www_authenticate = response.www_authenticate
|
||||||
self.assertEqual(www_authenticate.type, "digest")
|
self.assertEqual(www_authenticate.type, "digest")
|
||||||
self.assertEqual(www_authenticate.stale, None)
|
self.assertEqual(www_authenticate.stale, None)
|
||||||
|
|
||||||
if hasattr(g, "_login_user"):
|
if hasattr(g, "_login_user"):
|
||||||
delattr(g, "_login_user")
|
delattr(g, "_login_user")
|
||||||
www_authenticate.nonce = "bad"
|
www_authenticate.nonce = "bad"
|
||||||
auth_data: Authorization = Client.make_authorization(
|
auth_data = Client.make_authorization(
|
||||||
www_authenticate, uri, _USERNAME, _PASSWORD)
|
www_authenticate, admin_uri, _USERNAME, _PASSWORD)
|
||||||
response = self.client.get(uri, auth=auth_data)
|
response = self.client.get(admin_uri, auth=auth_data)
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, 401)
|
||||||
www_authenticate = response.www_authenticate
|
www_authenticate = response.www_authenticate
|
||||||
self.assertEqual(www_authenticate.stale, True)
|
self.assertEqual(www_authenticate.stale, True)
|
||||||
@ -162,8 +169,8 @@ class FlaskLoginTestCase(TestCase):
|
|||||||
if hasattr(g, "_login_user"):
|
if hasattr(g, "_login_user"):
|
||||||
delattr(g, "_login_user")
|
delattr(g, "_login_user")
|
||||||
auth_data = Client.make_authorization(
|
auth_data = Client.make_authorization(
|
||||||
www_authenticate, uri, _USERNAME, _PASSWORD + "2")
|
www_authenticate, admin_uri, _USERNAME, _PASSWORD + "2")
|
||||||
response = self.client.get(uri, auth=auth_data)
|
response = self.client.get(admin_uri, auth=auth_data)
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, 401)
|
||||||
www_authenticate = response.www_authenticate
|
www_authenticate = response.www_authenticate
|
||||||
self.assertEqual(www_authenticate.stale, False)
|
self.assertEqual(www_authenticate.stale, False)
|
||||||
@ -171,6 +178,6 @@ class FlaskLoginTestCase(TestCase):
|
|||||||
if hasattr(g, "_login_user"):
|
if hasattr(g, "_login_user"):
|
||||||
delattr(g, "_login_user")
|
delattr(g, "_login_user")
|
||||||
auth_data = Client.make_authorization(
|
auth_data = Client.make_authorization(
|
||||||
www_authenticate, uri, _USERNAME, _PASSWORD)
|
www_authenticate, admin_uri, _USERNAME, _PASSWORD)
|
||||||
response = self.client.get(uri, auth=auth_data)
|
response = self.client.get(admin_uri, auth=auth_data)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
Loading…
Reference in New Issue
Block a user