Files
threshold-ui/addons/api/mirage/config.js
T

748 lines
22 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2023-01-09 08:48:49 -08:00
import {
discoverEmberDataModels,
applyEmberDataSerializers,
} from 'ember-cli-mirage';
import { createServer, Response } from 'miragejs';
import environmentConfig from '../config/environment';
import { authHandler, deauthHandler } from './route-handlers/auth';
import { targetHandler } from './route-handlers/target';
import { pickRandomStatusString } from './factories/session';
import initializeMockIPC from './scenarios/ipc';
import makeBooleanFilter from './helpers/bexpr-filter';
import { faker } from '@faker-js/faker';
import { asciiCasts } from './data/asciicasts';
2020-05-19 18:10:39 -04:00
2023-01-09 08:48:49 -08:00
const isTesting = environmentConfig.environment === 'test';
2021-10-23 12:55:23 -04:00
2023-01-09 08:48:49 -08:00
// Main function
// More info about server configuration https://www.ember-cli-mirage.com/docs/advanced/server-configuration
export default function (mirageConfig) {
let finalConfig = {
...mirageConfig,
models: { ...discoverEmberDataModels(), ...mirageConfig.models },
serializers: applyEmberDataSerializers(mirageConfig.serializers),
routes,
};
return createServer(finalConfig);
}
// Only routes are defined here
function routes() {
initializeMockIPC(this, environmentConfig);
this.passthrough();
2020-05-19 18:10:39 -04:00
// make this `http://localhost:8080`, for example, if your API is on a different server
// this.urlPrefix = '';
2023-04-03 20:28:31 -07:00
this.get('/metadata.json', () => {
// this configuration is optional
const licensedFeatures = environmentConfig?.features?.licensedFeatures;
const edition = licensedFeatures ? 'enterprise' : 'oss';
const features = licensedFeatures ? Object.keys(licensedFeatures) : [];
const metadata = {
license: {
edition,
features,
},
};
return metadata;
});
2020-05-19 18:10:39 -04:00
// make this `/api`, for example, if your API is namespaced
2023-01-09 08:48:49 -08:00
this.namespace = environmentConfig.api.namespace;
2020-05-19 18:10:39 -04:00
// delay for each request, automatically set to 0 during testing
this.timing = 1;
2020-05-19 18:10:39 -04:00
// Scope resources
2021-03-04 15:47:38 -05:00
this.get(
'/scopes',
2021-12-13 13:34:28 -05:00
({ scopes }, { queryParams: { scope_id, recursive, filter } }) => {
let resultSet;
2021-03-04 15:47:38 -05:00
// Default parent scope is global
if (!scope_id) scope_id = 'global';
if (recursive && scope_id === 'global') {
2021-12-13 13:34:28 -05:00
resultSet = scopes.all();
2021-03-04 15:47:38 -05:00
} else {
2021-12-13 13:34:28 -05:00
resultSet = scopes.where((scope) => {
2021-03-04 15:47:38 -05:00
return scope.scope ? scope.scope.id === scope_id : false;
});
}
2021-12-13 13:34:28 -05:00
return resultSet.filter(makeBooleanFilter(filter));
}
2021-03-04 15:47:38 -05:00
);
this.post('/scopes', function ({ scopes }) {
2020-07-23 11:31:46 -04:00
// Parent scope comes through the payload via `scope_id`, but this needs
// to be expanded to a scope object `scope: {id:..., type:...}` before
// saving, which is the gist of this function.
const attrs = this.normalizedRequestAttrs();
const parentScopeAttrs = this.serialize(scopes.find(attrs.scopeId));
2020-07-23 11:31:46 -04:00
attrs.scope = parentScopeAttrs;
return scopes.create(attrs);
});
2020-07-24 19:12:20 -04:00
this.get('/scopes/:id');
this.patch('/scopes/:id');
this.del('/scopes/:id');
2020-06-04 16:53:37 -04:00
2020-06-18 15:51:40 -04:00
// Auth & IAM resources
2021-03-04 15:47:38 -05:00
this.get(
'/auth-methods',
({ authMethods }, { queryParams: { scope_id, recursive, filter } }) => {
let resultSet;
if (recursive && scope_id === 'global') {
resultSet = authMethods.all();
} else if (recursive) {
resultSet = authMethods.where((authMethod) => {
const authMethodModel = authMethods.find(authMethod.id);
return (
authMethod.scopeId === scope_id ||
authMethodModel?.scope?.scope?.id === scope_id
);
});
} else {
resultSet = authMethods.where(
(authMethod) => authMethod.scopeId === scope_id
);
}
return resultSet.filter(makeBooleanFilter(filter));
2021-03-04 15:47:38 -05:00
}
);
2022-11-22 12:06:49 -08:00
this.post('/auth-methods', function ({ authMethods }) {
const attrs = this.normalizedRequestAttrs();
if (attrs.type === 'oidc') {
attrs.attributes.state = 'active-public';
}
return authMethods.create(attrs);
});
2020-09-09 12:24:01 -04:00
this.get('/auth-methods/:id');
this.patch(
'/auth-methods/:id',
function ({ authMethods }, { params: { id } }) {
const attrs = this.normalizedRequestAttrs();
if (attrs.type === 'oidc') {
attrs.attributes.state = 'active-public';
}
return authMethods.find(id).update(attrs);
}
);
this.del('/auth-methods/:id', ({ authMethods }, { params: { id } }) => {
const authMethod = authMethods.find(id);
const scope = authMethod.scope;
authMethod.destroy();
// If the primary auth method is deleted, the associated scope's
// primary_auth_method_id field should be set to null.
2021-03-26 12:47:49 -04:00
if (scope.primaryAuthMethodId === id) {
scope.update({ primaryAuthMethodId: null });
}
2021-04-12 13:47:58 -04:00
return new Response(202);
});
2020-06-18 15:51:40 -04:00
// Auth Method Accounts
2021-03-04 15:47:38 -05:00
this.get(
'/accounts',
2021-11-12 11:22:16 -05:00
(
{ accounts },
{ queryParams: { auth_method_id: authMethodId, filter } }
) => {
let resultSet;
if (authMethodId) {
resultSet = accounts.where({ authMethodId });
} else {
resultSet = accounts.all();
}
return resultSet.filter(makeBooleanFilter(filter));
2021-03-04 15:47:38 -05:00
}
);
this.post('/accounts');
this.get('/accounts/:id');
this.patch('/accounts/:id');
this.del('/accounts/:id');
2021-03-04 15:47:38 -05:00
this.post(
'/accounts/:idMethod',
function ({ accounts }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const account = accounts.find(id);
const updatedAttrs = {
version: attrs.version,
attributes: account.attributes,
};
// Set new password
if (method === 'set-password') {
updatedAttrs.attributes.password = attrs.password;
}
// Set new password
if (method === 'change-password') {
updatedAttrs.attributes.password = attrs.new_password;
}
return account.update(updatedAttrs);
2020-09-17 10:24:22 -04:00
}
2021-03-04 15:47:38 -05:00
);
// Authenticate + auth methods custom routes
this.post('/auth-methods/:id_method', authHandler);
// Deauthenticate route
this.delete('/auth-tokens/:id', deauthHandler);
2020-06-23 15:06:30 -04:00
// IAM : Users
this.get(
'/users',
({ users }, { queryParams: { scope_id, recursive, filter } }) => {
let resultSet;
if (recursive && scope_id === 'global') {
resultSet = users.all();
} else if (recursive) {
resultSet = users.where((user) => {
const userModel = users.find(user.id);
return (
user.scopeId === scope_id ||
userModel?.scope?.scope?.id === scope_id
);
});
} else {
resultSet = users.where((user) => user.scopeId === scope_id);
}
return resultSet.filter(makeBooleanFilter(filter));
}
);
2020-09-09 16:53:41 -04:00
this.post('/users');
this.get('/users/:id');
this.patch('/users/:id');
this.del('/users/:id');
2020-09-30 10:45:39 -04:00
this.post('/users/:idMethod', function ({ users }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const user = users.find(id);
const updatedAttrs = {
version: attrs.version,
2021-03-04 15:47:38 -05:00
accountIds: user.accountIds,
2020-09-30 10:45:39 -04:00
};
// If adding accounts, push them into the array
if (method === 'add-accounts') {
2021-03-04 15:47:38 -05:00
attrs.accountIds.forEach((id) => {
2020-09-30 10:45:39 -04:00
if (!updatedAttrs.accountIds.includes(id)) {
updatedAttrs.accountIds.push(id);
}
});
}
// If deleting accounts, filter them out of the array
if (method === 'remove-accounts') {
2021-03-04 15:47:38 -05:00
updatedAttrs.accountIds = updatedAttrs.accountIds.filter((id) => {
2020-09-30 10:45:39 -04:00
return !attrs.accountIds.includes(id);
});
}
return user.update(updatedAttrs);
});
2020-09-09 16:53:41 -04:00
// IAM: Groups
this.get(
'/groups',
({ groups }, { queryParams: { scope_id, recursive, filter } }) => {
let resultSet;
if (recursive && scope_id === 'global') {
resultSet = groups.all();
} else if (recursive) {
resultSet = groups.where((group) => {
const groupModel = groups.find(group.id);
return (
group.scopeId === scope_id ||
groupModel?.scope?.scope?.id === scope_id
);
});
} else {
resultSet = groups.where((group) => group.scopeId === scope_id);
}
return resultSet.filter(makeBooleanFilter(filter));
}
);
2020-09-09 16:53:41 -04:00
this.post('/groups');
this.get('/groups/:id');
this.patch('/groups/:id');
this.del('/groups/:id');
2021-03-04 15:47:38 -05:00
this.post(
'/groups/:idMethod',
function ({ groups }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const group = groups.find(id);
const updatedAttrs = {
version: attrs.version,
memberIds: group.memberIds,
};
// If adding members, push them into the array
if (method === 'add-members') {
attrs.memberIds.forEach((id) => {
if (!updatedAttrs.memberIds.includes(id)) {
updatedAttrs.memberIds.push(id);
}
});
}
// If deleting members, filter them out of the array
if (method === 'remove-members') {
updatedAttrs.memberIds = updatedAttrs.memberIds.filter((id) => {
return !attrs.memberIds.includes(id);
});
}
return group.update(updatedAttrs);
2020-09-09 16:53:41 -04:00
}
2021-03-04 15:47:38 -05:00
);
// IAM: Roles
2020-09-10 11:03:44 -04:00
this.get('/roles', ({ roles }, { queryParams: { scope_id: scopeId } }) => {
return roles.where({ scopeId });
});
this.post('/roles');
this.get('/roles/:id');
this.patch('/roles/:id');
this.del('/roles/:id');
2021-03-04 15:47:38 -05:00
this.post(
'/roles/:idMethod',
function (
{ roles, users, groups, managedGroups },
{ params: { idMethod } }
) {
2021-03-04 15:47:38 -05:00
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const role = roles.find(id);
let updatedAttrs = {};
// Principals is a combined list of users, groups and managed groups, but in Mirage we've
2021-03-04 15:47:38 -05:00
// internally modelled them as separate lists. Therefore we must check
// the type by looking up the user or group first, then determine which
// list to add the principal to.
if (method === 'add-principals') {
updatedAttrs = {
version: attrs.version,
userIds: role.userIds,
groupIds: role.groupIds,
managedGroupIds: role.managedGroupIds,
2021-03-04 15:47:38 -05:00
};
attrs.principalIds.forEach((id) => {
const isUser = users.find(id);
const isGroup = groups.find(id);
const isManagedGroup = managedGroups.find(id);
2021-03-04 15:47:38 -05:00
if (isUser && !updatedAttrs.userIds.includes(id)) {
updatedAttrs.userIds.push(id);
}
if (isGroup && !updatedAttrs.groupIds.includes(id)) {
updatedAttrs.groupIds.push(id);
}
if (isManagedGroup && !updatedAttrs.managedGroupIds.includes(id)) {
updatedAttrs.managedGroupIds.push(id);
}
2021-03-04 15:47:38 -05:00
});
}
// If deleting principals, filter the ids out of users, groups and managed group lists,
2021-03-04 15:47:38 -05:00
// and in this case don't care about the type
if (method === 'remove-principals') {
updatedAttrs = {
version: attrs.version,
userIds: role.userIds,
groupIds: role.groupIds,
managedGroupIds: role.managedGroupIds,
2021-03-04 15:47:38 -05:00
};
updatedAttrs.userIds = updatedAttrs.userIds.filter((id) => {
return !attrs.principalIds.includes(id);
});
updatedAttrs.groupIds = updatedAttrs.groupIds.filter((id) => {
return !attrs.principalIds.includes(id);
});
updatedAttrs.managedGroupIds = updatedAttrs.managedGroupIds.filter(
(id) => {
return !attrs.principalIds.includes(id);
}
);
2021-03-04 15:47:38 -05:00
}
2021-03-04 15:47:38 -05:00
if (method === 'set-grants') {
updatedAttrs = {
version: attrs.version,
grantStrings: attrs.grantStrings,
};
}
2020-09-12 12:03:52 -04:00
2021-03-04 15:47:38 -05:00
return role.update(updatedAttrs);
2020-09-12 12:03:52 -04:00
}
2021-03-04 15:47:38 -05:00
);
2020-06-22 21:39:38 -04:00
// Other resources
2020-06-04 16:53:37 -04:00
// host-catalog
2021-03-04 15:47:38 -05:00
this.get(
'/host-catalogs',
({ hostCatalogs }, { queryParams: { scope_id: scopeId } }) => {
return hostCatalogs.where({ scopeId });
}
);
this.post(
'/host-catalogs',
function ({ hostCatalogs }, { queryParams: { plugin_name } }) {
const attrs = this.normalizedRequestAttrs();
if (plugin_name) {
attrs.type = 'plugin';
attrs.plugin = {
name: plugin_name,
};
}
return hostCatalogs.create(attrs);
}
);
this.get('/host-catalogs/:id');
this.patch('/host-catalogs/:id');
this.del('/host-catalogs/:id');
2020-06-17 13:20:57 -04:00
2020-08-14 15:16:35 -04:00
// host
2021-03-04 15:47:38 -05:00
this.get(
'/hosts',
function ({ hosts }, { queryParams: { host_catalog_id: hostCatalogId } }) {
return hosts.where({ hostCatalogId });
}
);
2020-09-10 17:59:31 -04:00
this.post('/hosts', function ({ hostCatalogs, hosts }) {
const attrs = this.normalizedRequestAttrs();
2020-09-10 17:59:31 -04:00
const hostCatalog = hostCatalogs.find(attrs.hostCatalogId);
attrs.scopeId = hostCatalog.scope.id;
return hosts.create(attrs);
});
2020-09-10 17:59:31 -04:00
this.get('/hosts/:id');
this.patch('/hosts/:id');
this.del('/hosts/:id');
2020-08-14 15:16:35 -04:00
2020-08-14 10:46:17 -04:00
// host-set
2021-03-04 15:47:38 -05:00
this.get(
'/host-sets',
function (
{ hostSets },
{ queryParams: { host_catalog_id: hostCatalogId } }
) {
return hostSets.where({ hostCatalogId });
}
);
2020-09-10 17:59:31 -04:00
this.post('/host-sets', function ({ hostCatalogs, hostSets }) {
const attrs = this.normalizedRequestAttrs();
const hostCatalog = hostCatalogs.find(attrs.hostCatalogId);
attrs.scopeId = hostCatalog.scope.id;
return hostSets.create(attrs);
});
2020-09-10 16:57:11 -04:00
this.get('/host-sets/:id');
this.patch('/host-sets/:id');
this.del('/host-sets/:id');
2021-03-04 15:47:38 -05:00
this.post(
'/host-sets/:idMethod',
function ({ hostSets }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const hostSet = hostSets.find(id);
const updatedAttrs = {
version: attrs.version,
hostIds: hostSet.hostIds,
};
2020-09-10 12:59:20 -04:00
2021-03-04 15:47:38 -05:00
// If adding hosts, push them into the array
if (method === 'add-hosts') {
attrs.hostIds.forEach((id) => {
if (!updatedAttrs.hostIds.includes(id)) {
updatedAttrs.hostIds.push(id);
}
});
}
2020-09-10 12:59:20 -04:00
2021-03-04 15:47:38 -05:00
// If deleting hosts, filter them out of the array
if (method === 'remove-hosts') {
updatedAttrs.hostIds = updatedAttrs.hostIds.filter((id) => {
return !attrs.hostIds.includes(id);
});
}
2020-09-10 12:59:20 -04:00
2021-03-04 15:47:38 -05:00
return hostSet.update(updatedAttrs);
}
);
2020-08-14 10:46:17 -04:00
2020-08-21 11:53:53 -04:00
// target
2021-03-04 15:47:38 -05:00
this.get(
'/targets',
function ({ targets }, { queryParams: { scope_id, recursive, filter } }) {
let resultSet;
2021-03-04 15:47:38 -05:00
if (recursive && scope_id === 'global') {
resultSet = targets.all();
2021-03-04 15:47:38 -05:00
} else if (recursive) {
resultSet = targets.where((target) => {
2021-03-04 15:47:38 -05:00
const targetModel = targets.find(target.id);
return (
target.scopeId === scope_id ||
targetModel?.scope?.scope?.id === scope_id
);
});
} else {
2021-12-16 16:56:35 -05:00
resultSet = targets.where((target) => target.scopeId === scope_id);
2021-03-04 15:47:38 -05:00
}
return resultSet.filter(makeBooleanFilter(filter));
}
2021-03-04 15:47:38 -05:00
);
2020-09-04 08:41:28 -04:00
this.post('/targets', function ({ targets }) {
2020-09-03 14:35:05 -04:00
const attrs = this.normalizedRequestAttrs();
return targets.create(attrs);
});
this.get('/targets/:id');
this.patch('/targets/:id');
this.del('/targets/:id');
this.post('/targets/:idMethod', targetHandler);
2020-09-18 12:54:31 -04:00
// session
2021-03-04 15:47:38 -05:00
this.get(
'/sessions',
function ({ sessions }, { queryParams: { scope_id, recursive, filter } }) {
let resultSet;
2021-03-04 15:47:38 -05:00
// To simulate changes to `session.status` that may occur in the backend,
// we quietly randomize the value of the field on GET.
// To populate sessions for logged in user,
// update alternate sessions to auth user.
// But only if not in testing mode.
// In tests, we need deterministic statuses.
2021-10-23 12:55:23 -04:00
if (!isTesting) {
2021-03-04 15:47:38 -05:00
sessions.all().models.forEach((session) => {
session.update({
2021-03-04 15:47:38 -05:00
status: pickRandomStatusString(),
});
2021-03-04 15:47:38 -05:00
if (session.id.split('-').pop() % 2)
session.update({
userId: 'authenticateduser',
});
});
2021-03-04 15:47:38 -05:00
}
if (recursive && scope_id === 'global') {
resultSet = sessions.all();
2021-03-04 15:47:38 -05:00
} else if (recursive) {
resultSet = sessions.where((session) => {
2021-03-04 15:47:38 -05:00
const sessionModel = sessions.find(session.id);
return (
session.scopeId === scope_id ||
sessionModel?.scope?.scope?.id === scope_id
);
});
} else {
resultSet = sessions.where((session) => session.scopeId === scope_id);
2021-03-04 15:47:38 -05:00
}
return resultSet.filter(makeBooleanFilter(filter));
2020-10-06 11:07:29 -04:00
}
2021-03-04 15:47:38 -05:00
);
this.get('/sessions/:id', function ({ sessions }, { params: { id } }) {
const session = sessions.find(id);
2021-10-23 12:55:23 -04:00
if (!isTesting) {
session.update({
status: pickRandomStatusString(),
});
}
return session;
2020-09-18 12:54:31 -04:00
});
2021-03-04 15:47:38 -05:00
this.post(
'/sessions/:idMethod',
function ({ sessions }, { params: { idMethod } }) {
const attrs = this.normalizedRequestAttrs();
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
const session = sessions.find(id);
const updatedAttrs = {
version: attrs.version,
};
if (method === 'cancel') {
updatedAttrs.status = 'canceling';
}
return session.update(updatedAttrs);
2020-09-18 15:38:32 -04:00
}
2021-03-04 15:47:38 -05:00
);
2020-09-18 15:38:32 -04:00
2020-09-22 11:03:30 -04:00
// auth-tokens
this.get('/auth-tokens/:id', function () {
// Set to 401 or 404 to simulate token invalidation, which will
// cause the session to fail restoration and logout the user.
return new Response(200);
});
// credential-stores
this.get(
'/credential-stores',
({ credentialStores }, { queryParams: { scope_id: scopeId } }) =>
credentialStores.where({ scopeId })
);
2021-07-14 07:59:15 -07:00
this.get('/credential-stores/:id');
2021-07-16 10:41:04 -07:00
this.post('/credential-stores');
2021-07-14 07:59:15 -07:00
this.patch('/credential-stores/:id');
2021-07-20 14:55:00 -07:00
this.del('/credential-stores/:id');
2021-07-14 07:59:15 -07:00
2021-07-12 16:03:06 -04:00
// credential-libraries
this.get(
'/credential-libraries',
(
{ credentialLibraries },
{ queryParams: { credential_store_id: credentialStoreId } }
) => credentialLibraries.where({ credentialStoreId })
);
this.get('/credential-libraries/:id');
2021-07-18 14:31:45 -04:00
this.post(
'/credential-libraries',
function ({ credentialStores, credentialLibraries }) {
const attrs = this.normalizedRequestAttrs();
const credentialStore = credentialStores.find(attrs.credentialStoreId);
attrs.scopeId = credentialStore.scope.id;
return credentialLibraries.create(attrs);
}
);
2021-07-12 16:03:06 -04:00
this.del('/credential-libraries/:id');
this.patch('/credential-libraries/:id');
2022-07-20 12:34:18 -07:00
// credentials
this.get(
'/credentials',
(
{ credentials },
{ queryParams: { credential_store_id: credentialStoreId } }
) => credentials.where({ credentialStoreId })
);
2022-07-20 12:34:18 -07:00
this.get('/credentials/:id');
this.post('/credentials', function ({ credentialStores, credentials }) {
const attrs = this.normalizedRequestAttrs();
const credentialStore = credentialStores.find(attrs.credentialStoreId);
attrs.scopeId = credentialStore.scope.id;
return credentials.create(attrs);
});
2022-07-20 12:34:18 -07:00
this.del('/credentials/:id');
this.patch('/credentials/:id');
2021-10-29 13:23:41 -07:00
// managed-groups
this.get(
'/managed-groups',
(
{ managedGroups },
{ queryParams: { auth_method_id: authMethodId, filter } }
) => {
let resultSet;
if (authMethodId) {
resultSet = managedGroups.where({ authMethodId });
} else {
resultSet = managedGroups.all();
}
return resultSet.filter(makeBooleanFilter(filter));
2021-10-29 13:23:41 -07:00
}
);
2022-01-14 16:56:59 -05:00
this.post('/managed-groups');
2021-11-09 10:29:49 -08:00
this.get('/managed-groups/:id');
2022-01-14 16:56:59 -05:00
this.patch('/managed-groups/:id');
2021-11-09 12:01:30 -08:00
this.del('/managed-groups/:id');
2021-10-29 13:23:41 -07:00
2023-03-03 13:27:14 -05:00
// workers
2022-07-28 14:57:53 -04:00
this.get(
'/workers',
({ workers }, { queryParams: { scope_id: scopeId } }) => {
return workers.where({ scopeId });
}
);
2022-08-09 16:12:56 -04:00
this.get('/workers/:id');
this.del('/workers/:id');
this.patch('/workers/:id');
2022-08-26 11:50:53 -04:00
this.post('/workers:create:worker-led', ({ workers, scopes }) => {
const globalScope = scopes.find('global');
// This POST only takes in a token so we need to generate a random worker to return
const newWorker = this.create('worker', {
type: 'pki',
scope: globalScope,
});
return workers.create(newWorker.attrs);
});
2022-07-28 14:57:53 -04:00
2023-03-03 13:27:14 -05:00
// storage-buckets
2023-03-23 16:18:45 -04:00
this.get(
'/storage-buckets',
({ storageBuckets }, { queryParams: { scope_id: scopeId, recursive } }) => {
if (recursive && scopeId === 'global') {
return storageBuckets.all();
}
return storageBuckets.where({ scopeId });
}
);
2023-03-03 13:27:14 -05:00
this.get('/storage-buckets/:id');
this.del('/storage-buckets/:id');
this.patch('/storage-buckets/:id');
this.post(
'/storage-buckets',
function ({ storageBuckets }, { queryParams: { plugin_name } }) {
2023-03-03 13:27:14 -05:00
const attrs = this.normalizedRequestAttrs();
if (plugin_name) {
attrs.type = 'plugin';
attrs.plugin = {
name: plugin_name,
};
}
// Remove the secrets and add the hmac in the response
delete attrs.secrets;
attrs.secrets_hmac = faker.git.commitSha();
2023-03-03 13:27:14 -05:00
return storageBuckets.create(attrs);
}
);
// session recordings
this.get(
'/session-recordings',
(
{ sessionRecordings },
{ queryParams: { scope_id: scopeId, recursive } }
) => {
if (recursive && scopeId === 'global') {
return sessionRecordings.all();
}
return sessionRecordings.where({ scopeId });
}
);
this.get(
'/session-recordings/:idMethod',
async ({ sessionRecordings }, { params: { idMethod } }) => {
const id = idMethod.split(':')[0];
const method = idMethod.split(':')[1];
if (method === 'download') {
return faker.helpers.arrayElement(asciiCasts);
} else {
return sessionRecordings.find(id);
}
}
);
2020-09-14 10:31:43 -04:00
/* Uncomment the following line and the Response import above
* Then change the response code to simulate error responses.
* this.get('/scopes', () => new Response(505));
*
* Update error payload to simulate specific error responses.
*/
// this.get('/host-catalogs', () => new Response(404));
// this.get('/scopes/:id', () => new Response(505, {}, {
// errors: [{
// status: 505,
// message: 'HTTP version not supported.',
// }]
// }));
2020-05-19 18:10:39 -04:00
}