Skip to content

Commit

Permalink
Merge pull request #2583 from onaio/deactivate-odk-tokens-with-no-exp…
Browse files Browse the repository at this point in the history
…ires

When an odk token expires is None, deactivate and replace
  • Loading branch information
FrankApiyo authored Apr 12, 2024
2 parents 1c5ad83 + efdfe80 commit 1afc2ce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 18 additions & 0 deletions onadata/apps/api/tests/viewsets/test_connect_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,24 @@ def test_retrieve_odk_token(self):
self.assertEqual(response.data["odk_token"], odk_token)
self.assertEqual(response.data["expires"], expires)

def test_deactivate_token_when_expires_is_None(self):
"""
Test that when a token's .expires field is nil, it will be deactivated
and a new one created in it's place
"""
view = ConnectViewSet.as_view({"post": "odk_token", "get": "odk_token"})

# Create an active tokens
token = ODKToken.objects.create(user=self.user)
ODKToken.objects.filter(pk=token.pk).update(expires=None)

request = self.factory.get("/", **self.extra)
request.session = self.client.session
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(ODKToken.objects.filter(status=ODKToken.ACTIVE)), 1)
self.assertNotEqual(response.data["odk_token"], token.raw_key)

def test_deactivates_multiple_active_odk_token(self):
"""
Test that the viewset deactivates tokens when two or more are
Expand Down
4 changes: 3 additions & 1 deletion onadata/apps/api/viewsets/connect_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ def odk_token(self, request, *args, **kwargs):
user=user, status=ODKToken.ACTIVE
)

if not created and timezone.now() > token.expires:
if not token.expires or (
not created and timezone.now() > token.expires
):
token.status = ODKToken.INACTIVE
token.save()
token = ODKToken.objects.create(user=user)
Expand Down

0 comments on commit 1afc2ce

Please sign in to comment.