# Path: open_connect/middleware/impersonation.py
# class ImpersonationMiddleware(object):
#     """Middleware to impersonate another user."""
#     # pylint: disable=no-self-use
#     def process_request(self, request):
#         """Set the request user to user to impersonate."""
#         # pylint: disable=invalid-name
#         User = get_user_model()
#         impersonate_id = request.session.get('impersonate_id', None)
#         request.user.impersonating = False
#         if not impersonate_id:
#             return
#         if not request.user.can_impersonate():
#             return
#         try:
#             user = User.objects.get(pk=impersonate_id)
#         except User.DoesNotExist:
#             return
#         else:
#             request.user = user
#             request.user.impersonating = True


from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.core.urlresolvers import reverse
from django.test import TestCase, RequestFactory
from open_connect.middleware.impersonation import ImpersonationMiddleware
        self.user = User.objects.create_user(
            username='impersonate@me.local', password='abc')
        self.admin = User.objects.create_user(
            username='admin@dj.local', password='abc')
        self.client.post(
            reverse('account_login'),
            {'login': 'admin@dj.local', 'password': 'abc'}
        )
        self.request_factory = RequestFactory()

    def test_process_request_admin_has_permission(self):
        """A user with permission should be able to impersonate another user."""
        # Make sure user has can_impersonate permission
        permission = Permission.objects.get_by_natural_key(
            'can_impersonate', 'accounts', 'user')
        self.admin.user_permissions.add(permission)
        self.admin.save()
        self.assertTrue(self.admin.has_perm('accounts.can_impersonate'))

        # Call the middleware
        middleware = ImpersonationMiddleware()
        request = self.request_factory.get('/')
        request.session = {'impersonate_id': self.user.pk}
        request.user = self.admin
        middleware.process_request(request)
        self.assertEqual(request.user, self.user)

    def test_process_request_no_permission(self):
        """A user without permission should not be able to impersonate."""
        self.assertFalse(self.user.has_perm('accounts.can_impersonate'))

# Path: open_connect/middleware/impersonation.py
# class ImpersonationMiddleware(object):
#     """Middleware to impersonate another user."""
#     # pylint: disable=no-self-use
#     def process_request(self, request):
#         """Set the request user to user to impersonate."""
#         # pylint: disable=invalid-name
#         User = get_user_model()
#         impersonate_id = request.session.get('impersonate_id', None)
#         request.user.impersonating = False
#         if not impersonate_id:
#             return
#         if not request.user.can_impersonate():
#             return
#         try:
#             user = User.objects.get(pk=impersonate_id)
#         except User.DoesNotExist:
#             return
#         else:
#             request.user = user
#             request.user.impersonating = True


# Path: open_connect/middleware/tests/test_impersonation.py
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.core.urlresolvers import reverse
from django.test import TestCase, RequestFactory
from open_connect.middleware.impersonation import ImpersonationMiddleware

        self.user = User.objects.create_user(
            username='impersonate@me.local', password='abc')
        self.admin = User.objects.create_user(
            username='admin@dj.local', password='abc')
        self.client.post(
            reverse('account_login'),
            {'login': 'admin@dj.local', 'password': 'abc'}
        )
        self.request_factory = RequestFactory()

    def test_process_request_admin_has_permission(self):
        """A user with permission should be able to impersonate another user."""
        # Make sure user has can_impersonate permission
        permission = Permission.objects.get_by_natural_key(
            'can_impersonate', 'accounts', 'user')
        self.admin.user_permissions.add(permission)
        self.admin.save()
        self.assertTrue(self.admin.has_perm('accounts.can_impersonate'))

        # Call the middleware
        middleware = ImpersonationMiddleware()
        request = self.request_factory.get('/')
        request.session = {'impersonate_id': self.user.pk}
        request.user = self.admin
        middleware.process_request(request)
        self.assertEqual(request.user, self.user)

    def test_process_request_no_permission(self):
        """A user without permission should not be able to impersonate."""
        self.assertFalse(self.user.has_perm('accounts.can_impersonate'))

        middleware = ImpersonationMiddleware()