Skip to content

Commit

Permalink
When an odk token expires is nil, deactivate and replace
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankApiyo committed Apr 11, 2024
1 parent 7d39096 commit b0ae639
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 @@ -180,7 +180,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 b0ae639

Please sign in to comment.