コード例 #1
0
ファイル: tests.py プロジェクト: saifrim/django-impersonate
class TestMiddleware(TestCase):
    def setUp(self):
        from impersonate.middleware import ImpersonateMiddleware

        self.superuser = UserFactory.create(
            username='******',
            is_superuser=True,
        )
        self.user = UserFactory.create(username='******')
        self.factory = RequestFactory()
        self.middleware = ImpersonateMiddleware()

    def _impersonated_request(self, use_id=True):
        request = self.factory.get('/')
        request.user = self.superuser
        request.session = {
            '_impersonate': self.user.id if use_id else self.user,
        }
        self.middleware.process_request(request)

        # Check request.user and request.user.real_user
        self.assertEqual(request.user, self.user)
        self.assertEqual(request.impersonator, self.superuser)
        self.assertTrue(request.user.is_impersonate)

    def test_impersonated_request(self):
        self._impersonated_request()

    def test_impersonated_request_non_id(self):
        ''' Test to ensure User objects don't kill the app.
            See Issue #15
        '''
        self._impersonated_request(use_id=False)
コード例 #2
0
ファイル: tests.py プロジェクト: philroche/django-impersonate
class TestMiddleware(TestCase):
    def setUp(self):
        from impersonate.middleware import ImpersonateMiddleware

        self.superuser = UserFactory.create(
            username='******',
            is_superuser=True,
        )
        self.user = UserFactory.create(username='******')
        self.factory = RequestFactory()
        self.middleware = ImpersonateMiddleware()

    def _impersonated_request(self, use_id=True):
        request = self.factory.get('/')
        request.user = self.superuser
        request.session = {
            '_impersonate': self.user.id if use_id else self.user,
        }
        self.middleware.process_request(request)

        # Check request.user and request.user.real_user
        self.assertEqual(request.user, self.user)
        self.assertEqual(request.impersonator, self.superuser)
        self.assertTrue(request.user.is_impersonate)

    def test_impersonated_request(self):
        self._impersonated_request()

    def test_impersonated_request_non_id(self):
        ''' Test to ensure User objects don't kill the app.
            See Issue #15
        '''
        self._impersonated_request(use_id=False)
コード例 #3
0
ファイル: tests.py プロジェクト: saifrim/django-impersonate
    def setUp(self):
        from impersonate.middleware import ImpersonateMiddleware

        self.superuser = UserFactory.create(
            username='******',
            is_superuser=True,
        )
        self.user = UserFactory.create(username='******')
        self.factory = RequestFactory()
        self.middleware = ImpersonateMiddleware()
コード例 #4
0
    def setUp(self):
        from impersonate.middleware import ImpersonateMiddleware

        self.superuser = UserFactory.create(
            username='******',
            is_superuser=True,
        )
        self.user = UserFactory.create(username='******')

        self.factory = RequestFactory()

        def dummy_get_response(request):
            return None

        self.middleware = ImpersonateMiddleware(dummy_get_response)
コード例 #5
0
ファイル: tests.py プロジェクト: philroche/django-impersonate
    def setUp(self):
        from impersonate.middleware import ImpersonateMiddleware

        self.superuser = UserFactory.create(
            username='******',
            is_superuser=True,
        )
        self.user = UserFactory.create(username='******')
        self.factory = RequestFactory()
        self.middleware = ImpersonateMiddleware()
コード例 #6
0
ファイル: tests.py プロジェクト: HayesData/SecretAgent
class TestMiddleware(TestCase):
    def setUp(self):
        from impersonate.middleware import ImpersonateMiddleware

        self.superuser = UserFactory.create(
            username='******',
            is_superuser=True,
        )
        self.user = UserFactory.create(username='******')
        self.factory = RequestFactory()
        self.middleware = ImpersonateMiddleware()

    def test_impersonated_request(self):
        request = self.factory.get('/')
        request.user = self.superuser
        request.session = {
            '_impersonate': self.user.id
        }
        self.middleware.process_request(request)

        # Check request.user and request.user.real_user
        self.assertEqual(request.user, self.user)
        self.assertEqual(request.impersonator, self.superuser)
        self.assertTrue(request.user.is_impersonate)
コード例 #7
0
class TestMiddleware(TestCase):
    def setUp(self):
        from impersonate.middleware import ImpersonateMiddleware

        self.superuser = UserFactory.create(
            username='******',
            is_superuser=True,
        )
        self.user = UserFactory.create(username='******')

        self.factory = RequestFactory()

        def dummy_get_response(request):
            return None

        self.middleware = ImpersonateMiddleware(dummy_get_response)

    def _impersonated_request(self, use_id=True, _impersonate_start=None):
        request = self.factory.get('/')
        request.user = self.superuser
        request.session = {
            '_impersonate': self.user.pk if use_id else self.user,
            '_impersonate_start': _impersonate_start,
        }
        self.middleware.process_request(request)

        # Check request.user and request.user.real_user
        self.assertEqual(request.user, self.user)
        self.assertEqual(request.impersonator, self.superuser)
        self.assertEqual(request.user.impersonator, self.superuser)
        self.assertEqual(request.real_user, self.superuser)
        self.assertTrue(request.user.is_impersonate)

    def test_impersonated_request(self):
        self._impersonated_request()

    def test_impersonated_request_non_id(self):
        ''' Test to ensure User objects don't kill the app.
            See Issue #15
        '''
        self._impersonated_request(use_id=False)

    @override_settings(IMPERSONATE={'MAX_DURATION': 3600})
    def test_impersonated_request_with_max_duration(self):
        self._impersonated_request(
            _impersonate_start=datetime.now(timezone.utc).timestamp())

    @override_settings(IMPERSONATE={'MAX_DURATION': 3600})
    def test_reject_without_start_time(self):
        ''' Test to ensure that requests without a start time
            are rejected when MAX_DURATION is set
        '''
        request = self.factory.get('/')
        request.user = self.superuser
        request.session = {
            '_impersonate': self.user.pk,
        }
        self.middleware.process_request(request)

        self.assertEqual(request.user, self.superuser)
        self.assertFalse(request.user.is_impersonate)

    @override_settings(IMPERSONATE={'MAX_DURATION': 3600})
    def test_reject_expired_impersonation(self):
        ''' Test to ensure that requests with a start time before (now - MAX_DURATION)
            are rejected
        '''
        request = self.factory.get('/')
        request.user = self.superuser
        request.session = {
            '_impersonate':
            self.user.pk,
            '_impersonate_start':
            (datetime.now(timezone.utc) - timedelta(seconds=3601)).timestamp(),
        }
        response = self.middleware.process_request(request)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.get('Location'), reverse('impersonate-stop'))

    def test_not_impersonated_request(self, use_id=True):
        """Check the real_user request attr is set correctly when **not** impersonating."""
        request = self.factory.get('/')
        request.user = self.user
        request.session = {}
        self.middleware.process_request(request)
        # Check request.user and request.user.real_user
        self.assertEqual(request.user, self.user)
        self.assertIsNone(request.impersonator, None)
        self.assertEqual(request.real_user, self.user)
        self.assertFalse(request.user.is_impersonate)