-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
107 additions
and
219 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { GroupKind } from '~/k8sTypes'; | ||
|
||
type MockGroupType = { | ||
name?: string; | ||
}; | ||
export const mockGroup = ({ name = 'odh-admins' }: MockGroupType): GroupKind => ({ | ||
metadata: { | ||
name, | ||
}, | ||
users: [], | ||
apiVersion: 'user.openshift.io/v1', | ||
kind: '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 |
---|---|---|
@@ -1,50 +1,64 @@ | ||
import { k8sListResource } from '@openshift/dynamic-plugin-sdk-utils'; | ||
import { mockK8sResourceList } from '~/__mocks__/mockK8sResourceList'; | ||
import { listGroups } from '~/api'; | ||
import { GroupKind } from '~/k8sTypes'; | ||
import { useK8sWatchResource } from '@openshift/dynamic-plugin-sdk-utils'; | ||
import { groupVersionKind, useAccessReview, useGroups } from '~/api'; | ||
import { testHook } from '~/__tests__/unit/testUtils/hooks'; | ||
import { GroupModel } from '~/api/models'; | ||
import { mockGroup } from '~/__mocks__/mockGroup'; | ||
|
||
jest.mock('@openshift/dynamic-plugin-sdk-utils', () => ({ | ||
k8sListResource: jest.fn(), | ||
useK8sWatchResource: jest.fn(), | ||
})); | ||
const k8sListResourceMock = jest.mocked(k8sListResource<GroupKind>); | ||
const groupMock: GroupKind = { | ||
apiVersion: 'v1', | ||
kind: 'Groupkind', | ||
metadata: { | ||
name: 'name', | ||
namespace: 'namespace', | ||
}, | ||
users: [], | ||
}; | ||
describe('listGroups', () => { | ||
it('should list groups when label selector is not present', async () => { | ||
k8sListResourceMock.mockResolvedValue(mockK8sResourceList([groupMock])); | ||
const result = await listGroups(); | ||
expect(k8sListResourceMock).toHaveBeenCalledTimes(1); | ||
expect(k8sListResourceMock).toHaveBeenCalledWith({ | ||
model: GroupModel, | ||
queryOptions: {}, | ||
}); | ||
expect(result).toStrictEqual([groupMock]); | ||
|
||
jest.mock('~/api/useAccessReview', () => ({ | ||
useAccessReview: jest.fn(), | ||
})); | ||
|
||
const useAccessReviewMock = jest.mocked(useAccessReview); | ||
const useK8sWatchResourceMock = useK8sWatchResource as jest.Mock; | ||
|
||
describe('useGroups', () => { | ||
it('should wrap useK8sWatchResource to watch groups', async () => { | ||
const mockReturnValue: ReturnType<typeof useK8sWatchResourceMock> = [[], false, undefined]; | ||
useAccessReviewMock.mockReturnValue([true, true]); | ||
useK8sWatchResourceMock.mockReturnValue(mockReturnValue); | ||
const { result } = testHook(useGroups)(); | ||
|
||
expect(useK8sWatchResourceMock).toHaveBeenCalledTimes(1); | ||
expect(useK8sWatchResourceMock).toHaveBeenCalledWith( | ||
{ | ||
isList: true, | ||
groupVersionKind: groupVersionKind(GroupModel), | ||
}, | ||
GroupModel, | ||
); | ||
expect(result.current).toStrictEqual(mockReturnValue); | ||
}); | ||
it('should list groups when label selector is present', async () => { | ||
k8sListResourceMock.mockResolvedValue(mockK8sResourceList([groupMock])); | ||
const result = await listGroups('labelSelector'); | ||
expect(k8sListResourceMock).toHaveBeenCalledTimes(1); | ||
expect(k8sListResourceMock).toHaveBeenCalledWith({ | ||
model: GroupModel, | ||
queryOptions: { queryParams: { labelSelector: 'labelSelector' } }, | ||
}); | ||
expect(result).toStrictEqual([groupMock]); | ||
|
||
it('should render list of groups', () => { | ||
const mockReturnValue: ReturnType<typeof useK8sWatchResourceMock> = [ | ||
[mockGroup({})], | ||
true, | ||
undefined, | ||
]; | ||
useAccessReviewMock.mockReturnValue([true, true]); | ||
useK8sWatchResourceMock.mockReturnValue(mockReturnValue); | ||
const { result } = testHook(useGroups)(); | ||
expect(useK8sWatchResourceMock).toHaveBeenCalledTimes(1); | ||
expect(useK8sWatchResourceMock).toHaveBeenCalledWith( | ||
{ | ||
isList: true, | ||
groupVersionKind: groupVersionKind(GroupModel), | ||
}, | ||
GroupModel, | ||
); | ||
expect(result.current).toStrictEqual(mockReturnValue); | ||
}); | ||
it('should handle errors and rethrow', async () => { | ||
k8sListResourceMock.mockRejectedValue(new Error('error')); | ||
await expect(listGroups()).rejects.toThrow('error'); | ||
expect(k8sListResourceMock).toHaveBeenCalledTimes(1); | ||
expect(k8sListResourceMock).toHaveBeenCalledWith({ | ||
model: GroupModel, | ||
queryOptions: {}, | ||
}); | ||
|
||
it('should handle 403 error', () => { | ||
useAccessReviewMock.mockReturnValue([false, true]); | ||
useK8sWatchResourceMock.mockReturnValue([undefined, true, undefined]); | ||
const { result } = testHook(useGroups)(); | ||
expect(useK8sWatchResourceMock).toHaveBeenCalledTimes(1); | ||
expect(useK8sWatchResourceMock).toHaveBeenCalledWith(null, GroupModel); | ||
expect(result.current).toStrictEqual([[], true, undefined]); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,13 +1,31 @@ | ||
import { k8sListResource } from '@openshift/dynamic-plugin-sdk-utils'; | ||
import { GroupKind } from '~/k8sTypes'; | ||
import { WatchK8sResult, useK8sWatchResource } from '@openshift/dynamic-plugin-sdk-utils'; | ||
import React from 'react'; | ||
import { AccessReviewResourceAttributes, GroupKind } from '~/k8sTypes'; | ||
import { GroupModel } from '~/api/models'; | ||
import { groupVersionKind } from '~/api/k8sUtils'; | ||
import { useAccessReview } from '~/api/useAccessReview'; | ||
|
||
export const listGroups = (labelSelector?: string): Promise<GroupKind[]> => { | ||
const queryOptions = { | ||
...(labelSelector && { queryParams: { labelSelector } }), | ||
}; | ||
return k8sListResource<GroupKind>({ | ||
model: GroupModel, | ||
queryOptions, | ||
}).then((listResource) => listResource.items); | ||
const accessReviewResource: AccessReviewResourceAttributes = { | ||
group: 'user.openshift.io', | ||
resource: 'groups', | ||
verb: 'list', | ||
}; | ||
|
||
export const useGroups = (): WatchK8sResult<GroupKind[]> => { | ||
const [allowList, accessReviewLoaded] = useAccessReview(accessReviewResource); | ||
const [groupData, loaded, error] = useK8sWatchResource<GroupKind[]>( | ||
allowList && accessReviewLoaded | ||
? { | ||
isList: true, | ||
groupVersionKind: groupVersionKind(GroupModel), | ||
} | ||
: null, | ||
GroupModel, | ||
); | ||
return React.useMemo(() => { | ||
if (!allowList) { | ||
return [[], true, undefined]; | ||
} | ||
return [groupData, loaded, error]; | ||
}, [error, groupData, loaded, allowList]); | ||
}; |
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
129 changes: 0 additions & 129 deletions
129
frontend/src/pages/projects/projectSharing/__tests__/useGroups.spec.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.