Skip to content

Commit

Permalink
Define a custom API root endpoint class
Browse files Browse the repository at this point in the history
closes pulp#2340
  • Loading branch information
lubosmj committed Jun 17, 2022
1 parent d77289c commit 68ee62d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/2340.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Made the API root endpoint accessible for anonymous users once again.
19 changes: 16 additions & 3 deletions pulpcore/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
SpectacularSwaggerView,
)
from rest_framework_nested import routers
from rest_framework.routers import APIRootView

from pulpcore.app.apps import pulp_plugin_configs
from pulpcore.app.views import OrphansView, PulpImporterImportCheckView, RepairView, StatusView
Expand Down Expand Up @@ -104,6 +105,19 @@ def __repr__(self):
return str(self.viewset)


class PulpAPIRootView(APIRootView):
"""A Pulp-defined APIRootView class with no authentication requirements."""

authentication_classes = []
permission_classes = []


class PulpDefaultRouter(routers.DefaultRouter):
"""A DefaultRouter class that benefits from the customized PulpAPIRootView class."""

APIRootView = PulpAPIRootView


all_viewsets = []
plugin_patterns = []
# Iterate over each app, including pulpcore and the plugins.
Expand All @@ -118,9 +132,6 @@ def __repr__(self):
for viewset in sorted_by_depth:
vs_tree.add_decendent(ViewSetNode(viewset))

#: The Pulp Platform v3 API router, which can be used to manually register ViewSets with the API.
root_router = routers.DefaultRouter()

urlpatterns = [
path(f"{API_ROOT}repair/", RepairView.as_view()),
path(f"{API_ROOT}status/", StatusView.as_view()),
Expand Down Expand Up @@ -184,6 +195,8 @@ def __repr__(self):
)
)

#: The Pulp Platform v3 API router, which can be used to manually register ViewSets with the API.
root_router = PulpDefaultRouter()

all_routers = [root_router] + vs_tree.register_with(root_router)
for router in all_routers:
Expand Down
5 changes: 5 additions & 0 deletions pulpcore/tests/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ def pulp_api_v3_path(cli_client):
return v3_api_root


@pytest.fixture(scope="session")
def pulp_api_v3_url(pulp_cfg, pulp_api_v3_path):
return f"{pulp_cfg.get_base_url()}{pulp_api_v3_path}"


@pytest.fixture
def random_artifact(random_artifact_factory):
return random_artifact_factory()
Expand Down
10 changes: 10 additions & 0 deletions pulpcore/tests/functional/api/test_root_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import asyncio
import pytest

from pulpcore.tests.functional.utils import get_response


@pytest.mark.parallel
def test_anonymous_access_to_root(pulp_api_v3_url):
response = asyncio.run(get_response(pulp_api_v3_url))
assert response.ok
7 changes: 7 additions & 0 deletions pulpcore/tests/functional/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Utilities for Pulpcore tests."""
import aiohttp

from functools import partial
from unittest import SkipTest

Expand All @@ -7,6 +9,11 @@
from pulpcore.client.pulpcore import ApiClient


async def get_response(url):
async with aiohttp.ClientSession() as session:
return await session.get(url)


skip_if = partial(selectors.skip_if, exc=SkipTest) # pylint:disable=invalid-name
"""The ``@skip_if`` decorator, customized for unittest.
Expand Down

0 comments on commit 68ee62d

Please sign in to comment.