Skip to content

Commit

Permalink
fix: Implement cache DELETE endpoint (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranav Ravichandran authored and d2lam committed Sep 27, 2018
1 parent 56e95aa commit 9b8bdb3
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
42 changes: 42 additions & 0 deletions plugins/caches.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,48 @@ exports.plugin = {
}
}
}
}, {
method: 'DELETE',
path: '/events/{id}/{cacheName}',
handler: async (request, h) => {
const { eventId } = request.auth.credentials;
const eventIdParam = request.params.id;

if (eventIdParam !== eventId) {
return boom.forbidden(`Credential only valid for ${eventId}`);
}

const cacheName = request.params.cacheName;
const cacheKey = `events/${eventIdParam}/${cacheName}`;

try {
await cache.drop(cacheKey);

return h.response();
} catch (err) {
throw err;
}
},
options: {
description: 'Delete event cache',
notes: 'Delete a specific cached object from an event',
tags: ['api', 'events'],
auth: {
strategies: ['token'],
scope: ['build']
},
plugins: {
'hapi-swagger': {
security: [{ token: [] }]
}
},
validate: {
params: {
id: SCHEMA_EVENT_ID,
cacheName: SCHEMA_CACHE_NAME
}
}
}
}]);
}
};
70 changes: 70 additions & 0 deletions test/plugins/caches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,74 @@ describe('events plugin test', () => {
});
});
});

describe('DELETE /events/:id/:cacheName', () => {
let getOptions;
let putOptions;
let deleteOptions;

beforeEach(() => {
getOptions = {
headers: {
'x-foo': 'bar'
},
credentials: {
eventId: mockEventID,
scope: ['build']
},
url: `/events/${mockEventID}/foo`
};
putOptions = {
method: 'PUT',
payload: 'THIS IS A TEST',
headers: {
'x-foo': 'bar',
'content-type': 'text/plain',
ignore: 'true'
},
credentials: {
eventId: mockEventID,
scope: ['build']
},
url: `/events/${mockEventID}/foo`
};
deleteOptions = {
method: 'DELETE',
headers: {
'x-foo': 'bar',
'content-type': 'text/plain',
ignore: 'true'
},
credentials: {
eventId: mockEventID,
scope: ['build']
},
url: `/events/${mockEventID}/foo`
};
});

it('returns 200 if not found', () => server.inject(getOptions).then((getResponse) => {
assert.equal(getResponse.statusCode, 404);

return server.inject(deleteOptions).then((deleteResponse) => {
assert.equal(deleteResponse.statusCode, 200);
});
}));

it('deletes an event cache', () => server.inject(putOptions).then((postResponse) => {
assert.equal(postResponse.statusCode, 202);

return server.inject(getOptions).then((getResponse) => {
assert.equal(getResponse.statusCode, 200);

return server.inject(deleteOptions).then((deleteResponse) => {
assert.equal(deleteResponse.statusCode, 200);

return server.inject(getOptions).then((getResponse2) => {
assert.equal(getResponse2.statusCode, 404);
});
});
});
}));
});
});

0 comments on commit 9b8bdb3

Please sign in to comment.