1 | var t = require('../test-lib/test.js');
|
2 | var assert = require('assert');
|
3 |
|
4 | var apos;
|
5 |
|
6 | describe('Attachment', function() {
|
7 |
|
8 | after(function(done) {
|
9 | return t.destroy(apos, done);
|
10 | });
|
11 |
|
12 | this.timeout(t.timeout);
|
13 |
|
14 | var uploadSource = __dirname + "/data/upload_tests/";
|
15 | var uploadTarget = __dirname + "/public/uploads/attachments/";
|
16 | var collectionName = 'aposAttachments';
|
17 |
|
18 | function wipeIt(callback) {
|
19 | deleteFolderRecursive(__dirname + '/public/uploads');
|
20 |
|
21 | function deleteFolderRecursive (path) {
|
22 | var files = [];
|
23 | if (fs.existsSync(path)) {
|
24 | files = fs.readdirSync(path);
|
25 | files.forEach(function(file, index) {
|
26 | var curPath = path + "/" + file;
|
27 | if (fs.lstatSync(curPath).isDirectory()) {
|
28 | deleteFolderRecursive(curPath);
|
29 | } else {
|
30 | fs.unlinkSync(curPath);
|
31 | }
|
32 | });
|
33 | fs.rmdirSync(path);
|
34 | }
|
35 | }
|
36 |
|
37 | apos.db.collection(collectionName, function(err, collection) {
|
38 | assert(!err);
|
39 | assert(collection);
|
40 | collection.remove({}, callback);
|
41 | });
|
42 |
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 | it('should be a property of the apos object', function(done) {
|
48 | this.timeout(t.timeout);
|
49 | this.slow(2000);
|
50 |
|
51 | apos = require('../index.js')({
|
52 | root: module,
|
53 | shortName: 'test',
|
54 |
|
55 | modules: {
|
56 | 'apostrophe-express': {
|
57 | port: 7900
|
58 | }
|
59 | },
|
60 | afterInit: function(callback) {
|
61 | assert(apos.attachments);
|
62 |
|
63 |
|
64 |
|
65 | apos.argv._ = [];
|
66 | return callback(null);
|
67 | },
|
68 | afterListen: function(err) {
|
69 | assert(!err);
|
70 | done();
|
71 | }
|
72 | });
|
73 | });
|
74 |
|
75 | describe('wipe', function() {
|
76 | it('should clear previous material if any', function(done) {
|
77 | wipeIt(done);
|
78 | });
|
79 | });
|
80 |
|
81 | var fs = require('fs');
|
82 |
|
83 | describe('accept', function() {
|
84 |
|
85 | function accept(filename, callback) {
|
86 | return apos.attachments.insert(apos.tasks.getReq(), {
|
87 | name: filename,
|
88 | path: uploadSource + filename
|
89 | }, function(err, info) {
|
90 | assert(!err);
|
91 | var t = uploadTarget + info._id + '-' + info.name + '.' + info.extension;
|
92 |
|
93 | assert(fs.existsSync(t));
|
94 |
|
95 |
|
96 | apos.db.collection(collectionName).findOne({
|
97 | _id: info._id
|
98 | }, function(err, result) {
|
99 | assert(!err);
|
100 | assert(result);
|
101 |
|
102 | return callback(result);
|
103 | });
|
104 | });
|
105 | }
|
106 |
|
107 | it('should upload a text file using the attachments api when user', function(done) {
|
108 | return accept('upload_apos_api.txt', function(result) {
|
109 | done();
|
110 | });
|
111 | });
|
112 |
|
113 | it('should upload an image file using the attachments api when user', function(done) {
|
114 | return accept('upload_image.png', function(result) {
|
115 | done();
|
116 | });
|
117 | });
|
118 |
|
119 | it('should not upload an exe file', function(done) {
|
120 | var filename = 'bad_file.exe';
|
121 |
|
122 | return apos.attachments.insert(apos.tasks.getReq(), {
|
123 | name: filename,
|
124 | path: uploadSource + filename
|
125 | }, function(err, info) {
|
126 | assert(err);
|
127 | assert(!info);
|
128 | done();
|
129 | });
|
130 | });
|
131 |
|
132 | it('should crop an image file when user', function(done) {
|
133 | return accept('crop_image.png', function(result) {
|
134 | var crop = { top: 10, left: 10, width: 80, height: 80 };
|
135 |
|
136 | return apos.attachments.crop(
|
137 | apos.tasks.getReq(),
|
138 | result._id,
|
139 | crop,
|
140 | function(err) {
|
141 | assert(!err);
|
142 |
|
143 |
|
144 | apos.db.collection(collectionName).findOne({
|
145 | _id: result._id
|
146 | }, function(err, result) {
|
147 | assert(!err);
|
148 | assert(result);
|
149 | assert(result.crops.length);
|
150 | var t = uploadTarget + result._id + '-' + result.name + '.' + result.crops[0].left + '.' + result.crops[0].top + '.' + result.crops[0].width + '.' + result.crops[0].height + '.' + result.extension;
|
151 | assert(fs.existsSync(t));
|
152 |
|
153 | done();
|
154 |
|
155 | });
|
156 | }
|
157 | );
|
158 | });
|
159 | });
|
160 |
|
161 | it('should clone an attachment', function(done) {
|
162 | return accept('clone.txt', function(result) {
|
163 |
|
164 | return apos.attachments.clone(apos.tasks.getReq(), result, function(err, targetInfo) {
|
165 | assert(!err);
|
166 | assert(targetInfo._id !== result._id);
|
167 |
|
168 |
|
169 | apos.db.collection(collectionName).findOne({
|
170 | _id: result._id
|
171 | }, function(err, result) {
|
172 | assert(!err);
|
173 | assert(result);
|
174 | var t = uploadTarget + result._id + '-' + result.name + '.' + result.extension;
|
175 | assert(fs.existsSync(t));
|
176 |
|
177 | done();
|
178 | });
|
179 | });
|
180 | });
|
181 | });
|
182 |
|
183 | it('should generate the "full" URL when no size specified for image', function() {
|
184 | var url = apos.attachments.url({
|
185 | group: 'images',
|
186 | name: 'test',
|
187 | extension: 'jpg',
|
188 | _id: 'test'
|
189 | });
|
190 | assert(url === '/uploads/attachments/test-test.full.jpg');
|
191 | });
|
192 |
|
193 | it('should generate the "one-half" URL when one-half size specified for image', function() {
|
194 | var url = apos.attachments.url({
|
195 | group: 'images',
|
196 | name: 'test',
|
197 | extension: 'jpg',
|
198 | _id: 'test'
|
199 | }, {
|
200 | size: 'one-half'
|
201 | });
|
202 | assert(url === '/uploads/attachments/test-test.one-half.jpg');
|
203 | });
|
204 |
|
205 | it('should generate the original URL when "original" size specified for image', function() {
|
206 | var url = apos.attachments.url({
|
207 | group: 'images',
|
208 | name: 'test',
|
209 | extension: 'jpg',
|
210 | _id: 'test'
|
211 | }, {
|
212 | size: 'original'
|
213 | });
|
214 | assert(url === '/uploads/attachments/test-test.jpg');
|
215 | });
|
216 |
|
217 | it('should generate the original URL when no size specified for pdf', function() {
|
218 | var url = apos.attachments.url({
|
219 | group: 'office',
|
220 | name: 'test',
|
221 | extension: 'pdf',
|
222 | _id: 'test'
|
223 | });
|
224 | assert(url === '/uploads/attachments/test-test.pdf');
|
225 | });
|
226 |
|
227 | it('should save and track docIds properly as part of an apostrophe-image', function() {
|
228 | var image = apos.images.newInstance();
|
229 | var req = apos.tasks.getReq();
|
230 | return apos.attachments.insert(apos.tasks.getReq(), {
|
231 | name: 'upload_image.png',
|
232 | path: uploadSource + 'upload_image.png'
|
233 | })
|
234 | .then(function(attachment) {
|
235 | assert(attachment);
|
236 | image.title = 'Test Image';
|
237 | image.attachment = attachment;
|
238 | return apos.images.insert(req, image);
|
239 | })
|
240 | .then(function(image) {
|
241 | assert(image);
|
242 | return apos.attachments.db.findOne({ _id: image.attachment._id });
|
243 | })
|
244 | .then(function(attachment) {
|
245 | assert(attachment.trash === false);
|
246 | assert(attachment.docIds);
|
247 | assert(attachment.docIds.length === 1);
|
248 | assert(attachment.docIds[0] === image._id);
|
249 | assert(attachment.trashDocIds);
|
250 | assert(attachment.trashDocIds.length === 0);
|
251 | try {
|
252 | var fd = fs.openSync(apos.rootDir + '/public' + apos.attachments.url(attachment, { size: 'original' }), 'r');
|
253 | assert(fd);
|
254 | fs.closeSync(fd);
|
255 | } catch (e) {
|
256 | assert(false);
|
257 | }
|
258 | return apos.images.trash(req, image._id);
|
259 | })
|
260 | .then(function() {
|
261 | return apos.attachments.db.findOne({ _id: image.attachment._id });
|
262 | })
|
263 | .then(function(attachment) {
|
264 | assert(attachment.trash);
|
265 | assert(attachment.docIds.length === 0);
|
266 | assert(attachment.trashDocIds.length === 1);
|
267 | try {
|
268 | fs.openSync(apos.rootDir + '/public' + apos.attachments.url(attachment, { size: 'original' }), 'r');
|
269 | } catch (e) {
|
270 | return true;
|
271 | }
|
272 | throw new Error('should not have been accessible');
|
273 | })
|
274 | .then(function() {
|
275 | return apos.images.rescue(req, image._id);
|
276 | })
|
277 | .then(function() {
|
278 | return apos.attachments.db.findOne({ _id: image.attachment._id });
|
279 | })
|
280 | .then(function(attachment) {
|
281 | assert(!attachment.trash);
|
282 | assert(attachment.docIds.length === 1);
|
283 | assert(attachment.trashDocIds.length === 0);
|
284 | try {
|
285 | var fd = fs.openSync(apos.rootDir + '/public' + apos.attachments.url(attachment, { size: 'original' }), 'r');
|
286 | assert(fd);
|
287 | fs.closeSync(fd);
|
288 | } catch (e) {
|
289 | assert(false);
|
290 | }
|
291 | });
|
292 | });
|
293 |
|
294 | });
|
295 |
|
296 | });
|