-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIU-3054 use users-keycloak interface for central-tenant permissions (#…
…2632) When the `users-keycloak` interface is present, use its endpoints to retrieve the logged-in user's permissions in the central tenant. Refs UIU-3054 Co-authored-by: Ryan Berger <[email protected]>
- Loading branch information
1 parent
9264df3
commit 51f6114
Showing
5 changed files
with
154 additions
and
20 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
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
104 changes: 104 additions & 0 deletions
104
src/hooks/useConsortiumPermissions/useConsortiumPermissionsUsersKeycloak.test.js
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,104 @@ | ||
import { renderHook, waitFor } from '@folio/jest-config-stripes/testing-library/react'; | ||
import { | ||
QueryClient, | ||
QueryClientProvider, | ||
} from 'react-query'; | ||
|
||
import { | ||
useOkapiKy, | ||
useStripes, | ||
} from '@folio/stripes/core'; | ||
|
||
import consortia from '../../../test/jest/fixtures/consortia'; | ||
import { | ||
MAX_RECORDS, | ||
} from '../../constants'; | ||
import useConsortiumPermissions from './useConsortiumPermissions'; | ||
|
||
jest.mock('@folio/stripes/core', () => ({ | ||
...jest.requireActual('@folio/stripes/core'), | ||
useNamespace: jest.fn(() => ['test']), | ||
useOkapiKy: jest.fn(), | ||
useStripes: jest.fn(), | ||
})); | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
// eslint-disable-next-line react/prop-types | ||
const wrapper = ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
); | ||
|
||
const user = { id: 'userId' }; | ||
const consortium = { | ||
...consortia[0], | ||
centralTenantId: 'mobius', | ||
}; | ||
|
||
const response = { | ||
id: 'userPermissionId', | ||
permissions: [ | ||
{ subPermissions: ['users.view'] }, | ||
{ subPermissions: ['consortia.view'] }, | ||
], | ||
}; | ||
|
||
describe('useConsortiumPermissions with users-keycloak', () => { | ||
const keycloakResponse = { | ||
'user': { | ||
'username': 'circ-admin', | ||
'id': '7cf60c03-1a83-4d40-9aec-961f2e55963f', | ||
}, | ||
'permissions': { | ||
'userId': '7cf60c03-1a83-4d40-9aec-961f2e55963f', | ||
'permissions': [ | ||
'monkey.bagel', | ||
'thunder.chicken', | ||
'consortia.view', | ||
] | ||
} | ||
}; | ||
|
||
const mockGet = jest.fn(() => ({ | ||
json: () => Promise.resolve(keycloakResponse), | ||
})); | ||
const setHeaderMock = jest.fn(); | ||
const kyMock = { | ||
extend: jest.fn(({ hooks: { beforeRequest } }) => { | ||
beforeRequest.forEach(handler => handler({ headers: { set: setHeaderMock } })); | ||
|
||
return { | ||
get: mockGet, | ||
}; | ||
}), | ||
}; | ||
|
||
beforeEach(() => { | ||
mockGet.mockClear(); | ||
useOkapiKy.mockClear().mockReturnValue(kyMock); | ||
useStripes.mockClear().mockReturnValue({ | ||
hasInterface: () => (true), | ||
user: { | ||
user: { ...user, consortium }, | ||
} | ||
}); | ||
}); | ||
|
||
it('should fetch consortium permissions via capabilities interface', async () => { | ||
const { result } = renderHook(() => useConsortiumPermissions(), { wrapper }); | ||
|
||
await waitFor(() => !result.current.isLoading); | ||
|
||
expect(mockGet).toHaveBeenCalledWith('users-keycloak/_self'); | ||
}); | ||
|
||
it('should return consortia permissions', async () => { | ||
const { result } = renderHook(() => useConsortiumPermissions(), { wrapper }); | ||
|
||
await waitFor(() => !result.current.isLoading); | ||
|
||
expect(result.current.permissions).toEqual({ 'consortia.view': true }); | ||
}); | ||
}); |