Skip to content

Commit

Permalink
Tidy test views (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz authored Apr 28, 2023
1 parent 0418c2d commit 11e91d0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
24 changes: 12 additions & 12 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ def test_options_empty_request_method(self):
HTTP_ORIGIN="https://example.com",
HTTP_ACCESS_CONTROL_REQUEST_METHOD="",
)
assert resp.status_code == 200
assert resp.status_code == HTTPStatus.OK

def test_options_no_headers(self):
resp = self.client.options("/")
assert resp.status_code == 404
assert resp.status_code == HTTPStatus.METHOD_NOT_ALLOWED

@override_settings(CORS_ALLOW_CREDENTIALS=True, CORS_ALLOW_ALL_ORIGINS=True)
def test_allow_all_origins_get(self):
Expand All @@ -212,7 +212,7 @@ def test_allow_all_origins_get(self):
HTTP_ORIGIN="https://example.com",
HTTP_ACCESS_CONTROL_REQUEST_METHOD="GET",
)
assert resp.status_code == 200
assert resp.status_code == HTTPStatus.OK
assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com"
assert resp["vary"] == "origin"

Expand All @@ -223,7 +223,7 @@ def test_allow_all_origins_options(self):
HTTP_ORIGIN="https://example.com",
HTTP_ACCESS_CONTROL_REQUEST_METHOD="GET",
)
assert resp.status_code == 200
assert resp.status_code == HTTPStatus.OK
assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com"
assert resp["vary"] == "origin"

Expand All @@ -237,8 +237,8 @@ def test_non_200_headers_still_set(self):
place to preserve this behaviour. See `ExceptionMiddleware` mention here:
https://docs.djangoproject.com/en/3.0/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware # noqa: E501
"""
resp = self.client.get("/test-401/", HTTP_ORIGIN="https://example.com")
assert resp.status_code == 401
resp = self.client.get("/unauthorized/", HTTP_ORIGIN="https://example.com")
assert resp.status_code == HTTPStatus.UNAUTHORIZED
assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com"

@override_settings(CORS_ALLOW_CREDENTIALS=True, CORS_ALLOW_ALL_ORIGINS=True)
Expand All @@ -252,7 +252,7 @@ def test_auth_view_options(self):
HTTP_ORIGIN="https://example.com",
HTTP_ACCESS_CONTROL_REQUEST_METHOD="GET",
)
assert resp.status_code == 200
assert resp.status_code == HTTPStatus.OK
assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com"
assert resp["Content-Length"] == "0"

Expand All @@ -267,7 +267,7 @@ def handler(*args, **kwargs):
HTTP_ACCESS_CONTROL_REQUEST_METHOD="GET",
)

assert resp.status_code == 200
assert resp.status_code == HTTPStatus.OK
assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp

def test_signal_handler_that_returns_true(self):
Expand All @@ -280,7 +280,7 @@ def handler(*args, **kwargs):
HTTP_ORIGIN="https://example.com",
HTTP_ACCESS_CONTROL_REQUEST_METHOD="GET",
)
assert resp.status_code == 200
assert resp.status_code == HTTPStatus.OK
assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.com"

@override_settings(CORS_ALLOWED_ORIGINS=["https://example.com"])
Expand All @@ -294,15 +294,15 @@ def allow_api_to_all(sender, request, **kwargs):
HTTP_ORIGIN="https://example.org",
HTTP_ACCESS_CONTROL_REQUEST_METHOD="GET",
)
assert resp.status_code == 200
assert resp.status_code == HTTPStatus.OK
assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp

resp = self.client.options(
"/api/something/",
HTTP_ORIGIN="https://example.org",
HTTP_ACCESS_CONTROL_REQUEST_METHOD="GET",
)
assert resp.status_code == 200
assert resp.status_code == HTTPStatus.OK
assert resp[ACCESS_CONTROL_ALLOW_ORIGIN] == "https://example.org"

@override_settings(CORS_ALLOWED_ORIGINS=["https://example.com"])
Expand Down Expand Up @@ -379,5 +379,5 @@ def test_works_if_view_deletes_cors_enabled(self):
Just in case something crazy happens in the view or other middleware,
check that get_response doesn't fall over if `_cors_enabled` is removed
"""
resp = self.client.get("/delete-is-enabled/", HTTP_ORIGIN="https://example.com")
resp = self.client.get("/delete-enabled/", HTTP_ORIGIN="https://example.com")
assert ACCESS_CONTROL_ALLOW_ORIGIN in resp
31 changes: 5 additions & 26 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
from __future__ import annotations

from django.http import Http404
from django.http import HttpResponse
from django.urls import path


def test_view(request):
if request.method != "GET":
raise Http404()
return HttpResponse("Test view")


async def async_(request):
return HttpResponse("Test view")


def test_view_http401(request):
return HttpResponse("Unauthorized", status=401)


def test_view_that_deletes_is_enabled(request):
del request._cors_enabled
return HttpResponse()

from tests import views

urlpatterns = [
path("", test_view),
path("foo/", test_view),
path("async/", async_),
path("test-401/", test_view_http401),
path("delete-is-enabled/", test_view_that_deletes_is_enabled),
path("", views.index),
path("async/", views.async_),
path("unauthorized/", views.unauthorized),
path("delete-enabled/", views.delete_enabled_attribute),
]
24 changes: 24 additions & 0 deletions tests/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations

from http import HTTPStatus

from django.http import HttpResponse
from django.views.decorators.http import require_GET


@require_GET
def index(request):
return HttpResponse("Index")


async def async_(request):
return HttpResponse("Asynchronous")


def unauthorized(request):
return HttpResponse("Unauthorized", status=HTTPStatus.UNAUTHORIZED)


def delete_enabled_attribute(request):
del request._cors_enabled
return HttpResponse()

0 comments on commit 11e91d0

Please sign in to comment.