Skip to content

Commit

Permalink
Merge branch 'main' into harmony-1538-2
Browse files Browse the repository at this point in the history
  • Loading branch information
indiejames committed Oct 12, 2023
2 parents 243aa34 + b60fc5d commit 5255d27
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
31 changes: 31 additions & 0 deletions services/harmony/app/middleware/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NextFunction } from 'express';
import HarmonyRequest from '../models/harmony-request';

/**
* Set the request operation extendDimensions value to the default if applicable
* @param req - The client request
*/
export function setExtendDimensionsDefault(req: HarmonyRequest): void {
const extend = req.context.serviceConfig?.capabilities?.extend;
const defaultExtendDimensions = req.context.serviceConfig?.capabilities?.default_extend_dimensions;
// set extendDimension to the default if there is one configured and no provided value
if (extend && defaultExtendDimensions && !req.operation?.extendDimensions) {
req.operation.extendDimensions = defaultExtendDimensions;
}
}

/**
* Express.js middleware that inject default extend parameter into operation if applicable
*
* @param req - The client request
* @param res - The client response
* @param next - The next function in the middleware chain
*/
export default async function extendDefault(req: HarmonyRequest, res, next: NextFunction): Promise<void> {
try {
setExtendDimensionsDefault(req);
next();
} catch (error) {
next(error);
}
}
2 changes: 2 additions & 0 deletions services/harmony/app/models/services/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface ServiceCapabilities {
};
output_formats?: string[];
reprojection?: boolean;
extend?: boolean;
default_extend_dimensions?: string[];
}

export interface ServiceStep {
Expand Down
2 changes: 2 additions & 0 deletions services/harmony/app/routers/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { getStagingBucketPolicy } from '../frontends/staging-bucket-policy';
import { parseGridMiddleware } from '../util/grids';
import docsPage from '../frontends/docs/docs';
import { getCollectionCapabilitiesJson } from '../frontends/capabilities';
import extendDefault from '../middleware/extend';
export interface RouterConfig {
PORT?: string | number; // The port to run the frontend server on
BACKEND_PORT?: string | number; // The port to run the backend server on
Expand Down Expand Up @@ -198,6 +199,7 @@ export default function router({ skipEarthdataLogin = 'false' }: RouterConfig):
result.use(logged(cmrUmmCollectionReader));
result.use(logged(cmrGranuleLocator));
result.use(logged(addRequestContextToOperation));
result.use(logged(extendDefault));
result.use(logged(redirectWithoutTrailingSlash));

result.get('/', asyncHandler(landingPage));
Expand Down
90 changes: 90 additions & 0 deletions services/harmony/test/middleware/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { expect } from 'chai';
import { describe, it, beforeEach } from 'mocha';
import HarmonyRequest from '../../app/models/harmony-request';
import DataOperation from '../../app/models/data-operation';
import { setExtendDimensionsDefault } from '../../app/middleware/extend';

describe('extend serivce default value', function () {
beforeEach(function () {
const collectionId = 'C123-TEST';
const shortName = 'harmony_example';
const versionId = '1';
const operation = new DataOperation();
operation.addSource(collectionId, shortName, versionId);
const harmonyRequest = {
operation: operation,
context: {
serviceConfig: {
name: 'extend-service',
type: { name: 'turbo' },
collections: [{ id: collectionId }],
capabilities: {
extend: true,
default_extend_dimensions: ['mirror_step'],
},
},
},
} as HarmonyRequest;
this.req = harmonyRequest;
});

describe('and the request does not provide dimension extension', function () {
it('extendDimensions is set to the default', function () {
setExtendDimensionsDefault(this.req);
expect(this.req.operation.extendDimensions).to.eql(['mirror_step']);
});
});

describe('and the request provides dimension extension', function () {
beforeEach(function () {
this.req.operation.extendDimensions = ['lat', 'lon'];
});

it('extendDimensions is set to the provided value, not the default', function () {
setExtendDimensionsDefault(this.req);
expect(this.req.operation.extendDimensions).to.eql(['lat', 'lon']);
});
});
});

describe('extend serivce misconfigured without default value', function () {
beforeEach(function () {
const collectionId = 'C123-TEST';
const shortName = 'harmony_example';
const versionId = '1';
const operation = new DataOperation();
operation.addSource(collectionId, shortName, versionId);
const harmonyRequest = {
operation: operation,
context: {
serviceConfig: {
name: 'extend-service',
type: { name: 'turbo' },
collections: [{ id: collectionId }],
capabilities: {
extend: true,
},
},
},
} as HarmonyRequest;
this.req = harmonyRequest;
});

describe('and the request does not provide dimension extension', function () {
it('extendDimensions is set to the default', function () {
setExtendDimensionsDefault(this.req);
expect(this.req.operation.extendDimensions).to.not.exist;
});
});

describe('and the request provides dimension extension', function () {
beforeEach(function () {
this.req.operation.extendDimensions = ['lat', 'lon'];
});

it('extendDimensions is set to the provided value, not the default', function () {
setExtendDimensionsDefault(this.req);
expect(this.req.operation.extendDimensions).to.eql(['lat', 'lon']);
});
});
});

0 comments on commit 5255d27

Please sign in to comment.