Skip to content

Commit

Permalink
fix public endpoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devxpy committed Jun 26, 2024
1 parent cbb52d4 commit 9510f40
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion auth/auth_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def force_authentication():
AppUser.objects.get_or_create(
email="[email protected]",
defaults=dict(
is_anonymous=True,
is_anonymous=False,
uid=get_random_doc_id(),
balance=10**9,
disable_rate_limits=True,
Expand Down
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def iterq():


@pytest.fixture
def threadpool_subtest(subtests, max_workers: int = 8):
def threadpool_subtest(subtests, max_workers: int = 128):
ts = []

def submit(fn, *args, msg=None, **kwargs):
Expand Down
29 changes: 13 additions & 16 deletions daras_ai_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from daras_ai_v2.db import (
ANONYMOUS_USER_COOKIE,
)
from daras_ai_v2.fastapi_tricks import get_route_path
from daras_ai_v2.grid_layout_widget import grid_layout
from daras_ai_v2.html_spinner_widget import html_spinner
from daras_ai_v2.manage_api_keys_widget import manage_api_keys
Expand Down Expand Up @@ -1739,23 +1740,9 @@ def _render(pr: PublishedRun):

grid_layout(3, published_runs, _render)

def ensure_authentication(self, next_url: str | None = None):
if not self.request.user or self.request.user.is_anonymous:
raise RedirectException(self.get_auth_url(next_url))

def get_auth_url(self, next_url: str | None = None) -> str:
return furl(
"/login",
query_params={"next": furl(next_url or self.request.url).set(origin=None)},
).tostr()

def _history_tab(self):
assert self.request, "request must be set to render history tab"
if not self.request.user:
redirect_url = furl(
"/login", query_params={"next": furl(self.request.url).set(origin=None)}
)
raise RedirectException(str(redirect_url))
self.ensure_authentication(anon_ok=True)

uid = self.request.user.uid
if self.is_current_user_admin():
uid = self.request.query_params.get("uid", uid)
Expand Down Expand Up @@ -1788,6 +1775,16 @@ def _history_tab(self):
f"""<button type="button" class="btn btn-theme">Load More</button>"""
)

def ensure_authentication(self, next_url: str | None = None, anon_ok: bool = False):
if not self.request.user or (self.request.user.is_anonymous and not anon_ok):
raise RedirectException(self.get_auth_url(next_url))

def get_auth_url(self, next_url: str | None = None) -> str:
from routers.root import login

next_url = str(furl(next_url or self.request.url).set(origin=None))
return str(furl(get_route_path(login), query_params=dict(next=next_url)))

def _render_run_preview(self, saved_run: SavedRun):
published_run: PublishedRun | None = (
saved_run.parent_version.published_run if saved_run.parent_version else None
Expand Down
13 changes: 10 additions & 3 deletions tests/test_public_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _test_get_path(path):


@pytest.mark.django_db
def test_all_tabs(threadpool_subtest):
def test_all_tabs(force_authentication, threadpool_subtest):
for page_cls in all_api_pages + all_hidden_pages:
for slug in page_cls.slug_versions:
for tab in RecipeTabs:
Expand All @@ -66,5 +66,12 @@ def test_all_examples(threadpool_subtest):


def _test_post_path(url):
r = client.post(url, json={}, allow_redirects=True)
assert r.ok, r.content
for _ in range(10):
r = client.post(url, json={}, allow_redirects=False)
if r.is_redirect:
url = r.headers["Location"]
continue
assert r.ok, r.content
return
else:
assert False, f"Too many redirects: {url}"

0 comments on commit 9510f40

Please sign in to comment.