test/mocks/cockpit: add permission function
Add the types relevant types for the `cockpit.permission` function so that we can establish whether the user has the correct privileges to run the frontend on-prem.
This commit is contained in:
parent
96eb0fe767
commit
49b2196c7b
3 changed files with 43 additions and 0 deletions
26
src/Utilities/useIsCockpitAdmin.tsx
Normal file
26
src/Utilities/useIsCockpitAdmin.tsx
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { useEffect, useState } from 'react';
|
||||
|
||||
import cockpit from 'cockpit';
|
||||
|
||||
export const useIsCockpitAdmin = () => {
|
||||
const [isAdmin, setIsAdmin] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const permission = cockpit.permission({ admin: true });
|
||||
|
||||
const onChangeListener = () => {
|
||||
setIsAdmin(permission.allowed);
|
||||
};
|
||||
|
||||
permission.addEventListener('changed', onChangeListener);
|
||||
|
||||
// Check the initial state
|
||||
onChangeListener();
|
||||
|
||||
return () => {
|
||||
permission.removeEventListener('changed', onChangeListener);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return isAdmin;
|
||||
};
|
||||
15
src/test/mocks/cockpit/cockpitPermission.ts
Normal file
15
src/test/mocks/cockpit/cockpitPermission.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
|
||||
type PermissionObject = {
|
||||
allowed: boolean;
|
||||
addEventListener(eventType: string, callback: () => void): void;
|
||||
removeEventListener(eventType: string, callback: () => void): void;
|
||||
};
|
||||
|
||||
export const cockpitPermission = ({ admin = false }): PermissionObject => {
|
||||
return {
|
||||
allowed: admin,
|
||||
addEventListener: (eventType: string, callback: () => void) => {},
|
||||
removeEventListener: (eventType: string, callback: () => void) => {},
|
||||
};
|
||||
};
|
||||
|
|
@ -4,6 +4,7 @@ import path from 'path';
|
|||
|
||||
import { cockpitFile } from './cockpitFile';
|
||||
import { cockpitHTTP } from './cockpitHTTP';
|
||||
import { cockpitPermission } from './cockpitPermission';
|
||||
|
||||
type userinfo = {
|
||||
home: string;
|
||||
|
|
@ -31,4 +32,5 @@ export default {
|
|||
});
|
||||
},
|
||||
http: cockpitHTTP,
|
||||
permission: cockpitPermission,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue