Skip to content

Commit

Permalink
ready to merge
Browse files Browse the repository at this point in the history
  • Loading branch information
vsun757 committed Jan 4, 2024
1 parent 4bdd999 commit dafc60b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
1 change: 0 additions & 1 deletion app/controllers/marking-definitions-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ exports.updateFull = async function(req, res) {
return res.status(200).send(markingDefinition);
}
} catch (err) {
console.log(err);
if (err.message === markingDefinitionsService.errors.cannotUpdateStaticObject) {
console.log("error triggered");
logger.warn('Unable to update marking definition, cannot update static object');
Expand Down
39 changes: 17 additions & 22 deletions app/services/marking-definitions-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ const BaseService = require('./_base.service');
const MarkingDefinitionRepository = require('../repository/marking-definition-repository');
const { MissingParameterError, BadlyFormattedParameterError, DuplicateIdError } = require('../exceptions');

const errors = {
missingParameter: 'Missing required parameter',
badlyFormattedParameter: 'Badly formatted parameter',
duplicateId: 'Duplicate id',
notFound: 'Document not found',
invalidQueryStringParameter: 'Invalid query string parameter',
cannotUpdateStaticObject: 'Cannot update static object'
};
exports.errors = errors;

// NOTE: A marking definition does not support the modified or revoked properties!!

class MarkingDefinitionsService extends BaseService {

errors = {
missingParameter: 'Missing required parameter',
badlyFormattedParameter: 'Badly formatted parameter',
duplicateId: 'Duplicate id',
notFound: 'Document not found',
invalidQueryStringParameter: 'Invalid query string parameter',
cannotUpdateStaticObject: 'Cannot update static object'
};

createIsAsync = true;

async create(data, options) {
// This function handles two use cases:
// 1. This is a completely new object. Create a new object and generate the stix.id if not already
Expand Down Expand Up @@ -82,23 +81,23 @@ class MarkingDefinitionsService extends BaseService {
throw err;
}
}
};
}


async updateFull(stixId, data, callback) {

if (data?.workspace?.workflow?.state === 'static') {
if (callback) {
return callback(new Error(errors.cannotUpdateStaticObject));
return callback(new Error(this.errors.cannotUpdateStaticObject));

Check warning on line 91 in app/services/marking-definitions-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/marking-definitions-service.js#L91

Added line #L91 was not covered by tests
}

throw new Error(errors.cannotUpdateStaticObject);
throw new Error(this.errors.cannotUpdateStaticObject);
}

const newDoc = await super.updateFull(stixId, data, callback);

return newDoc;
};
}

async retrieveById(stixId, options, callback) {
try {
Expand Down Expand Up @@ -139,14 +138,10 @@ class MarkingDefinitionsService extends BaseService {
if (!stixId) {
throw new MissingParameterError;

Check warning on line 139 in app/services/marking-definitions-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/marking-definitions-service.js#L139

Added line #L139 was not covered by tests
}

try {
const markingDefinition = await MarkingDefinition.findOneAndRemove({ 'stix.id': stixId });
//Note: markingDefinition is null if not found
return markingDefinition;
} catch (err) {
throw err;
}

const markingDefinition = await this.repository.model.findOneAndRemove({ 'stix.id': stixId });
//Note: markingDefinition is null if not found
return markingDefinition;

}

Expand Down
9 changes: 4 additions & 5 deletions app/tests/api/marking-definitions/marking-definitions.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const request = require('supertest');
const res = require('supertest');
const { expect } = require('expect');

const database = require('../../../lib/database-in-memory');
Expand Down Expand Up @@ -64,7 +63,7 @@ describe('Marking Definitions API', function () {

it('POST /api/marking-definitions does not create an empty marking definition', async function () {
const body = { };
const res = await request(app)
await request(app)
.post('/api/marking-definitions')
.send(body)
.set('Accept', 'application/json')
Expand Down Expand Up @@ -115,7 +114,7 @@ describe('Marking Definitions API', function () {
});

it('GET /api/marking-definitions/:id should not return a marking definition when the id cannot be found', async function () {
const res = await request(app)
await request(app)
.get('/api/marking-definitions/not-an-id')
.set('Accept', 'application/json')
.set('Cookie', `${ login.passportCookieName }=${ passportCookie.value }`)
Expand Down Expand Up @@ -169,7 +168,7 @@ describe('Marking Definitions API', function () {

it('POST /api/marking-definitions does not create a marking definition with the same id', async function () {
const body = markingDefinition1;
const res = await request(app)
await request(app)
.post('/api/marking-definitions')
.send(body)
.set('Accept', 'application/json')
Expand All @@ -178,7 +177,7 @@ describe('Marking Definitions API', function () {
});

it('DELETE /api/marking-definitions deletes a marking definition', async function () {
const res = await request(app)
await request(app)
.delete('/api/marking-definitions/' + markingDefinition1.stix.id)
.set('Cookie', `${ login.passportCookieName }=${ passportCookie.value }`)
.expect(204);
Expand Down

0 comments on commit dafc60b

Please sign in to comment.