Skip to content

Commit

Permalink
fix: update user status change handling and improve test coverage
Browse files Browse the repository at this point in the history
- Modified the handle_get_request function to redirect to the admin interface and provide user activation feedback.
- Updated test cases to reflect changes in response status codes and added a new test for invalid mode handling.
  • Loading branch information
psyray committed Sep 18, 2024
1 parent 81fb9c8 commit 34072cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
7 changes: 5 additions & 2 deletions web/dashboard/tests/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ def test_user_not_found(self):

def test_get_request(self):
response = self.client.get(reverse('admin_interface_update') + f'?user={self.user_to_test.id}&mode=change_status')
self.assertEqual(response.status_code, 200)
self.assertTrue(json.loads(response.content)['status'])
self.assertEqual(response.status_code, 302)

def test_get_request_with_invalid_mode(self):
response = self.client.get(reverse('admin_interface_update') + f'?user={self.user_to_test.id}&mode=wrong_mode')
self.assertEqual(response.status_code, 400)

def test_post_request_update(self):
data = {
Expand Down
18 changes: 15 additions & 3 deletions web/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.shortcuts import redirect, render, get_object_or_404
from django.utils import timezone
from django.utils.text import slugify
from django.http import HttpResponseRedirect, JsonResponse
from django.http import HttpResponseRedirect, HttpResponseBadRequest, JsonResponse
from django.urls import reverse
from django.template.defaultfilters import slugify
from rolepermissions.roles import assign_role, clear_roles
Expand Down Expand Up @@ -237,8 +237,20 @@ def handle_get_request(request, user):
if mode == 'change_status':
user.is_active = not user.is_active
user.save()
return JsonResponse({'status': True})
return JsonResponse({'status': False, 'error': 'Invalid mode'}, status=400)
if user.is_active:
messages.add_message(
request,
messages.INFO,
f'User {user.username} successfully activated.'
)
else:
messages.add_message(
request,
messages.INFO,
f'User {user.username} successfully deactivated.'
)
return HttpResponseRedirect(reverse('admin_interface'))
return HttpResponseBadRequest(reverse('admin_interface'), status=400)


def handle_post_request(request, user):
Expand Down

0 comments on commit 34072cb

Please sign in to comment.