Skip to content

Commit

Permalink
Feature/pdct 1274 Fix logic for validating corpora IDs (#348)
Browse files Browse the repository at this point in the history
* Rename function from decode_configuration_token

* Remove space between hyphen and command

* Raise custom HTTP exception on decode error & reuse config vars

* Remove commented out code

* Apply custom app config token to search query

* Bump CPR SDK to 1.4.3

* Don't mock vespa search adapter

* Revert "Don't mock vespa search adapter"

This reverts commit 358d1c2.

* Raise HTTPException in router not service layer

* Bump CPR SDK to 1.4.4

* Wrap decode in try except block

* Make all vespa search tests use valid_token

* Reduce duplication

* Use fastAPI status for contextualising HTTP return code

* Move corpus verification to search router

* Move metadata vespa tests into separate file

* Raise 400 error if corpus validation fails

* Make sure ruff version is compatible with VScode ext

* Consolidate steps as make start includes vespa_setup

* Use different secret key for custom app token

* Make audience localhost:3000 for test token

* Fix validate_corpora_ids

* Log actual JWT error to console

* Remove validation of corpora from decode tests

* Bump to 1.16.1

* Use fastapi status to contextualise http error code

* Add tests for vespa search returning 400 when config token invalid

* Make sure audience has trailing slash

* Rename from SECRET_KEY to differentiate between keys

* Bump to 1.16.0

* Fix decode token unit tests

* Calculate valid config token for tests on the fly

* Update debug message

* Validate corpora IDs against db by default

* Bump to 1.16.1

* Bump to 1.16.3

* Remove debug
  • Loading branch information
katybaulch authored Sep 23, 2024
1 parent d72317d commit be9bd8d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/api/api_v1/routers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def search_documents(
)

if not validate_corpora_ids(db, allowed_corpora_ids):
msg = "One or more of the given corpora do not exist in the database."
msg = "Error validating corpora IDs."
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=msg,
Expand Down
26 changes: 17 additions & 9 deletions app/db/crud/helpers/validate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Optional, cast

from db_client.models.dfce.family import Corpus
from sqlalchemy import distinct, select
Expand All @@ -7,16 +8,23 @@
_LOGGER = logging.getLogger(__name__)


def validate_corpora_ids(db: Session, allowed_corpora_ids: list[str]) -> bool:
"""Validate whether all given corpus IDs exist in the DB.
def validate_corpora_ids(
db: Session, corpora_ids: list[str], allowed_corpora_ids: Optional[list[str]] = None
) -> bool:
"""Validate all given corpus IDs against a list of allowed corpora.
:param Session db: The DB session to connect to.
:param list[str] allowed_corpora_ids: The corpus import IDs we want
to validate.
:return bool: Return whether or not all the corpora exist in the DB.
:param list[str] corpora_ids: The corpus import IDs we want to
validate.
:param Optional[list[str]] allowed_corpora_ids: The corpus import
IDs we want to validate against.
:return bool: Return whether or not all the corpora are valid.
"""
existing_corpora_in_db = db.scalars(select(distinct(Corpus.import_id))).all()
validate_success = all(
corpus in allowed_corpora_ids for corpus in existing_corpora_in_db
)
if allowed_corpora_ids is None:
allowed_corpora_ids = cast(
list, db.scalars(select(distinct(Corpus.import_id))).all()
)
_LOGGER.info(allowed_corpora_ids) # TODO remove in part 2.

validate_success = all(corpus in allowed_corpora_ids for corpus in corpora_ids)
return validate_success
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "navigator_backend"
version = "1.16.2"
version = "1.16.3"
description = ""
authors = ["CPR-dev-team <[email protected]>"]
packages = [{ include = "app" }, { include = "tests" }]
Expand Down
5 changes: 1 addition & 4 deletions tests/search/vespa/test_search_raises_on_token_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ def test_search_with_invalid_corpus_id_in_token(
expected_status_code=status.HTTP_400_BAD_REQUEST,
)

assert (
response["detail"]
== "One or more of the given corpora do not exist in the database."
)
assert response["detail"] == "Error validating corpora IDs."


@pytest.mark.search
Expand Down

0 comments on commit be9bd8d

Please sign in to comment.