Skip to content
Open
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
28 changes: 2 additions & 26 deletions api/src/controllers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type QueryParams = {
location?: string;
isAvailableOnline?: string;
timespan?: string;
filterOutExhibitions?: string;
linkedWork?: string | string[];
} & PaginationQueryParameters;

Expand Down Expand Up @@ -87,12 +86,6 @@ const isAvailableOnlineValidator = queryValidator({
singleValue: true,
});

const filterOutExhibitionsValidator = queryValidator({
name: 'filterOutExhibitions',
allowed: ['true'],
singleValue: true,
});

const timespanValidator = queryValidator({
name: 'timespan',
allowed: timespans,
Expand Down Expand Up @@ -122,7 +115,7 @@ const formatAliasMap: Record<string, string> = {
};

const paramsValidator = (params: QueryParams): QueryParams => {
const { isAvailableOnline, filterOutExhibitions, format, ...rest } = params;
const { isAvailableOnline, format, ...rest } = params;

if (params.location)
locationsValidator({
Expand Down Expand Up @@ -191,14 +184,7 @@ const paramsValidator = (params: QueryParams): QueryParams => {
isAvailableOnline,
});

const hasFilterOutExhibitions =
filterOutExhibitions &&
filterOutExhibitionsValidator({
filterOutExhibitions,
});

// For isAvailableOnline and filterOutExhibitions,
// we are ignoring all values passed in but "true".
// For isAvailableOnline, we are ignoring all values passed in but "true".
// Anything else should remove the param from the query
return {
...rest,
Expand All @@ -207,7 +193,6 @@ const paramsValidator = (params: QueryParams): QueryParams => {
? { excludeFormat: excludeFormats.join(',') }
: {}),
...(hasIsAvailableOnline ? { isAvailableOnline } : {}),
...(hasFilterOutExhibitions ? { filterOutExhibitions } : {}),
};
};

Expand Down Expand Up @@ -345,15 +330,6 @@ const eventsController = (clients: Clients, config: Config): EventsHandler => {
isChildScheduledEvent: true,
},
},
...(validParams.filterOutExhibitions
? [
{
term: {
'filter.format': EVENT_EXHIBITION_FORMAT_ID,
},
},
]
: []),
...(validParams.excludeFormat
? [
{
Expand Down
49 changes: 49 additions & 0 deletions api/test/__snapshots__/query.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,55 @@ exports[`addressables query makes the expected query to ES for a given set of qu
}
`;

exports[`addressables query makes the expected query to ES for multiple linkedWork parameters 1`] = `
{
"_source": [
"display",
],
"from": 0,
"index": "test-addressables",
"query": {
"bool": {
"must": [
{
"multi_match": {
"fields": [
"id",
"uid",
"query.title.*^100",
"query.contributors.*^10",
"query.contributors.keyword^100",
"query.body.*",
"query.description.*",
],
"minimum_should_match": "-25%",
"operator": "or",
"query": "sculpture",
"type": "cross_fields",
},
},
{
"terms": {
"query.linkedWorks": [
"work123",
"work456",
],
},
},
],
"must_not": [
{
"term": {
"query.tags": "delist",
},
},
],
},
},
"size": 20,
}
`;

exports[`articles query makes the expected query to ES for a given set of query parameters 1`] = `
{
"_source": [
Expand Down
16 changes: 8 additions & 8 deletions api/test/addressables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ describe('GET /all', () => {
title: `test doc ${i}`,
},
}));
const api = mockedApi(docs);
const { agent } = mockedApi(docs);

const response = await api.get(`/all`);
const response = await agent.get(`/all`);
expect(response.statusCode).toBe(200);
expect(response.body.results).toStrictEqual(docs.map(d => d.display));
});
Expand All @@ -22,17 +22,17 @@ describe('GET /all', () => {
display: { title: 'Document with works' },
},
];
const api = mockedApi(docs);
const { agent } = mockedApi(docs);

const response = await api.get(`/all?linkedWork=work123`);
const response = await agent.get(`/all?linkedWork=work123`);
expect(response.statusCode).toBe(200);
expect(response.body.results).toBeDefined();
});

it('returns 400 for invalid linkedWork format', async () => {
const api = mockedApi([]);
const { agent } = mockedApi([]);

const response = await api.get(`/all?linkedWork=invalid-work-id!`);
const response = await agent.get(`/all?linkedWork=invalid-work-id!`);
expect(response.statusCode).toBe(400);
expect(response.body.description).toContain('Invalid work ID format');
});
Expand All @@ -44,9 +44,9 @@ describe('GET /all', () => {
display: { title: 'Health article' },
},
];
const api = mockedApi(docs);
const { agent } = mockedApi(docs);

const response = await api.get(`/all?query=health&linkedWork=work123`);
const response = await agent.get(`/all?query=health&linkedWork=work123`);
expect(response.statusCode).toBe(200);
expect(response.body.results).toBeDefined();
});
Expand Down
8 changes: 4 additions & 4 deletions api/test/article.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ describe('GET /articles/:id', () => {
it('returns a document for the given ID', async () => {
const testId = '123';
const testDoc = { title: 'test-article' };
const api = mockedApi([{ id: testId, display: testDoc }]);
const { agent } = mockedApi([{ id: testId, display: testDoc }]);

const response = await api.get(`/articles/${testId}`);
const response = await agent.get(`/articles/${testId}`);
expect(response.statusCode).toBe(200);
expect(response.body).toStrictEqual(testDoc);
});

it('returns a 404 if no document for the given ID exists', async () => {
const api = mockedApi([]);
const { agent } = mockedApi([]);

const response = await api.get(`/articles/123`);
const response = await agent.get(`/articles/123`);
expect(response.statusCode).toBe(404);
});
});
22 changes: 12 additions & 10 deletions api/test/articles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ describe('GET /articles', () => {
title: `test doc ${i}`,
},
}));
const api = mockedApi(docs);
const { agent } = mockedApi(docs);

const response = await api.get(`/articles`);
const response = await agent.get(`/articles`);
expect(response.statusCode).toBe(200);
expect(response.body.results).toStrictEqual(docs.map(d => d.display));
});
Expand All @@ -22,17 +22,17 @@ describe('GET /articles', () => {
display: { title: 'Article with works' },
},
];
const api = mockedApi(docs);
const { agent } = mockedApi(docs);

const response = await api.get(`/articles?linkedWork=work123`);
const response = await agent.get(`/articles?linkedWork=work123`);
expect(response.statusCode).toBe(200);
expect(response.body.results).toBeDefined();
});

it('returns 400 for invalid linkedWork format', async () => {
const api = mockedApi([]);
const { agent } = mockedApi([]);

const response = await api.get(`/articles?linkedWork=invalid-work-id!`);
const response = await agent.get(`/articles?linkedWork=invalid-work-id!`);
expect(response.statusCode).toBe(400);
expect(response.body.description).toContain('Invalid work ID format');
});
Expand All @@ -44,9 +44,11 @@ describe('GET /articles', () => {
display: { title: 'Health article' },
},
];
const api = mockedApi(docs);
const { agent } = mockedApi(docs);

const response = await api.get(`/articles?query=health&linkedWork=work123`);
const response = await agent.get(
`/articles?query=health&linkedWork=work123`
);
expect(response.statusCode).toBe(200);
expect(response.body.results).toBeDefined();
});
Expand All @@ -58,9 +60,9 @@ describe('GET /articles', () => {
display: { title: 'Article with multiple works' },
},
];
const api = mockedApi(docs);
const { agent } = mockedApi(docs);

const response = await api.get(`/articles?linkedWork=work123,work456`);
const response = await agent.get(`/articles?linkedWork=work123,work456`);
expect(response.statusCode).toBe(200);
expect(response.body.results).toBeDefined();
});
Expand Down
8 changes: 4 additions & 4 deletions api/test/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ describe('GET /events/:id', () => {
it('returns a document for the given ID', async () => {
const testId = 'abc';
const testDoc = { title: 'test-event' };
const api = mockedApi([{ id: testId, display: testDoc }]);
const { agent } = mockedApi([{ id: testId, display: testDoc }]);

const response = await api.get(`/events/${testId}`);
const response = await agent.get(`/events/${testId}`);
expect(response.statusCode).toBe(200);
expect(response.body).toStrictEqual(testDoc);
});

it('returns a 404 if no document for the given ID exists', async () => {
const api = mockedApi([]);
const { agent } = mockedApi([]);

const response = await api.get(`/events/abc`);
const response = await agent.get(`/events/abc`);
expect(response.statusCode).toBe(404);
});
});
4 changes: 2 additions & 2 deletions api/test/venues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ describe('GET /venues', () => {
jest.useFakeTimers().setSystemTime(new Date('2024-04-02T08:00:00.000Z'));

const testId = '123';
const api = mockedApi([
const { agent } = mockedApi([
{ id: testId, display: venueDisplay, data: venueData },
]);

const response = await api.get(`/venues`);
const response = await agent.get(`/venues`);
expect(response.statusCode).toBe(200);
expect(response.body.results[0]).toEqual({
...venueDisplay,
Expand Down