Skip to content

Commit 4c6c275

Browse files
committed
Test PATCH deletion.
Two tests are skipped; these require an ACL check inside of the handler.
1 parent 338795e commit 4c6c275

File tree

6 files changed

+159
-8
lines changed

6 files changed

+159
-8
lines changed

lib/handlers/patch/n3-patcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function applyPatch (patchObject, target, targetKB) {
5858
if (err) {
5959
const message = err.message || err // returns string at the moment
6060
debug('PATCH FAILED. Returning 409. Message: \'' + message + '\'')
61-
return reject(error(409, 'Error when applying the patch'))
61+
return reject(error(409, `The patch could not be applied. ${message}`))
6262
}
6363
resolve(targetKB)
6464
})

test/integration/patch.js

Lines changed: 154 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { assert } = require('chai')
33
const ldnode = require('../../index')
44
const path = require('path')
55
const supertest = require('supertest')
6+
const fs = require('fs')
67
const { read, rm, backup, restore } = require('../test-utils')
78

89
// Server settings
@@ -103,7 +104,7 @@ describe('PATCH', () => {
103104

104105
it('does not modify the file', () => {
105106
assert.equal(read('patch/read-only.ttl'),
106-
'<a> <b> <c>.\n')
107+
'<a> <b> <c>.\n<d> <e> <f>.\n')
107108
})
108109
})
109110

