Skip to content

Commit

Permalink
Merge pull request #329 from center-for-threat-informed-defense/mitig…
Browse files Browse the repository at this point in the history
…ations-test-297

Mitigations test 297
  • Loading branch information
vsun757 authored Dec 7, 2023
2 parents 3815ca7 + dcdf738 commit 642292a
Show file tree
Hide file tree
Showing 29 changed files with 1,129 additions and 2,086 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
"program": "${workspaceFolder}/bin/www",
"outputCapture": "std",
"envFile": "${workspaceFolder}/.env"
},
{
"type": "node",
"request": "launch",
"name": "Run Regression Tests",
"skipFiles": [
"<node_internals>/**"
],
"runtimeExecutable": "npm",
"args": [
"run",
"test"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"outputCapture": "std",
"envFile": "${workspaceFolder}/.env"
}
]
}
11 changes: 7 additions & 4 deletions app/controllers/assets-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ exports.retrieveById = async function(req, res) {
};

exports.retrieveVersionById = async function(req, res) {
const options = {
retrieveDataComponents: req.query.retrieveDataComponents
}

// TODO Remove after confirming these are not being used by assetsService.retrieveVersionById
// const options = {
// retrieveDataComponents: req.query.retrieveDataComponents
// }

try {
const asset = await assetsService.retrieveVersionById(req.params.stixId, req.params.modified, options);
// const asset = await assetsService.retrieveVersionById(req.params.stixId, req.params.modified, options);
const asset = await assetsService.retrieveVersionById(req.params.stixId, req.params.modified);
if (!asset) {
return res.status(404).send('Asset not found.');
}
Expand Down
199 changes: 100 additions & 99 deletions app/controllers/groups-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const groupsService = require('../services/groups-service');
const logger = require('../lib/logger');
const { DuplicateIdError, BadlyFormattedParameterError, InvalidTypeError, InvalidQueryStringParameterError } = require('../exceptions');

exports.retrieveAll = function(req, res) {
exports.retrieveAll = async function(req, res) {
const options = {
offset: req.query.offset || 0,
limit: req.query.limit || 0,
Expand All @@ -14,100 +15,101 @@ exports.retrieveAll = function(req, res) {
lastUpdatedBy: req.query.lastUpdatedBy,
includePagination: req.query.includePagination
}
try {
const results = await groupsService.retrieveAll(options);

groupsService.retrieveAll(options, function(err, results) {
if (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get groups. Server error.');
}
else {
if (options.includePagination) {
logger.debug(`Success: Retrieved ${ results.data.length } of ${ results.pagination.total } total group(s)`);
}
else {
logger.debug(`Success: Retrieved ${ results.length } group(s)`);
}
return res.status(200).send(results);
} catch (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get groups. Server error.');
}
});

};

exports.retrieveById = function(req, res) {
exports.retrieveById = async function(req, res) {
const options = {
versions: req.query.versions || 'latest'
}
try {
const groups = await groupsService.retrieveById(req.params.stixId, options);

groupsService.retrieveById(req.params.stixId, options, function (err, groups) {
if (err) {
if (err.message === groupsService.errors.badlyFormattedParameter) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
}
else if (err.message === groupsService.errors.invalidQueryStringParameter) {
logger.warn('Invalid query string: versions=' + req.query.versions);
return res.status(400).send('Query string parameter versions is invalid.');
}
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get groups. Server error.');
}

if (groups.length === 0) {
return res.status(404).send('Group not found.');
}
else {
if (groups.length === 0) {
return res.status(404).send('Group not found.');
}
else {
logger.debug(`Success: Retrieved ${ groups.length } group(s) with id ${ req.params.stixId }`);
return res.status(200).send(groups);
}
logger.debug(`Success: Retrieved ${ groups.length } group(s) with id ${ req.params.stixId }`);
return res.status(200).send(groups);
}
});

} catch (err) {
if (err instanceof BadlyFormattedParameterError) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
}
else if (err instanceof InvalidQueryStringParameterError) {
logger.warn('Invalid query string: versions=' + req.query.versions);
return res.status(400).send('Query string parameter versions is invalid.');
}
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get groups. Server error.');
}
}

};

exports.retrieveVersionById = function(req, res) {
groupsService.retrieveVersionById(req.params.stixId, req.params.modified, function (err, group) {
if (err) {
if (err.message === groupsService.errors.badlyFormattedParameter) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
}
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get group. Server error.');
}
} else {
if (!group) {
return res.status(404).send('Group not found.');
}
else {
logger.debug(`Success: Retrieved group with id ${group.id}`);
return res.status(200).send(group);
}
exports.retrieveVersionById = async function(req, res) {

try {
const group = await groupsService.retrieveVersionById(req.params.stixId, req.params.modified);
if (!group) {
return res.status(404).send('Group not found.');
}
else {
logger.debug(`Success: Retrieved group with id ${group.id}`);
return res.status(200).send(group);
}
} catch (err) {
if (err instanceof BadlyFormattedParameterError) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
}
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get group. Server error.');
}
});
}

};

exports.create = async function(req, res) {
// Get the data from the request
const groupData = req.body;
const options = {
import: false,
userAccountId: req.user?.userAccountId
};

// Create the group
try {
const options = {
import: false,
userAccountId: req.user?.userAccountId
};

const group = await groupsService.create(groupData, options);
logger.debug('Success: Created group with id ' + group.stix.id);
return res.status(201).send(group);
}
catch(err) {
if (err.message === groupsService.errors.duplicateId) {
if (err instanceof DuplicateIdError) {
logger.warn('Duplicate stix.id and stix.modified');
return res.status(409).send('Unable to create group. Duplicate stix.id and stix.modified properties.');
}
else if (err.message === groupsService.errors.invalidType) {
else if (err instanceof InvalidTypeError) {
logger.warn('Invalid stix.type');
return res.status(400).send('Unable to create group. stix.type must be intrusion-set');
}
Expand All @@ -118,58 +120,57 @@ exports.create = async function(req, res) {
}
};

exports.updateFull = function(req, res) {
exports.updateFull = async function(req, res) {
// Get the data from the request
const groupData = req.body;

// Create the group
groupsService.updateFull(req.params.stixId, req.params.modified, groupData, function(err, group) {
if (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update group. Server error.");
}
else {
if (!group) {
return res.status(404).send('Group not found.');
} else {
logger.debug("Success: Updated group with id " + group.stix.id);
return res.status(200).send(group);
}
try {
// Create the group
const group = await groupsService.updateFull(req.params.stixId, req.params.modified, groupData);
if (!group) {
return res.status(404).send('Group not found.');
} else {
logger.debug("Success: Updated group with id " + group.stix.id);
return res.status(200).send(group);
}
});
} catch (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update group. Server error.");
}

};

exports.deleteVersionById = function(req, res) {
groupsService.deleteVersionById(req.params.stixId, req.params.modified, function (err, group) {
if (err) {
logger.error('Delete group failed. ' + err);
return res.status(500).send('Unable to delete group. Server error.');
}
else {
if (!group) {
return res.status(404).send('Group not found.');
} else {
logger.debug("Success: Deleted group with id " + group.stix.id);
return res.status(204).end();
}
exports.deleteVersionById = async function(req, res) {

try {
const group = await groupsService.deleteVersionById(req.params.stixId, req.params.modified);
if (!group) {
return res.status(404).send('Group not found.');
} else {
logger.debug("Success: Deleted group with id " + group.stix.id);
return res.status(204).end();
}
});
} catch (err) {
logger.error('Delete group failed. ' + err);
return res.status(500).send('Unable to delete group. Server error.');
}

};

exports.deleteById = function(req, res) {
groupsService.deleteById(req.params.stixId, function (err, groups) {
if (err) {
logger.error('Delete group failed. ' + err);
return res.status(500).send('Unable to delete group. Server error.');
exports.deleteById = async function(req, res) {

try {
const groups = await groupsService.deleteById(req.params.stixId);
if (groups.deletedCount === 0) {
return res.status(404).send('Group not found.');
}
else {
if (groups.deletedCount === 0) {
return res.status(404).send('Group not found.');
}
else {
logger.debug(`Success: Deleted group with id ${ req.params.stixId }`);
return res.status(204).end();
}
logger.debug(`Success: Deleted group with id ${ req.params.stixId }`);
return res.status(204).end();
}
});
} catch (err) {
logger.error('Delete group failed. ' + err);
return res.status(500).send('Unable to delete group. Server error.');
}

};
Loading

0 comments on commit 642292a

Please sign in to comment.