-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add permission support in lightspeed
Signed-off-by: Karthik <[email protected]>
- Loading branch information
1 parent
63c80a2
commit 1967890
Showing
6 changed files
with
213 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
workspaces/lightspeed/plugins/lightspeed/src/components/__tests__/LightspeedPage.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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 { IdentityApi, identityApiRef } from '@backstage/core-plugin-api'; | ||
import { usePermission } from '@backstage/plugin-permission-react'; | ||
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils'; | ||
|
||
import { screen, waitFor } from '@testing-library/react'; | ||
|
||
import { LightspeedPage } from '../LightspeedPage'; | ||
|
||
jest.mock('../LightSpeedChat', () => ({ | ||
LightspeedChat: () => <>LightspeedChat</>, | ||
})); | ||
|
||
jest.mock('@backstage/plugin-permission-react', () => ({ | ||
usePermission: jest.fn(), | ||
RequirePermission: jest.fn(), | ||
})); | ||
|
||
const identityApi = { | ||
async getCredentials() { | ||
return { token: 'test-token' }; | ||
}, | ||
} as IdentityApi; | ||
|
||
jest.mock('@mui/material', () => ({ | ||
...jest.requireActual('@mui/material'), | ||
makeStyles: () => () => { | ||
return { | ||
container: 'container', | ||
}; | ||
}, | ||
})); | ||
|
||
jest.mock('../../hooks/useAllModels', () => ({ | ||
useAllModels: jest.fn().mockResolvedValue({ | ||
data: [], | ||
}), | ||
})); | ||
|
||
const mockUsePermission = usePermission as jest.MockedFunction< | ||
typeof usePermission | ||
>; | ||
|
||
describe('LightspeedPage', () => { | ||
it('should not display chatbot if permission checks are in loading phase', async () => { | ||
mockUsePermission.mockReturnValue({ loading: true, allowed: true }); | ||
|
||
await renderInTestApp( | ||
<TestApiProvider apis={[[identityApiRef, identityApi]]}> | ||
<LightspeedPage /> | ||
</TestApiProvider>, | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.queryByText('LightspeedChat')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should display permission required alert', async () => { | ||
mockUsePermission.mockReturnValue({ loading: false, allowed: false }); | ||
|
||
await renderInTestApp( | ||
<TestApiProvider apis={[[identityApiRef, identityApi]]}> | ||
<LightspeedPage /> | ||
</TestApiProvider>, | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Permission required')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should display lightspeed chatbot', async () => { | ||
mockUsePermission.mockReturnValue({ loading: false, allowed: true }); | ||
|
||
await renderInTestApp( | ||
<TestApiProvider apis={[[identityApiRef, identityApi]]}> | ||
<LightspeedPage /> | ||
</TestApiProvider>, | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('LightspeedChat')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
workspaces/lightspeed/plugins/lightspeed/src/hooks/useLightspeedDeletePermission.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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 { usePermission } from '@backstage/plugin-permission-react'; | ||
|
||
import { lightspeedConversationsDeletePermission } from '@red-hat-developer-hub/backstage-plugin-lightspeed-common'; | ||
|
||
export const useLightspeedDeletePermission = () => { | ||
const lightspeedDeletePermissionResult = usePermission({ | ||
permission: lightspeedConversationsDeletePermission, | ||
}); | ||
|
||
return lightspeedDeletePermissionResult; | ||
}; |
36 changes: 36 additions & 0 deletions
36
workspaces/lightspeed/plugins/lightspeed/src/hooks/useLightspeedViewPermission.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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 { usePermission } from '@backstage/plugin-permission-react'; | ||
|
||
import { | ||
lightspeedConversationsCreatePermission, | ||
lightspeedConversationsReadPermission, | ||
} from '@red-hat-developer-hub/backstage-plugin-lightspeed-common'; | ||
|
||
export const useLightspeedViewPermission = () => { | ||
const canReadConversations = usePermission({ | ||
permission: lightspeedConversationsReadPermission, | ||
}); | ||
|
||
const canCreateConversations = usePermission({ | ||
permission: lightspeedConversationsCreatePermission, | ||
}); | ||
|
||
return { | ||
loading: canReadConversations.loading || canCreateConversations.loading, | ||
allowed: canReadConversations.allowed && canCreateConversations.allowed, | ||
}; | ||
}; |