blob: 47e938a2334989274a1b90b7ead7149a7d0984d9 [file] [log] [blame]
'use strict';
require('../tools/js/v3-models.js');
const assert = require('assert');
global.FormData = require('form-data');
const MockData = require('./resources/mock-data.js');
const TestServer = require('./resources/test-server.js');
const TemporaryFile = require('./resources/temporary-file.js').TemporaryFile;
const prepareServerTest = require('./resources/common-operations.js').prepareServerTest;
describe('/privileged-api/upload-file', function () {
prepareServerTest(this);
TemporaryFile.inject();
function makeRandomAlnumStringForLength(length)
{
let string = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++)
string += characters.charAt(Math.floor(Math.random() * charactersLength));
return string;
}
it('should return "NotFileSpecified" when newFile not is specified', () => {
return PrivilegedAPI.sendRequest('upload-file', {}, {useFormData: true}).then(() => {
assert(false, 'should never be reached');
}, (error) => {
assert.strictEqual(error, 'NoFileSpecified');
});
});
it('should return "FileSizeLimitExceeded" when the file is too big', () => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', TestServer.testConfig().uploadFileLimitInMB + 1).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true}).then(() => {
assert(false, 'should never be reached');
}, (error) => {
assert.strictEqual(error, 'FileSizeLimitExceeded');
});
});
});
it('should upload a file when the filesize is smaller than the limit', () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
let uploadedFile;
return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', limitInMB).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((response) => {
uploadedFile = response['uploadedFile'];
return db.selectAll('uploaded_files', 'id');
}).then((rows) => {
assert.strictEqual(rows.length, 1);
assert.strictEqual(rows[0].id, parseInt(uploadedFile.id));
assert.strictEqual(parseInt(rows[0].size), limitInMB * 1024 * 1024);
assert.strictEqual(parseInt(rows[0].size), parseInt(uploadedFile.size));
assert.strictEqual(rows[0].filename, 'some.dat');
assert.strictEqual(rows[0].filename, uploadedFile.filename);
assert.strictEqual(rows[0].extension, '.dat');
assert.strictEqual(rows[0].sha256, '5256ec18f11624025905d057d6befb03d77b243511ac5f77ed5e0221ce6d84b5');
assert.strictEqual(rows[0].sha256, uploadedFile.sha256);
});
});
it('should not create a duplicate files when the identical files are uploaded', () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
let uploadedFile1;
let uploadedFile2;
return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', limitInMB).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((response) => {
uploadedFile1 = response['uploadedFile'];
return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB);
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((response) => {
uploadedFile2 = response['uploadedFile'];
return db.selectAll('uploaded_files', 'id');
}).then((rows) => {
assert.deepStrictEqual(uploadedFile1, uploadedFile2);
assert.strictEqual(rows.length, 1);
assert.strictEqual(rows[0].id, parseInt(uploadedFile1.id));
assert.strictEqual(parseInt(rows[0].size), limitInMB * 1024 * 1024);
assert.strictEqual(parseInt(rows[0].size), parseInt(uploadedFile1.size));
assert.strictEqual(rows[0].filename, 'some.dat');
assert.strictEqual(rows[0].filename, uploadedFile1.filename);
assert.strictEqual(rows[0].extension, '.dat');
assert.strictEqual(rows[0].sha256, '5256ec18f11624025905d057d6befb03d77b243511ac5f77ed5e0221ce6d84b5');
assert.strictEqual(rows[0].sha256, uploadedFile1.sha256);
});
});
it('should not create a duplicate files when the identical files are uploaded', () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
let uploadedFile1;
let uploadedFile2;
return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', limitInMB).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((response) => {
uploadedFile1 = response['uploadedFile'];
return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB);
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((response) => {
uploadedFile2 = response['uploadedFile'];
return db.selectAll('uploaded_files', 'id');
}).then((rows) => {
assert.deepStrictEqual(uploadedFile1, uploadedFile2);
assert.strictEqual(rows.length, 1);
assert.strictEqual(rows[0].id, parseInt(uploadedFile1.id));
assert.strictEqual(parseInt(rows[0].size), limitInMB * 1024 * 1024);
assert.strictEqual(parseInt(rows[0].size), parseInt(uploadedFile1.size));
assert.strictEqual(rows[0].filename, 'some.dat');
assert.strictEqual(rows[0].filename, uploadedFile1.filename);
assert.strictEqual(rows[0].extension, '.dat');
assert.strictEqual(rows[0].sha256, '5256ec18f11624025905d057d6befb03d77b243511ac5f77ed5e0221ce6d84b5');
assert.strictEqual(rows[0].sha256, uploadedFile1.sha256);
});
});
it('should re-upload the file when the previously uploaded file had been deleted', () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
let uploadedFile1;
let uploadedFile2;
return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', limitInMB).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((response) => {
uploadedFile1 = response['uploadedFile'];
return db.query(`UPDATE uploaded_files SET file_deleted_at = now() at time zone 'utc'`);
}).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB);
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((response) => {
uploadedFile2 = response['uploadedFile'];
return db.selectAll('uploaded_files', 'id');
}).then((rows) => {
assert.notStrictEqual(uploadedFile1.id, uploadedFile2.id);
assert.strictEqual(rows.length, 2);
assert.strictEqual(rows[0].id, parseInt(uploadedFile1.id));
assert.strictEqual(rows[1].id, parseInt(uploadedFile2.id));
assert.strictEqual(rows[0].filename, 'some.dat');
assert.strictEqual(rows[0].filename, uploadedFile1.filename);
assert.strictEqual(rows[1].filename, 'other.dat');
assert.strictEqual(rows[1].filename, uploadedFile2.filename);
assert.strictEqual(parseInt(rows[0].size), limitInMB * 1024 * 1024);
assert.strictEqual(parseInt(rows[0].size), parseInt(uploadedFile1.size));
assert.strictEqual(parseInt(rows[0].size), parseInt(uploadedFile2.size));
assert.strictEqual(rows[0].size, rows[1].size);
assert.strictEqual(rows[0].sha256, '5256ec18f11624025905d057d6befb03d77b243511ac5f77ed5e0221ce6d84b5');
assert.strictEqual(rows[0].sha256, uploadedFile1.sha256);
assert.strictEqual(rows[0].sha256, uploadedFile2.sha256);
assert.strictEqual(rows[0].sha256, rows[1].sha256);
assert.strictEqual(rows[0].extension, '.dat');
assert.strictEqual(rows[1].extension, '.dat');
});
});
it('should pick up at most two file extensions', () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
return TemporaryFile.makeTemporaryFileOfSizeInMB('some.other.tar.gz5', limitInMB).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then(() => {
return db.selectAll('uploaded_files', 'id');
}).then((rows) => {
assert.strictEqual(rows.length, 1);
assert.strictEqual(parseInt(rows[0].size), limitInMB * 1024 * 1024);
assert.strictEqual(rows[0].mime, 'application/octet-stream');
assert.strictEqual(rows[0].filename, 'some.other.tar.gz5');
assert.strictEqual(rows[0].extension, '.tar.gz5');
assert.strictEqual(rows[0].sha256, '5256ec18f11624025905d057d6befb03d77b243511ac5f77ed5e0221ce6d84b5');
});
});
it('should delete an old file when uploading the file would result in the quota being exceeded', () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', limitInMB, 'a').then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB, 'b');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('another.dat', limitInMB, 'c');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then(() => {
return db.selectAll('uploaded_files', 'id');
}).then((rows) => {
assert.strictEqual(rows.length, 3);
assert.strictEqual(rows[0].filename, 'some.dat');
assert.notStrictEqual(rows[0].deleted_at, null);
assert.strictEqual(rows[1].filename, 'other.dat');
assert.strictEqual(rows[1].deleted_at, null);
assert.strictEqual(rows[2].filename, 'another.dat');
assert.strictEqual(rows[2].deleted_at, null);
})
});
it('should prune all removable files to get space for a file upload', async () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
const userQuotaInMB = TestServer.testConfig().uploadUserQuotaInMB;
const splitCount = 40;
const contentLength = userQuotaInMB * 1024 * 1024 / splitCount;
for (let i = 0; i < splitCount; i += 1) {
const content = makeRandomAlnumStringForLength(contentLength);
const stream = await TemporaryFile.makeTemporaryFile('file-' + i, content);
await PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}
let rows = await db.selectAll('uploaded_files');
assert.strictEqual(rows.length, splitCount);
const stream = await TemporaryFile.makeTemporaryFileOfSizeInMB('limit.dat', limitInMB, 'a');
await PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
rows = await db.selectAll('uploaded_files');
assert.strictEqual(rows.length, splitCount + 1);
const deletedFiles = rows.filter((row) => row['deleted_at']);
assert.strictEqual(deletedFiles.length, 16);
});
it('should not prune files that have been deleted', async () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
let stream = await TemporaryFile.makeTemporaryFileOfSizeInMB('limit.dat', limitInMB, 'a');
let response = await PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
const fileA = response.uploadedFile;
stream = await TemporaryFile.makeTemporaryFileOfSizeInMB('limit.dat', limitInMB, 'b');
response = await PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
const fileB = response.uploadedFile;
await MockData.addMockData(db, ['completed', 'completed', 'completed', 'completed']);
await db.query('UPDATE commit_set_items SET commitset_root_file=$1 WHERE commitset_set=401 AND commitset_commit=87832', [fileA.id]);
await db.query('UPDATE commit_set_items SET commitset_root_file=$1 WHERE commitset_set=401 AND commitset_commit=93116', [fileB.id]);
stream = await TemporaryFile.makeTemporaryFileOfSizeInMB('limit.dat', limitInMB, 'c');
response = await PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
const fileC = response.uploadedFile;
await db.query('UPDATE commit_set_items SET commitset_root_file=$1 WHERE commitset_set=402 AND commitset_commit=87832', [fileC.id]);
const deletedFileA = await db.selectFirstRow('uploaded_files', {id: fileA.id});
assert.ok(deletedFileA.deleted_at);
stream = await TemporaryFile.makeTemporaryFileOfSizeInMB('limit.dat', limitInMB, 'd');
await PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
const deletedFileB = await db.selectFirstRow('uploaded_files', {id: fileB.id});
assert.ok(deletedFileB.deleted_at);
});
it('should return "FileSizeQuotaExceeded" when there is no file to delete', () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
let fileA;
return MockData.addMockData(db).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('some.patch', limitInMB, 'a');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((result) => {
fileA = result.uploadedFile;
return TemporaryFile.makeTemporaryFileOfSizeInMB('other.patch', limitInMB, 'b');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((result) => {
const fileB = result.uploadedFile;
return Promise.all([
db.query('UPDATE commit_set_items SET (commitset_patch_file, commitset_requires_build) = ($1, TRUE) WHERE commitset_set = 402 AND commitset_commit = 87832', [fileA.id]),
db.query('UPDATE commit_set_items SET (commitset_patch_file, commitset_requires_build) = ($1, TRUE) WHERE commitset_set = 402 AND commitset_commit = 96336', [fileB.id])
]);
}).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB, 'c');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true}).then(() => {
assert(false, 'should never be reached');
}, (error) => {
assert.strictEqual(error, 'FileSizeQuotaExceeded');
});
});
});
it('should delete old patches that belong to finished build requests', () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
let fileA;
return MockData.addMockData(db, ['completed', 'completed', 'failed', 'canceled']).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('some.patch', limitInMB, 'a');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((result) => {
fileA = result.uploadedFile;
return TemporaryFile.makeTemporaryFileOfSizeInMB('other.patch', limitInMB, 'b');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((result) => {
const fileB = result.uploadedFile;
return Promise.all([
db.query('UPDATE commit_set_items SET (commitset_patch_file, commitset_requires_build) = ($1, TRUE) WHERE commitset_set = 402 AND commitset_commit = 87832', [fileA.id]),
db.query('UPDATE commit_set_items SET (commitset_patch_file, commitset_requires_build) = ($1, TRUE) WHERE commitset_set = 402 AND commitset_commit = 96336', [fileB.id])
]);
}).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('another.dat', limitInMB, 'c');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then(() => {
return db.selectAll('uploaded_files', 'id');
}).then((rows) => {
assert.strictEqual(rows.length, 3);
assert.strictEqual(rows[0].filename, 'some.patch');
assert.notStrictEqual(rows[0].deleted_at, null);
assert.strictEqual(rows[1].filename, 'other.patch');
assert.strictEqual(rows[1].deleted_at, null);
assert.strictEqual(rows[2].filename, 'another.dat');
assert.strictEqual(rows[2].deleted_at, null);
});
});
it('should delete old build products that belong to finished build requests before deleting patches', () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
let fileA;
return MockData.addMockData(db, ['completed', 'completed', 'failed', 'canceled']).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('some.patch', limitInMB, 'a');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((result) => {
fileA = result.uploadedFile;
return TemporaryFile.makeTemporaryFileOfSizeInMB('root.tar.gz', limitInMB, 'b');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then((result) => {
const fileB = result.uploadedFile;
return db.query(`UPDATE commit_set_items SET (commitset_patch_file, commitset_root_file, commitset_requires_build) = ($1, $2, TRUE)
WHERE commitset_set = 402 AND commitset_commit = 87832`, [fileA.id, fileB.id]);
}).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('another.dat', limitInMB, 'c');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then(() => {
return db.selectAll('uploaded_files', 'id');
}).then((rows) => {
assert.strictEqual(rows.length, 3);
assert.strictEqual(rows[0].filename, 'some.patch');
assert.strictEqual(rows[0].deleted_at, null);
assert.strictEqual(rows[1].filename, 'root.tar.gz');
assert.notStrictEqual(rows[1].deleted_at, null);
assert.strictEqual(rows[2].filename, 'another.dat');
assert.strictEqual(rows[2].deleted_at, null);
});
});
it('should return "FileSizeQuotaExceeded" when the total quota is exceeded due to files uploaded by other users', () => {
const db = TestServer.database();
const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
let fileA;
return MockData.addMockData(db).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', limitInMB, 'a');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB, 'b');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then(() => {
return db.query('UPDATE uploaded_files SET file_author = $1', ['someUser']);
}).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('another.dat', limitInMB, 'c');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
}).then(() => {
return db.query('UPDATE uploaded_files SET file_author = $1 WHERE file_author IS NULL', ['anotherUser']);
}).then(() => {
return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB, 'c');
}).then((stream) => {
return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true}).then(() => {
assert(false, 'should never be reached');
}, (error) => {
assert.strictEqual(error, 'FileSizeQuotaExceeded');
});
});
});
});