Пример #1
0
    def test_deleting_from_cache(self):
        """We have a cached value, but what happens when we delete?"""
        fake_user = utils.fake_user(pk=1, username="******")
        fake_override_model = utils.FakeOverrideModel()
        fake_override_model.user = fake_user
        fake_override_qs = self.mimic.create_mock_anything()
        # Because we're simulating a delete
        fake_override_qs.order_by("priority").and_return([])

        def fake_filter(*args, **kwargs):
            return fake_override_qs

        models.UserTemplateOverride.objects.filter = fake_filter
        signals.model_to_dict = utils.mock_model_to_dict

        cache_key = middleware.TemplateOverrideMiddleware.get_tmpl_override_cache_key(fake_user)

        self.mimic.replay_all()

        cache.set(
            cache_key, ('[{"override_dir": "/override", "pk": 1,' '"priority": 1, "override_name": "test"}]'), 600
        )

        signals.update_user_override_cache(models.UserTemplateOverride, fake_override_model)
        expected = "[]"

        self.assertEqual(cache.get(cache_key), expected)
Пример #2
0
    def test_update_user_override_cache(self):
        """Does our signal update cache as we expect?"""
        fake_user = utils.fake_user(pk=1, username="******")
        fake_override_model = utils.FakeOverrideModel()
        fake_override_model.user = fake_user
        fake_override_qs = self.mimic.create_mock_anything()
        fake_override_qs.order_by("priority").and_return([fake_override_model])

        def fake_filter(*args, **kwargs):
            return fake_override_qs

        models.UserTemplateOverride.objects.filter = fake_filter
        signals.model_to_dict = utils.mock_model_to_dict

        cache_key = middleware.TemplateOverrideMiddleware.get_tmpl_override_cache_key(fake_user)

        self.mimic.replay_all()

        cache.set(cache_key, "[]", 600)

        # Verify that we have a negative override cache value
        self.assertEqual(cache.get(cache_key), "[]")

        signals.update_user_override_cache(models.UserTemplateOverride, fake_override_model)
        expected = json.dumps([utils.mock_model_to_dict(fake_override_model)])

        # Verify that our cache key matches what we "pull" from db.
        self.assertEqual(cache.get(cache_key), expected)
Пример #3
0
    def test_update_user_override_cache(self):
        """Does our signal update cache as we expect?"""
        fake_user = utils.fake_user(pk=1, username='******')
        fake_override_model = utils.FakeOverrideModel()
        fake_override_model.user = fake_user
        fake_override_qs = self.mimic.create_mock_anything()
        fake_override_qs.order_by('priority').and_return([fake_override_model])

        def fake_filter(*args, **kwargs):
            return fake_override_qs

        models.UserTemplateOverride.objects.filter = fake_filter
        signals.model_to_dict = utils.mock_model_to_dict

        cache_key = middleware.TemplateOverrideMiddleware.get_tmpl_override_cache_key(
            fake_user)

        self.mimic.replay_all()

        cache.set(cache_key, '[]', 600)

        # Verify that we have a negative override cache value
        self.assertEqual(cache.get(cache_key), '[]')

        signals.update_user_override_cache(models.UserTemplateOverride,
                                           fake_override_model)
        expected = json.dumps([utils.mock_model_to_dict(fake_override_model)])

        # Verify that our cache key matches what we "pull" from db.
        self.assertEqual(cache.get(cache_key), expected)
Пример #4
0
    def test_deleting_from_cache(self):
        """We have a cached value, but what happens when we delete?"""
        fake_user = utils.fake_user(pk=1, username='******')
        fake_override_model = utils.FakeOverrideModel()
        fake_override_model.user = fake_user
        fake_override_qs = self.mimic.create_mock_anything()
        # Because we're simulating a delete
        fake_override_qs.order_by('priority').and_return([])

        def fake_filter(*args, **kwargs):
            return fake_override_qs

        models.UserTemplateOverride.objects.filter = fake_filter
        signals.model_to_dict = utils.mock_model_to_dict

        cache_key = middleware.TemplateOverrideMiddleware.get_tmpl_override_cache_key(
            fake_user)

        self.mimic.replay_all()

        cache.set(
            cache_key,
            ('[{"override_dir": "/override", "pk": 1,'
             '"priority": 1, "override_name": "test"}]'),
            600,
        )

        signals.update_user_override_cache(models.UserTemplateOverride,
                                           fake_override_model)
        expected = '[]'

        self.assertEqual(cache.get(cache_key), expected)
Пример #5
0
    def test_unauthenticated_user_is_skipped(self):
        """Make sure we don't do any lookups for unauth'ed users."""
        template_dirs = ('./templates',)
        staticfiles_dirs = ('./static',)
        middleware.settings = utils.fake_settings(
            TEMPLATE_DIRS=template_dirs,
            STATICFILES_DIRS=staticfiles_dirs,
            PROJECT_PATH='.',
        )

        fake_request = utils.FakeRequest()
        fake_request.user = utils.fake_user(is_authenticated=lambda self: False)

        self.assertFalse(fake_request.user.is_authenticated())

        mid_inst = middleware.TemplateOverrideMiddleware()

        mid_inst.process_request(fake_request)

        # Assert that nothing has changed, our unauthenticated user is a
        # noop.
        self.assertEqual(
            middleware.settings.TEMPLATE_DIRS, template_dirs
        )

        self.assertEqual(
            middleware.settings.STATICFILES_DIRS, staticfiles_dirs
        )