2022-01-05 18:28:56 +02:00
|
|
|
import _ from 'lodash';
|
2025-02-26 14:13:50 +13:00
|
|
|
import { QueryObserverResult } from '@tanstack/react-query';
|
2022-01-05 18:28:56 +02:00
|
|
|
|
2022-09-02 18:30:34 +03:00
|
|
|
import { Team } from '@/react/portainer/users/teams/types';
|
2022-03-16 08:35:32 +02:00
|
|
|
import { Role, User, UserId } from '@/portainer/users/types';
|
2024-09-25 11:55:07 +12:00
|
|
|
import {
|
|
|
|
ContainerEngine,
|
|
|
|
Environment,
|
|
|
|
} from '@/react/portainer/environments/types';
|
2022-03-16 08:35:32 +02:00
|
|
|
|
|
|
|
export function createMockUsers(
|
|
|
|
count: number,
|
2025-07-14 17:16:33 +12:00
|
|
|
roles: Role | Role[] | ((id: UserId) => Role)
|
2022-03-16 08:35:32 +02:00
|
|
|
): User[] {
|
2022-01-05 18:28:56 +02:00
|
|
|
return _.range(1, count + 1).map((value) => ({
|
|
|
|
Id: value,
|
|
|
|
Username: `user${value}`,
|
2022-03-16 08:35:32 +02:00
|
|
|
Role: getRoles(roles, value),
|
2022-01-05 18:28:56 +02:00
|
|
|
RoleName: '',
|
|
|
|
AuthenticationMethod: '',
|
|
|
|
Checked: false,
|
|
|
|
EndpointAuthorizations: {},
|
|
|
|
PortainerAuthorizations: {},
|
2023-11-22 14:21:07 +13:00
|
|
|
UseCache: false,
|
2023-02-19 09:47:50 +05:30
|
|
|
ThemeSettings: {
|
|
|
|
color: 'auto',
|
|
|
|
},
|
2022-01-05 18:28:56 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-03-16 08:35:32 +02:00
|
|
|
function getRoles(
|
|
|
|
roles: Role | Role[] | ((id: UserId) => Role),
|
|
|
|
id: UserId
|
|
|
|
): Role {
|
|
|
|
if (typeof roles === 'function') {
|
|
|
|
return roles(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof roles === 'number') {
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
2025-07-14 17:16:33 +12:00
|
|
|
// Roles is an array
|
|
|
|
if (roles.length === 0) {
|
|
|
|
throw new Error('No roles provided');
|
|
|
|
}
|
|
|
|
|
|
|
|
// The number of roles is not necessarily the same length as the number of users
|
|
|
|
// so we need to distribute the roles evenly and consistently
|
|
|
|
return roles[(id - 1) % roles.length];
|
2022-03-16 08:35:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function createMockTeams(count: number): Team[] {
|
2022-01-05 18:28:56 +02:00
|
|
|
return _.range(1, count + 1).map((value) => ({
|
|
|
|
Id: value,
|
|
|
|
Name: `team${value}`,
|
|
|
|
}));
|
|
|
|
}
|
2022-02-25 12:22:56 +13:00
|
|
|
|
|
|
|
export function createMockSubscriptions(count: number) {
|
|
|
|
const subscriptions = _.range(1, count + 1).map((x) => ({
|
|
|
|
id: `/subscriptions/subscription-${x}`,
|
|
|
|
subscriptionId: `subscription-${x}`,
|
|
|
|
}));
|
|
|
|
|
|
|
|
return { value: subscriptions };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createMockResourceGroups(subscription: string, count: number) {
|
|
|
|
const resourceGroups = _.range(1, count + 1).map((x) => ({
|
|
|
|
id: `/subscriptions/${subscription}/resourceGroups/resourceGroup-${x}`,
|
|
|
|
name: `resourcegroup-${x}`,
|
|
|
|
}));
|
|
|
|
|
|
|
|
return { value: resourceGroups };
|
|
|
|
}
|
2022-06-22 20:11:46 +03:00
|
|
|
|
|
|
|
export function createMockEnvironment(): Environment {
|
|
|
|
return {
|
|
|
|
TagIds: [],
|
|
|
|
GroupId: 1,
|
|
|
|
Type: 1,
|
2024-09-25 11:55:07 +12:00
|
|
|
ContainerEngine: ContainerEngine.Docker,
|
2022-06-22 20:11:46 +03:00
|
|
|
Name: 'environment',
|
|
|
|
Status: 1,
|
|
|
|
URL: 'url',
|
|
|
|
Snapshots: [],
|
2022-09-21 16:49:42 +12:00
|
|
|
Kubernetes: {
|
2024-12-11 10:15:46 +13:00
|
|
|
Flags: {
|
|
|
|
IsServerMetricsDetected: true,
|
|
|
|
IsServerIngressClassDetected: true,
|
|
|
|
IsServerStorageDetected: true,
|
|
|
|
},
|
2022-09-21 16:49:42 +12:00
|
|
|
Snapshots: [],
|
|
|
|
Configuration: {
|
|
|
|
IngressClasses: [],
|
|
|
|
IngressAvailabilityPerNamespace: false,
|
2022-10-25 09:41:30 +13:00
|
|
|
AllowNoneIngressClass: false,
|
2022-09-21 16:49:42 +12:00
|
|
|
},
|
|
|
|
},
|
2024-12-11 10:15:46 +13:00
|
|
|
UserAccessPolicies: {},
|
|
|
|
TeamAccessPolicies: {},
|
|
|
|
ComposeSyntaxMaxVersion: '0',
|
2022-06-22 20:11:46 +03:00
|
|
|
EdgeKey: '',
|
2023-03-03 14:47:10 +13:00
|
|
|
EnableGPUManagement: false,
|
2022-06-22 20:11:46 +03:00
|
|
|
Id: 3,
|
|
|
|
UserTrusted: false,
|
|
|
|
Edge: {
|
|
|
|
AsyncMode: false,
|
|
|
|
PingInterval: 0,
|
|
|
|
CommandInterval: 0,
|
|
|
|
SnapshotInterval: 0,
|
|
|
|
},
|
2022-06-23 10:25:56 +03:00
|
|
|
SecuritySettings: {
|
|
|
|
allowBindMountsForRegularUsers: false,
|
|
|
|
allowPrivilegedModeForRegularUsers: false,
|
|
|
|
allowContainerCapabilitiesForRegularUsers: false,
|
|
|
|
allowDeviceMappingForRegularUsers: false,
|
|
|
|
allowHostNamespaceForRegularUsers: false,
|
|
|
|
allowStackManagementForRegularUsers: false,
|
|
|
|
allowSysctlSettingForRegularUsers: false,
|
|
|
|
allowVolumeBrowserForRegularUsers: false,
|
|
|
|
enableHostManagementFeatures: false,
|
|
|
|
},
|
2023-02-23 01:43:33 +05:30
|
|
|
DeploymentOptions: {
|
|
|
|
overrideGlobalOptions: false,
|
|
|
|
hideAddWithForm: true,
|
|
|
|
hideWebEditor: false,
|
|
|
|
hideFileUpload: false,
|
|
|
|
},
|
2022-07-18 11:02:14 +12:00
|
|
|
Gpus: [],
|
2022-09-21 16:49:42 +12:00
|
|
|
Agent: { Version: '1.0.0' },
|
2023-02-23 01:43:33 +05:30
|
|
|
EnableImageNotification: false,
|
|
|
|
ChangeWindow: {
|
|
|
|
Enabled: false,
|
|
|
|
EndTime: '',
|
|
|
|
StartTime: '',
|
|
|
|
},
|
2023-06-18 12:18:55 +07:00
|
|
|
StatusMessage: {
|
2023-08-14 12:34:58 +12:00
|
|
|
detail: '',
|
|
|
|
summary: '',
|
2023-06-18 12:18:55 +07:00
|
|
|
},
|
2022-06-22 20:11:46 +03:00
|
|
|
};
|
|
|
|
}
|
2025-02-26 14:13:50 +13:00
|
|
|
|
|
|
|
export function createMockQueryResult<TData, TError = unknown>(
|
|
|
|
data: TData,
|
|
|
|
overrides?: Partial<QueryObserverResult<TData, TError>>
|
|
|
|
) {
|
|
|
|
const defaultResult = {
|
|
|
|
data,
|
|
|
|
dataUpdatedAt: 0,
|
|
|
|
error: null,
|
|
|
|
errorUpdatedAt: 0,
|
|
|
|
failureCount: 0,
|
|
|
|
errorUpdateCount: 0,
|
|
|
|
failureReason: null,
|
|
|
|
isError: false,
|
|
|
|
isFetched: true,
|
|
|
|
isFetchedAfterMount: true,
|
|
|
|
isFetching: false,
|
|
|
|
isInitialLoading: false,
|
|
|
|
isLoading: false,
|
|
|
|
isLoadingError: false,
|
|
|
|
isPaused: false,
|
|
|
|
isPlaceholderData: false,
|
|
|
|
isPreviousData: false,
|
|
|
|
isRefetchError: false,
|
|
|
|
isRefetching: false,
|
|
|
|
isStale: false,
|
|
|
|
isSuccess: true,
|
|
|
|
refetch: async () => defaultResult,
|
|
|
|
remove: () => {},
|
|
|
|
status: 'success',
|
|
|
|
fetchStatus: 'idle',
|
|
|
|
};
|
|
|
|
|
|
|
|
return { ...defaultResult, ...overrides };
|
|
|
|
}
|