@@ -24,7 +24,8 @@ const {
2424 UNIQUE_JOB_SUFFIX_ID ,
2525 PRESETS ,
2626 TRANSFORMATIONS ,
27- PUBLIC_ID_PREFIX
27+ PUBLIC_ID_PREFIX ,
28+ UNIQUE_TEST_FOLDER ,
2829} = testConstants ;
2930
3031const {
@@ -905,83 +906,96 @@ describe("api", function () {
905906 ) ) ;
906907 } ) ) ;
907908 } ) ;
908- // For this test to work, "Auto-create folders" should be enabled in the Upload Settings.
909- // Replace `it` with `it.skip` below if you want to disable it.
910- it ( "should list folders in cloudinary" , function ( ) {
911- this . timeout ( TIMEOUT . LONG ) ;
912- return Q . all ( [
913- uploadImage ( {
914- public_id : 'test_folder1/item' ,
915- tags : UPLOAD_TAGS ,
916- } ) ,
917- uploadImage ( {
918- public_id : 'test_folder2/item' ,
919- tags : UPLOAD_TAGS ,
920- } ) ,
921- uploadImage ( {
922- public_id : 'test_folder2/item' ,
923- tags : UPLOAD_TAGS ,
924- } ) ,
925- uploadImage ( {
926- public_id : 'test_folder1/test_subfolder1/item' ,
927- tags : UPLOAD_TAGS ,
928- } ) ,
929- uploadImage ( {
930- public_id : 'test_folder1/test_subfolder2/item' ,
931- tags : UPLOAD_TAGS ,
932- } ) ,
933- ] ) . then ( wait ( 3000 ) ) . then ( function ( results ) {
934- return Q . all ( [ cloudinary . v2 . api . root_folders ( ) , cloudinary . v2 . api . sub_folders ( 'test_folder1' ) ] ) ;
935- } ) . then ( function ( results ) {
936- var folder , root , root_folders , sub_1 ;
937- root = results [ 0 ] ;
938- root_folders = ( ( ) => {
939- var j , len , ref , results1 ;
940- ref = root . folders ;
941- results1 = [ ] ;
942- for ( j = 0 , len = ref . length ; j < len ; j ++ ) {
943- folder = ref [ j ] ;
944- results1 . push ( folder . name ) ;
945- }
946- return results1 ;
947- } ) ( ) ;
948- sub_1 = results [ 1 ] ;
949- expect ( root_folders ) . to . contain ( 'test_folder1' ) ;
950- expect ( root_folders ) . to . contain ( 'test_folder2' ) ;
951- expect ( sub_1 . folders [ 0 ] . path ) . to . eql ( 'test_folder1/test_subfolder1' ) ;
952- expect ( sub_1 . folders [ 1 ] . path ) . to . eql ( 'test_folder1/test_subfolder2' ) ;
953- return cloudinary . v2 . api . sub_folders ( 'test_folder_not_exists' ) ;
954- } ) . then ( ( result ) => {
955- console . log ( 'error test_folder_not_exists should not pass to "then" handler but "catch"' ) ;
956- expect ( ) . fail ( 'error test_folder_not_exists should not pass to "then" handler but "catch"' ) ;
957- } ) . catch ( ( err ) => {
958- let error = err . error ;
959- return expect ( error . message ) . to . eql ( 'Can\'t find folder with path test_folder_not_exists' ) ;
960- } ) ;
961- } ) ;
962- describe ( "delete folders" , function ( ) {
963- this . timeout ( TIMEOUT . MEDIUM ) ;
964- const folderPath = "test_folder/delete_folder/" + TEST_TAG ;
965- before ( function ( ) {
966- return uploadImage ( {
967- folder : folderPath ,
968- tags : UPLOAD_TAGS
969- } ) . delay ( 2 * 1000 ) . then ( function ( ) {
970- return cloudinary . v2 . api . delete_resources_by_prefix ( folderPath )
971- . then ( ( ) => cloudinary . v2 . api . sub_folders ( folderPath ) . then ( folder => {
972- expect ( folder ) . not . to . be ( null ) ;
973- expect ( folder [ "total_count" ] ) . to . eql ( 0 ) ;
974- expect ( folder [ "folders" ] ) . to . be . empty ;
975- } ) ) ;
909+ describe ( "folders" , function ( ) {
910+ // For this test to work, "Auto-create folders" should be enabled in the Upload Settings.
911+ // Replace `it` with `it.skip` below if you want to disable it.
912+ it ( "should list folders in cloudinary" , function ( ) {
913+ this . timeout ( TIMEOUT . LONG ) ;
914+ return Q . all ( [
915+ uploadImage ( {
916+ public_id : 'test_folder1/item' ,
917+ tags : UPLOAD_TAGS ,
918+ } ) ,
919+ uploadImage ( {
920+ public_id : 'test_folder2/item' ,
921+ tags : UPLOAD_TAGS ,
922+ } ) ,
923+ uploadImage ( {
924+ public_id : 'test_folder2/item' ,
925+ tags : UPLOAD_TAGS ,
926+ } ) ,
927+ uploadImage ( {
928+ public_id : 'test_folder1/test_subfolder1/item' ,
929+ tags : UPLOAD_TAGS ,
930+ } ) ,
931+ uploadImage ( {
932+ public_id : 'test_folder1/test_subfolder2/item' ,
933+ tags : UPLOAD_TAGS ,
934+ } ) ,
935+ ] ) . then ( wait ( TIMEOUT . SHORT ) )
936+ . then ( function ( results ) {
937+ return Q . all ( [ cloudinary . v2 . api . root_folders ( ) , cloudinary . v2 . api . sub_folders ( 'test_folder1' ) ] ) ;
938+ } ) . then ( function ( results ) {
939+ var folder , root , root_folders , sub_1 ;
940+ root = results [ 0 ] ;
941+ root_folders = ( ( ) => {
942+ var j , len , ref , results1 ;
943+ ref = root . folders ;
944+ results1 = [ ] ;
945+ for ( j = 0 , len = ref . length ; j < len ; j ++ ) {
946+ folder = ref [ j ] ;
947+ results1 . push ( folder . name ) ;
948+ }
949+ return results1 ;
950+ } ) ( ) ;
951+ sub_1 = results [ 1 ] ;
952+ expect ( root_folders ) . to . contain ( 'test_folder1' ) ;
953+ expect ( root_folders ) . to . contain ( 'test_folder2' ) ;
954+ expect ( sub_1 . folders [ 0 ] . path ) . to . eql ( 'test_folder1/test_subfolder1' ) ;
955+ expect ( sub_1 . folders [ 1 ] . path ) . to . eql ( 'test_folder1/test_subfolder2' ) ;
956+ return cloudinary . v2 . api . sub_folders ( 'test_folder_not_exists' ) ;
957+ } ) . then ( wait ( TIMEOUT . LONG ) ) . then ( ( result ) => {
958+ console . log ( 'error test_folder_not_exists should not pass to "then" handler but "catch"' ) ;
959+ expect ( ) . fail ( 'error test_folder_not_exists should not pass to "then" handler but "catch"' ) ;
960+ } ) . catch ( ( { error } ) => expect ( error . message ) . to . eql ( 'Can\'t find folder with path test_folder_not_exists' ) ) ;
961+ } ) ;
962+ describe ( "create_folder" , function ( ) {
963+ it ( "should create a new folder" , function ( ) {
964+ const folderPath = `${ UNIQUE_TEST_FOLDER } ` ;
965+ const expectedPath = `folders/${ folderPath } ` ;
966+ return helper . mockPromise ( function ( xhr , write , request ) {
967+ cloudinary . v2 . api . create_folder ( folderPath ) ;
968+ sinon . assert . calledWith ( request , sinon . match ( {
969+ pathname : sinon . match ( expectedPath ) ,
970+ method : sinon . match ( "POST" ) ,
971+ } ) ) ;
972+ } ) ;
976973 } ) ;
977974 } ) ;
978- it ( 'should delete an empty folder' , function ( ) {
975+ describe ( "delete_folder" , function ( ) {
979976 this . timeout ( TIMEOUT . MEDIUM ) ;
980- return cloudinary . v2 . api . delete_folder (
981- folderPath
982- ) . delay ( 2 * 1000 ) . then ( ( ) => cloudinary . v2 . api . sub_folders ( folderPath )
983- ) . then ( ( ) => expect ( ) . fail ( )
984- ) . catch ( ( { error} ) => expect ( error . message ) . to . contain ( "Can't find folder with path" ) ) ;
977+ const folderPath = "test_folder/delete_folder/" + TEST_TAG ;
978+ before ( function ( ) {
979+ return uploadImage ( {
980+ folder : folderPath ,
981+ tags : UPLOAD_TAGS ,
982+ } ) . delay ( 2 * 1000 ) . then ( function ( ) {
983+ return cloudinary . v2 . api . delete_resources_by_prefix ( folderPath )
984+ . then ( ( ) => cloudinary . v2 . api . sub_folders ( folderPath ) . then ( folder => {
985+ expect ( folder ) . not . to . be ( null ) ;
986+ expect ( folder [ "total_count" ] ) . to . eql ( 0 ) ;
987+ expect ( folder [ "folders" ] ) . to . be . empty ;
988+ } ) ) ;
989+ } ) ;
990+ } ) ;
991+ it ( 'should delete an empty folder' , function ( ) {
992+ this . timeout ( TIMEOUT . MEDIUM ) ;
993+ return cloudinary . v2 . api . delete_folder (
994+ folderPath
995+ ) . delay ( 2 * 1000 ) . then ( ( ) => cloudinary . v2 . api . sub_folders ( folderPath )
996+ ) . then ( ( ) => expect ( ) . fail ( )
997+ ) . catch ( ( { error} ) => expect ( error . message ) . to . contain ( "Can't find folder with path" ) ) ;
998+ } ) ;
985999 } ) ;
9861000 } ) ;
9871001 describe ( '.restore' , function ( ) {
0 commit comments