From 7df19ba72f71372bf4d863f131cd67e108ef250b Mon Sep 17 00:00:00 2001 From: Dan Mihaila Date: Thu, 9 May 2024 12:02:23 +0300 Subject: [PATCH 1/2] HDX-9801 tests for app identifier --- .../test_endpoints_vs_encode_identifier.py | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/test_endpoints/test_endpoints_vs_encode_identifier.py diff --git a/tests/test_endpoints/test_endpoints_vs_encode_identifier.py b/tests/test_endpoints/test_endpoints_vs_encode_identifier.py new file mode 100644 index 00000000..6776ac80 --- /dev/null +++ b/tests/test_endpoints/test_endpoints_vs_encode_identifier.py @@ -0,0 +1,63 @@ +# import base64 +import pytest +import logging + +from httpx import AsyncClient +from main import app +# from tests.test_endpoints.endpoint_data import endpoint_data + +log = logging.getLogger(__name__) + +ENDPOINT_ROUTER_LIST = [ + '/api/v1/admin1', + '/api/v1/admin2', + '/api/v1/dataset', + '/api/v1/themes/food_security', + '/api/v1/themes/humanitarian_needs', + '/api/v1/location', + '/api/v1/themes/national_risk', + '/api/v1/themes/3W', + '/api/v1/org', + '/api/v1/org_type', + '/api/v1/themes/population', + '/api/v1/population_group', + '/api/v1/population_status', + '/api/v1/resource', + '/api/v1/sector', +] + + +APP_IDENTIFIER = 'aGFwaV90ZXN0OmhhcGlAaHVtZGF0YS5vcmc=' +query_parameters = {'app_identifier': APP_IDENTIFIER} + + +@pytest.mark.asyncio +async def test_endpoints_vs_encode_identifier(event_loop, refresh_db, enable_hapi_identifier_filtering): + log.info('started test_endpoints_vs_encode_identifier') + + for endpoint_router in ENDPOINT_ROUTER_LIST: + async with AsyncClient(app=app, base_url='http://test') as ac: + response = await ac.get(endpoint_router) + assert response.status_code == 400 + + async with AsyncClient(app=app, base_url='http://test', params=query_parameters) as ac: + response = await ac.get(endpoint_router) + assert response.status_code == 200 + response_items = response.json() + assert len(response_items) > 0 + + +@pytest.mark.asyncio +async def test_encode_identifier(event_loop, refresh_db, enable_hapi_identifier_filtering): + # testing the encode identifier endpoint + endpoint_router = '/api/v1/encode_identifier' + + async with AsyncClient(app=app, base_url='http://test') as ac: + response = await ac.get(endpoint_router) + assert response.status_code == 200 + + async with AsyncClient(app=app, base_url='http://test', params=query_parameters) as ac: + response = await ac.get(endpoint_router) + assert response.status_code == 200 + response_items = response.json() + assert len(response_items) > 0 From a81212124c805bc24f580571956878796de2ce67 Mon Sep 17 00:00:00 2001 From: Dan Mihaila Date: Thu, 9 May 2024 12:10:53 +0300 Subject: [PATCH 2/2] HDX-9801 tests for app identifier --- tests/conftest.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b3e98a06..80c9c555 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,15 +9,13 @@ from sqlalchemy.orm import sessionmaker, Session from typing import List -from hdx_hapi.config.config import get_config - SAMPLE_DATA_SQL_FILE = 'alembic/versions/afd54d1a867e_insert_sample_data.sql' def pytest_sessionstart(session): - os.environ['HAPI_DB_NAME'] = 'hapi_test' - os.environ['HAPI_IDENTIFIER_FILTERING'] = 'False' + os.environ['HAPI_DB_NAME'] = 'hapi_test' + os.environ['HAPI_IDENTIFIER_FILTERING'] = 'False' @pytest.fixture(scope='session') @@ -34,6 +32,9 @@ def log(): @pytest.fixture(scope='session') def session_maker() -> sessionmaker[Session]: + # we don't want to import get_config before env vars are set for tests in pytest_sessionstart method + from hdx_hapi.config.config import get_config + engine = create_engine( get_config().SQL_ALCHEMY_PSYCOPG2_DB_URI, ) @@ -46,9 +47,7 @@ def list_of_db_tables(log: Logger, session_maker: sessionmaker[Session]) -> List # log.info('Getting list of db tables') session = session_maker() try: - result = session.execute( - text('SELECT tablename FROM pg_tables WHERE schemaname = \'public\'') - ) + result = session.execute(text("SELECT tablename FROM pg_tables WHERE schemaname = 'public'")) return [row[0] for row in result if row != 'alembic_version'] except Exception as e: raise e @@ -71,6 +70,7 @@ def clear_db_tables(log: Logger, session_maker: sessionmaker[Session], list_of_d finally: db_session.close() + @pytest.fixture(scope='function') def populate_test_data(log: Logger, session_maker: sessionmaker[Session]): log.info('Populating with test data') @@ -88,7 +88,17 @@ def populate_test_data(log: Logger, session_maker: sessionmaker[Session]): finally: db_session.close() + @pytest.fixture(scope='function') def refresh_db(clear_db_tables, populate_test_data): pass + +@pytest.fixture(scope='function') +def enable_hapi_identifier_filtering(): + import hdx_hapi.config.config as config + + initial_config_id_filtering = config.CONFIG.HAPI_IDENTIFIER_FILTERING + config.CONFIG.HAPI_IDENTIFIER_FILTERING = True + yield + config.CONFIG.HAPI_IDENTIFIER_FILTERING = initial_config_id_filtering