def test_super_user_exempt(self): """ Make sure that superuser is exempt from IP whitelists. """ expected = '10.112.12.12' req = self.fake_request(expected) req.user.is_superuser = False whitelist = Whitelist.objects.create(name='asdf', slug='asdf') whitelist.range_set.create(ip='127.0.0.1', cidr=32) strat = Strategy() strat.return_one = lambda *a, **kw: whitelist.pk strat.actions = [['return_one', 'pk']] self.assertTrue( isinstance(strat.process_view(req, None, (), {}), HttpResponseForbidden) ) req.user.is_superuser = True self.assertEqual( strat.process_view(req, None, (), {}), None )
def test_process_view_passes_curried_mithril_reset_view(self): expected = fmt_ip(random.randint(1, 0xFFFFFFFF)) req = self.fake_request(expected) strat = Strategy() wl = Whitelist.objects.create( name='okay', slug='okay' ) wl.range_set.create( ip='0.0.0.0', cidr=32 ) strat.return_one = lambda *a, **kw: wl.pk strat.actions = [['return_one', 'pk']] view = lambda:None view.mithril_reset = \ lambda request, view, args, kwargs: check( request, view, args, kwargs) def check(request, target_view, target_args, target_kwargs): self.assertTrue(request is req) self.assertEqual(target_view, view) return expected self.assertEqual( strat.process_view(req, view, (), {}), expected )
def test_process_view_skips_lookups_raising_fielderror(self): expected = fmt_ip(random.randint(1, 0xFFFFFFFF)) req = self.fake_request(expected) strat = Strategy() strat.return_one = lambda *a, **kw: 1 strat.actions = [['return_one', 'anything']] self.assertEqual( strat.process_view(req, None, (), {}), None )
def test_process_view_skips_methods_returning_no_whitelists(self): expected = fmt_ip(random.randint(1, 0xFFFFFFFF)) req = self.fake_request(expected) strat = Strategy() # there are no whitelists at this point, # so a 'pk' lookup will return nothing! strat.return_one = lambda *a, **kw: 1 strat.actions = [['return_one', 'pk']] self.assertEqual( strat.process_view(req, None, (), {}), None )
def test_process_view_passes_forbidden_response_class(self): expected = fmt_ip(random.randint(1, 0xFFFFFFFF)) req = self.fake_request(expected) strat = Strategy() wl = Whitelist.objects.create( name='okay', slug='okay' ) wl.range_set.create( ip='0.0.0.0', cidr=32 ) strat.return_one = lambda *a, **kw: wl.pk strat.actions = [['return_one', 'pk']] self.assertTrue( isinstance(strat.process_view(req, None, (), {}), HttpResponseForbidden) )