Skip to content

Commit

Permalink
SPARQL endpoint opt in (#257)
Browse files Browse the repository at this point in the history
* Make SPARQL endpoint opt-in feature; to prevent direct database by default.
Add example responses / mediatypes to SPARQL get endpoint.

* fix: failing tests

Test suite updated to enable the sparql endpoint now that it is opt in.
Also modified the sparql endpoint assertions as they were always true.

* 1 -> true

---------

Co-authored-by: Lawon Lewis <[email protected]>
  • Loading branch information
recalcitrantsupplant and lalewis1 authored Aug 27, 2024
1 parent 86a9c36 commit c5d3931
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion prez/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ def assemble_app(

app.include_router(management_router)
app.include_router(ogc_records_router)
app.include_router(sparql_router)
if _settings.enable_sparql_endpoint:
app.include_router(sparql_router)
app.include_router(identifier_router)
app.openapi = partial(
prez_open_api_metadata,
Expand Down
1 change: 1 addition & 0 deletions prez/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Settings(BaseSettings):
EP["system/profile-listing"],
EP["system/profile-object"],
]
enable_sparql_endpoint: bool = False

@field_validator("prez_version")
@classmethod
Expand Down
3 changes: 2 additions & 1 deletion prez/routers/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from prez.dependencies import get_data_repo, get_system_repo
from prez.renderers.renderer import return_annotated_rdf
from prez.repositories import Repo
from prez.routers.api_extras_examples import responses
from prez.services.connegp_service import NegotiatedPMTs

PREZ = Namespace("https://prez.dev/")
Expand All @@ -35,7 +36,7 @@ async def sparql_post_passthrough(
)


@router.get("/sparql")
@router.get("/sparql", responses=responses)
async def sparql_get_passthrough(
query: str,
request: Request,
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# os.environ["SPARQL_ENDPOINT"] = "http://localhost:3030/dataset"
# os.environ["SPARQL_REPO_TYPE"] = "remote"
os.environ["ENABLE_SPARQL_ENDPOINT"] = "true"

from pathlib import Path

Expand Down
8 changes: 4 additions & 4 deletions tests/test_sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ def test_select(client):
r = client.get(
"/sparql?query=SELECT%20*%0AWHERE%20%7B%0A%20%20%3Fs%20%3Fp%20%3Fo%0A%7D%20LIMIT%201"
)
assert (r.status_code, 200)
assert r.status_code == 200


def test_construct(client):
"""check that a valid construct query returns a 200 response."""
r = client.get(
"/sparql?query=CONSTRUCT%20%7B%0A%20%20%3Fs%20%3Fp%20%3Fo%0A%7D%20WHERE%20%7B%0A%20%20%3Fs%20%3Fp%20%3Fo%0A%7D%20LIMIT%201"
)
assert (r.status_code, 200)
assert r.status_code == 200


def test_ask(client):
"""check that a valid ask query returns a 200 response."""
r = client.get(
"/sparql?query=PREFIX%20ex%3A%20%3Chttp%3A%2F%2Fexample.com%2Fdatasets%2F%3E%0APREFIX%20dcterms%3A%20%3Chttp%3A%2F%2Fpurl.org%2Fdc%2Fterms%2F%3E%0A%0AASK%0AWHERE%20%7B%0A%20%20%3Fsubject%20dcterms%3Atitle%20%3Ftitle%20.%0A%20%20FILTER%20CONTAINS(LCASE(%3Ftitle)%2C%20%22sandgate%22)%0A%7D"
)
assert (r.status_code, 200)
assert r.status_code == 200


def test_post(client):
Expand All @@ -31,7 +31,7 @@ def test_post(client):
"format": "application/x-www-form-urlencoded",
},
)
assert (r.status_code, 200)
assert r.status_code == 200


def test_post_invalid_data(client):
Expand Down

0 comments on commit c5d3931

Please sign in to comment.