diff --git a/awx/api/generics.py b/awx/api/generics.py index dfe60f52188d..9c400d0b388e 100644 --- a/awx/api/generics.py +++ b/awx/api/generics.py @@ -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 @@ -81,6 +81,7 @@ class LoggedLoginView(auth_views.LoginView): + def get(self, request, *args, **kwargs): if is_proxied_request(): next = request.GET.get('next', "") @@ -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.'), }, diff --git a/awx/main/tests/functional/api/test_auth.py b/awx/main/tests/functional/api/test_auth.py index 7ecfe9de9582..49a9c7640df2 100644 --- a/awx/main/tests/functional/api/test_auth.py +++ b/awx/main/tests/functional/api/test_auth.py @@ -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(): @@ -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