Ejemplo n.º 1
0
def test_raises_on_cookie_auth():
    "It should raise if user picked Cookie based auth and csrf=False"

    class Auth(APIKeyCookie):
        def authenticate(self, request, key):
            return request.COOKIES[key] == "foo"

    api = NinjaAPI(auth=Auth(), csrf=False)

    @api.get("/some")
    def some_method(request):
        pass

    with pytest.raises(ConfigError):
        api._validate()
Ejemplo n.º 2
0
def test_raises_on_cookie_auth():
    "It should raise if user picked Cookie based auth and csrf=False"

    class Auth(APIKeyCookie):
        def authenticate(self, request, key):
            return request.COOKIES[key] == "foo"

    api = NinjaAPI(auth=Auth(), csrf=False)

    @api.get("/some")
    def some_method(request):
        pass

    with pytest.raises(ConfigError):
        api._validate()

    try:
        import os

        os.environ["NINJA_SKIP_REGISTRY"] = ""

        # Check for wrong error reported
        match = "Looks like you created multiple NinjaAPIs"
        with pytest.raises(ConfigError, match=match):
            api.urls

        # django debug server can attempt to import the urls twice when errors exist
        # verify we get the correct error reported
        match = "Cookie Authentication must be used with CSRF"
        with pytest.raises(ConfigError, match=match):
            with mock.patch(
                    "ninja.main._imported_while_running_in_debug_server",
                    True):
                api.urls

    finally:
        os.environ["NINJA_SKIP_REGISTRY"] = "yes"