Skip to content

Commit

Permalink
all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
vsun757 committed Jan 9, 2024
1 parent 703cca1 commit 4b428ce
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
1 change: 0 additions & 1 deletion app/controllers/data-sources-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const dataSourcesService = require('../services/data-sources-service');
const logger = require('../lib/logger');
const { DuplicateIdError, BadlyFormattedParameterError, InvalidQueryStringParameterError } = require('../exceptions');
const { InvalidPointerError } = require('@apidevtools/json-schema-ref-parser');

exports.retrieveAll = async function(req, res) {
const options = {
Expand Down
64 changes: 32 additions & 32 deletions app/services/data-sources-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const dataSourceRepository = require('../repository/data-source-repository');
const identitiesService = require('./identities-service');
const dataComponentsService = require('./data-components-service');
const {lastUpdatedByQueryHelper} = require('../lib/request-parameter-helper');
const BaseService = require('./_base.service');
const { MissingParameterError, BadlyFormattedParameterError, InvalidQueryStringParameterError } = require('../exceptions');

Expand All @@ -18,6 +17,33 @@ class DataSourcesService extends BaseService {
invalidQueryStringParameter: 'Invalid query string parameter'
}


async addExtraData(dataSource, retrieveDataComponents) {
await identitiesService.addCreatedByAndModifiedByIdentities(dataSource);
if (retrieveDataComponents) {
await this.addDataComponents(dataSource);
}
}

async addExtraDataToAll(dataSources, retrieveDataComponents) {
for (const dataSource of dataSources) {
// eslint-disable-next-line no-await-in-loop
await this.addExtraData(dataSource, retrieveDataComponents);
}
}

async addDataComponents(dataSource) {

Check failure on line 35 in app/services/data-sources-service.js

View workflow job for this annotation

GitHub Actions / static-checks

Expected 'this' to be used by class async method 'addDataComponents'
// We have to work with the latest version of all data components to avoid mishandling a situation
// where an earlier version of a data component may reference a data source, but the latest
// version doesn't.

// Retrieve the latest version of all data components
const allDataComponents = await dataComponentsService.retrieveAllAsync({ includeDeprecated: true, includeRevoked: true });

// Add the data components that reference the data source
dataSource.dataComponents = allDataComponents.filter(dataComponent => dataComponent.stix.x_mitre_data_source_ref === dataSource.stix.id);
}

async retrieveById(stixId, options, callback) {
try {
// versions=all Retrieve all versions of the data source with the stixId
Expand All @@ -32,7 +58,7 @@ class DataSourcesService extends BaseService {

if (options.versions === 'all') {
const dataSources = await this.repository.model.find({ 'stix.id': stixId }).lean().exec();
await addExtraDataToAll(dataSources, options.retrieveDataComponents);
await this.addExtraDataToAll(dataSources, options.retrieveDataComponents);
if (callback) {
return callback(null, dataSources);
}
Expand All @@ -42,7 +68,7 @@ class DataSourcesService extends BaseService {

// Note: document is null if not found
if (dataSource) {
await addExtraData(dataSource, options.retrieveDataComponents);
await this.addExtraData(dataSource, options.retrieveDataComponents);
if (callback) {
return callback(null, [dataSource]);
}
Expand Down Expand Up @@ -72,7 +98,7 @@ class DataSourcesService extends BaseService {
throw err;
}
}
};
}


async retrieveVersionById(stixId, modified, options, callback) {
Expand All @@ -97,7 +123,7 @@ class DataSourcesService extends BaseService {

// Note: document is null if not found
if (dataSource) {
await addExtraData(dataSource, options.retrieveDataComponents);
await this.addExtraData(dataSource, options.retrieveDataComponents);
if (callback) {
return callback(null, dataSource);
}
Expand All @@ -121,34 +147,8 @@ class DataSourcesService extends BaseService {
throw err;
}
}
};


async addExtraData(dataSource, retrieveDataComponents) {
await identitiesService.addCreatedByAndModifiedByIdentities(dataSource);
if (retrieveDataComponents) {
await this.addDataComponents(dataSource);
}
}

async addExtraDataToAll(dataSources, retrieveDataComponents) {
for (const dataSource of dataSources) {
// eslint-disable-next-line no-await-in-loop
await this.addExtraData(dataSource, retrieveDataComponents);
}
}

async addDataComponents(dataSource) {
// We have to work with the latest version of all data components to avoid mishandling a situation
// where an earlier version of a data component may reference a data source, but the latest
// version doesn't.

// Retrieve the latest version of all data components
const allDataComponents = await dataComponentsService.retrieveAllAsync({ includeDeprecated: true, includeRevoked: true });

// Add the data components that reference the data source
dataSource.dataComponents = allDataComponents.filter(dataComponent => dataComponent.stix.x_mitre_data_source_ref === dataSource.stix.id);
}


}

Expand Down
12 changes: 6 additions & 6 deletions app/tests/api/data-sources/data-sources.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('Data Sources API', function () {

it('POST /api/data-sources does not create an empty data source', async function () {
const body = { };
const res = await request(app)
await request(app)
.post('/api/data-sources')
.send(body)
.set('Accept', 'application/json')
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('Data Sources API', function () {
});

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

it('POST /api/data-sources does not create a data source with the same id and modified date', async function () {
const body = dataSource1;
const res = await request(app)
await request(app)
.post('/api/data-sources')
.send(body)
.set('Accept', 'application/json')
Expand Down Expand Up @@ -408,23 +408,23 @@ describe('Data Sources API', function () {
});

it('DELETE /api/data-sources/:id should not delete a data source when the id cannot be found', async function () {
const res = await request(app)
await request(app)
.delete('/api/data-sources/not-an-id')
.set('Cookie', `${ login.passportCookieName }=${ passportCookie.value }`)
.expect(404);

});

it('DELETE /api/data-sources/:id/modified/:modified deletes a data source', async function () {
const res = await request(app)
await request(app)
.delete('/api/data-sources/' + dataSource1.stix.id + '/modified/' + dataSource1.stix.modified)
.set('Cookie', `${ login.passportCookieName }=${ passportCookie.value }`)
.expect(204);

});

it('DELETE /api/data-sources/:id should delete all the data sources with the same stix id', async function () {
const res = await request(app)
await request(app)
.delete('/api/data-sources/' + dataSource2.stix.id)
.set('Cookie', `${ login.passportCookieName }=${ passportCookie.value }`)
.expect(204);
Expand Down

0 comments on commit 4b428ce

Please sign in to comment.