From d3375d2de7f34cea5fcab5e52bbde409d17ba395 Mon Sep 17 00:00:00 2001 From: Pieter-jan vandenbussche Date: Mon, 7 May 2018 14:04:51 +0200 Subject: [PATCH 1/2] use newResource.id in 'resource already exists' error --- lib/MemoryHandler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/MemoryHandler.js b/lib/MemoryHandler.js index 51c8042f..43d8fd37 100644 --- a/lib/MemoryHandler.js +++ b/lib/MemoryHandler.js @@ -70,7 +70,7 @@ MemoryStore.prototype.create = (request, newResource, callback) => { status: '403', code: 'EFORBIDDEN', title: 'Requested resource already exists', - detail: `The requested resource already exists of type ${request.params.type} with id ${request.params.id}` + detail: `The requested resource already exists of type ${request.params.type} with id ${newResource.id}` }) } // Push the newResource into our in-memory store. From fcb9362b7322a84f8b2a2d87e2e2fe6279f72096 Mon Sep 17 00:00:00 2001 From: Pieter-jan vandenbussche Date: Mon, 7 May 2018 14:05:07 +0200 Subject: [PATCH 2/2] write test --- test/post-resource-client-generated-id.js | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/post-resource-client-generated-id.js b/test/post-resource-client-generated-id.js index 925e90f1..65aeafc1 100644 --- a/test/post-resource-client-generated-id.js +++ b/test/post-resource-client-generated-id.js @@ -74,6 +74,36 @@ describe('Testing jsonapi-server', () => { done() }) }) + + it('errors when creating resource with existing id', done => { + const existingId = 'cc5cca2e-0dd8-4b95-8cfc-a11230e73116' + const data = { + method: 'post', + url: 'http://localhost:16006/rest/people', + headers: { + 'Content-Type': 'application/vnd.api+json' + }, + body: JSON.stringify({ + 'data': { + 'id': existingId, + 'type': 'people', + 'attributes': { + firstname: 'Harry', + lastname: 'Potter', + email: 'harry.potter@hogwarts.edu.uk' + } + } + }) + } + helpers.request(data, (err, res, json) => { + assert.equal(err, null) + json = helpers.validateError(json) + assert.equal(json.errors[0].detail, `The requested resource already exists of type people with id ${existingId}`, 'Expecting detail with correct type and id') + assert.equal(res.statusCode, '403', 'Expecting 403') + + done() + }) + }) }) })