housekeep(fastapi): rewrite test_auth with fixtures

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-11-30 16:30:33 -08:00
parent 735c5f57cb
commit d6cb3b9fac
No known key found for this signature in database
GPG key ID: F7E46DED420788F3

View file

@ -11,35 +11,40 @@ from aurweb.models.session import Session
from aurweb.models.user import User from aurweb.models.user import User
from aurweb.testing.requests import Request from aurweb.testing.requests import Request
user = backend = request = None
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def setup(db_test): def setup(db_test):
global user, backend, request return
@pytest.fixture
def user() -> User:
with db.begin(): with db.begin():
user = db.create(User, Username="test", Email="test@example.com", user = db.create(User, Username="test", Email="test@example.com",
RealName="Test User", Passwd="testPassword", RealName="Test User", Passwd="testPassword",
AccountTypeID=USER_ID) AccountTypeID=USER_ID)
yield user
backend = BasicAuthBackend()
request = Request() @pytest.fixture
def backend() -> BasicAuthBackend:
yield BasicAuthBackend()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_auth_backend_missing_sid(): async def test_auth_backend_missing_sid(backend: BasicAuthBackend):
# The request has no AURSID cookie, so authentication fails, and # The request has no AURSID cookie, so authentication fails, and
# AnonymousUser is returned. # AnonymousUser is returned.
_, result = await backend.authenticate(request) _, result = await backend.authenticate(Request())
assert not result.is_authenticated() assert not result.is_authenticated()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_auth_backend_invalid_sid(): async def test_auth_backend_invalid_sid(backend: BasicAuthBackend):
# Provide a fake AURSID that won't be found in the database. # Provide a fake AURSID that won't be found in the database.
# This results in our path going down the invalid sid route, # This results in our path going down the invalid sid route,
# which gives us an AnonymousUser. # which gives us an AnonymousUser.
request = Request()
request.cookies["AURSID"] = "fake" request.cookies["AURSID"] = "fake"
_, result = await backend.authenticate(request) _, result = await backend.authenticate(request)
assert not result.is_authenticated() assert not result.is_authenticated()
@ -55,13 +60,15 @@ async def test_auth_backend_invalid_user_id():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_basic_auth_backend(): async def test_basic_auth_backend(user: User, backend: BasicAuthBackend):
# This time, everything matches up. We expect the user to # This time, everything matches up. We expect the user to
# equal the real_user. # equal the real_user.
now_ts = datetime.utcnow().timestamp() now_ts = datetime.utcnow().timestamp()
with db.begin(): with db.begin():
db.create(Session, UsersID=user.ID, SessionID="realSession", db.create(Session, UsersID=user.ID, SessionID="realSession",
LastUpdateTS=now_ts + 5) LastUpdateTS=now_ts + 5)
request = Request()
request.cookies["AURSID"] = "realSession" request.cookies["AURSID"] = "realSession"
_, result = await backend.authenticate(request) _, result = await backend.authenticate(request)
assert result == user assert result == user