Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalid response type on post request #15609

Merged
merged 1 commit into from
Nov 14, 2024
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
5 changes: 3 additions & 2 deletions awx/api/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.core.exceptions import FieldDoesNotExist
from django.db import connection, transaction
from django.db.models.fields.related import OneToOneRel
from django.http import QueryDict
from django.http import QueryDict, JsonResponse
from django.shortcuts import get_object_or_404, redirect
from django.template.loader import render_to_string
from django.utils.encoding import smart_str
Expand Down Expand Up @@ -81,6 +81,7 @@


class LoggedLoginView(auth_views.LoginView):

def get(self, request, *args, **kwargs):
if is_proxied_request():
next = request.GET.get('next', "")
Expand All @@ -105,7 +106,7 @@ def get(self, request, *args, **kwargs):
def post(self, request, *args, **kwargs):
if is_proxied_request():
# Give a message, saying to login via AAP
return Response(
return JsonResponse(
{
'detail': _('Please log in via Platform Authentication.'),
},
Expand Down
27 changes: 25 additions & 2 deletions awx/main/tests/functional/api/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import pytest

from django.contrib import auth
from django.http import JsonResponse

from django.test import Client

from rest_framework.test import APIRequestFactory

from awx.api.generics import LoggedLoginView
import awx.api.generics
from rest_framework.reverse import reverse as drf_reverse

from pytest_mock import MockerFixture


@pytest.mark.django_db
def test_invalid_login():
Expand All @@ -21,6 +25,25 @@ def test_invalid_login():
request = factory.post(url, data)
request.user = anon

response = LoggedLoginView.as_view()(request)
response = awx.api.generics.LoggedLoginView.as_view()(request)

assert response.status_code == 401


@pytest.mark.django_db
def test_invalid_post(mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch):
url = drf_reverse('api:login')
factory = APIRequestFactory()
request = factory.post(url)

is_proxied_request_mock = mocker.Mock(
autospec=True,
name='is_proxied_request',
return_value=True,
)
monkeypatch.setattr(awx.api.generics, 'is_proxied_request', is_proxied_request_mock)
response = awx.api.generics.LoggedLoginView.as_view()(request)

assert isinstance(response, JsonResponse)
assert b'Please log in via Platform Authentication.' in response.content
assert response.status_code == 401
Loading