refactor: move app auth proviter to core api

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2024-04-05 16:51:17 +02:00
committed by Patrik Oldsberg
parent feab471511
commit b602171cc2
9 changed files with 10 additions and 46 deletions
-6
View File
@@ -6,9 +6,6 @@
import { default as React_2 } from 'react';
import { ReactNode } from 'react';
// @public
export function AppAuthProvider(props: { children: ReactNode }): JSX.Element;
// @public
export function CookieAuthRedirect(): React_2.JSX.Element | null;
@@ -24,9 +21,6 @@ export type CookieAuthRefreshProviderProps = {
children: ReactNode;
};
// @public
export function isProtectedApp(): boolean;
// @public
export function useCookieAuthRefresh(options: {
pluginId: string;
@@ -1,103 +0,0 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed 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.
*/
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import { componentsApiRef } from '@backstage/frontend-plugin-api';
import { discoveryApiRef, fetchApiRef } from '@backstage/core-plugin-api';
import { AppAuthProvider } from './AppAuthProvider';
const now = 1710316886171;
const tenMinutesInMilliseconds = 10 * 60 * 1000;
const tenMinutesFromNowInMilliseconds = now + tenMinutesInMilliseconds;
const expiresAt = new Date(tenMinutesFromNowInMilliseconds).toISOString();
jest.mock('@backstage/core-plugin-api', () => {
return {
...jest.requireActual('@backstage/core-plugin-api'),
useApp: jest.fn().mockReturnValue({
getComponents: () => ({ Progress: () => <div data-testid="progress" /> }),
}),
useApi: jest.fn(ref => {
if (ref === discoveryApiRef) {
return {
getBaseUrl: jest.fn().mockResolvedValue('http://localhost:7000/app'),
};
}
if (ref === fetchApiRef) {
return {
fetch: jest.fn().mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue({ expiresAt }),
}),
};
}
if (ref === componentsApiRef) {
return {
getComponent: jest
.fn()
.mockReturnValue(() => <div data-testid="progress" />),
};
}
throw new Error(`Attempted to use an unmocked API reference: ${ref}`);
}),
};
});
describe('AppAuthProvider', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers({ now });
});
afterEach(() => {
jest.useRealTimers();
});
it('should render the children when app mode is undefined', async () => {
render(<AppAuthProvider>Test content</AppAuthProvider>);
await waitFor(() =>
expect(screen.getByText('Test content')).toBeInTheDocument(),
);
});
it('should render the children the app mode is public', async () => {
render(
<>
<meta name="backstage-app-mode" content="public" />
<AppAuthProvider>Test content</AppAuthProvider>
</>,
);
await waitFor(() =>
expect(screen.getByText('Test content')).toBeInTheDocument(),
);
});
it('should render the children wrapped in the CookieAuthRefreshProvider', async () => {
render(
<>
<meta name="backstage-app-mode" content="protected" />
<AppAuthProvider>Test content</AppAuthProvider>
</>,
);
await waitFor(() =>
expect(screen.getByText('Test content')).toBeInTheDocument(),
);
});
});
@@ -1,37 +0,0 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed 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.
*/
import React, { ReactNode } from 'react';
import { CookieAuthRefreshProvider } from '@backstage/plugin-auth-react';
import { isProtectedApp } from './isProtectedApp';
/**
* @public
* A provider that will protect the app when running in protected experimental mode.
*/
export function AppAuthProvider(props: { children: ReactNode }): JSX.Element {
const { children } = props;
if (isProtectedApp()) {
return (
<CookieAuthRefreshProvider pluginId="app">
{children}
</CookieAuthRefreshProvider>
);
}
return <>{children}</>;
}
@@ -1,18 +0,0 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed 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.
*/
export { isProtectedApp } from './isProtectedApp';
export { AppAuthProvider } from './AppAuthProvider';
@@ -1,25 +0,0 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed 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.
*/
/**
* @public
* An utility function that detects whether the app is operating in protected mode.
*/
export function isProtectedApp() {
const element = document.querySelector('meta[name="backstage-app-mode"]');
const appMode = element?.getAttribute('content') ?? 'public';
return appMode === 'protected';
}
@@ -17,6 +17,5 @@
// The index file in ./components/ is typically responsible for selecting
// which components are public API and should be exported from the package.
export * from './AppAuthProvider';
export * from './CookieAuthRedirect';
export * from './CookieAuthRefreshProvider';