# Path: brasilcomvc/common/views.py
# class AnonymousRequiredMixin(object):
# 
#     '''
#     Make any view be accessible by unauthenticated users only
#     '''
# 
#     def dispatch(self, request, *args, **kwargs):
#         if request.user.is_authenticated():
#             return redirect(settings.LOGIN_REDIRECT_URL)
#         return super(AnonymousRequiredMixin, self).dispatch(
#             request, *args, **kwargs)
# 
# class LoginRequiredMixin(object):
# 
#     '''
#     Bind login requirement to any view
#     '''
# 
#     @method_decorator(login_required)
#     def dispatch(self, *args, **kwargs):
#         return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)


from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponse
from django.test import RequestFactory, TestCase
from django.views.generic import View
from ..views import AnonymousRequiredMixin, LoginRequiredMixin


User = get_user_model()


class RequiresLogin(LoginRequiredMixin, View):

    def get(self, *args, **kwargs):
        return HttpResponse('OK')


class RequiresAnonymous(AnonymousRequiredMixin, View):

    def get(self, *args, **kwargs):
        return HttpResponse('OK')


class LoginRequiredMixinTestCase(TestCase):

    def test_anonymous_user_should_be_redirected_to_login_url(self):
        view = RequiresLogin.as_view()
        request = RequestFactory().get('/')
        request.user = AnonymousUser()
        response = view(request)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            response['Location'], '{}?next=/'.format(settings.LOGIN_URL))

    def test_authenticated_user_should_access_protected_view(self):
        view = RequiresLogin.as_view()

# Path: brasilcomvc/common/views.py
# class AnonymousRequiredMixin(object):
# 
#     '''
#     Make any view be accessible by unauthenticated users only
#     '''
# 
#     def dispatch(self, request, *args, **kwargs):
#         if request.user.is_authenticated():
#             return redirect(settings.LOGIN_REDIRECT_URL)
#         return super(AnonymousRequiredMixin, self).dispatch(
#             request, *args, **kwargs)
# 
# class LoginRequiredMixin(object):
# 
#     '''
#     Bind login requirement to any view
#     '''
# 
#     @method_decorator(login_required)
#     def dispatch(self, *args, **kwargs):
#         return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)


# Path: brasilcomvc/common/tests/test_views.py
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponse
from django.test import RequestFactory, TestCase
from django.views.generic import View
from ..views import AnonymousRequiredMixin, LoginRequiredMixin



User = get_user_model()


class RequiresLogin(LoginRequiredMixin, View):

    def get(self, *args, **kwargs):
        return HttpResponse('OK')


class RequiresAnonymous(AnonymousRequiredMixin, View):

    def get(self, *args, **kwargs):
        return HttpResponse('OK')


class LoginRequiredMixinTestCase(TestCase):

    def test_anonymous_user_should_be_redirected_to_login_url(self):
        view = RequiresLogin.as_view()
        request = RequestFactory().get('/')
        request.user = AnonymousUser()
        response = view(request)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            response['Location'], '{}?next=/'.format(settings.LOGIN_URL))

    def test_authenticated_user_should_access_protected_view(self):
        view = RequiresLogin.as_view()

        request = RequestFactory().get('/')