Skip to content

Commit 8e4d8f6

Browse files
authored
BB2-3256: Updated queryset for auth grants (#1224)
* Updated queryset for auth grants * Added test * Tests and fixes
1 parent c85b8a2 commit 8e4d8f6

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

apps/authorization/views.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from datetime import datetime
2+
3+
from django.db.models import Q
14
from django.http import HttpResponse
25
from django.utils.decorators import method_decorator
36
from django.views.decorators.csrf import csrf_exempt
@@ -46,7 +49,10 @@ class AuthorizedGrants(viewsets.GenericViewSet,
4649
serializer_class = DataAccessGrantSerializer
4750

4851
def get_queryset(self):
49-
return DataAccessGrant.objects.select_related("application").filter(beneficiary=self.request.user)
52+
return DataAccessGrant.objects.select_related("application").filter(
53+
Q(expiration_date__gt=datetime.now()) | Q(expiration_date=None),
54+
beneficiary=self.request.user
55+
)
5056

5157

5258
@method_decorator(csrf_exempt, name="dispatch")

apps/dot_ext/tests/test_views.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import json
22
import base64
3+
from datetime import date, timedelta
4+
35
from django.conf import settings
46
from django.http import HttpRequest
57
from django.urls import reverse
@@ -465,6 +467,43 @@ def test_get_tokens_success(self):
465467
]
466468
self.assertEqual(result, expected)
467469

470+
# Check tokens endpoint doesn't return expired
471+
application2 = self._create_application(
472+
"an expired app",
473+
grant_type=Application.GRANT_AUTHORIZATION_CODE,
474+
redirect_uris="http://example.it",
475+
user=anna
476+
)
477+
DataAccessGrant.objects.update_or_create(
478+
beneficiary=anna, application=application2, expiration_date=date.today() - timedelta(days=1)
479+
)
480+
response = self.client.get(
481+
"/v1/o/tokens/",
482+
headers={
483+
"authorization": self._create_authorization_header(
484+
application.client_id, application.client_secret_plain
485+
),
486+
"x-authentication": self._create_authentication_header(self.test_uuid),
487+
},
488+
)
489+
self.assertEqual(response.status_code, 200)
490+
result = response.json()
491+
expected = [
492+
{
493+
"id": result[0]["id"],
494+
"user": anna.id,
495+
"application": {
496+
"id": application.id,
497+
"name": "an app",
498+
"logo_uri": "",
499+
"tos_uri": "",
500+
"policy_uri": "",
501+
"contacts": "",
502+
},
503+
}
504+
]
505+
self.assertEqual(result, expected)
506+
468507
def test_get_tokens_on_inactive_app(self):
469508
anna = self._create_user(self.test_username, "123456")
470509
# create a couple of capabilities

0 commit comments

Comments
 (0)