Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

from django.conf import settings
from django.contrib.auth.models import User
from django.core.management import call_command

from rdmo.accounts.utils import set_group_permissions
Expand Down Expand Up @@ -54,3 +55,15 @@ def files(settings, tmp_path):
def json_data():
json_file = Path(settings.BASE_DIR) / 'import' / 'catalogs.json'
return {'elements': json.loads(json_file.read_text())}


@pytest.fixture
def login(client):
def force_login_user(username):
try:
user = User.objects.get(username=username)
client.force_login(user)
except User.DoesNotExist:
pass

return force_login_user
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ dev = [
"twine>=5.1.1,<7.0",
"wheel>=0.42,<0.46",
"rdmo[allauth]",
"rdmo[openapi]",
"rdmo[pytest]",
]
gunicorn = [
Expand All @@ -104,7 +105,7 @@ pytest = [
"pytest-xdist>=3.3,<4.0",
]
openapi = [
"drf-spectacular>=0.28.0,<1.0.0"
"drf-spectacular[sidecar]>=0.28.0,<1.0.0"
]

[project.urls]
Expand Down
5 changes: 4 additions & 1 deletion rdmo/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@
},
'PREPROCESSING_HOOKS': [
'rdmo.core.schema.filter_endpoints'
]
],
'SWAGGER_UI_DIST': 'SIDECAR',
'SWAGGER_UI_FAVICON_HREF': 'SIDECAR',
'REDOC_DIST': 'SIDECAR',
}

SETTINGS_EXPORT = [
Expand Down
99 changes: 51 additions & 48 deletions rdmo/core/tests/test_openapi.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
#import pytest

# from django.contrib.auth.models import User

# from rest_framework.status import HTTP_200_OK, HTTP_302_FOUND

# pytestmark = pytest.mark.django_db

# users = (
# ("admin", HTTP_200_OK),
# ("user", HTTP_200_OK),
# ("anonymous", HTTP_302_FOUND),
# )


# @pytest.mark.parametrize("username,status_code", users)
# def test_openapi_schema(client, settings, username, status_code):
# if username != "anonymous":
# user = User.objects.get(username=username)
# client.force_login(user)
# response = client.get("/api/v1/")
# assert response.status_code == status_code
# # TODO check yaml response
# # TODO check json response


# @pytest.mark.parametrize("username,status_code", users)
# def test_openapi_swagger_ui(client, username, status_code):
# if username != "anonymous":
# user = User.objects.get(username=username)
# client.force_login(user)
# response = client.get("/api/v1/swagger/")
# assert response.status_code == status_code
# if username != "anonymous":
# # logged in user can access the swagger ui
# assert '<div id="swagger-ui"></div>' in str(response.content)


# @pytest.mark.parametrize("username,status_code", users)
# def test_openapi_redoc_ui(client, username, status_code):
# if username != "anonymous":
# user = User.objects.get(username=username)
# client.force_login(user)
# response = client.get("/api/v1/redoc/")
# assert response.status_code == status_code
# if username != "anonymous":
# # logged in user can access the redoc ui
# assert '<redoc spec-url="/api/v1/"></redoc>' in str(response.content)
import pytest

import yaml

pytestmark = pytest.mark.django_db

users = (
'admin',
'user',
'anonymous'
)


@pytest.mark.parametrize('username', users)
def test_openapi_schema(db, client, login, settings, username):
login(username)

response = client.get('/api/v1/schema/')

if username in ['admin', 'user']:
assert response.status_code == 200
schema = yaml.safe_load(response.content)
assert schema['openapi'] == '3.0.3'
assert len(schema['paths']) == 123
else:
assert response.status_code == 302


@pytest.mark.parametrize('username', users)
def test_openapi_swagger_ui(client, login, username):
login(username)

response = client.get('/api/v1/swagger/')

if username in ['admin', 'user']:
assert response.status_code == 200
assert '<div id="swagger-ui"></div>' in str(response.content)
else:
assert response.status_code == 302

@pytest.mark.parametrize('username', users)
def test_openapi_redoc_ui(client, login, username):
login(username)

response = client.get('/api/v1/redoc/')

if username in ['admin', 'user']:
assert response.status_code == 200
assert '<redoc spec-url="/api/v1/schema/"></redoc>' in str(response.content)
else:
assert response.status_code == 302
12 changes: 12 additions & 0 deletions testing/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@
ACCOUNT_SIGNUP = True
SOCIALACCOUNT = False

INSTALLED_APPS += [
'drf_spectacular',
'drf_spectacular_sidecar'
]

REST_FRAMEWORK.update({
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning',
'DEFAULT_VERSION': 'v1',
'ALLOWED_VERSIONS': ('v1', ),
})

PROJECT_TABLE_PAGE_SIZE = 5

PROJECT_SEND_ISSUE = True
Expand Down
2 changes: 1 addition & 1 deletion testing/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

path('', include('rdmo.core.urls')),
path('api/v1/', include('rdmo.core.urls.v1')),
# path('api/v1/', include('rdmo.core.urls.v1.openapi', namespace='v1')),
path('api/v1/', include('rdmo.core.urls.v1.openapi', namespace='v1')),

path('admin/', admin.site.urls),
]