Fixed the AuthenticationTestCase and FlaskLoginTestCase test cases to store the user instead of finding the user through flask-login or g, so that the visit tests work without the application context.

This commit is contained in:
依瑪貓 2022-12-03 11:55:52 +08:00
parent bbaebbc80d
commit 491da61a79
2 changed files with 10 additions and 9 deletions

View File

@ -63,8 +63,9 @@ class AuthenticationTestCase(TestCase):
auth: DigestAuth = DigestAuth(realm=_REALM)
auth.init_app(app)
pw_hash: str = make_password_hash(_REALM, _USERNAME, _PASSWORD)
user_db: t.Dict[str, User] = {_USERNAME: User(_USERNAME, pw_hash)}
self.user: User = User(
_USERNAME, make_password_hash(_REALM, _USERNAME, _PASSWORD))
user_db: t.Dict[str, User] = {_USERNAME: self.user}
@auth.register_get_password
def get_password_hash(username: str) -> t.Optional[str]:
@ -140,7 +141,7 @@ class AuthenticationTestCase(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.decode("UTF-8"),
f"Hello, {_USERNAME}! #2")
self.assertEqual(g.user.visits, 1)
self.assertEqual(self.user.visits, 1)
def test_stale_opaque(self) -> None:
"""Tests the stale and opaque value.
@ -217,4 +218,4 @@ class AuthenticationTestCase(TestCase):
response = self.client.get(admin_uri)
self.assertEqual(response.status_code, 200)
self.assertEqual(g.user.visits, 2)
self.assertEqual(self.user.visits, 2)

View File

@ -21,7 +21,6 @@
import typing as t
from secrets import token_urlsafe
import flask_login
from flask import Response, Flask, g, redirect, request
from flask_testing import TestCase
from werkzeug.datastructures import WWWAuthenticate, Authorization
@ -86,8 +85,9 @@ class FlaskLoginTestCase(TestCase):
auth: DigestAuth = DigestAuth(realm=_REALM)
auth.init_app(app)
pw_hash: str = make_password_hash(_REALM, _USERNAME, _PASSWORD)
user_db: t.Dict[str, User] = {_USERNAME: User(_USERNAME, pw_hash)}
self.user: User = User(
_USERNAME, make_password_hash(_REALM, _USERNAME, _PASSWORD))
user_db: t.Dict[str, User] = {_USERNAME: self.user}
@auth.register_get_password
def get_password_hash(username: str) -> t.Optional[str]:
@ -166,7 +166,7 @@ class FlaskLoginTestCase(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.decode("UTF-8"),
f"Hello, {_USERNAME}! #2")
self.assertEqual(flask_login.current_user.visits, 1)
self.assertEqual(self.user.visits, 1)
def test_stale_opaque(self) -> None:
"""Tests the stale and opaque value.
@ -252,4 +252,4 @@ class FlaskLoginTestCase(TestCase):
response = self.client.get(admin_uri)
self.assertEqual(response.status_code, 200)
self.assertEqual(flask_login.current_user.visits, 2)
self.assertEqual(self.user.visits, 2)