sadpandajoe commented on code in PR #45522:
URL: https://github.com/apache/superset/pull/45522#discussion_r4401186154


##########
superset/extensions/utils.py:
##########
@@ -240,14 +240,35 @@ def get_loaded_extension(
 
 
 def build_extension_data(extension: LoadedExtension) -> dict[str, Any]:
+    from superset.extensions.view import permitted_page_paths, 
registered_page_paths
+
     manifest = extension.manifest
+    registered = set(registered_page_paths(current_app, manifest.id))
+    permitted = permitted_page_paths(current_app, manifest)
     extension_data: dict[str, Any] = {
         "id": manifest.id,
         "publisher": manifest.publisher,
         "name": extension.name,
         "version": extension.version,
         "description": manifest.description or "",
         "dependencies": manifest.dependencies,
+        "contributions": {
+            **manifest.contributions.model_dump(exclude_none=True),
+            # Every declared page is reported, with what became of it.
+            # Dropping the ones that did not get a rule would make a page the
+            # server refused indistinguishable from one never declared, and
+            # the extension's own bundle calls registerPage for every page it
+            # ships -- so the frontend needs to tell "refused, nothing you can
+            # do" apart from "you did not declare this".
+            "pages": [

Review Comment:
   The `registered` / `permitted` flags this builds are what decide whether the 
menu entry, the component registration and the refusal UI appear, but no test 
calls `build_extension_data`: the backend tests cover `registered_page_paths` 
and `permitted_page_paths` separately, and the frontend tests hand-write their 
own page declarations. Swapping the two flags, or reporting a refused page as 
`registered: true`, would pass every current test. Could a unit test call the 
real serializer for an open page, a permission-gated page the user lacks, and a 
refused (colliding) path, and assert the `(registered, permitted)` pair for 
each is `(true, true)`, `(true, false)` and `(false, false)`?



##########
superset-frontend/src/core/pages/navigation.test.tsx:
##########
@@ -0,0 +1,154 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * @fileoverview `useSubPath` / `useNavigate` under a subdirectory deployment.
+ *
+ * Superset mounts its router as `<Router basename={applicationRoot()}>`, so
+ * `location.pathname` arrives with the app root already removed and
+ * `history.push` has it added back. These tests run against a real `Router`
+ * carrying a basename, because that is the only way to tell a correct
+ * implementation from one that happens to work when the basename is empty.
+ */
+
+import { ReactNode } from 'react';
+import { BrowserRouter } from 'react-router-dom';
+import { logging } from '@apache-superset/core/utils';
+import { render, screen } from 'spec/helpers/testing-library';
+import { PageRootContext, useNavigate, useSubPath } from './index';
+
+const APP_ROOT = '/analytics';
+
+// The app root is read once at module evaluation, so it cannot be changed
+// per test without resetting modules -- which would hand this module a second
+// React instance and break hooks. The two helpers are mocked instead, with
+// the behaviour they have under a subdirectory deployment.
+jest.mock('src/utils/navigationUtils', () => ({
+  ...jest.requireActual('src/utils/navigationUtils'),
+  ensureAppRoot: (path: string) =>
+    path.startsWith(`${APP_ROOT}/`) || path === APP_ROOT
+      ? path
+      : `${APP_ROOT}${path}`,
+  stripAppRoot: (path: string) =>
+    path.startsWith(`${APP_ROOT}/`) ? path.slice(APP_ROOT.length) : path,
+}));
+
+const renderAt = (pathname: string, root = '/review-queue', target = '/42') => 
{
+  window.history.replaceState({}, '', `${APP_ROOT}${pathname}`);
+
+  const Probe = () => {
+    const subPath = useSubPath();
+    const navigate = useNavigate();
+    return (
+      <>
+        <span data-test="sub-path">{subPath}</span>
+        <button type="button" onClick={() => navigate(target)}>
+          go
+        </button>
+      </>
+    );
+  };
+
+  const wrapper = ({ children }: { children: ReactNode }) => (
+    <BrowserRouter basename={APP_ROOT}>
+      <PageRootContext.Provider value={root}>
+        {children}
+      </PageRootContext.Provider>
+    </BrowserRouter>
+  );
+
+  render(<Probe />, { wrapper });
+};
+
+afterEach(() => {
+  window.history.replaceState({}, '', '/');
+  jest.restoreAllMocks();
+});
+
+test('useSubPath reads the path beneath the page under an app root', () => {

Review Comment:
   Every case here renders a page root of `/review-queue` under the 
`/analytics` app root, so none of them catches a double strip. If `useSubPath` 
called `stripAppRoot(pathname)` again, `/review-queue/42` is untouched by it 
and these tests still pass, while a page whose root starts with the app-root 
segment (say `/analytics/review-queue` at `/analytics`) would lose that segment 
and read its sub-path as `/`. Could you add a case like 
`renderAt('/analytics/review-queue/42', '/analytics/review-queue')` that 
expects `/42`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to