Skip to content

Commit

Permalink
Issue-493 | login will now update last_login for user
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Dec 1, 2020
1 parent a5f0f0b commit 4d1a541
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
35 changes: 33 additions & 2 deletions core/users/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from mock import Mock, patch
from mock import Mock, patch, ANY
from rest_framework.authtoken.models import Token

from core.collections.tests.factories import OrganizationCollectionFactory
from core.common.constants import ACCESS_TYPE_NONE, HEAD, OCL_ORG_ID
from core.common.tests import OCLTestCase
from core.common.tests import OCLTestCase, OCLAPITestCase
from core.orgs.models import Organization
from core.sources.tests.factories import OrganizationSourceFactory
from core.users.constants import USER_OBJECT_TYPE
Expand Down Expand Up @@ -151,3 +151,34 @@ def test_set_token(self):

user.set_token('token')
self.assertEqual(user.auth_token.key, 'token')


class TokenAuthenticationViewTest(OCLAPITestCase):
def test_login(self):
response = self.client.post('/users/login/', {})

self.assertEqual(response.status_code, 400)
self.assertEqual(
response.data,
dict(username=['This field is required.'], password=['This field is required.'])
)

response = self.client.post('/users/login/', dict(username='foo', password='bar'))

self.assertEqual(response.status_code, 400)
self.assertEqual(
response.data,
dict(non_field_errors=["Unable to log in with provided credentials."])
)

user = UserProfileFactory()
user.set_password('password')
user.save()
self.assertIsNone(user.last_login)

response = self.client.post('/users/login/', dict(username=user.username, password='password'))

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, dict(token=ANY))
user.refresh_from_db()
self.assertIsNotNone(user.last_login)
10 changes: 1 addition & 9 deletions core/users/urls.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
from django.conf.urls import url
from django.urls import re_path, include
from drf_yasg.utils import swagger_auto_schema
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.authtoken.views import obtain_auth_token

from core.common.constants import NAMESPACE_PATTERN
from core.orgs import views as org_views
from . import views

decorated_auth_view = swagger_auto_schema(
method='post',
request_body=AuthTokenSerializer
)(obtain_auth_token)

urlpatterns = [
re_path(r'^$', views.UserListView.as_view(), name='userprofile-list'),
url('login/', decorated_auth_view, name='user-login'),
url('login/', views.TokenAuthenticationView.as_view(), name='user-login'),
re_path(
r'^(?P<user>' + NAMESPACE_PATTERN + ')/$',
views.UserDetailView.as_view(),
Expand Down
19 changes: 19 additions & 0 deletions core/users/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from django.contrib.auth.models import update_last_login
from drf_yasg.utils import swagger_auto_schema
from pydash import get
from rest_framework import mixins, status
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.generics import RetrieveAPIView, UpdateAPIView, DestroyAPIView
from rest_framework.permissions import IsAdminUser, IsAuthenticated, AllowAny
from rest_framework.response import Response
Expand All @@ -12,6 +16,21 @@
from .models import UserProfile


class TokenAuthenticationView(ObtainAuthToken):
"""Implementation of ObtainAuthToken with last_login update"""

@swagger_auto_schema(request_body=AuthTokenSerializer)
def post(self, request, *args, **kwargs):
result = super().post(request, *args, **kwargs)
try:
user = UserProfile.objects.get(username=request.data['username'])
update_last_login(None, user)
except: # pylint: disable=bare-except
pass

return result


class UserBaseView(BaseAPIView):
lookup_field = 'user'
pk_field = 'username'
Expand Down

0 comments on commit 4d1a541

Please sign in to comment.