-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DM-48733: Another template rendering endpoint
Expose template rendering for a GitHub notebook template at a different path. This path includes the GitHub URL path to the notebook. This is an additional path, all existing functionality, including the existing template rendering path, remains unchanged. We need to deploy Times Square to environments where some users should not have permissions to execute notebooks, but they should have permissions to render notebook templates for certain GitHub-based notebooks. This will let us configure that access via methods that apply permissions based on URL paths.
- Loading branch information
Showing
7 changed files
with
216 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
"""Tests for the `/v1/rendered... endpoints.""" | ||
|
||
from datetime import UTC, datetime | ||
from pathlib import Path | ||
|
||
import nbformat | ||
import pytest | ||
from httpx import AsyncClient | ||
from redis.asyncio import Redis | ||
from safir.arq import MockArqQueue | ||
from safir.database import create_async_session, create_database_engine | ||
from structlog import get_logger | ||
|
||
from timessquare.config import config | ||
from timessquare.domain.githubcheckout import NotebookSidecarFile | ||
from timessquare.domain.page import PageModel | ||
from timessquare.services.page import PageService | ||
from timessquare.storage.nbhtmlcache import NbHtmlCacheStore | ||
from timessquare.storage.noteburstjobstore import NoteburstJobStore | ||
from timessquare.storage.page import PageStore | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_rendered(client: AsyncClient) -> None: | ||
"""Test the /v1/rendered APIs with mocked sources (i.e. this does not test | ||
syncing content from GitHub, only reading github-backed apges. | ||
""" | ||
data_path = Path(__file__).parent.joinpath("../../data") | ||
demo_path = data_path / "demo.ipynb" | ||
sidecar_path = data_path / "times-square-demo" / "demo.yaml" | ||
|
||
engine = create_database_engine( | ||
str(config.database_url), config.database_password.get_secret_value() | ||
) | ||
session = await create_async_session(engine) | ||
|
||
redis = Redis.from_url(str(config.redis_url)) | ||
|
||
page_service = PageService( | ||
page_store=PageStore(session=session), | ||
html_cache=NbHtmlCacheStore(redis), | ||
job_store=NoteburstJobStore(redis), | ||
http_client=client, | ||
logger=get_logger(), | ||
arq_queue=MockArqQueue(), | ||
) | ||
|
||
# In real life, parameters | ||
sidecar = NotebookSidecarFile.parse_yaml(sidecar_path.read_text()) | ||
|
||
await page_service.add_page_to_store( | ||
PageModel( | ||
name="1", | ||
ipynb=demo_path.read_text(), | ||
parameters=sidecar.export_parameters(), | ||
title="Demo", | ||
date_added=datetime.now(UTC), | ||
date_deleted=None, | ||
github_owner="lsst-sqre", | ||
github_repo="times-square-demo", | ||
repository_path_prefix="", | ||
repository_display_path_prefix="", | ||
repository_path_stem="demo", | ||
repository_sidecar_extension=".yaml", | ||
repository_source_extension=".ipynb", | ||
repository_source_sha="1" * 40, | ||
repository_sidecar_sha="1" * 40, | ||
) | ||
) | ||
|
||
await session.commit() | ||
|
||
# Close the set up database | ||
await session.remove() | ||
await engine.dispose() | ||
|
||
rendered_url = ( | ||
f"{config.path_prefix}/v1/" | ||
f"rendered/github/lsst-sqre/times-square-demo/demo" | ||
) | ||
|
||
r = await client.get(rendered_url) | ||
assert r.status_code == 200 | ||
notebook = nbformat.reads(r.text, as_version=4) | ||
assert notebook.cells[0].source == ( | ||
"# Times Square demo\n" | ||
"\n" | ||
"Plot parameters:\n" | ||
"\n" | ||
"- Amplitude: A = 4\n" | ||
"- Y offset: y0 = 0\n" | ||
"- Wavelength: lambd = 2" | ||
) | ||
assert notebook.metadata["times-square"]["values"] == { | ||
"A": 4, | ||
"y0": 0, | ||
"lambd": 2, | ||
} | ||
|
||
# Render the page template with some parameters set | ||
r = await client.get(rendered_url, params={"A": 2}) | ||
assert r.status_code == 200 | ||
notebook = nbformat.reads(r.text, as_version=4) | ||
assert notebook.cells[0].source == ( | ||
"# Times Square demo\n" | ||
"\n" | ||
"Plot parameters:\n" | ||
"\n" | ||
"- Amplitude: A = 2\n" | ||
"- Y offset: y0 = 0\n" | ||
"- Wavelength: lambd = 2" | ||
) | ||
assert notebook.metadata["times-square"]["values"] == { | ||
"A": 2, | ||
"y0": 0, | ||
"lambd": 2, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters