| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							|  |  |  | var	assert = require('assert'); | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | var path = require('path'); | 
					
						
							| 
									
										
										
										
											2017-06-28 15:02:17 +02:00
										 |  |  | var request = require('request'); | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | var db = require('./mocks/databasemock'); | 
					
						
							|  |  |  | var categories = require('../src/categories'); | 
					
						
							|  |  |  | var topics = require('../src/topics'); | 
					
						
							|  |  |  | var user = require('../src/user'); | 
					
						
							|  |  |  | var groups = require('../src/groups'); | 
					
						
							|  |  |  | var privileges = require('../src/privileges'); | 
					
						
							|  |  |  | var meta = require('../src/meta'); | 
					
						
							| 
									
										
										
										
											2017-10-31 18:03:54 -04:00
										 |  |  | var socketUser = require('../src/socket.io/user'); | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | var helpers = require('./helpers'); | 
					
						
							| 
									
										
										
										
											2018-09-20 17:05:52 -04:00
										 |  |  | var file = require('../src/file'); | 
					
						
							|  |  |  | var image = require('../src/image'); | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('Upload Controllers', function () { | 
					
						
							|  |  |  | 	var tid; | 
					
						
							|  |  |  | 	var cid; | 
					
						
							|  |  |  | 	var pid; | 
					
						
							|  |  |  | 	var adminUid; | 
					
						
							|  |  |  | 	var regularUid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	before(function (done) { | 
					
						
							|  |  |  | 		async.series({ | 
					
						
							|  |  |  | 			category: function (next) { | 
					
						
							|  |  |  | 				categories.create({ | 
					
						
							|  |  |  | 					name: 'Test Category', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					description: 'Test category created by testing script', | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			adminUid: function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				user.create({ username: 'admin', password: 'barbar' }, next); | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			regularUid: function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				user.create({ username: 'regular', password: 'zugzug' }, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 		}, function (err, results) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return done(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			adminUid = results.adminUid; | 
					
						
							|  |  |  | 			regularUid = results.regularUid; | 
					
						
							|  |  |  | 			cid = results.category.cid; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.post({ uid: adminUid, title: 'test topic title', content: 'test topic content', cid: results.category.cid }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2020-01-19 14:57:06 -05:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return done(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 				tid = result.topicData.tid; | 
					
						
							|  |  |  | 				pid = result.postData.pid; | 
					
						
							| 
									
										
										
										
											2020-01-19 14:57:06 -05:00
										 |  |  | 				groups.join('administrators', adminUid, done); | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('regular user uploads', function () { | 
					
						
							|  |  |  | 		var jar; | 
					
						
							|  |  |  | 		var csrf_token; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2017-06-02 16:51:36 -04:00
										 |  |  | 			helpers.loginUser('regular', 'zugzug', function (err, _jar, _csrf_token) { | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				jar = _jar; | 
					
						
							|  |  |  | 				csrf_token = _csrf_token; | 
					
						
							| 
									
										
										
										
											2020-05-26 21:57:38 -04:00
										 |  |  | 				privileges.global.give(['groups:upload:post:file'], 'registered-users', done); | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should upload an image to a post', function (done) { | 
					
						
							| 
									
										
										
										
											2018-01-03 13:27:30 -05:00
										 |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/post/upload', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert(Array.isArray(body)); | 
					
						
							|  |  |  | 				assert(body[0].url); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 12:49:29 -04:00
										 |  |  | 		it('should upload an image to a post and then delete the upload', function (done) { | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/post/upload', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert(Array.isArray(body)); | 
					
						
							|  |  |  | 				assert(body[0].url); | 
					
						
							| 
									
										
										
										
											2018-11-30 21:35:57 -05:00
										 |  |  | 				var name = body[0].url.replace(nconf.get('relative_path') + nconf.get('upload_url'), ''); | 
					
						
							| 
									
										
										
										
											2018-05-26 12:49:29 -04:00
										 |  |  | 				socketUser.deleteUpload({ uid: regularUid }, { uid: regularUid, name: name }, function (err) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					db.getSortedSetRange('uid:' + regularUid + ':uploads', 0, -1, function (err, uploads) { | 
					
						
							|  |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert.equal(uploads.includes(name), false); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 14:57:06 -05:00
										 |  |  | 		it('should not allow deleting if path is not correct', function (done) { | 
					
						
							|  |  |  | 			socketUser.deleteUpload({ uid: adminUid }, { uid: regularUid, name: '../../bkconfig.json' }, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:invalid-path]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should not allow deleting if path is not correct', function (done) { | 
					
						
							|  |  |  | 			socketUser.deleteUpload({ uid: adminUid }, { uid: regularUid, name: '/files/../../bkconfig.json' }, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:invalid-path]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 17:34:34 +03:00
										 |  |  | 		it('should resize and upload an image to a post', function (done) { | 
					
						
							| 
									
										
										
										
											2018-10-24 20:03:03 -04:00
										 |  |  | 			var oldValue = meta.config.resizeImageWidth; | 
					
						
							|  |  |  | 			meta.config.resizeImageWidth = 10; | 
					
						
							|  |  |  | 			meta.config.resizeImageWidthThreshold = 10; | 
					
						
							| 
									
										
										
										
											2018-01-03 13:27:30 -05:00
										 |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/post/upload', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							| 
									
										
										
										
											2017-02-24 17:34:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert(Array.isArray(body)); | 
					
						
							|  |  |  | 				assert(body[0].url); | 
					
						
							| 
									
										
										
										
											2018-09-20 22:09:20 -04:00
										 |  |  | 				assert(body[0].url.match(/\/assets\/uploads\/files\/\d+-test-resized\.png/)); | 
					
						
							| 
									
										
										
										
											2018-10-24 20:03:03 -04:00
										 |  |  | 				meta.config.resizeImageWidth = oldValue; | 
					
						
							|  |  |  | 				meta.config.resizeImageWidthThreshold = 1520; | 
					
						
							| 
									
										
										
										
											2017-02-24 17:34:34 +03:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should upload a file to a post', function (done) { | 
					
						
							| 
									
										
										
										
											2017-04-14 13:38:58 -04:00
										 |  |  | 			var oldValue = meta.config.allowedFileExtensions; | 
					
						
							|  |  |  | 			meta.config.allowedFileExtensions = 'png,jpg,bmp,html'; | 
					
						
							| 
									
										
										
										
											2018-01-03 13:27:30 -05:00
										 |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/post/upload', path.join(__dirname, '../test/files/503.html'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							| 
									
										
										
										
											2017-04-14 13:38:58 -04:00
										 |  |  | 				meta.config.allowedFileExtensions = oldValue; | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert(Array.isArray(body)); | 
					
						
							|  |  |  | 				assert(body[0].url); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 17:05:52 -04:00
										 |  |  | 		it('should fail to upload image to post if image dimensions are too big', function (done) { | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/post/upload', path.join(__dirname, '../test/files/toobig.jpg'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 500); | 
					
						
							|  |  |  | 				assert(body.error, '[[error:invalid-image-dimensions]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail to upload image to post if image is broken', function (done) { | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/post/upload', path.join(__dirname, '../test/files/brokenimage.png'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 500); | 
					
						
							|  |  |  | 				assert(body.error, 'invalid block type'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail if file is not an image', function (done) { | 
					
						
							|  |  |  | 			file.isFileTypeAllowed(path.join(__dirname, '../test/files/notanimage.png'), function (err) { | 
					
						
							| 
									
										
										
										
											2019-01-19 12:44:14 -05:00
										 |  |  | 				assert.equal(err.message, 'Input file contains unsupported image format'); | 
					
						
							| 
									
										
										
										
											2018-09-20 17:05:52 -04:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 19:53:03 -04:00
										 |  |  | 		it('should fail if file is not an image', function (done) { | 
					
						
							|  |  |  | 			image.isFileTypeAllowed(path.join(__dirname, '../test/files/notanimage.png'), function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, 'Input file contains unsupported image format'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 17:05:52 -04:00
										 |  |  | 		it('should fail if file is not an image', function (done) { | 
					
						
							|  |  |  | 			image.size(path.join(__dirname, '../test/files/notanimage.png'), function (err) { | 
					
						
							| 
									
										
										
										
											2019-01-19 12:44:14 -05:00
										 |  |  | 				assert.equal(err.message, 'Input file contains unsupported image format'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail if file is missing', function (done) { | 
					
						
							|  |  |  | 			image.size(path.join(__dirname, '../test/files/doesnotexist.png'), function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, 'Input file is missing'); | 
					
						
							| 
									
										
										
										
											2018-09-20 17:05:52 -04:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 17:34:34 +03:00
										 |  |  | 		it('should fail if topic thumbs are disabled', function (done) { | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/topic/thumb/upload', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 500); | 
					
						
							|  |  |  | 				assert.equal(body.error, '[[error:topic-thumbnails-are-disabled]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail if file is not image', function (done) { | 
					
						
							|  |  |  | 			meta.config.allowTopicsThumbnail = 1; | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/topic/thumb/upload', path.join(__dirname, '../test/files/503.html'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 500); | 
					
						
							|  |  |  | 				assert.equal(body.error, '[[error:invalid-file]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should upload topic thumb', function (done) { | 
					
						
							|  |  |  | 			meta.config.allowTopicsThumbnail = 1; | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/topic/thumb/upload', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert(Array.isArray(body)); | 
					
						
							|  |  |  | 				assert(body[0].path); | 
					
						
							|  |  |  | 				assert(body[0].url); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 18:03:54 -04:00
										 |  |  | 		it('should not allow non image uploads', function (done) { | 
					
						
							|  |  |  | 			socketUser.updateCover({ uid: 1 }, { uid: 1, imageData: 'data:text/html;base64,PHN2Zy9vbmxvYWQ9YWxlcnQoMik+' }, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:invalid-image]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 23:10:49 -04:00
										 |  |  | 		it('should not allow svg uploads', function (done) { | 
					
						
							|  |  |  | 			socketUser.updateCover({ uid: 1 }, { uid: 1, imageData: 'data:image/svg;base64,PHN2Zy9vbmxvYWQ9YWxlcnQoMik+' }, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:invalid-image]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 18:03:54 -04:00
										 |  |  | 		it('should not allow non image uploads', function (done) { | 
					
						
							|  |  |  | 			socketUser.uploadCroppedPicture({ uid: 1 }, { uid: 1, imageData: 'data:text/html;base64,PHN2Zy9vbmxvYWQ9YWxlcnQoMik+' }, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:invalid-image]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-04-06 15:16:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 23:10:49 -04:00
										 |  |  | 		it('should not allow svg uploads', function (done) { | 
					
						
							|  |  |  | 			socketUser.uploadCroppedPicture({ uid: 1 }, { uid: 1, imageData: 'data:image/svg;base64,PHN2Zy9vbmxvYWQ9YWxlcnQoMik+' }, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:invalid-image]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:16:28 -04:00
										 |  |  | 		it('should delete users uploads if account is deleted', function (done) { | 
					
						
							|  |  |  | 			var jar; | 
					
						
							|  |  |  | 			var uid; | 
					
						
							|  |  |  | 			var url; | 
					
						
							|  |  |  | 			var file = require('../src/file'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					user.create({ username: 'uploader', password: 'barbar' }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (_uid, next) { | 
					
						
							|  |  |  | 					uid = _uid; | 
					
						
							|  |  |  | 					helpers.loginUser('uploader', 'barbar', next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (jar, csrf_token, next) { | 
					
						
							|  |  |  | 					helpers.uploadFile(nconf.get('url') + '/api/post/upload', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (res, body, next) { | 
					
						
							|  |  |  | 					assert(body); | 
					
						
							|  |  |  | 					assert(body[0].url); | 
					
						
							|  |  |  | 					url = body[0].url; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					user.delete(1, uid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2018-10-04 11:08:01 -04:00
										 |  |  | 				function (userData, next) { | 
					
						
							| 
									
										
										
										
											2018-04-06 15:16:28 -04:00
										 |  |  | 					var filePath = path.join(nconf.get('upload_path'), url.replace('/assets/uploads', '')); | 
					
						
							|  |  |  | 					file.exists(filePath, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (exists, next) { | 
					
						
							|  |  |  | 					assert(!exists); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-10-31 18:03:54 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	describe('admin uploads', function () { | 
					
						
							|  |  |  | 		var jar; | 
					
						
							|  |  |  | 		var csrf_token; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2017-06-02 16:51:36 -04:00
										 |  |  | 			helpers.loginUser('admin', 'barbar', function (err, _jar, _csrf_token) { | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				jar = _jar; | 
					
						
							|  |  |  | 				csrf_token = _csrf_token; | 
					
						
							| 
									
										
										
										
											2020-01-19 14:57:06 -05:00
										 |  |  | 				done(); | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should upload site logo', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-20 21:27:56 +03:00
										 |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/admin/uploadlogo', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert(Array.isArray(body)); | 
					
						
							| 
									
										
										
										
											2017-02-25 16:04:04 +03:00
										 |  |  | 				assert.equal(body[0].url, nconf.get('relative_path') + '/assets/uploads/system/site-logo.png'); | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-07 14:45:44 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:34:36 -04:00
										 |  |  | 		it('should fail to upload invalid file type', function (done) { | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/admin/category/uploadpicture', path.join(__dirname, '../test/files/503.html'), { params: JSON.stringify({ cid: cid }) }, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(body.error, '[[error:invalid-image-type, image/png, image/jpeg, image/pjpeg, image/jpg, image/gif, image/svg+xml]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail to upload category image with invalid json params', function (done) { | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/admin/category/uploadpicture', path.join(__dirname, '../test/files/test.png'), { params: 'invalid json' }, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-05-10 15:51:19 -04:00
										 |  |  | 				assert.equal(body.error, '[[error:invalid-json]]'); | 
					
						
							| 
									
										
										
										
											2017-05-10 15:34:36 -04:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 14:45:44 +03:00
										 |  |  | 		it('should upload category image', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/admin/category/uploadpicture', path.join(__dirname, '../test/files/test.png'), { params: JSON.stringify({ cid: cid }) }, jar, csrf_token, function (err, res, body) { | 
					
						
							| 
									
										
										
										
											2016-11-07 14:45:44 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert(Array.isArray(body)); | 
					
						
							| 
									
										
										
										
											2017-02-25 16:04:04 +03:00
										 |  |  | 				assert.equal(body[0].url, nconf.get('relative_path') + '/assets/uploads/category/category-1.png'); | 
					
						
							| 
									
										
										
										
											2016-11-07 14:45:44 +03:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-10 15:34:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail to upload invalid sound file', function (done) { | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/admin/upload/sound', path.join(__dirname, '../test/files/test.png'), { }, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 500); | 
					
						
							|  |  |  | 				assert.equal(body.error, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should upload sound file', function (done) { | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/admin/upload/sound', path.join(__dirname, '../test/files/test.wav'), { }, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-10 16:27:44 -04:00
										 |  |  | 		it('should upload default avatar', function (done) { | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/admin/uploadDefaultAvatar', path.join(__dirname, '../test/files/test.png'), { }, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert.equal(body[0].url, nconf.get('relative_path') + '/assets/uploads/system/avatar-default.png'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should upload og image', function (done) { | 
					
						
							|  |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/admin/uploadOgImage', path.join(__dirname, '../test/files/test.png'), { }, jar, csrf_token, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert.equal(body[0].url, nconf.get('relative_path') + '/assets/uploads/system/og-image.png'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 14:45:44 +03:00
										 |  |  | 		it('should upload favicon', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-20 21:27:56 +03:00
										 |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/admin/uploadfavicon', path.join(__dirname, '../test/files/favicon.ico'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							| 
									
										
										
										
											2016-11-07 14:45:44 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert(Array.isArray(body)); | 
					
						
							| 
									
										
										
										
											2017-02-08 11:41:24 -07:00
										 |  |  | 				assert.equal(body[0].url, '/assets/uploads/system/favicon.ico'); | 
					
						
							| 
									
										
										
										
											2016-11-07 14:45:44 +03:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should upload touch icon', function (done) { | 
					
						
							| 
									
										
										
										
											2017-06-28 15:02:17 +02:00
										 |  |  | 			var touchiconAssetPath = '/assets/uploads/system/touchicon-orig.png'; | 
					
						
							| 
									
										
										
										
											2017-02-20 21:27:56 +03:00
										 |  |  | 			helpers.uploadFile(nconf.get('url') + '/api/admin/uploadTouchIcon', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) { | 
					
						
							| 
									
										
										
										
											2016-11-07 14:45:44 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert(Array.isArray(body)); | 
					
						
							| 
									
										
										
										
											2017-06-28 15:02:17 +02:00
										 |  |  | 				assert.equal(body[0].url, touchiconAssetPath); | 
					
						
							|  |  |  | 				meta.config['brand:touchIcon'] = touchiconAssetPath; | 
					
						
							|  |  |  | 				request(nconf.get('url') + '/apple-touch-icon', function (err, res, body) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 					assert(body); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-11-07 14:45:44 +03:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-01 17:33:19 +03:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }); |