1+ require ( 'dotenv' ) . load ( {
2+ silent : true ,
3+ } ) ;
4+
15const expect = require ( "expect.js" ) ;
26const sinon = require ( 'sinon' ) ;
37const ClientRequest = require ( '_http_client' ) . ClientRequest ;
48const http = require ( 'http' ) ;
59const Q = require ( 'q' ) ;
610const cloudinary = require ( "../cloudinary" ) ;
711const helper = require ( "./spechelper" ) ;
12+ const wait = require ( './testUtils/helpers/wait' ) ;
813const sharedExamples = helper . sharedExamples ;
914const itBehavesLike = helper . itBehavesLike ;
1015const uploadImage = helper . uploadImage ;
1116
1217const testConstants = require ( './testUtils/testConstants' ) ;
18+ const API_V2 = cloudinary . v2 . api ;
1319
1420const {
1521 TIMEOUT ,
@@ -65,13 +71,7 @@ const EXPLICIT_TRANSFORMATION2 = {
6571 overlay : `text:Arial_60:${ TEST_TAG } ` ,
6672} ;
6773
68- function wait ( ms = 0 ) {
69- return new Promise ( ( resolve ) => {
70- setTimeout ( ( ) => {
71- resolve ( ) ;
72- } , ms ) ;
73- } ) ;
74- }
74+
7575
7676sharedExamples ( "a list with a cursor" , function ( testFunc , ...args ) {
7777 specify ( ":max_results" , function ( ) {
@@ -795,7 +795,7 @@ describe("api", function () {
795795 } ) ;
796796 } ) ;
797797 } ) ;
798- describe . skip ( "quality override" , function ( ) {
798+ describe ( "quality override" , function ( ) {
799799 const mocked = helper . mockTest ( ) ;
800800 const qualityValues = [ "auto:advanced" , "auto:best" , "80:420" , "none" ] ;
801801 qualityValues . forEach ( quality => {
@@ -805,13 +805,16 @@ describe("api", function () {
805805 } ) ;
806806 } ) ;
807807 } ) ;
808- it . skip ( "should support setting manual moderation status" , ( ) => {
808+ it ( "should support setting manual moderation status" , function ( ) {
809809 this . timeout ( TIMEOUT . LONG ) ;
810810 return uploadImage ( {
811811 moderation : "manual" ,
812812 } ) . then ( upload_result => cloudinary . v2 . api . update ( upload_result . public_id , {
813813 moderation_status : "approved" ,
814- } ) ) . then ( api_result => expect ( api_result . moderation [ 0 ] . status ) . to . eql ( "approved" ) ) ;
814+ } ) ) . then ( api_result => expect ( api_result . moderation [ 0 ] . status ) . to . eql ( "approved" ) )
815+ . catch ( ( err ) => {
816+ console . og ( err ) ;
817+ } ) ;
815818 } ) ;
816819 it ( "should support requesting ocr info" , function ( ) {
817820 this . timeout ( TIMEOUT . MEDIUM ) ;
@@ -904,7 +907,7 @@ describe("api", function () {
904907 } ) ;
905908 // For this test to work, "Auto-create folders" should be enabled in the Upload Settings.
906909 // Replace `it` with `it.skip` below if you want to disable it.
907- it . skip ( "should list folders in cloudinary" , function ( ) {
910+ it ( "should list folders in cloudinary" , function ( ) {
908911 this . timeout ( TIMEOUT . LONG ) ;
909912 return Q . all ( [
910913 uploadImage ( {
@@ -927,7 +930,7 @@ describe("api", function () {
927930 public_id : 'test_folder1/test_subfolder2/item' ,
928931 tags : UPLOAD_TAGS ,
929932 } ) ,
930- ] ) . then ( function ( results ) {
933+ ] ) . then ( wait ( 3000 ) ) . then ( function ( results ) {
931934 return Q . all ( [ cloudinary . v2 . api . root_folders ( ) , cloudinary . v2 . api . sub_folders ( 'test_folder1' ) ] ) ;
932935 } ) . then ( function ( results ) {
933936 var folder , root , root_folders , sub_1 ;
@@ -951,7 +954,10 @@ describe("api", function () {
951954 } ) . then ( ( result ) => {
952955 console . log ( 'error test_folder_not_exists should not pass to "then" handler but "catch"' ) ;
953956 expect ( ) . fail ( 'error test_folder_not_exists should not pass to "then" handler but "catch"' ) ;
954- } ) . catch ( ( { error } ) => expect ( error . message ) . to . eql ( 'Can\'t find folder with path test_folder_not_exists' ) ) ;
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+ } ) ;
955961 } ) ;
956962 describe ( "delete folders" , function ( ) {
957963 this . timeout ( TIMEOUT . MEDIUM ) ;
@@ -986,7 +992,7 @@ describe("api", function () {
986992 public_id : publicId ,
987993 backup : true ,
988994 tags : UPLOAD_TAGS ,
989- } ) . then ( ( ) => cloudinary . v2 . api . resource ( publicId ) ) . then ( ( resource ) => {
995+ } ) . then ( wait ( 2000 ) ) . then ( ( ) => cloudinary . v2 . api . resource ( publicId ) ) . then ( ( resource ) => {
990996 expect ( resource ) . not . to . be ( null ) ;
991997 expect ( resource . bytes ) . to . eql ( 3381 ) ;
992998 return cloudinary . v2 . api . delete_resources ( publicId ) ;
@@ -1004,67 +1010,88 @@ describe("api", function () {
10041010 expect ( resource ) . not . to . be ( null ) ;
10051011 expect ( resource . bytes ) . to . eql ( 3381 ) ;
10061012 } ) ) ;
1007- it ( 'should restore a deleted resource by versions' , function ( ) {
1008- return uploadImage ( {
1009- public_id : PUBLIC_ID_BACKUP_1 ,
1010- backup : true ,
1011- } ) . then ( ( ) => uploadImage ( {
1012- public_id : PUBLIC_ID_BACKUP_2 ,
1013- backup : true ,
1014- } ) . then ( wait ( TIMEOUT . SHORT ) ) ) . then ( ( uploadResponse ) => {
1015- expect ( uploadResponse ) . not . to . be ( null ) ;
1016- } ) . then ( wait ( TIMEOUT . SHORT ) )
1017- . then ( ( ) => cloudinary . v2 . api . delete_resources ( [ PUBLIC_ID_BACKUP_1 , PUBLIC_ID_BACKUP_2 ] ) )
1018- . then ( ( deleteResponse ) => {
1019- expect ( deleteResponse ) . to . have . property ( "deleted" ) ;
1020- } )
1021- . then ( wait ( TIMEOUT . SHORT ) )
1022- . then ( ( ) => Q . all ( [
1023- cloudinary . v2 . api . resource ( PUBLIC_ID_BACKUP_1 , { versions : true } ) ,
1024- cloudinary . v2 . api . resource ( PUBLIC_ID_BACKUP_2 , { versions : true } ) ] ) )
1025- . then ( ( resources ) => {
1026- expect ( resources . length ) . to . be ( 2 ) ;
10271013
1028- const version_1 = resources [ 0 ] . versions [ 0 ] . version_id ;
1029- const version_2 = resources [ 1 ] . versions [ 0 ] . version_id ;
1030- return cloudinary . v2 . api . restore ( [ PUBLIC_ID_BACKUP_1 , PUBLIC_ID_BACKUP_2 ] , { versions : [ version_1 , version_2 ] } ) ;
1031- } ) . then ( ( restoreResponse ) => {
1032- expect ( restoreResponse [ PUBLIC_ID_BACKUP_1 ] . bytes ) . to . eql ( 3381 ) ;
1033- expect ( restoreResponse [ PUBLIC_ID_BACKUP_2 ] . bytes ) . to . eql ( 3381 ) ;
1034- } ) . then ( ( ) => cloudinary . v2 . api . delete_resources ( [ PUBLIC_ID_BACKUP_1 , PUBLIC_ID_BACKUP_2 ] ) )
1035- . then ( ( deleteResponse ) => {
1036- expect ( deleteResponse ) . to . have . property ( "deleted" ) ;
1037- } ) ;
1014+ it ( 'should restore different versions of a deleted asset' , async function ( ) {
1015+ this . timeout ( TIMEOUT . LARGE ) ;
1016+ // Upload the same file twice (upload->delete->upload->delete)
1017+
1018+ // Upload and delete a file
1019+ const firstUpload = await uploadImage ( { public_id : PUBLIC_ID_BACKUP_1 , backup : true } ) ;
1020+ await wait ( 1000 ) ( ) ;
1021+
1022+ const firstDelete = await API_V2 . delete_resources ( [ PUBLIC_ID_BACKUP_1 ] ) ;
1023+
1024+
1025+ // Upload and delete it again, this time add angle to create a different 'version'
1026+ const secondUpload = await uploadImage ( { public_id : PUBLIC_ID_BACKUP_1 , backup : true , angle : '0' } ) ;
1027+ await wait ( 1000 ) ( ) ;
1028+
1029+ const secondDelete = await API_V2 . delete_resources ( [ PUBLIC_ID_BACKUP_1 ] ) ;
1030+
1031+
1032+ // Sanity, ensure these uploads are different before we continue
1033+ expect ( firstUpload . bytes ) . not . to . equal ( secondUpload . bytes ) ;
1034+
1035+ // Ensure all files were uploaded correctly
1036+ expect ( firstUpload ) . not . to . be ( null ) ;
1037+ expect ( secondUpload ) . not . to . be ( null ) ;
1038+
1039+ // Ensure all files were deleted correctly
1040+ expect ( firstDelete ) . to . have . property ( "deleted" ) ;
1041+ expect ( secondDelete ) . to . have . property ( "deleted" ) ;
1042+
1043+ // Get the versions versions of the deleted asset
1044+ const getVersionsResp = await API_V2 . resource ( PUBLIC_ID_BACKUP_1 , { versions : true } ) ;
1045+
1046+ const firstAssetVersion = getVersionsResp . versions [ 0 ] . version_id ;
1047+ const secondAssetVersion = getVersionsResp . versions [ 1 ] . version_id ;
1048+
1049+ // Restore first version, ensure it's equal to the upload size
1050+ await wait ( 1000 ) ( ) ;
1051+ const firstVerRestore = await API_V2 . restore ( [ PUBLIC_ID_BACKUP_1 ] , { versions : [ firstAssetVersion ] } ) ;
1052+ expect ( firstVerRestore [ PUBLIC_ID_BACKUP_1 ] . bytes ) . to . eql ( firstUpload . bytes ) ;
1053+
1054+ // Restore second version, ensure it's equal to the upload size
1055+ const secondVerRestore = await API_V2 . restore ( [ PUBLIC_ID_BACKUP_1 ] , { versions : [ secondAssetVersion ] } ) ;
1056+ expect ( secondVerRestore [ PUBLIC_ID_BACKUP_1 ] . bytes ) . to . eql ( secondUpload . bytes ) ;
1057+
1058+ // Cleanup,
1059+ const finalDeleteResp = await API_V2 . delete_resources ( [ PUBLIC_ID_BACKUP_1 ] ) ;
1060+ expect ( finalDeleteResp ) . to . have . property ( "deleted" ) ;
10381061 } ) ;
1039- it ( 'should restore an old deleted resource by versions' , function ( ) {
1040- return uploadImage ( {
1041- public_id : PUBLIC_ID_BACKUP_3 ,
1042- backup : true ,
1043- } ) . then ( ( ) => uploadImage ( {
1044- public_id : PUBLIC_ID_BACKUP_3 ,
1045- angle : '0' ,
1046- backup : true ,
1047- } ) . then ( wait ( TIMEOUT . SHORT ) ) ) . then ( ( uploadResponse ) => {
1048- expect ( uploadResponse ) . not . to . be ( null ) ;
1049- } ) . then ( wait ( TIMEOUT . SHORT ) )
1050- . then ( ( ) => cloudinary . v2 . api . delete_resources ( [ PUBLIC_ID_BACKUP_3 ] ) )
1051- . then ( ( deleteResponse ) => {
1052- expect ( deleteResponse ) . to . have . property ( "deleted" ) ;
1053- } )
1054- . then ( wait ( TIMEOUT . SHORT ) )
1055- . then ( ( ) => cloudinary . v2 . api . resource ( PUBLIC_ID_BACKUP_3 , { versions : true } ) )
1056- . then ( ( resources ) => {
1057- expect ( resources . versions . length ) . to . be ( 2 ) ;
10581062
1059- const old_version = resources . versions [ 0 ] . version_id ;
1060- return cloudinary . v2 . api . restore ( PUBLIC_ID_BACKUP_3 , { versions : old_version } ) ;
1061- } ) . then ( ( restoreResponse ) => {
1062- expect ( restoreResponse [ PUBLIC_ID_BACKUP_3 ] . bytes ) . to . eql ( 2353 ) ;
1063- } )
1064- . then ( ( ) => cloudinary . v2 . api . delete_resources ( [ PUBLIC_ID_BACKUP_3 ] ) )
1065- . then ( ( deleteResponse ) => {
1066- expect ( deleteResponse ) . to . have . property ( "deleted" ) ;
1067- } ) ;
1063+ it ( 'should restore two different deleted assets' , async ( ) => {
1064+ // Upload two different files
1065+ const firstUpload = await uploadImage ( { public_id : PUBLIC_ID_BACKUP_1 , backup : true } ) ;
1066+ const secondUpload = await uploadImage ( { public_id : PUBLIC_ID_BACKUP_2 , backup : true , angle : '0' } ) ;
1067+
1068+ // delete both resources
1069+ const deleteAll = await API_V2 . delete_resources ( [ PUBLIC_ID_BACKUP_1 , PUBLIC_ID_BACKUP_2 ] ) ;
1070+
1071+ // Expect correct deletion of the assets
1072+ expect ( deleteAll . deleted [ PUBLIC_ID_BACKUP_1 ] ) . to . be ( "deleted" ) ;
1073+ expect ( deleteAll . deleted [ PUBLIC_ID_BACKUP_2 ] ) . to . be ( "deleted" ) ;
1074+
1075+ const getFirstAssetVersion = await API_V2 . resource ( PUBLIC_ID_BACKUP_1 , { versions : true } ) ;
1076+ const getSecondAssetVersion = await API_V2 . resource ( PUBLIC_ID_BACKUP_2 , { versions : true } ) ;
1077+
1078+ const firstAssetVersion = getFirstAssetVersion . versions [ 0 ] . version_id ;
1079+ const secondAssetVersion = getSecondAssetVersion . versions [ 0 ] . version_id ;
1080+
1081+ const IDS_TO_RESTORE = [ PUBLIC_ID_BACKUP_1 , PUBLIC_ID_BACKUP_2 ] ;
1082+ const VERSIONS_TO_RESTORE = [ firstAssetVersion , secondAssetVersion ] ;
1083+
1084+ const restore = await API_V2 . restore ( IDS_TO_RESTORE , { versions : VERSIONS_TO_RESTORE } ) ;
1085+
1086+ // Expect correct restorations
1087+ expect ( restore [ PUBLIC_ID_BACKUP_1 ] . bytes ) . to . equal ( firstUpload . bytes ) ;
1088+ expect ( restore [ PUBLIC_ID_BACKUP_2 ] . bytes ) . to . equal ( secondUpload . bytes ) ;
1089+
1090+ // Cleanup
1091+ const finalDelete = await API_V2 . delete_resources ( [ PUBLIC_ID_BACKUP_1 , PUBLIC_ID_BACKUP_2 ] ) ;
1092+ // Expect correct deletion of the assets
1093+ expect ( finalDelete . deleted [ PUBLIC_ID_BACKUP_1 ] ) . to . be ( "deleted" ) ;
1094+ expect ( finalDelete . deleted [ PUBLIC_ID_BACKUP_2 ] ) . to . be ( "deleted" ) ;
10681095 } ) ;
10691096 } ) ;
10701097 describe ( 'mapping' , function ( ) {
@@ -1172,22 +1199,25 @@ describe("api", function () {
11721199 } ) ;
11731200 } ) ;
11741201 } ) ;
1175- describe . skip ( "access_mode" , function ( ) {
1176- var access_mode_tag , i , publicId ;
1202+ describe ( "access_mode" , function ( ) {
1203+ let access_mode_tag , i , publicId ;
11771204 i = 0 ;
11781205 this . timeout ( TIMEOUT . LONG ) ;
11791206 publicId = "" ;
11801207 access_mode_tag = '' ;
1181- beforeEach ( function ( ) {
1208+
1209+ beforeEach ( async function ( ) {
11821210 access_mode_tag = TEST_TAG + "access_mode" + i ++ ;
1183- return uploadImage ( {
1211+ const result = await uploadImage ( {
11841212 access_mode : "authenticated" ,
11851213 tags : UPLOAD_TAGS . concat ( [ access_mode_tag ] ) ,
1186- } ) . then ( ( result ) => {
1187- publicId = result . public_id ;
1188- expect ( result . access_mode ) . to . be ( "authenticated" ) ;
11891214 } ) ;
1215+
1216+ await wait ( 5000 ) ( ) ;
1217+ publicId = result . public_id ;
1218+ expect ( result . access_mode ) . to . be ( "authenticated" ) ;
11901219 } ) ;
1220+
11911221 it ( "should update access mode by ids" , ( ) => cloudinary . v2 . api . update_resources_access_mode_by_ids ( "public" , [ publicId ] ) . then ( ( result ) => {
11921222 var resource ;
11931223 expect ( result . updated ) . to . be . an ( 'array' ) ;
0 commit comments