content.append(",".join(item.values())) return "\n".join(content) def operation(request): return [ {"name": "Jonathan", "lastname": "Doe"}, {"name": "Sarah", "lastname": "Calvin"}, ] api_xml = NinjaAPI(renderer=XMLRenderer()) api_csv = NinjaAPI(renderer=CSVRenderer()) api_xml.get("/test")(operation) api_csv.get("/test")(operation) @pytest.mark.parametrize( "api,content_type,expected_content", [ ( api_xml, "text/xml; charset=utf-8", '<?xml version="1.0" encoding="utf-8"?>\n<data>' "<item><name>Jonathan</name><lastname>Doe</lastname></item>" "<item><name>Sarah</name><lastname>Calvin</lastname></item>" "</data>", ), (
@api.exception_handler(CustomException) def on_custom_error(request, exc): return api.create_response(request, {"custom": True}, status=401) for path, auth in [ ("django_auth", django_auth), ("callable", callable_auth), ("apikeyquery", KeyQuery()), ("apikeyheader", KeyHeader()), ("apikeycookie", KeyCookie()), ("basic", BasicAuth()), ("bearer", BearerAuth()), ("customexception", KeyHeaderCustomException()), ]: api.get(f"/{path}", auth=auth, operation_id=path)(demo_operation) client = TestClient(api) class MockUser(str): is_authenticated = True BODY_UNAUTHORIZED_DEFAULT = dict(detail="Unauthorized") @pytest.mark.parametrize( "path,kwargs,expected_code,expected_body", [ ("/django_auth", {}, 401, BODY_UNAUTHORIZED_DEFAULT),
def demo_operaiton(request): return {"auth": request.auth} api = NinjaAPI() for path, auth in [ ("django_auth", django_auth), ("callable", callble_auth), ("apikeyquery", KeyQuery()), ("apikeyheader", KeyHeader()), ("apikeycookie", KeyCookie()), ("basic", BasicAuth()), ("bearer", BearerAuth()), ]: api.get(f"/{path}", auth=auth)(demo_operaiton) client = NinjaClient(api) class MockUser(str): is_authenticated = True @pytest.mark.parametrize( "path,kwargs,expected_code", [ ("/django_auth", {}, 401), ("/django_auth", dict(user=MockUser("admin")), 200), ("/callable", {}, 401), ("/callable?auth=demo", {}, 200),