6927785e980734cb56c63147be7333e4987d1b54
[WebKit-https.git] / Websites / perf.webkit.org / server-tests / privileged-api-upload-file-tests.js
1 'use strict';
2
3 require('../tools/js/v3-models.js');
4
5 const assert = require('assert');
6 global.FormData = require('form-data');
7
8 const TestServer = require('./resources/test-server.js');
9 const TemporaryFile = require('./resources/temporary-file.js').TemporaryFile;
10
11 describe('/privileged-api/upload-file', function () {
12     this.timeout(5000);
13     TestServer.inject();
14
15     TemporaryFile.inject();
16
17     it('should return "NotFileSpecified" when newFile not is specified', () => {
18         return PrivilegedAPI.sendRequest('upload-file', {}, {useFormData: true}).then(() => {
19             assert(false, 'should never be reached');
20         }, (error) => {
21             assert.equal(error, 'NoFileSpecified');
22         });
23     });
24
25     it('should return "FileSizeLimitExceeded" when the file is too big', () => {
26         return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', TestServer.testConfig().uploadFileLimitInMB + 1).then((stream) => {
27             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true}).then(() => {
28                 assert(false, 'should never be reached');
29             }, (error) => {
30                 assert.equal(error, 'FileSizeLimitExceeded');
31             });
32         });
33     });
34
35     it('should upload a file when the filesize is smaller than the limit', () => {
36         const db = TestServer.database();
37         const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
38         let uploadedFile;
39         return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', limitInMB).then((stream) => {
40             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
41         }).then((response) => {
42             uploadedFile = response['uploadedFile'];
43             return db.connect().then(() => db.selectAll('uploaded_files', 'id'));
44         }).then((rows) => {
45             assert.equal(rows.length, 1);
46             assert.equal(rows[0].id, uploadedFile.id);
47             assert.equal(rows[0].size, limitInMB * 1024 * 1024);
48             assert.equal(rows[0].size, uploadedFile.size);
49             assert.equal(rows[0].filename, 'some.dat');
50             assert.equal(rows[0].filename, uploadedFile.filename);
51             assert.equal(rows[0].extension, '.dat');
52             assert.equal(rows[0].sha256, '5256ec18f11624025905d057d6befb03d77b243511ac5f77ed5e0221ce6d84b5');
53             assert.equal(rows[0].sha256, uploadedFile.sha256);
54         });
55     });
56
57     it('should not create a duplicate files when the identical files are uploaded', () => {
58         const db = TestServer.database();
59         const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
60         let uploadedFile1;
61         let uploadedFile2;
62         return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', limitInMB).then((stream) => {
63             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
64         }).then((response) => {
65             uploadedFile1 = response['uploadedFile'];
66             return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB);
67         }).then((stream) => {
68             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
69         }).then((response) => {
70             uploadedFile2 = response['uploadedFile'];
71             return db.connect().then(() => db.selectAll('uploaded_files', 'id'));
72         }).then((rows) => {
73             assert.deepEqual(uploadedFile1, uploadedFile2);
74             assert.equal(rows.length, 1);
75             assert.equal(rows[0].id, uploadedFile1.id);
76             assert.equal(rows[0].size, limitInMB * 1024 * 1024);
77             assert.equal(rows[0].size, uploadedFile1.size);
78             assert.equal(rows[0].filename, 'some.dat');
79             assert.equal(rows[0].filename, uploadedFile1.filename);
80             assert.equal(rows[0].extension, '.dat');
81             assert.equal(rows[0].sha256, '5256ec18f11624025905d057d6befb03d77b243511ac5f77ed5e0221ce6d84b5');
82             assert.equal(rows[0].sha256, uploadedFile1.sha256);
83         });
84     });
85
86     it('should not create a duplicate files when the identical files are uploaded', () => {
87         const db = TestServer.database();
88         const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
89         let uploadedFile1;
90         let uploadedFile2;
91         return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', limitInMB).then((stream) => {
92             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
93         }).then((response) => {
94             uploadedFile1 = response['uploadedFile'];
95             return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB);
96         }).then((stream) => {
97             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
98         }).then((response) => {
99             uploadedFile2 = response['uploadedFile'];
100             return db.connect().then(() => db.selectAll('uploaded_files', 'id'));
101         }).then((rows) => {
102             assert.deepEqual(uploadedFile1, uploadedFile2);
103             assert.equal(rows.length, 1);
104             assert.equal(rows[0].id, uploadedFile1.id);
105             assert.equal(rows[0].size, limitInMB * 1024 * 1024);
106             assert.equal(rows[0].size, uploadedFile1.size);
107             assert.equal(rows[0].filename, 'some.dat');
108             assert.equal(rows[0].filename, uploadedFile1.filename);
109             assert.equal(rows[0].extension, '.dat');
110             assert.equal(rows[0].sha256, '5256ec18f11624025905d057d6befb03d77b243511ac5f77ed5e0221ce6d84b5');
111             assert.equal(rows[0].sha256, uploadedFile1.sha256);
112         });
113     });
114
115     it('should re-upload the file when the previously uploaded file had been deleted', () => {
116         const db = TestServer.database();
117         const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
118         let uploadedFile1;
119         let uploadedFile2;
120         return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', limitInMB).then((stream) => {
121             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
122         }).then((response) => {
123             uploadedFile1 = response['uploadedFile'];
124             return db.connect().then(() => db.query(`UPDATE uploaded_files SET file_deleted_at = now() at time zone 'utc'`));
125         }).then(() => {
126             return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB);
127         }).then((stream) => {
128             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
129         }).then((response) => {
130             uploadedFile2 = response['uploadedFile'];
131             return db.connect().then(() => db.selectAll('uploaded_files', 'id'));
132         }).then((rows) => {
133             assert.notEqual(uploadedFile1.id, uploadedFile2.id);
134             assert.equal(rows.length, 2);
135             assert.equal(rows[0].id, uploadedFile1.id);
136             assert.equal(rows[1].id, uploadedFile2.id);
137
138             assert.equal(rows[0].filename, 'some.dat');
139             assert.equal(rows[0].filename, uploadedFile1.filename);
140             assert.equal(rows[1].filename, 'other.dat');
141             assert.equal(rows[1].filename, uploadedFile2.filename);
142
143             assert.equal(rows[0].size, limitInMB * 1024 * 1024);
144             assert.equal(rows[0].size, uploadedFile1.size);
145             assert.equal(rows[0].size, uploadedFile2.size);
146             assert.equal(rows[0].size, rows[1].size);
147             assert.equal(rows[0].sha256, '5256ec18f11624025905d057d6befb03d77b243511ac5f77ed5e0221ce6d84b5');
148             assert.equal(rows[0].sha256, uploadedFile1.sha256);
149             assert.equal(rows[0].sha256, uploadedFile2.sha256);
150             assert.equal(rows[0].sha256, rows[1].sha256);
151             assert.equal(rows[0].extension, '.dat');
152             assert.equal(rows[1].extension, '.dat');
153         });
154     });
155
156     it('should pick up at most two file extensions', () => {
157         const db = TestServer.database();
158         const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
159         return TemporaryFile.makeTemporaryFileOfSizeInMB('some.other.tar.gz', limitInMB).then((stream) => {
160             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
161         }).then(() => {
162             return db.connect().then(() => db.selectAll('uploaded_files', 'id'))
163         }).then((rows) => {
164             assert.equal(rows.length, 1);
165             assert.equal(rows[0].size, limitInMB * 1024 * 1024);
166             assert.equal(rows[0].mime, 'application/octet-stream');
167             assert.equal(rows[0].filename, 'some.other.tar.gz');
168             assert.equal(rows[0].extension, '.tar.gz');
169             assert.equal(rows[0].sha256, '5256ec18f11624025905d057d6befb03d77b243511ac5f77ed5e0221ce6d84b5');
170         });
171     });
172
173     it('should return "FileSizeQuotaExceeded" when the total file size exceeds the quota allowed per user', () => {
174         const db = TestServer.database();
175         const limitInMB = TestServer.testConfig().uploadFileLimitInMB;
176         return TemporaryFile.makeTemporaryFileOfSizeInMB('some.dat', limitInMB, 'a').then((stream) => {
177             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
178         }).then(() => {
179             return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB, 'b');
180         }).then((stream) => {
181             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true});
182         }).then(() => {
183             return TemporaryFile.makeTemporaryFileOfSizeInMB('other.dat', limitInMB, 'c');
184         }).then((stream) => {
185             return PrivilegedAPI.sendRequest('upload-file', {newFile: stream}, {useFormData: true}).then(() => {
186                 assert(false, 'should never be reached');
187             }, (error) => {
188                 assert.equal(error, 'FileSizeQuotaExceeded');
189             });
190         });
191     });
192 });