Skip to content

Commit

Permalink
Add token-is-valid endpoint and update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
terjekv committed Nov 21, 2024
1 parent 39d75db commit e25383d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion mreg/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
urlpatterns = [
path('token-logout/', views.TokenLogout.as_view()),
path('token-auth/', views.ObtainExpiringAuthToken.as_view()),
path('token-is-valid/', views.TokenIsValid.as_view()),
path('meta/version', views.MregVersion.as_view()),
path('meta/heartbeat', views.MetaHeartbeat.as_view()),
path('meta/libraries', views.MetaVersions.as_view()),
path('meta/heartbeat', views.MetaHeartbeat.as_view()),
]
20 changes: 15 additions & 5 deletions mreg/api/v1/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,26 @@ def test_token_rotation(self):

def test_token_usage(self):
old = ExpiringToken.objects.get(user=self.user)
self.assert_get("/api/token-is-valid/")
self.assert_get("/hosts/")
new = ExpiringToken.objects.get(user=self.user)
assert old.created == new.created
self.assertLess(old.last_used, new.last_used)

def test_is_active_false(self):
self.assert_get("/api/token-is-valid/")
self.assert_get("/hosts/")
self.user.is_active = False
self.user.save()
self.assert_get_and_401("/hosts/")
self.assert_get_and_401("/api/token-is-valid/")

def test_is_deleted(self):
self.assert_get("/hosts/")
self.assert_get("/api/token-is-valid/")
self.user.delete()
self.assert_get_and_401("/hosts/")
self.assert_get_and_401("/api/token-is-valid/")

def test_login_with_invalid_credentials(self):
self.client = APIClient()
Expand All @@ -287,15 +292,20 @@ def test_login_with_invalid_credentials(self):
class APIMetaTestCase(MregAPITestCase):
"""Test the meta API endpoint."""

def test_meta_versions_admin_200_ok(self):
response = self.assert_get_and_200("/api/meta/versions")
for key in ('rest_framework_version', 'django_version', 'python_version'):
def test_meta_libraries_admin_200_ok(self):
response = self.assert_get_and_200("/api/meta/libraries")
for key in ('djangorestframework', 'django', 'python'):
with self.subTest(key=key):
self.assertTrue(key in response.data)

def test_meta_versions_user_403_forbidden(self):
def test_meta_libraries_user_403_forbidden(self):
self.client = self.get_token_client(superuser=False)
self.assert_get_and_403("/api/meta/versions")
self.assert_get_and_403("/api/meta/libraries")

def test_meta_version_user_ok(self):
self.client = self.get_token_client(superuser=False)
response = self.assert_get("/api/meta/version")
self.assertTrue('version' in response.data)

def test_meta_heartbeat_user_200_ok(self):
self.client = self.get_token_client(superuser=False)
Expand Down
6 changes: 6 additions & 0 deletions mreg/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ def post(self, request: Request):
request.user.delete()
return Response(status=status.HTTP_200_OK)

class TokenIsValid(APIView):

permission_classes = (IsAuthenticated,)

def get(self, request: Request):
return Response(status=status.HTTP_200_OK)

class MregVersion(APIView):

Expand Down

0 comments on commit e25383d

Please sign in to comment.