Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-keys): add has_expired to MasterAPIKey response #3432

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading