Skip to content

Commit

Permalink
Merge pull request #893 from uktrade/TSS-1979_Active_Users_Data_Set
Browse files Browse the repository at this point in the history
TSS-1979: Active Users Data Set
  • Loading branch information
abarolo authored Oct 17, 2024
2 parents 04e7342 + da8909d commit fb170e9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
8 changes: 6 additions & 2 deletions api/dataset/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.urls import path

from api.dataset.views import BarrierList, FeedbackDataWorkspaceListView, UserList
from api.user.views import UserActivityLogList

from .views import BarrierList, FeedbackDataWorkspaceListView

app_name = "dataset"

urlpatterns = [
Expand All @@ -18,4 +17,9 @@
UserActivityLogList.as_view(),
name="user-activity-log",
),
path(
"dataset/v1/users",
UserList.as_view(),
name="user-list",
),
]
17 changes: 16 additions & 1 deletion api/dataset/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.auth import get_user_model
from hawkrest import HawkAuthentication
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
Expand All @@ -8,7 +9,9 @@
from api.feedback.models import Feedback
from api.feedback.serializers import FeedbackSerializer
from api.user.models import UserActvitiyLog
from api.user.serializers import UserActvitiyLogSerializer
from api.user.serializers import UserActvitiyLogSerializer, UserSerializer

UserModel = get_user_model()


class BarrierList(generics.ListAPIView):
Expand Down Expand Up @@ -50,3 +53,15 @@ class UserActivityLogView(generics.ListAPIView):

queryset = UserActvitiyLog.objects.all()
serializer_class = UserActvitiyLogSerializer


class UserList(generics.ListAPIView):
authentication_classes = (HawkAuthentication,)
permission_classes = (IsAuthenticated,)

def get_queryset(self):
return UserModel.objects.values(
"id", "email", "first_name", "last_name", "last_login"
).order_by("id")

serializer_class = UserSerializer
13 changes: 13 additions & 0 deletions api/user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,16 @@ class Meta:
"event_type",
"event_description",
]


class UserSerializer(serializers.ModelSerializer):

class Meta:
model = UserModel
fields = [
"id",
"email",
"first_name",
"last_name",
"last_login",
]
30 changes: 30 additions & 0 deletions tests/dataset/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import freezegun
import time_machine
from django.contrib.auth import get_user_model
from mock import PropertyMock, patch
from pytz import UTC
from rest_framework import status
Expand All @@ -20,6 +21,7 @@
from tests.barriers.factories import BarrierFactory

freezegun.configure(extend_ignore_list=["transformers"])
User = get_user_model()


class TestBarriersDataset(APITestMixin):
Expand Down Expand Up @@ -278,3 +280,31 @@ def test_logs(self):
response = self.api_client.get(url)
assert response.status_code == status.HTTP_200_OK
assert len(response.data["results"]) == 2


class TestUserDataset(APITestMixin):
def test_base_user(self):
url = reverse("dataset:user-list")
response = self.api_client.get(url)

assert response.status_code == status.HTTP_200_OK
assert len(response.data["results"]) == 1

user = User.objects.first()
ts1 = datetime(2024, 9, 1, 0, 0, 0)
with freezegun.freeze_time(ts1):
self.api_client.force_login(user)

response = self.api_client.get(url)

assert response.data["results"][0]["last_login"] == ts1.strftime(
"%Y-%m-%dT%H:%M:%SZ"
)

def test_user_added(self):
create_test_user()
url = reverse("dataset:user-list")
response = self.api_client.get(url)

assert status.HTTP_200_OK == response.status_code
assert len(response.data["results"]) == 2

0 comments on commit fb170e9

Please sign in to comment.