Skip to content

Commit 6378213

Browse files
committed
Failing LDP tests
1 parent 4925cef commit 6378213

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

lib/ldp.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const utils = require('./utils')
1010
const error = require('./http-error')
1111
const stringToStream = require('./utils').stringToStream
1212
const serialize = require('./utils').serialize
13+
const overQuota = require('./utils').overQuota
1314
const extend = require('extend')
1415
const rimraf = require('rimraf')
1516
const ldpContainer = require('./ldp-container')
@@ -266,24 +267,33 @@ class LDP {
266267
return callback(error(409,
267268
'PUT not supported on containers, use POST instead'))
268269
}
269-
// First, create the enclosing directory, if necessary
270-
const dirName = path.dirname(filePath)
271-
mkdirp(dirName, (err) => {
272-
if (err) {
273-
debug.handlers('PUT -- Error creating directory: ' + err)
274-
return callback(error(err,
275-
'Failed to create the path to the new resource'))
270+
271+
// First check if we are above quota
272+
overQuota(root, this.serverUri).then((isOverQuota) => {
273+
if (isOverQuota) {
274+
return callback(error(413,
275+
'User has exceeded their storage quota'))
276276
}
277-
// Directory created, now write the file
278-
const file = stream.pipe(fs.createWriteStream(filePath))
279-
file.on('error', function () {
280-
callback(error(500, 'Error writing data'))
281-
})
282-
file.on('finish', function () {
283-
debug.handlers('PUT -- Wrote data to: ' + filePath)
284-
callback(null)
277+
278+
// Second, create the enclosing directory, if necessary
279+
const dirName = path.dirname(filePath)
280+
mkdirp(dirName, (err) => {
281+
if (err) {
282+
debug.handlers('PUT -- Error creating directory: ' + err)
283+
return callback(error(err,
284+
'Failed to create the path to the new resource'))
285+
}
286+
// Directory created, now write the file
287+
const file = stream.pipe(fs.createWriteStream(filePath))
288+
file.on('error', function () {
289+
callback(error(500, 'Error writing data'))
290+
})
291+
file.on('finish', function () {
292+
debug.handlers('PUT -- Wrote data to: ' + filePath)
293+
callback(null)
294+
})
285295
})
286-
})
296+
}).catch(() => { throw error(500, 'Error finding user quota') })
287297
}
288298

289299
exists (hostname, path, callback) {

lib/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ async function overQuota (root, serverUri) {
299299
}
300300
// TODO: cache this value?
301301
var size = await actualSize(root)
302+
console.log(size, quota)
302303
return (size > quota)
303304
}
304305

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@
101101
"snyk": "^1.88.2",
102102
"standard": "^8.6.0",
103103
"supertest": "^3.0.0",
104-
"whatwg-url": "^6.1.0"
104+
"whatwg-url": "^6.1.0",
105+
"randombytes": "^2.0.1"
105106
},
106107
"main": "index.js",
107108
"scripts": {

test/integration/ldp-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var ns = require('solid-namespace')($rdf)
44
var LDP = require('../../lib/ldp')
55
var path = require('path')
66
var stringToStream = require('../../lib/utils').stringToStream
7+
var randomBytes = require('randombytes')
78

89
// Helper functions for the FS
910
var rm = require('./../utils').rm
@@ -15,6 +16,7 @@ var fs = require('fs')
1516
describe('LDP', function () {
1617
var ldp = new LDP({
1718
root: path.join(__dirname, '..'),
19+
serverUri: 'https://localhost',
1820
webid: false
1921
})
2022

@@ -133,6 +135,23 @@ describe('LDP', function () {
133135
done()
134136
})
135137
})
138+
139+
it('Write a larger file', function (done) {
140+
var randstream = stringToStream(randomBytes(2100))
141+
ldp.put('localhost', '/resources/testQuota.txt', randstream, function (err) {
142+
console.log(err)
143+
assert.notOk(err)
144+
done()
145+
})
146+
})
147+
it('should fail if a over quota', function (done) {
148+
var hellostream = stringToStream('hello world')
149+
ldp.put('localhost', '/resources/testOverQuota.txt', hellostream, function (err) {
150+
console.log(err)
151+
assert.equal(err.status, 413)
152+
done()
153+
})
154+
})
136155
})
137156

138157
describe('delete', function () {

test/settings/serverSide.ttl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@prefix dct: <http://purl.org/dc/terms/>.
2+
@prefix pim: <http://www.w3.org/ns/pim/space#>.
3+
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
4+
@prefix unit: <http://www.w3.invalid/ns#>.
5+
6+
<>
7+
a pim:ConfigurationFile;
8+
9+
dct:description "Administrative settings for the server that are only readable to the user." .
10+
11+
</>
12+
solid:storageQuota "1230"^^unit:kilobyte .
13+

0 commit comments

Comments
 (0)