implement identity too
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
@@ -18,10 +18,13 @@ import {
|
||||
DiscoveryApi,
|
||||
ErrorApi,
|
||||
FetchApi,
|
||||
IdentityApi,
|
||||
StorageApi,
|
||||
} from '@backstage/core-plugin-api';
|
||||
import { MockFetchApi, registerMswTestHooks } from '@backstage/test-utils';
|
||||
import {
|
||||
MockFetchApi,
|
||||
mockApis,
|
||||
registerMswTestHooks,
|
||||
} from '@backstage/test-utils';
|
||||
import { rest } from 'msw';
|
||||
import { setupServer } from 'msw/node';
|
||||
import { UserSettingsStorage } from './UserSettingsStorage';
|
||||
@@ -35,13 +38,8 @@ describe('Persistent Storage API', () => {
|
||||
const mockDiscoveryApi = {
|
||||
getBaseUrl: async () => mockBaseUrl,
|
||||
};
|
||||
const mockIdentityApi: Partial<IdentityApi> = {
|
||||
getCredentials: async () => ({ token: 'a-token' }),
|
||||
};
|
||||
const mockIdentityApiFallback: Partial<IdentityApi> = {
|
||||
// This API recreates the guest mode, where the WebStorage is used as fallback
|
||||
getCredentials: async () => ({}),
|
||||
};
|
||||
const mockIdentityApi = mockApis.identity({ token: 'a-token' });
|
||||
const mockIdentityApiFallback = mockApis.identity();
|
||||
|
||||
const createPersistentStorage = (
|
||||
args?: Partial<{
|
||||
@@ -55,7 +53,7 @@ describe('Persistent Storage API', () => {
|
||||
errorApi: mockErrorApi,
|
||||
fetchApi: new MockFetchApi(),
|
||||
discoveryApi: mockDiscoveryApi,
|
||||
identityApi: mockIdentityApi as IdentityApi,
|
||||
identityApi: mockIdentityApi,
|
||||
...args,
|
||||
});
|
||||
};
|
||||
@@ -72,13 +70,13 @@ describe('Persistent Storage API', () => {
|
||||
errorApi: mockErrorApi,
|
||||
fetchApi: new MockFetchApi(),
|
||||
discoveryApi: mockDiscoveryApi,
|
||||
identityApi: mockIdentityApiFallback as IdentityApi,
|
||||
identityApi: mockIdentityApiFallback,
|
||||
...args,
|
||||
});
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks();
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should return undefined for values which are unset', async () => {
|
||||
|
||||
+6
-7
@@ -20,16 +20,15 @@ import { DefaultSettingsPage } from './DefaultSettingsPage';
|
||||
import { UserSettingsTab } from '../UserSettingsTab';
|
||||
import { useOutlet } from 'react-router-dom';
|
||||
import { SettingsLayout } from '../SettingsLayout';
|
||||
import { CatalogApi, catalogApiRef } from '@backstage/plugin-catalog-react';
|
||||
import { catalogApiRef } from '@backstage/plugin-catalog-react';
|
||||
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
|
||||
|
||||
jest.mock('react-router-dom', () => ({
|
||||
...jest.requireActual('react-router-dom'),
|
||||
useOutlet: jest.fn().mockReturnValue(undefined),
|
||||
}));
|
||||
|
||||
const catalogApiMock: jest.Mocked<CatalogApi> = {
|
||||
getEntityByRef: jest.fn(),
|
||||
} as any;
|
||||
const catalogApi = catalogApiMock();
|
||||
|
||||
describe('<DefaultSettingsPage />', () => {
|
||||
beforeEach(() => {
|
||||
@@ -38,7 +37,7 @@ describe('<DefaultSettingsPage />', () => {
|
||||
|
||||
it('should render the settings page with 3 tabs', async () => {
|
||||
const { container } = await renderInTestApp(
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApiMock]]}>
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
|
||||
<DefaultSettingsPage />
|
||||
</TestApiProvider>,
|
||||
);
|
||||
@@ -54,7 +53,7 @@ describe('<DefaultSettingsPage />', () => {
|
||||
</UserSettingsTab>
|
||||
);
|
||||
const { container } = await renderInTestApp(
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApiMock]]}>
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
|
||||
<DefaultSettingsPage tabs={[advancedTabRoute]} />
|
||||
</TestApiProvider>,
|
||||
);
|
||||
@@ -71,7 +70,7 @@ describe('<DefaultSettingsPage />', () => {
|
||||
</SettingsLayout.Route>
|
||||
);
|
||||
const { container } = await renderInTestApp(
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApiMock]]}>
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
|
||||
<DefaultSettingsPage tabs={[advancedTabRoute]} />
|
||||
</TestApiProvider>,
|
||||
);
|
||||
|
||||
@@ -14,32 +14,28 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { renderInTestApp, TestApiRegistry } from '@backstage/test-utils';
|
||||
import {
|
||||
renderInTestApp,
|
||||
TestApiRegistry,
|
||||
mockApis,
|
||||
} from '@backstage/test-utils';
|
||||
import { screen } from '@testing-library/react';
|
||||
import React from 'react';
|
||||
import { UserSettingsIdentityCard } from './UserSettingsIdentityCard';
|
||||
import { ApiProvider } from '@backstage/core-app-api';
|
||||
import { identityApiRef } from '@backstage/core-plugin-api';
|
||||
import { catalogApiRef, entityRouteRef } from '@backstage/plugin-catalog-react';
|
||||
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
|
||||
|
||||
const apiRegistry = TestApiRegistry.from(
|
||||
[
|
||||
identityApiRef,
|
||||
{
|
||||
getProfileInfo: jest.fn(async () => ({})),
|
||||
getBackstageIdentity: jest.fn(async () => ({
|
||||
type: 'user' as const,
|
||||
userEntityRef: 'foo:bar/foobar',
|
||||
ownershipEntityRefs: ['user:default/test-ownership'],
|
||||
})),
|
||||
},
|
||||
],
|
||||
[
|
||||
catalogApiRef,
|
||||
{
|
||||
getEntityByRef: jest.fn(),
|
||||
},
|
||||
mockApis.identity({
|
||||
userEntityRef: 'foo:bar/foobar',
|
||||
ownershipEntityRefs: ['user:default/test-ownership'],
|
||||
}),
|
||||
],
|
||||
[catalogApiRef, catalogApiMock.mock()],
|
||||
);
|
||||
|
||||
describe('<UserSettingsIdentityCard />', () => {
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
MockErrorApi,
|
||||
TestApiProvider,
|
||||
renderInTestApp,
|
||||
mockApis,
|
||||
} from '@backstage/test-utils';
|
||||
import { errorApiRef, identityApiRef } from '@backstage/core-plugin-api';
|
||||
import { fireEvent, waitFor, screen } from '@testing-library/react';
|
||||
@@ -35,9 +36,9 @@ describe('<UserSettingsMenu />', () => {
|
||||
});
|
||||
|
||||
it('handles errors that occur when signing out', async () => {
|
||||
const failingIdentityApi = {
|
||||
signOut: jest.fn().mockRejectedValue(new Error('Logout error')),
|
||||
};
|
||||
const failingIdentityApi = mockApis.identity.mock({
|
||||
signOut: () => Promise.reject(new Error('Logout error')),
|
||||
});
|
||||
const mockErrorApi = new MockErrorApi({ collect: true });
|
||||
await renderInTestApp(
|
||||
<TestApiProvider
|
||||
|
||||
@@ -14,35 +14,30 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { renderInTestApp, TestApiRegistry } from '@backstage/test-utils';
|
||||
import {
|
||||
renderInTestApp,
|
||||
TestApiRegistry,
|
||||
mockApis,
|
||||
} from '@backstage/test-utils';
|
||||
import { screen } from '@testing-library/react';
|
||||
import React from 'react';
|
||||
import { identityApiRef } from '@backstage/core-plugin-api';
|
||||
import { catalogApiRef, entityRouteRef } from '@backstage/plugin-catalog-react';
|
||||
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
|
||||
import { ApiProvider } from '@backstage/core-app-api';
|
||||
import { UserSettingsProfileCard } from './UserSettingsProfileCard';
|
||||
|
||||
const apiRegistry = TestApiRegistry.from(
|
||||
[
|
||||
identityApiRef,
|
||||
{
|
||||
getProfileInfo: jest.fn(async () => ({})),
|
||||
getBackstageIdentity: jest.fn(async () => ({
|
||||
type: 'user' as const,
|
||||
userEntityRef: 'foo:bar/foobar',
|
||||
ownershipEntityRefs: ['user:default/test-ownership'],
|
||||
})),
|
||||
},
|
||||
],
|
||||
[identityApiRef, mockApis.identity()],
|
||||
[
|
||||
catalogApiRef,
|
||||
{
|
||||
getEntityByRef: jest.fn(async () => {
|
||||
return {
|
||||
catalogApiMock({
|
||||
entities: [
|
||||
{
|
||||
apiVersion: 'backstage.io/v1beta1',
|
||||
kind: 'User',
|
||||
metadata: {
|
||||
name: 'Guest',
|
||||
name: 'test',
|
||||
annotations: {},
|
||||
},
|
||||
spec: {
|
||||
@@ -50,9 +45,9 @@ const apiRegistry = TestApiRegistry.from(
|
||||
picture: 'https://example.com/avatar.png',
|
||||
},
|
||||
},
|
||||
};
|
||||
}),
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
@@ -22,20 +22,15 @@ import { useOutlet } from 'react-router-dom';
|
||||
import { SettingsLayout } from '../SettingsLayout';
|
||||
import { screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import {
|
||||
CatalogApi,
|
||||
catalogApiRef,
|
||||
entityRouteRef,
|
||||
} from '@backstage/plugin-catalog-react';
|
||||
import { catalogApiRef, entityRouteRef } from '@backstage/plugin-catalog-react';
|
||||
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
|
||||
|
||||
jest.mock('react-router-dom', () => ({
|
||||
...jest.requireActual('react-router-dom'),
|
||||
useOutlet: jest.fn().mockReturnValue(undefined),
|
||||
}));
|
||||
|
||||
const catalogApiMock: jest.Mocked<CatalogApi> = {
|
||||
getEntityByRef: jest.fn(),
|
||||
} as any;
|
||||
const catalogApi = catalogApiMock();
|
||||
|
||||
describe('<SettingsPage />', () => {
|
||||
beforeEach(() => {
|
||||
@@ -44,7 +39,7 @@ describe('<SettingsPage />', () => {
|
||||
|
||||
it('should render the default settings page with 3 tabs', async () => {
|
||||
const { container } = await renderInTestApp(
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApiMock]]}>
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
|
||||
<SettingsPage />
|
||||
</TestApiProvider>,
|
||||
{
|
||||
@@ -64,7 +59,7 @@ describe('<SettingsPage />', () => {
|
||||
);
|
||||
(useOutlet as jest.Mock).mockReturnValue(advancedTabRoute);
|
||||
const { container } = await renderInTestApp(
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApiMock]]}>
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
|
||||
<SettingsPage />
|
||||
</TestApiProvider>,
|
||||
{
|
||||
@@ -85,7 +80,7 @@ describe('<SettingsPage />', () => {
|
||||
);
|
||||
(useOutlet as jest.Mock).mockReturnValue(advancedTabRoute);
|
||||
const { container } = await renderInTestApp(
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApiMock]]}>
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
|
||||
<SettingsPage />
|
||||
</TestApiProvider>,
|
||||
{
|
||||
@@ -115,7 +110,7 @@ describe('<SettingsPage />', () => {
|
||||
);
|
||||
(useOutlet as jest.Mock).mockReturnValue(customLayout);
|
||||
const { container } = await renderInTestApp(
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApiMock]]}>
|
||||
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
|
||||
<SettingsPage />
|
||||
</TestApiProvider>,
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user