Skip to content

Commit

Permalink
feat(api-keys): add has_expired to MasterAPIKey response (#3432)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell authored Feb 16, 2024
1 parent 86cc3da commit 0a28ee0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/api_keys/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Meta:
"expiry_date",
"key",
"is_admin",
"has_expired",
)
read_only_fields = ("prefix", "created", "key")

Expand Down
27 changes: 27 additions & 0 deletions api/tests/integration/api_keys/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from datetime import timedelta
from typing import Any

import pytest
from django.urls import reverse
from django.utils import timezone
from rest_framework.test import APIClient


@pytest.fixture()
def _expired_api_key(admin_client: APIClient, organisation: int) -> dict[str, Any]:
url = reverse(
"api-v1:organisations:organisation-master-api-keys-list",
args=[organisation],
)
data = {
"name": "test_key",
"organisation": organisation,
"expiry_date": timezone.now() - timedelta(hours=1),
}
response = admin_client.post(url, data=data)
return response.json()


@pytest.fixture()
def expired_api_key_prefix(_expired_api_key: dict[str, Any]) -> str:
return _expired_api_key["prefix"]
31 changes: 30 additions & 1 deletion api/tests/integration/api_keys/test_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from rest_framework import status
from rest_framework.test import APIClient

from organisations.models import Organisation


def test_create_master_api_key_returns_key_in_response(admin_client, organisation):
# Given
Expand Down Expand Up @@ -56,19 +58,46 @@ def test_delete_master_api_key(admin_client, organisation, admin_master_api_key_
assert response.status_code == status.HTTP_204_NO_CONTENT


def test_list_master_api_keys(admin_client, organisation, admin_master_api_key_prefix):
def test_list_master_api_keys(
admin_client: APIClient,
organisation: int,
admin_master_api_key_prefix: str,
) -> None:
# Given
url = reverse(
"api-v1:organisations:organisation-master-api-keys-list",
args=[organisation],
)

# When
response = admin_client.get(url)

# Then
assert response.status_code == status.HTTP_200_OK
assert response.json()["count"] == 1
assert response.json()["results"][0]["prefix"] == admin_master_api_key_prefix
assert response.json()["results"][0]["has_expired"] is False


def test_list_master_api_keys__when_expired(
admin_client: APIClient,
organisation: Organisation,
expired_api_key_prefix: str,
) -> None:
# Given
url = reverse(
"api-v1:organisations:organisation-master-api-keys-list",
args=[organisation],
)

# When
response = admin_client.get(url)

# Then
assert response.status_code == status.HTTP_200_OK
assert response.json()["count"] == 1
assert response.json()["results"][0]["prefix"] == expired_api_key_prefix
assert response.json()["results"][0]["has_expired"] is True


def test_retrieve_master_api_key(
Expand Down

0 comments on commit 0a28ee0

Please sign in to comment.