Skip to content

Commit

Permalink
feat: add get validated user fields view
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-canon committed Feb 26, 2025
1 parent ed55f19 commit 1e4201f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 10 deletions.
44 changes: 41 additions & 3 deletions eox_nelp/user_profile/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""This file contains all the test for the user_profile API V1 views.py file.
"""
This file contains all the tests for the user_profile API V1 views.py file.
Classes:
UpdateUserDataTestCase: Class to test update_user_data view
Test Cases:
- UpdateUserDataTestCase: Tests for update_user_data view.
- GetValidatedUserFieldsTestCase: Tests for get_validated_user_fields view.
"""


from unittest.mock import patch

from ddt import ddt
Expand Down Expand Up @@ -174,3 +177,38 @@ def test_update_extra_info_fields(self, cdd_task_mock):
self.assertEqual(self.user.extrainfo.arabic_first_name, payload["arabic_first_name"])
self.assertEqual(self.user.extrainfo.arabic_last_name, payload["arabic_last_name"])
cdd_task_mock.delay.assert_called_with(user_id=self.user.id, action_name="cdd")


class GetValidatedUserFieldsTestCase(APITestCase):
"""Test case for get_validated_user_fields view."""

reverse_viewname = "user-profile-api:v1:validated-fields"

def setUp(self):
"""Set up a test user and authenticate the client."""
self.user = User.objects.create(username="testuser")
self.client.force_authenticate(user=self.user)

@patch("eox_nelp.user_profile.api.v1.views.validate_required_user_fields")
def test_get_validated_user_fields_successfully(self, mock_validate_required_user_fields):
"""
Test that the view returns the expected validated fields.
Expected behavior:
- The function should return a JSON response with validated fields.
- Status code 200.
- The function should call validate_required_user_fields with the correct user instance.
"""
expected_response = {
"account": {"first_name": [], "last_name": []},
"profile": {"city": [], "country": [], "phone_number": ["Empty field"], "mailing_address": []},
"extra_info": {"arabic_name": [], "arabic_first_name": [], "arabic_last_name": []},
}
mock_validate_required_user_fields.return_value = expected_response
url_endpoint = reverse(self.reverse_viewname)

response = self.client.get(url_endpoint)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertDictEqual(response.json(), expected_response)
mock_validate_required_user_fields.assert_called_once_with(self.user)
5 changes: 3 additions & 2 deletions eox_nelp/user_profile/api/v1/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""eox_nelp puser_profile.api v1 urls"""
"""eox_nelp user_profile.api v1 urls"""
from django.urls import path

from eox_nelp.user_profile.api.v1.views import update_user_data
from eox_nelp.user_profile.api.v1.views import get_validated_user_fields, update_user_data

app_name = "eox_nelp" # pylint: disable=invalid-name

urlpatterns = [
path("update-user-data/", update_user_data, name="update-user-data"),
path("validated-fields/", get_validated_user_fields, name="validated-fields"),
]
51 changes: 46 additions & 5 deletions eox_nelp/user_profile/api/v1/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Generic views for NELP user's profile application.
"""
Views for the NELP user profile application.
Function-views:
- generate_otp: generate and send via SMS the OTP related a user. Saved in cache.
- validate_otp: Compare and check if the proposed OTP match the User OTP saved in cache.
If match, updates the profile phone_number.
This module provides API endpoints for managing and validating user profile data, including updating user information
and retrieving validation errors for required fields.
Available Views:
- update_user_data: Updates user profile fields, handling extra account and extra info fields where necessary.
- get_validated_user_fields: Returns a JSON response with validated user fields based on validation rules
defined in the REQUIRED_USER_FIELDS setting.
"""
import logging

Expand All @@ -19,6 +22,7 @@
from eox_nelp.edxapp_wrapper.user_api import accounts, errors
from eox_nelp.one_time_password.view_decorators import validate_otp
from eox_nelp.pearson_vue.tasks import real_time_import_task_v2
from eox_nelp.user_profile.required_fields_validation import validate_required_user_fields
from eox_nelp.utils import save_extrainfo_field

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -92,3 +96,40 @@ def update_user_data(request):
)

return Response({"message": "User's fields has been updated successfully"}, status=status.HTTP_200_OK)


@api_view(["GET"])
@authentication_classes((SessionAuthenticationAllowInactiveUser,))
@permission_classes((IsAuthenticated,))
def get_validated_user_fields(request):
"""
View to retrieve validated fields for the authenticated user in JSON format.
## Usage
### **GET** /eox-nelp/api/user-profile/v1/validated-fields/
**Response Example**
```json
{
"account": {
"first_name": [],
"last_name": []
},
"profile": {
"city": [],
"country": [],
"phone_number": ["Empty field"],
"mailing_address": []
},
"extra_info": {
"arabic_name": [],
"arabic_first_name": [],
"arabic_last_name": []
}
}
```
"""
validated_fields = validate_required_user_fields(request.user)

return Response(validated_fields, status=status.HTTP_200_OK)

0 comments on commit 1e4201f

Please sign in to comment.