Skip to content

Commit

Permalink
fix: invalid response type on post request (#15609)
Browse files Browse the repository at this point in the history
  • Loading branch information
pb82 authored Nov 14, 2024
1 parent 989a438 commit 5448757
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
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

0 comments on commit 5448757

Please sign in to comment.