Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

b/2527 fix parsing of dcatConfig query param #10

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe('Output Plugin', () => {
});
})

it('Properly passes custom dcat configurations to getDataStreamDcatUs11', async () => {
it('Properly passes a site\'s custom dcat configurations to getDataStreamDcatUs11 when no dcatConfig is provided', async () => {
// Change fetchSite's return value to include a custom dcat config
const customConfigSiteModel: IModel = _.cloneDeep(mockSiteModel);
customConfigSiteModel.data.feeds = {
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('Output Plugin', () => {
});
});

it('Properly passes the ?dcatConfig query param to getDataStreamDcatUs11', async () => {
it('Properly passes a valid stringified ?dcatConfig query param to getDataStreamDcatUs11', async () => {
const dcatConfig = {
planet: 'tatooine'
}
Expand All @@ -213,6 +213,22 @@ describe('Output Plugin', () => {
});
});

it('Properly passes the ?dcatConfig query param as an object to getDataStreamDcatUs11', async () => {
const dcatConfig = {
planet: 'tatooine'
}

await request(app)
.get('/dcat')
.query({ dcatConfig })
.set('host', siteHostName)
.expect('Content-Type', /application\/json/)
.expect(200)
.expect(() => {
expect(mockGetDataStreamDcatUs11).toHaveBeenCalledWith(mockSiteModel.item, dcatConfig);
});
});

it('Passes the site dcat config to getDataStreamDcatUs11 when ?dcatConfig is invalid json', async () => {
// Change fetchSite's return value to include a custom dcat config
const customConfigSiteModel: any = _.cloneDeep(mockSiteModel);
Expand Down
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ export = class OutputDcatUs11 {
const siteModel = await fetchSite(req.hostname, this.getRequestOptions(portalUrl));

// Use dcatConfig query param if provided, else default to site's config
const dcatConfig = typeof req.query.dcatConfig === 'object'
? req.query.dcatConfig
: _.get(siteModel, 'data.feeds.dcatUS11');
let dcatConfig = typeof req.query.dcatConfig === 'string'
? this.parseProvidedDcatConfig(req.query.dcatConfig as string)
: req.query.dcatConfig;

if (!dcatConfig) {
dcatConfig = _.get(siteModel, 'data.feeds.dcatUS11');
}

const { stream: dcatStream, dependencies } = getDataStreamDcatUs11(siteModel.item, dcatConfig);
const apiTerms = getApiTermsFromDependencies(dependencies);
Expand Down Expand Up @@ -69,6 +73,14 @@ export = class OutputDcatUs11 {
};
}

private parseProvidedDcatConfig(dcatConfig: string) {
try {
return JSON.parse(dcatConfig);
} catch (err) {
return undefined;
}
}

private getCatalogSearchRequest(
catalog: any,
portalUrl: string,
Expand Down