@ -24,6 +24,7 @@ describe('Upload Controllers', () => {
let pid ;
let adminUid ;
let regularUid ;
let maliciousUid ;
before ( ( done ) => {
async . series ( {
@ -39,12 +40,16 @@ describe('Upload Controllers', () => {
regularUid : function ( next ) {
user . create ( { username : 'regular' , password : 'zugzug' } , next ) ;
} ,
maliciousUid : function ( next ) {
user . create ( { username : 'malicioususer' , password : 'herpderp' } , next ) ;
} ,
} , ( err , results ) => {
if ( err ) {
return done ( err ) ;
}
adminUid = results . adminUid ;
regularUid = results . regularUid ;
maliciousUid = results . maliciousUid ;
cid = results . category . cid ;
topics . post ( { uid : adminUid , title : 'test topic title' , content : 'test topic content' , cid : results . category . cid } , ( err , result ) => {
@ -132,7 +137,6 @@ describe('Upload Controllers', () => {
} ) ;
} ) ;
it ( 'should upload a file to a post' , ( done ) => {
const oldValue = meta . config . allowedFileExtensions ;
meta . config . allowedFileExtensions = 'png,jpg,bmp,html' ;
@ -157,16 +161,6 @@ describe('Upload Controllers', () => {
} ) ;
} ) ;
it ( 'should fail to upload image to post if image is broken' , ( done ) => {
helpers . uploadFile ( ` ${ nconf . get ( 'url' ) } /api/post/upload ` , path . join ( _ _dirname , '../test/files/brokenimage.png' ) , { } , jar , csrf _token , ( err , res , body ) => {
assert . ifError ( err ) ;
assert . strictEqual ( res . statusCode , 500 ) ;
assert ( body && body . status && body . status . message ) ;
assert ( body . status . message . startsWith ( 'Input file has corrupt header: pngload: end of stream' ) ) ;
done ( ) ;
} ) ;
} ) ;
it ( 'should fail if file is not an image' , ( done ) => {
image . isFileTypeAllowed ( path . join ( _ _dirname , '../test/files/notanimage.png' ) , ( err ) => {
assert . strictEqual ( err . message , 'Input file contains unsupported image format' ) ;
@ -322,6 +316,47 @@ describe('Upload Controllers', () => {
} ) ;
} ) ;
describe ( 'regular user uploads rate limits' , ( ) => {
let jar ;
let csrf _token ;
before ( ( done ) => {
helpers . loginUser ( 'malicioususer' , 'herpderp' , ( err , _jar , _csrf _token ) => {
assert . ifError ( err ) ;
jar = _jar ;
csrf _token = _csrf _token ;
privileges . global . give ( [ 'groups:upload:post:file' ] , 'registered-users' , done ) ;
} ) ;
} ) ;
it ( 'should fail if the user exceeds the upload rate limit threshold' , ( done ) => {
const oldValue = meta . config . allowedFileExtensions ;
meta . config . allowedFileExtensions = 'png,jpg,bmp,html' ;
// why / 2? see: helpers.uploadFile for a weird quirk where we actually upload 2 files per upload in our tests.
async . times ( meta . config . uploadRateLimitThreshold / 2 , ( i , next ) => {
helpers . uploadFile ( ` ${ nconf . get ( 'url' ) } /api/post/upload ` , path . join ( _ _dirname , '../test/files/503.html' ) , { } , jar , csrf _token , ( err , res , body ) => {
if ( i + 1 > meta . config . uploadRateLimitThreshold / 2 ) {
assert . strictEqual ( res . statusCode , 500 ) ;
assert . strictEqual ( body . error , '[[error:upload-ratelimit-reached]]' ) ;
} else {
assert . ifError ( err ) ;
assert . strictEqual ( res . statusCode , 200 ) ;
assert ( body && body . status && body . response && body . response . images ) ;
assert ( Array . isArray ( body . response . images ) ) ;
assert ( body . response . images [ 0 ] . url ) ;
}
next ( err ) ;
} ) ;
} , ( err ) => {
meta . config . allowedFileExtensions = oldValue ;
assert . ifError ( err ) ;
done ( ) ;
} ) ;
} ) ;
} ) ;
describe ( 'admin uploads' , ( ) => {
let jar ;
let csrf _token ;