@@ -116,7 +117,7 @@ describe('PATCH', () => {
116117
.set('Content-Type', 'text/n3')
117118
.send(n3Patch(`
118119
<> p:patches <https://tim.localhost:7777/new.ttl>;
119-
p:insert { <d> <e> <f>. }.`
120+
p:insert { <x> <y> <z>. }.`
120121
))
121122
.expect(200)
122123
.then(response => {
@@ -126,7 +127,7 @@ describe('PATCH', () => {
126127

127128
it('creates the file', () => {
128129
assert.equal(read('patch/new.ttl'),
129-
'@prefix : </new.ttl#>.\n@prefix tim: </>.\n\ntim:d tim:e tim:f.\n\n')
130+
'@prefix : </new.ttl#>.\n@prefix tim: </>.\n\ntim:x tim:y tim:z.\n\n')
130131
})
131132
})
132133

@@ -140,7 +141,7 @@ describe('PATCH', () => {
140141
.set('Content-Type', 'text/n3')
141142
.send(n3Patch(`
142143
<> p:patches <https://tim.localhost:7777/append-only.ttl>;
143-
p:insert { <d> <e> <f>. }.`
144+
p:insert { <x> <y> <z>. }.`
144145
))
145146
.expect(200)
146147
.then(response => {
@@ -150,7 +151,7 @@ describe('PATCH', () => {
150151

151152
it('patches the file', () => {
152153
assert.equal(read('patch/append-only.ttl'),
153-
'@prefix : </append-only.ttl#>.\n@prefix tim: </>.\n\ntim:a tim:b tim:c.\n\ntim:d tim:e tim:f.\n\n')
154+
'@prefix : </append-only.ttl#>.\n@prefix tim: </>.\n\ntim:a tim:b tim:c.\n\ntim:d tim:e tim:f.\n\ntim:x tim:y tim:z.\n\n')
154155
})
155156
})
156157

@@ -164,7 +165,7 @@ describe('PATCH', () => {
164165
.set('Content-Type', 'text/n3')
165166
.send(n3Patch(`
166167
<> p:patches <https://tim.localhost:7777/write-only.ttl>;
167-
p:insert { <d> <e> <f>. }.`
168+
p:insert { <x> <y> <z>. }.`
168169
))
169170
.expect(200)
170171
.then(response => {
@@ -174,7 +175,153 @@ describe('PATCH', () => {
174175

175176
it('patches the file', () => {
176177
assert.equal(read('patch/write-only.ttl'),
177-
'@prefix : </write-only.ttl#>.\n@prefix tim: </>.\n\ntim:a tim:b tim:c.\n\ntim:d tim:e tim:f.\n\n')
178+
'@prefix : </write-only.ttl#>.\n@prefix tim: </>.\n\ntim:a tim:b tim:c.\n\ntim:d tim:e tim:f.\n\ntim:x tim:y tim:z.\n\n')
179+
})
180+
})
181+
})
182+
183+
describe('deleting', () => {
184+
describe('from a resource with read-only access', () => {
185+
it('returns a 403', () =>
186+
request.patch('/read-only.ttl')
187+
.set('Authorization', `Bearer ${userCredentials}`)
188+
.set('Content-Type', 'text/n3')
189+
.send(n3Patch(`
190+
<> p:patches <https://tim.localhost:7777/read-only.ttl>;
191+
p:delete { <a> <b> <c>. }.`
192+
))
193+
.expect(403)
194+
.then(response => {
195+
assert.include(response.text, 'Access denied')
196+
})
197+
)
198+
199+
it('does not modify the file', () => {
200+
assert.equal(read('patch/read-only.ttl'),
201+
'<a> <b> <c>.\n<d> <e> <f>.\n')
202+
})
203+
})
204+
205+
describe('from a non-existing file', () => {
206+
after(() => rm('patch/new.ttl'))
207+
208+
it('returns a 409', () =>
209+
request.patch('/new.ttl')
210+
.set('Authorization', `Bearer ${userCredentials}`)
211+
.set('Content-Type', 'text/n3')
212+
.send(n3Patch(`
213+
<> p:patches <https://tim.localhost:7777/new.ttl>;
214+
p:delete { <a> <b> <c>. }.`
215+
))
216+
.expect(409)
217+
.then(response => {
218+
assert.include(response.text, 'The patch could not be applied')
219+
})
220+
)
221+
222+
it('does not create the file', () => {
223+
assert.isFalse(fs.existsSync('patch/new.ttl'))
224+
})
225+
})
226+
227+
describe.skip('from a resource with append-only access', () => {
228+
before(() => backup('patch/append-only.ttl'))
229+
after(() => restore('patch/append-only.ttl'))
230+
231+
it('returns a 403', () =>
232+
request.patch('/append-only.ttl')
233+
.set('Authorization', `Bearer ${userCredentials}`)
234+
.set('Content-Type', 'text/n3')
235+
.send(n3Patch(`
236+
<> p:patches <https://tim.localhost:7777/append-only.ttl>;
237+
p:delete { <a> <b> <c>. }.`
238+
))
239+
.expect(403)
240+
.then(response => {
241+
assert.include(response.text, 'Access denied')
242+
})
243+
)
244+
245+
it('does not modify the file', () => {
246+
assert.equal(read('patch/append-only.ttl'),
247+
'<a> <b> <c>.\n<d> <e> <f>.\n')
248+
})
249+
})
250+
251+
describe.skip('from a resource with write-only access', () => {
252+
before(() => backup('patch/write-only.ttl'))
253+
after(() => restore('patch/write-only.ttl'))
254+
255+
// Allowing the delete would either return 200 or 409,
256+
// thereby incorrectly giving the user (guess-based) read access;
257+
// therefore, we need to return 403.
258+
it('returns a 403', () =>
259+
request.patch('/write-only.ttl')
260+
.set('Authorization', `Bearer ${userCredentials}`)
261+
.set('Content-Type', 'text/n3')
262+
.send(n3Patch(`
263+
<> p:patches <https://tim.localhost:7777/write-only.ttl>;
264+
p:delete { <a> <b> <c>. }.`
265+
))
266+
.expect(403)
267+
.then(response => {
268+
assert.include(response.text, 'Access denied')
269+
})
270+
)
271+
272+
it('does not modify the file', () => {
273+
assert.equal(read('patch/append-only.ttl'),
274+
'<a> <b> <c>.\n<d> <e> <f>.\n')
275+
})
276+
})
277+
278+
describe('from a resource with read-write access', () => {
279+
describe('with a patch for existing data', () => {
280+
before(() => backup('patch/read-write.ttl'))
281+
after(() => restore('patch/read-write.ttl'))
282+
283+
it('returns a 200', () =>
284+
request.patch('/read-write.ttl')
285+
.set('Authorization', `Bearer ${userCredentials}`)
286+
.set('Content-Type', 'text/n3')
287+
.send(n3Patch(`
288+
<> p:patches <https://tim.localhost:7777/read-write.ttl>;
289+
p:delete { <a> <b> <c>. }.`
290+
))
291+
.expect(200)
292+
.then(response => {
293+
assert.include(response.text, 'Patch applied successfully')
294+
})
295+
)
296+
297+
it('patches the file', () => {
298+
assert.equal(read('patch/read-write.ttl'),
299+
'@prefix : </read-write.ttl#>.\n@prefix tim: </>.\n\ntim:d tim:e tim:f.\n\n')
300+
})
301+
})
302+
303+
describe('with a patch for non-existing data', () => {
304+
before(() => backup('patch/read-write.ttl'))
305+
after(() => restore('patch/read-write.ttl'))
306+
307+
it('returns a 409', () =>
308+
request.patch('/read-write.ttl')
309+
.set('Authorization', `Bearer ${userCredentials}`)
310+
.set('Content-Type', 'text/n3')
311+
.send(n3Patch(`
312+
<> p:patches <https://tim.localhost:7777/read-write.ttl>;
313+
p:delete { <x> <y> <z>. }.`
314+
))
315+
.expect(409)
316+
.then(response => {
317+
assert.include(response.text, 'The patch could not be applied')
318+
})
319+
)
320+
321+
it('does not change the file', () => {
322+
assert.equal(read('patch/read-write.ttl'),
323+
'<a> <b> <c>.\n<d> <e> <f>.\n')
324+
})
178325
})
179326
})
180327
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
<a> <b> <c>.
2+
<d> <e> <f>.

test/resources/patch/read-only.ttl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
<a> <b> <c>.
2+
<d> <e> <f>.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
<a> <b> <c>.
2+
<d> <e> <f>.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
<a> <b> <c>.
2+
<d> <e> <f>.

0 commit comments

Comments
 (0)