# Path: account/decorators.py
# def login_required(func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
#     """
#     Decorator for views that checks that the user is logged in, redirecting
#     to the log in page if necessary.
#     """
#     def decorator(view_func):
#         @functools.wraps(view_func)
#         def _wrapped_view(request, *args, **kwargs):
#             if request.user.is_authenticated:
#                 return view_func(request, *args, **kwargs)
#             return handle_redirect_to_login(
#                 request,
#                 redirect_field_name=redirect_field_name,
#                 login_url=login_url
#             )
#         return _wrapped_view
#     if func:
#         return decorator(func)
#     return decorator


from unittest import mock
from django.http import HttpResponse
from django.test import TestCase
from account.decorators import login_required




@login_required
def mock_view(request, *args, **kwargs):
    return HttpResponse("OK", status=200)


class LoginRequiredDecoratorTestCase(TestCase):

    def test_authenticated_user_is_allowed(self):
        request = mock.MagicMock()
        request.user.is_authenticated = True
        response = mock_view(request)
        self.assertEqual(response.status_code, 200)

    def test_unauthenticated_user_gets_redirected(self):
        request = mock.MagicMock()
        request.user.is_authenticated = False

# Path: account/decorators.py
# def login_required(func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
#     """
#     Decorator for views that checks that the user is logged in, redirecting
#     to the log in page if necessary.
#     """
#     def decorator(view_func):
#         @functools.wraps(view_func)
#         def _wrapped_view(request, *args, **kwargs):
#             if request.user.is_authenticated:
#                 return view_func(request, *args, **kwargs)
#             return handle_redirect_to_login(
#                 request,
#                 redirect_field_name=redirect_field_name,
#                 login_url=login_url
#             )
#         return _wrapped_view
#     if func:
#         return decorator(func)
#     return decorator


# Path: account/tests/test_decorators.py
from unittest import mock
from django.http import HttpResponse
from django.test import TestCase
from account.decorators import login_required





@login_required
def mock_view(request, *args, **kwargs):
    return HttpResponse("OK", status=200)


class LoginRequiredDecoratorTestCase(TestCase):

    def test_authenticated_user_is_allowed(self):
        request = mock.MagicMock()
        request.user.is_authenticated = True
        response = mock_view(request)
        self.assertEqual(response.status_code, 200)

    def test_unauthenticated_user_gets_redirected(self):
        request = mock.MagicMock()
        request.user.is_authenticated = False

        response = mock_view(request)