def test_get_strategy(self):
        settings = fake_settings(
            MITHRIL_STRATEGY='mithril.tests.fake_strategy.FakeStrategy'
        )

        middleware = WhitelistMiddleware()
        strategy = middleware.get_strategy(settings)
        self.assertTrue(isinstance(strategy, FakeStrategy)) 
    def test_process_view_skips_exempt(self):
        anything = lambda *a: a
        anything.mithril_exempt = True
        mw = WhitelistMiddleware()

        self.assertEqual(
            mw.process_view(None, anything, None, None),
            None
        )
Example #3
0
    def __call__(self, request, *args, **kwargs):
        whitelist = self.obj_from_request(request, *args, **kwargs)
        strategy = WhitelistMiddleware().get_strategy()
        current_ip = strategy.get_ip_from_request(request)

        form_args = (current_ip, whitelist)
        form_kwargs = {}
        if hasattr(self, 'range_form_class'):
            form_kwargs = {'range_form_class': self.range_form_class}

        if request.method == 'POST':
            form_args += (request.POST,) 
        form = self.form_class(*form_args, **form_kwargs)
        if form.is_valid():
            self.save_form(request, form, *args, **kwargs)
            return HttpResponseRedirect('.')

        return self.respond(request, form, whitelist, *args, **kwargs)
    def test_process_view_delegates_to_strategy(self):
        strategy = FakeStrategy()
        expected = random.randint(0, 10)

        def collect(*a, **kw):
            strategy.args = a
            strategy.kwargs = kw
            return expected

        strategy.process_view = collect

        mw = WhitelistMiddleware()
        mw.get_strategy = lambda *a: strategy

        result = mw.process_view(expected, expected, [], {})

        self.assertEqual(result, expected)
        self.assertEqual(strategy.args, (expected, expected))
        self.assertEqual(strategy.kwargs, {})