• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Brunowar12 / TaskManagerSystem / 15166388190

21 May 2025 03:32PM UTC coverage: 92.47% (-0.3%) from 92.77%
15166388190

push

github

web-flow
Merge pull request #27 from Brunowar12/bugfix-and-pep

Bug fixes and code style cleanup

159 of 183 new or added lines in 24 files covered. (86.89%)

10 existing lines in 6 files now uncovered.

1621 of 1753 relevant lines covered (92.47%)

5.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/users/views.py
1
from rest_framework import viewsets, status, serializers
6✔
2
from rest_framework.decorators import action
6✔
3
from rest_framework.permissions import AllowAny, IsAuthenticated
6✔
4
from rest_framework.response import Response
6✔
5
from rest_framework.throttling import AnonRateThrottle
6✔
6

7
from api.utils import error_response
6✔
8
from .serializers import (
6✔
9
    UserRegistrationSerializer, UserLoginSerializer,
10
    UserProfileSerializer
11
)
12
from .services import UserService
6✔
13

14
class AuthViewSet(viewsets.GenericViewSet):
6✔
15
    """
16
    Handles user registration, login, and logout
17
    """
18
    
19
    throttle_classes = [AnonRateThrottle]
6✔
20

21
    def get_serializer_class(self):
6✔
22
        # Used for schema generation
23
        if getattr(self, 'swagger_fake_view', False):
×
24
            return serializers.Serializer
×
25

26
        # Dynamically choose serializer based on action
27
        if self.action == 'register':
×
28
            return UserRegistrationSerializer
×
29
        elif self.action == 'login':
×
30
            return UserLoginSerializer
×
31

32
    def get_permissions(self):
6✔
33
        if self.action in ['register', 'login']:
6✔
34
            return [AllowAny()]
6✔
35
        return [IsAuthenticated()]
6✔
36

37
    @action(detail=False, methods=['post'])
6✔
38
    def register(self, request):
6✔
39
        """ Registers a new user """
40
        data = UserService.register_user(request.data)
6✔
41
        return Response(data, status=status.HTTP_201_CREATED)
6✔
42

43
    @action(detail=False, methods=['post'])
6✔
44
    def login(self, request):
6✔
45
        """ Logs in a user and returns tokens """
46
        data = UserService.login_user(request.data)
6✔
47
        return Response(data, status=status.HTTP_200_OK)
6✔
48

49
    @action(detail=False, methods=['post'])
6✔
50
    def logout(self, request):
6✔
51
        """ Logs out a user by blacklisting the refresh token """
52
        refresh_token = request.data.get("refresh")
6✔
53
        error_messages_map = {
6✔
54
            "Invalid token format": "Incorrect token format",
55
            "Token expired": "Expired token",
56
            "Token already revoked": "The token has already been revoked",
57
        }
58
        
59
        if not refresh_token:
6✔
60
            return error_response("Refresh token is required")
×
61
        
62
        try:
6✔
63
            data = UserService.logout_user(refresh_token)
6✔
64
            return Response(data, status=status.HTTP_200_OK)
6✔
65
        except ValueError as e:
6✔
66
            user_message = error_messages_map.get(str(e), "Unknown token error")
6✔
67
            return error_response(user_message, status.HTTP_401_UNAUTHORIZED, exc=e)
6✔
UNCOV
68
        except Exception as e:
×
UNCOV
69
            return error_response(
×
70
                "Internal server error",
71
                status.HTTP_500_INTERNAL_SERVER_ERROR,
72
                exc=e,
73
            )
74

75

76
class UserViewSet(viewsets.GenericViewSet):
6✔
77
    """
78
    Handles viewing and updating the authenticated user's profile
79
    """
80
    permission_classes = [IsAuthenticated]
6✔
81
    serializer_class = UserProfileSerializer
6✔
82

83
    @action(detail=False, methods=['get'])
6✔
84
    def profile(self, request):
6✔
85
        """ Returns the authenticated user's profile """
86
        serializer = self.get_serializer(request.user)
6✔
87
        return Response(serializer.data)
6✔
88

89
    @action(detail=False, methods=['put', 'patch'])
6✔
90
    def update_profile(self, request):
6✔
91
        """ Updates the authenticated user's profile """
92
        data = UserService.update_profile(request.user, request.data)
6✔
93
        return Response(data)
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc