From c3588d6b3c41c2fe41db73f7e03d643e24e7fb5d Mon Sep 17 00:00:00 2001 From: Andrew Myers Date: Thu, 16 Nov 2023 05:58:22 -0500 Subject: [PATCH] Update tests to set session data the new way --- tests/test_batch_actions.py | 63 ++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/tests/test_batch_actions.py b/tests/test_batch_actions.py index 566b647..350f604 100644 --- a/tests/test_batch_actions.py +++ b/tests/test_batch_actions.py @@ -1,19 +1,23 @@ import pytest +from fastapi.testclient import TestClient from httpx import AsyncClient from typing import Type, Callable from chowda.config import AUTH0_API_AUDIENCE +from starlette.responses import Response @pytest.mark.asyncio -@pytest.mark.skip(reason="TODO: Setting session data for test is buggy") -async def test_download_mmif_as_clammer( - async_client: AsyncClient, set_session_data: Type[Callable] -): - set_session_data( - {'user': {"name": "test user", f"{AUTH0_API_AUDIENCE}/roles": ["clammer"]}} - ) - +async def test_download_mmif_as_clammer(async_client: AsyncClient): async with async_client as ac: + await ac.post( + "/test/session", + json={ + "user": { + "name": "test user", + f"{AUTH0_API_AUDIENCE}/roles": ["clammer"], + } + }, + ) response = await ac.get( "/admin/api/batch/action", params={"pks": [2, 3], "name": "download_mmif"}, @@ -22,15 +26,17 @@ async def test_download_mmif_as_clammer( @pytest.mark.asyncio -@pytest.mark.skip(reason="TODO: Setting session data for test is buggy") -async def test_download_mmif_as_admin( - async_client: AsyncClient, set_session_data: Type[Callable] -): - set_session_data( - {'user': {"name": "test user", f"{AUTH0_API_AUDIENCE}/roles": ["admin"]}} - ) - +async def test_download_mmif_as_admin(async_client: AsyncClient): async with async_client as ac: + await ac.post( + "/test/session", + json={ + "user": { + "name": "test user", + f"{AUTH0_API_AUDIENCE}/roles": ["admin"], + } + }, + ) response = await ac.get( "/admin/api/batch/action", params={"pks": [2, 3], "name": "download_mmif"}, @@ -39,11 +45,7 @@ async def test_download_mmif_as_admin( @pytest.mark.asyncio -@pytest.mark.skip(reason="TODO: Setting session data for test is buggy") -async def test_download_mmif_unauthenticated( - async_client: AsyncClient, set_session_data: Type[Callable] -): - set_session_data(None) +async def test_download_mmif_unauthenticated(async_client: AsyncClient): """Assumes no user is in the session; responds with 401 Unauthorized.""" async with async_client as ac: response = await ac.get( @@ -54,15 +56,18 @@ async def test_download_mmif_unauthenticated( @pytest.mark.asyncio -@pytest.mark.skip(reason="TODO: Setting session data for test is buggy") -async def test_download_mmif_insufficient_role( - async_client: AsyncClient, set_session_data: Type[Callable] -): - set_session_data( - {'user': {"name": "test user", f"{AUTH0_API_AUDIENCE}/roles": ["peon"]}} - ) - +async def test_download_mmif_insufficient_role(async_client: AsyncClient): async with async_client as ac: + await ac.post( + "/test/session", + json={ + "user": { + "name": "test user", + f"{AUTH0_API_AUDIENCE}/roles": ["peon"], + } + }, + ) + response = await ac.get( "/admin/api/batch/action", params={"pks": [2, 3], "name": "download_mmif"},