-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rbac): save and display role description in the frontend (#1206)
fix(rbac): save and display role description in the frontend Signed-off-by: Oleksandr Andriienko <[email protected]>
- Loading branch information
1 parent
2855713
commit ff61266
Showing
11 changed files
with
354 additions
and
79 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
84 changes: 84 additions & 0 deletions
84
plugins/rbac/src/components/RoleOverview/AboutCard.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,84 @@ | ||
import React from 'react'; | ||
|
||
import { renderInTestApp } from '@backstage/test-utils'; | ||
|
||
import { Role } from '@janus-idp/backstage-plugin-rbac-common'; | ||
|
||
import { useRole } from '../../hooks/useRole'; | ||
import { AboutCard } from './AboutCard'; | ||
|
||
jest.mock('../../hooks/useRole', () => ({ | ||
useRole: jest.fn(), | ||
})); | ||
|
||
const mockRole: Role = { | ||
name: 'role:default/rbac-admin', | ||
memberReferences: ['user:default/tom', 'group:default/performance-dev-team'], | ||
metadata: { | ||
source: 'rest', | ||
description: 'performance dev team', | ||
}, | ||
}; | ||
|
||
const mockRoleWithoutDescription: Role = { | ||
name: 'role:default/rbac-admin', | ||
memberReferences: ['user:default/tom', 'group:default/performance-dev-team'], | ||
metadata: { | ||
source: 'rest', | ||
description: undefined, | ||
}, | ||
}; | ||
|
||
const mockUseRole = useRole as jest.MockedFunction<typeof useRole>; | ||
|
||
describe('AboutCard', () => { | ||
it('should show role metadata information', async () => { | ||
mockUseRole.mockReturnValue({ | ||
loading: false, | ||
role: mockRole, | ||
roleError: { | ||
name: '', | ||
message: '', | ||
}, | ||
}); | ||
const { queryByText } = await renderInTestApp( | ||
<AboutCard roleName="role:default/rbac_admin" />, | ||
); | ||
expect(queryByText('About')).not.toBeNull(); | ||
expect(queryByText('performance dev team')).not.toBeNull(); | ||
}); | ||
|
||
it('should display stub, when role description is absent', async () => { | ||
mockUseRole.mockReturnValue({ | ||
loading: false, | ||
role: mockRoleWithoutDescription, | ||
roleError: { | ||
name: '', | ||
message: '', | ||
}, | ||
}); | ||
const { queryByText } = await renderInTestApp( | ||
<AboutCard roleName="role:default/rbac_admin" />, | ||
); | ||
expect(queryByText('About')).not.toBeNull(); | ||
expect(queryByText('No description')).not.toBeNull(); | ||
}); | ||
|
||
it('should show an error if api call fails', async () => { | ||
mockUseRole.mockReturnValue({ | ||
loading: false, | ||
role: mockRole, | ||
roleError: { | ||
name: 'Role not found', | ||
message: 'Role not found', | ||
}, | ||
}); | ||
const { queryByText } = await renderInTestApp( | ||
<AboutCard roleName="role:default/rbac_admin" />, | ||
); | ||
expect( | ||
queryByText('Error: Something went wrong while fetching role'), | ||
).not.toBeNull(); | ||
expect(queryByText('Role not found')).not.toBeNull(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
|
||
import { mockMembers } from '../__fixtures__/mockMembers'; | ||
import { useRole } from './useRole'; | ||
|
||
const apiMock = { | ||
getRole: jest.fn().mockImplementation(), | ||
getMembers: jest.fn().mockImplementation(), | ||
}; | ||
|
||
jest.mock('@backstage/core-plugin-api', () => { | ||
const actualApi = jest.requireActual('@backstage/core-plugin-api'); | ||
return { | ||
...actualApi, | ||
useApi: jest.fn().mockImplementation(() => { | ||
return apiMock; | ||
}), | ||
}; | ||
}); | ||
|
||
describe('useRole', () => { | ||
beforeEach(() => { | ||
apiMock.getRole = jest.fn().mockImplementation(async () => { | ||
return [ | ||
{ | ||
memberReferences: [ | ||
'group:default/admins', | ||
'user:default/amelia.park', | ||
'user:default/calum.leavy', | ||
'group:default/team-b', | ||
'group:default/team-c', | ||
], | ||
name: 'role:default/rbac_admin', | ||
metadata: { | ||
source: 'rest', | ||
description: 'default rbac admin group', | ||
}, | ||
}, | ||
]; | ||
}); | ||
apiMock.getMembers = jest.fn().mockImplementation(async () => mockMembers); | ||
}); | ||
|
||
describe('useRole', () => { | ||
it('should throw an error on get role', async () => { | ||
apiMock.getRole = jest.fn().mockImplementation(() => { | ||
throw new Error('Some error message'); | ||
}); | ||
const { result } = renderHook(() => useRole('role:default/rbac_admin')); | ||
await waitFor(() => { | ||
expect(result.current.loading).toBeFalsy(); | ||
expect(result.current.roleError.message).toEqual('Some error message'); | ||
}); | ||
}); | ||
|
||
it('should return role', async () => { | ||
const { result } = renderHook(() => useRole('role:default/rbac_admin')); | ||
await waitFor(() => { | ||
expect(result.current.loading).toBeFalsy(); | ||
expect(result.current.role).toEqual({ | ||
memberReferences: [ | ||
'group:default/admins', | ||
'user:default/amelia.park', | ||
'user:default/calum.leavy', | ||
'group:default/team-b', | ||
'group:default/team-c', | ||
], | ||
name: 'role:default/rbac_admin', | ||
metadata: { | ||
source: 'rest', | ||
description: 'default rbac admin group', | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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,31 @@ | ||
import { useAsync } from 'react-use'; | ||
|
||
import { useApi } from '@backstage/core-plugin-api'; | ||
|
||
import { Role } from '@janus-idp/backstage-plugin-rbac-common'; | ||
|
||
import { rbacApiRef } from '../api/RBACBackendClient'; | ||
|
||
export const useRole = ( | ||
roleEntityRef: string, | ||
): { | ||
loading: boolean; | ||
role: Role | undefined; | ||
roleError: Error; | ||
} => { | ||
const rbacApi = useApi(rbacApiRef); | ||
const { | ||
value: roles, | ||
loading, | ||
error: roleError, | ||
} = useAsync(async () => await rbacApi.getRole(roleEntityRef)); | ||
|
||
return { | ||
loading, | ||
role: Array.isArray(roles) ? roles[0] : undefined, | ||
roleError: (roleError as Error) || { | ||
name: (roles as Response)?.status, | ||
message: `Error fetching the role. ${(roles as Response)?.statusText}`, | ||
}, | ||
}; | ||
}; |
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
Oops, something went wrong.