Ejemplo n.º 1
0
 def test_consume_decorator_fails_if_user_doesnt_have_required_mana(self):
     request = mock.MagicMock(user=self.user)
     view_func = mock.MagicMock(return_value=HttpResponse(), __name__="irrelevant")
     view = mana_consume(view_func, 100)
     response = view(request)
     self.assertFalse(view_func.called)
     # This operation should result in an error, anyway
     self.assertEqual(response.status_code, 403)  # Forbidden makes sense
Ejemplo n.º 2
0
    def test_shortcuts(self):
        def arbitrary_view_func(*args, **kwargs):
            return HttpResponse()
        request = mock.MagicMock(user=self.user)
        # Increment decorator
        view = mana_produce(arbitrary_view_func, 100)
        response = view(request)
        self.assertEqual(request.user.mana, 100)
        view = mana_produce(arbitrary_view_func, lambda req: 100)
        response = view(request)
        self.assertEqual(request.user.mana, 200)

        # Decrement decorator
        view = mana_consume(arbitrary_view_func, 100)
        response = view(request)
        self.assertEqual(request.user.mana, 100)
        # A decrement decorator with a negative value does not really make sense
        self.assertRaises(ImproperlyConfigured, lambda: mana_consume(arbitrary_view_func, -100))