Skip to content

Commit

Permalink
test number of queries for ACME views
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasertl committed Jan 1, 2025
1 parent 1dde79e commit 6c2ee1d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
13 changes: 9 additions & 4 deletions ca/django_ca/tests/acme/views/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import pytest
from freezegun import freeze_time
from pytest_django import DjangoAssertNumQueries
from pytest_django.fixtures import SettingsWrapper

from django_ca.models import CertificateAuthority
Expand All @@ -33,9 +34,11 @@
URL = reverse("django_ca:acme-directory")


def test_default(client: Client, root: CertificateAuthority) -> None:
def test_default(
django_assert_num_queries: DjangoAssertNumQueries, client: Client, root: CertificateAuthority
) -> None:
"""Test the default directory view."""
with mock.patch("secrets.token_bytes", return_value=b"foobar"):
with mock.patch("secrets.token_bytes", return_value=b"foobar"), django_assert_num_queries(1):
response = client.get(URL)
assert response.status_code == HTTPStatus.OK
req = response.wsgi_request
Expand All @@ -49,10 +52,12 @@ def test_default(client: Client, root: CertificateAuthority) -> None:
}


def test_named_ca(client: Client, root: CertificateAuthority) -> None:
def test_named_ca(
django_assert_num_queries: DjangoAssertNumQueries, client: Client, root: CertificateAuthority
) -> None:
"""Test getting directory for named CA."""
url = reverse("django_ca:acme-directory", kwargs={"serial": root.serial})
with mock.patch("secrets.token_bytes", return_value=b"foobar"):
with mock.patch("secrets.token_bytes", return_value=b"foobar"), django_assert_num_queries(1):
response = client.get(url)
assert response.status_code == HTTPStatus.OK
assert response["Content-Type"] == "application/json"
Expand Down
14 changes: 10 additions & 4 deletions ca/django_ca/tests/acme/views/test_new_nonce.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@
from django.test import Client
from django.urls import reverse

import pytest
from pytest_django import DjangoAssertNumQueries
from pytest_django.fixtures import SettingsWrapper

from django_ca.tests.base.constants import CERT_DATA

URL = reverse("django_ca:acme-new-nonce", kwargs={"serial": CERT_DATA["root"]["serial"]})


def test_get_nonce(client: Client) -> None:
@pytest.mark.django_db
def test_get_nonce(django_assert_num_queries: DjangoAssertNumQueries, client: Client) -> None:
"""Test that getting multiple nonces returns unique nonces."""
nonces = []
for _i in range(1, 5):
response = client.head(URL)
with django_assert_num_queries(0):
response = client.head(URL)
assert response.status_code == HTTPStatus.OK
assert len(response["replay-nonce"]) == 43
assert response["cache-control"] == "no-store"
Expand All @@ -38,9 +42,11 @@ def test_get_nonce(client: Client) -> None:
assert len(nonces) == len(set(nonces))


def test_get_request(client: Client) -> None:
@pytest.mark.django_db
def test_get_request(django_assert_num_queries: DjangoAssertNumQueries, client: Client) -> None:
"""RFC 8555, section 7.2 also specifies a GET request."""
response = client.get(URL)
with django_assert_num_queries(0):
response = client.get(URL)
assert response.status_code == HTTPStatus.NO_CONTENT
assert len(response["replay-nonce"]) == 43
assert response["cache-control"] == "no-store"
Expand Down

0 comments on commit 6c2ee1d

Please sign in to comment.