-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable v1 root drf browsable and fix staticfiles (#945)
Signed-off-by: Alex <aizquier@redhat.com> Co-authored-by: Madhu Kanoor <mkanoor@redhat.com>
- Loading branch information
1 parent
3c68bc2
commit c1fca5a
Showing
7 changed files
with
188 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 2024 Red Hat, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import logging | ||
|
||
from django.conf import settings | ||
from django.urls import URLPattern, URLResolver | ||
from rest_framework.response import Response | ||
from rest_framework.reverse import reverse | ||
from rest_framework.views import APIView | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class ApiV1RootView(APIView): | ||
def get(self, request, *args, **kwargs): | ||
urls = get_api_v1_urls(request=request) | ||
return Response(urls) | ||
|
||
|
||
def get_api_v1_urls(request=None): | ||
from aap_eda.api import urls | ||
|
||
def list_urls(urls): | ||
url_list = {} | ||
for url in urls: | ||
if isinstance(url, URLResolver): | ||
url_list.update(list_urls(url.url_patterns)) | ||
elif isinstance(url, URLPattern): | ||
name = url.name | ||
if not name: | ||
LOGGER.warning( | ||
"URL %s has no name, DRF browsable API will omit it", | ||
url.pattern, | ||
) | ||
continue | ||
if url.pattern.regex.groups: | ||
continue | ||
url_list[name] = reverse(name, request=request) | ||
return url_list | ||
|
||
if settings.ALLOW_LOCAL_RESOURCE_MANAGEMENT: | ||
urls = urls.v1_urls | ||
else: | ||
urls = urls.eda_v1_urls | ||
|
||
return list_urls(urls) |
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,99 @@ | ||
# Copyright 2024 Red Hat, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import pytest | ||
|
||
from tests.integration.constants import api_url_v1 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"expected_slugs,use_shared_resource", | ||
[ | ||
pytest.param( | ||
[ | ||
"/status/", | ||
"/openapi.json", | ||
"/openapi.yaml", | ||
"/docs", | ||
"/redoc", | ||
"/auth/session/login/", | ||
"/auth/session/logout/", | ||
"/auth/token/refresh/", | ||
"/users/me/", | ||
"/projects/", | ||
"/rulebooks/", | ||
"/activations/", | ||
"/activation-instances/", | ||
"/audit-rules/", | ||
"/users/", | ||
"/event-streams/", | ||
"/users/me/awx-tokens/", | ||
"/credential-types/", | ||
"/eda-credentials/", | ||
"/decision-environments/", | ||
"/organizations/", | ||
"/teams/", | ||
], | ||
True, | ||
id="with_shared_resource", | ||
), | ||
pytest.param( | ||
[ | ||
"/status/", | ||
"/role_definitions/", | ||
"/role_user_assignments/", | ||
"/role_team_assignments/", | ||
"/role_metadata/", | ||
"/service-index/metadata/", | ||
"/service-index/validate-local-account/", | ||
"/service-index/resources/", | ||
"/service-index/resource-types/", | ||
"/service-index/", | ||
"/openapi.json", | ||
"/openapi.yaml", | ||
"/docs", | ||
"/redoc", | ||
"/auth/session/login/", | ||
"/auth/session/logout/", | ||
"/auth/token/refresh/", | ||
"/users/me/", | ||
"/projects/", | ||
"/rulebooks/", | ||
"/activations/", | ||
"/activation-instances/", | ||
"/audit-rules/", | ||
"/users/", | ||
"/event-streams/", | ||
"/users/me/awx-tokens/", | ||
"/credential-types/", | ||
"/eda-credentials/", | ||
"/decision-environments/", | ||
"/organizations/", | ||
"/teams/", | ||
], | ||
False, | ||
id="no_shared_resource", | ||
), | ||
], | ||
) | ||
@pytest.mark.django_db | ||
def test_v1_root(admin_client, request, expected_slugs, use_shared_resource): | ||
if use_shared_resource: | ||
request.getfixturevalue("use_shared_resource_setting") | ||
response = admin_client.get(f"{api_url_v1}/") | ||
assert response.status_code == 200 | ||
assert len(response.data) == len(expected_slugs) | ||
for slug in expected_slugs: | ||
assert any( | ||
slug in url for url in response.data.values() | ||
), f"Expected slug {slug} not found in response" |
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