Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDX-9801 tests for app identifier #84

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,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
Expand All @@ -74,6 +72,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')
Expand All @@ -91,7 +90,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
63 changes: 63 additions & 0 deletions tests/test_endpoints/test_endpoints_vs_encode_identifier.py
Original file line number Diff line number Diff line change
@@ -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
Loading