1+ const assert = require ( 'assert' ) ;
12const Q = require ( 'q' ) ;
23const sinon = require ( 'sinon' ) ;
34const cloudinary = require ( "../../../../cloudinary" ) ;
@@ -96,14 +97,16 @@ describe("structured metadata api", function () {
9697 // Create the metadata fields required for the tests
9798 return Q . allSettled (
9899 metadata_fields_to_create . map ( field => createMetadataFieldForTest ( field ) )
99- ) . finally ( function ( ) { } ) ;
100+ ) . finally ( function ( ) {
101+ } ) ;
100102 } ) ;
101103
102104 after ( function ( ) {
103105 // Delete all metadata fields created during testing
104106 return Q . allSettled (
105107 metadata_fields_external_ids . map ( field => api . delete_metadata_field ( field ) )
106- ) . finally ( function ( ) { } ) ;
108+ ) . finally ( function ( ) {
109+ } ) ;
107110 } ) ;
108111
109112 describe ( "list_metadata_fields" , function ( ) {
@@ -123,7 +126,7 @@ describe("structured metadata api", function () {
123126 it ( "should return metadata field by external id" , function ( ) {
124127 return api . metadata_field_by_field_id ( EXTERNAL_ID_GENERAL )
125128 . then ( ( result ) => {
126- expect ( [ result , { label : EXTERNAL_ID_GENERAL } ] ) . to . beAMetadataField ( ) ;
129+ expect ( [ result , { label : EXTERNAL_ID_GENERAL } ] ) . to . beAMetadataField ( ) ;
127130 } ) ;
128131 } ) ;
129132 } ) ;
@@ -174,7 +177,10 @@ describe("structured metadata api", function () {
174177 expect ( result ) . to . beAMetadataField ( ) ;
175178 return api . metadata_field_by_field_id ( EXTERNAL_ID_DATE ) ;
176179 } ) . then ( ( result ) => {
177- expect ( [ result , { ...metadata , mandatory : false } ] ) . to . beAMetadataField ( ) ;
180+ expect ( [ result , {
181+ ...metadata ,
182+ mandatory : false
183+ } ] ) . to . beAMetadataField ( ) ;
178184 } ) ;
179185 } ) ;
180186 it ( "should create enum metadata field" , function ( ) {
@@ -195,7 +201,7 @@ describe("structured metadata api", function () {
195201 sinon . assert . calledWith ( writeSpy , sinon . match ( helper . apiJsonParamMatcher ( 'external_id' , EXTERNAL_ID_ENUM ) ) ) ;
196202 sinon . assert . calledWith ( writeSpy , sinon . match ( helper . apiJsonParamMatcher ( 'type' , 'enum' ) ) ) ;
197203 sinon . assert . calledWith ( writeSpy , sinon . match ( helper . apiJsonParamMatcher ( 'label' , EXTERNAL_ID_ENUM ) ) ) ;
198- sinon . assert . calledWith ( writeSpy , sinon . match ( helper . apiJsonParamMatcher ( 'datasource' , { values : datasource_single } ) ) ) ;
204+ sinon . assert . calledWith ( writeSpy , sinon . match ( helper . apiJsonParamMatcher ( 'datasource' , { values : datasource_single } ) ) ) ;
199205 } ) ;
200206 } ) ;
201207 it ( "should create set metadata field" , function ( ) {
@@ -251,7 +257,7 @@ describe("structured metadata api", function () {
251257
252258 describe ( "update_metadata_field_datasource" , function ( ) {
253259 it ( "should update metadata field datasource by external id" , function ( ) {
254- return api . update_metadata_field_datasource ( EXTERNAL_ID_ENUM_2 , { values : datasource_single } )
260+ return api . update_metadata_field_datasource ( EXTERNAL_ID_ENUM_2 , { values : datasource_single } )
255261 . then ( ( ) => api . metadata_field_by_field_id ( EXTERNAL_ID_ENUM_2 ) )
256262 . then ( ( result ) => {
257263 expect ( result . datasource ) . to . beADatasource ( ) ;
@@ -283,7 +289,7 @@ describe("structured metadata api", function () {
283289 expect ( result . message ) . to . eql ( "ok" ) ;
284290 return api . add_metadata_field ( metadata ) ;
285291 } )
286- . catch ( ( { error } ) => {
292+ . catch ( ( { error} ) => {
287293 expect ( error ) . not . to . be ( void 0 ) ;
288294 expect ( error . http_code ) . to . eql ( 400 ) ;
289295 expect ( error . message ) . to . contain ( `external id ${ EXTERNAL_ID_DELETE_2 } already exists` ) ;
@@ -393,7 +399,7 @@ describe("structured metadata api", function () {
393399 . then ( ( result ) => {
394400 expect ( result ) . to . beAMetadataField ( ) ;
395401 return api . metadata_field_by_field_id ( EXTERNAL_ID_INT_VALIDATION_2 ) ;
396- } ) . catch ( ( { error } ) => {
402+ } ) . catch ( ( { error} ) => {
397403 expect ( error ) . not . to . be ( void 0 ) ;
398404 expect ( error . http_code ) . to . eql ( 400 ) ;
399405 expect ( error . message ) . to . contain ( `default_value is invalid` ) ;
@@ -509,4 +515,86 @@ describe("structured metadata api", function () {
509515 } )
510516 } ) ;
511517 } ) ;
518+
519+ describe ( 'rules' , ( ) => {
520+ it ( 'should allow listing metadata rules' , ( ) => {
521+ const expectedPath = '/metadata_rules' ;
522+ return helper . provideMockObjects ( function ( mockXHR , writeSpy , requestSpy ) {
523+ api . list_metadata_rules ( ) ;
524+ sinon . assert . calledWith ( requestSpy , sinon . match ( {
525+ pathname : sinon . match ( new RegExp ( expectedPath ) ) ,
526+ method : sinon . match ( 'GET' )
527+ } ) ) ;
528+ } ) ;
529+ } ) ;
530+
531+ it ( 'should allow adding new metadata rules' , ( ) => {
532+ const expectedPath = '/metadata_rules' ;
533+ return helper . provideMockObjects ( function ( mockXHR , writeSpy , requestSpy ) {
534+ const newMetadataRule = {
535+ metadata_field_id : 'field_id' ,
536+ name : 'rule_name' ,
537+ condition : { } ,
538+ result : { }
539+ } ;
540+ api . add_metadata_rule ( newMetadataRule ) ;
541+
542+ sinon . assert . calledWith ( requestSpy , sinon . match ( {
543+ pathname : sinon . match ( new RegExp ( expectedPath ) ) ,
544+ method : sinon . match ( 'POST' )
545+ } ) ) ;
546+
547+ sinon . assert . calledOnce ( writeSpy ) ;
548+
549+ const firstCallArgs = JSON . parse ( writeSpy . firstCall . args [ 0 ] ) ;
550+ assert . deepStrictEqual ( firstCallArgs , {
551+ metadata_field_id : 'field_id' ,
552+ name : 'rule_name' ,
553+ condition : { } ,
554+ result : { }
555+ } ) ;
556+ } ) ;
557+ } ) ;
558+
559+ it ( 'should allow editing metadata rules' , ( ) => {
560+ const expectedPath = '/metadata_rules/some-metadata-rule-id' ;
561+ return helper . provideMockObjects ( function ( mockXHR , writeSpy , requestSpy ) {
562+ const ruleUpdate = {
563+ metadata_field_id : 'new_field_id' ,
564+ name : 'new_rule_name' ,
565+ condition : { } ,
566+ result : { } ,
567+ state : 'inactive'
568+ } ;
569+ api . update_metadata_rule ( 'some-metadata-rule-id' , ruleUpdate ) ;
570+
571+ sinon . assert . calledWith ( requestSpy , sinon . match ( {
572+ pathname : sinon . match ( new RegExp ( expectedPath ) ) ,
573+ method : sinon . match ( 'PUT' )
574+ } ) ) ;
575+
576+ sinon . assert . calledOnce ( writeSpy ) ;
577+
578+ const firstCallArgs = JSON . parse ( writeSpy . firstCall . args [ 0 ] ) ;
579+ assert . deepStrictEqual ( firstCallArgs , {
580+ metadata_field_id : 'new_field_id' ,
581+ name : 'new_rule_name' ,
582+ condition : { } ,
583+ result : { } ,
584+ state : 'inactive'
585+ } ) ;
586+ } ) ;
587+ } ) ;
588+
589+ it ( 'should allow removing existing metadata rules' , ( ) => {
590+ const expectedPath = '/metadata_rules/some-metadata-rule-id' ;
591+ return helper . provideMockObjects ( function ( mockXHR , writeSpy , requestSpy ) {
592+ api . delete_metadata_rule ( 'some-metadata-rule-id' ) ;
593+ sinon . assert . calledWith ( requestSpy , sinon . match ( {
594+ pathname : sinon . match ( new RegExp ( expectedPath ) ) ,
595+ method : sinon . match ( 'DELETE' )
596+ } ) ) ;
597+ } ) ;
598+ } ) ;
599+ } ) ;
512600} ) ;
0 commit comments