2013-06-19 15:06:23 -04:00
module ( "Discourse.Utilities" ) ;
var utils = Discourse . Utilities ;
test ( "emailValid" , function ( ) {
ok ( utils . emailValid ( 'Bob@example.com' ) , "allows upper case in the first part of emails" ) ;
ok ( utils . emailValid ( 'bob@EXAMPLE.com' ) , "allows upper case in the email domain" ) ;
} ) ;
var validUpload = utils . validateFilesForUpload ;
test ( "validateFilesForUpload" , function ( ) {
ok ( ! validUpload ( null ) , "no files are invalid" ) ;
ok ( ! validUpload ( undefined ) , "undefined files are invalid" ) ;
ok ( ! validUpload ( [ ] ) , "empty array of files is invalid" ) ;
} ) ;
test ( "uploading one file" , function ( ) {
this . stub ( bootbox , "alert" ) ;
ok ( ! validUpload ( [ 1 , 2 ] ) ) ;
2013-07-01 02:19:03 +02:00
ok ( bootbox . alert . calledWith ( Em . String . i18n ( 'post.errors.too_many_uploads' ) ) ) ;
2013-06-19 15:06:23 -04:00
} ) ;
2013-07-01 02:19:03 +02:00
test ( "ensures an authorized upload" , function ( ) {
var html = { name : "unauthorized.html" } ;
var extensions = Discourse . SiteSettings . authorized _extensions . replace ( /\|/g , ", " ) ;
2013-06-19 15:06:23 -04:00
this . stub ( bootbox , "alert" ) ;
ok ( ! validUpload ( [ html ] ) ) ;
2013-07-01 02:19:03 +02:00
ok ( bootbox . alert . calledWith ( Em . String . i18n ( 'post.errors.upload_not_authorized' , { authorized _extensions : extensions } ) ) ) ;
2013-06-19 15:06:23 -04:00
} ) ;
test ( "prevents files that are too big from being uploaded" , function ( ) {
2013-07-01 02:19:03 +02:00
var image = { name : "image.png" , size : 10 * 1024 } ;
2013-06-19 15:06:23 -04:00
Discourse . SiteSettings . max _upload _size _kb = 5 ;
this . stub ( bootbox , "alert" ) ;
ok ( ! validUpload ( [ image ] ) ) ;
2013-07-01 02:19:03 +02:00
ok ( bootbox . alert . calledWith ( Em . String . i18n ( 'post.errors.upload_too_large' , { max _size _kb : 5 } ) ) ) ;
2013-06-19 15:06:23 -04:00
} ) ;
test ( "allows valid uploads to go through" , function ( ) {
2013-07-01 02:19:03 +02:00
var image = { name : "image.png" , size : 10 * 1024 } ;
2013-06-19 15:06:23 -04:00
Discourse . SiteSettings . max _upload _size _kb = 15 ;
this . stub ( bootbox , "alert" ) ;
ok ( validUpload ( [ image ] ) ) ;
ok ( ! bootbox . alert . calledOnce ) ;
} ) ;
2013-07-01 02:19:03 +02:00
var isAuthorized = function ( filename ) {
return utils . isAuthorizedUpload ( { name : filename } ) ;
} ;
test ( "isAuthorizedUpload" , function ( ) {
ok ( isAuthorized ( "image.png" ) ) ;
ok ( isAuthorized ( "image.jpg" ) ) ;
ok ( ! isAuthorized ( "image.txt" ) ) ;
ok ( ! isAuthorized ( "" ) ) ;
} ) ;