@@ -144,7 +144,12 @@ describe('Documents tests', () => {
144144
145145 test ( `${ permission } key: Get documents with filters` , async ( ) => {
146146 const client = await getClient ( permission ) ;
147- await client . index ( indexPk . uid ) . updateFilterableAttributes ( [ 'id' ] ) ;
147+
148+ const { taskUid : updateFilterableAttributesTaskUid } = await client
149+ . index ( indexPk . uid )
150+ . updateFilterableAttributes ( [ 'id' ] ) ;
151+ await client . waitForTask ( updateFilterableAttributesTaskUid ) ;
152+
148153 const { taskUid } = await client
149154 . index ( indexPk . uid )
150155 . addDocuments ( dataset ) ;
@@ -780,6 +785,42 @@ Hint: It might not be working because maybe you're not up to date with the Meili
780785 expect ( index . primaryKey ) . toEqual ( null ) ;
781786 expect ( task . status ) . toEqual ( 'failed' ) ;
782787 } ) ;
788+
789+ test ( `${ permission } key: test updateDocumentsByFunction` , async ( ) => {
790+ const client = await getClient ( permission ) ;
791+ const index = client . index < ( typeof dataset ) [ number ] > ( indexPk . uid ) ;
792+ const adminKey = await getKey ( 'Admin' ) ;
793+
794+ const { taskUid : updateFilterableAttributesTaskUid } =
795+ await index . updateFilterableAttributes ( [ 'id' ] ) ;
796+ await client . waitForTask ( updateFilterableAttributesTaskUid ) ;
797+
798+ await fetch ( `${ HOST } /experimental-features` , {
799+ body : JSON . stringify ( { editDocumentsByFunction : true } ) ,
800+ headers : {
801+ Authorization : `Bearer ${ adminKey } ` ,
802+ 'Content-Type' : 'application/json' ,
803+ } ,
804+ method : 'PATCH' ,
805+ } ) ;
806+
807+ const { taskUid : addDocumentsTaskUid } =
808+ await index . addDocuments ( dataset ) ;
809+ await index . waitForTask ( addDocumentsTaskUid ) ;
810+
811+ const { taskUid : updateDocumentsByFunctionTaskUid } =
812+ await index . updateDocumentsByFunction ( {
813+ context : { ctx : 'Harry' } ,
814+ filter : 'id = 4' ,
815+ function : 'doc.comment = `Yer a wizard, ${context.ctx}!`' ,
816+ } ) ;
817+
818+ await client . waitForTask ( updateDocumentsByFunctionTaskUid ) ;
819+
820+ const doc = await index . getDocument ( 4 ) ;
821+
822+ expect ( doc ) . toHaveProperty ( 'comment' , 'Yer a wizard, Harry!' ) ;
823+ } ) ;
783824 } ,
784825 ) ;
785826
@@ -831,6 +872,24 @@ Hint: It might not be working because maybe you're not up to date with the Meili
831872 client . index ( indexPk . uid ) . deleteAllDocuments ( ) ,
832873 ) . rejects . toHaveProperty ( 'cause.code' , ErrorStatusCode . INVALID_API_KEY ) ;
833874 } ) ;
875+
876+ test ( `${ permission } key: Try updateDocumentsByFunction and be denied` , async ( ) => {
877+ const client = await getClient ( permission ) ;
878+ const adminKey = await getKey ( 'Admin' ) ;
879+
880+ await fetch ( `${ HOST } /experimental-features` , {
881+ body : JSON . stringify ( { editDocumentsByFunction : true } ) ,
882+ headers : {
883+ Authorization : `Bearer ${ adminKey } ` ,
884+ 'Content-Type' : 'application/json' ,
885+ } ,
886+ method : 'PATCH' ,
887+ } ) ;
888+
889+ await expect (
890+ client . index ( indexPk . uid ) . updateDocumentsByFunction ( { function : '' } ) ,
891+ ) . rejects . toHaveProperty ( 'cause.code' , ErrorStatusCode . INVALID_API_KEY ) ;
892+ } ) ;
834893 } ,
835894 ) ;
836895
@@ -900,6 +959,27 @@ Hint: It might not be working because maybe you're not up to date with the Meili
900959 ErrorStatusCode . MISSING_AUTHORIZATION_HEADER ,
901960 ) ;
902961 } ) ;
962+
963+ test ( `${ permission } key: Try updateDocumentsByFunction and be denied` , async ( ) => {
964+ const client = await getClient ( permission ) ;
965+ const adminKey = await getKey ( 'Admin' ) ;
966+
967+ await fetch ( `${ HOST } /experimental-features` , {
968+ body : JSON . stringify ( { editDocumentsByFunction : true } ) ,
969+ headers : {
970+ Authorization : `Bearer ${ adminKey } ` ,
971+ 'Content-Type' : 'application/json' ,
972+ } ,
973+ method : 'PATCH' ,
974+ } ) ;
975+
976+ await expect (
977+ client . index ( indexPk . uid ) . updateDocumentsByFunction ( { function : '' } ) ,
978+ ) . rejects . toHaveProperty (
979+ 'cause.code' ,
980+ ErrorStatusCode . MISSING_AUTHORIZATION_HEADER ,
981+ ) ;
982+ } ) ;
903983 } ,
904984 ) ;
905985
@@ -991,5 +1071,28 @@ Hint: It might not be working because maybe you're not up to date with the Meili
9911071 `Request to ${ strippedHost } /${ route } has failed` ,
9921072 ) ;
9931073 } ) ;
1074+
1075+ test ( `Test updateDocumentsByFunction route` , async ( ) => {
1076+ const route = `indexes/${ indexPk . uid } /documents/edit` ;
1077+ const client = new MeiliSearch ( { host } ) ;
1078+ const strippedHost = trailing ? host . slice ( 0 , - 1 ) : host ;
1079+ const adminKey = await getKey ( 'Admin' ) ;
1080+
1081+ await fetch ( `${ HOST } /experimental-features` , {
1082+ body : JSON . stringify ( { editDocumentsByFunction : true } ) ,
1083+ headers : {
1084+ Authorization : `Bearer ${ adminKey } ` ,
1085+ 'Content-Type' : 'application/json' ,
1086+ } ,
1087+ method : 'PATCH' ,
1088+ } ) ;
1089+
1090+ await expect (
1091+ client . index ( indexPk . uid ) . updateDocumentsByFunction ( { function : '' } ) ,
1092+ ) . rejects . toHaveProperty (
1093+ 'message' ,
1094+ `Request to ${ strippedHost } /${ route } has failed` ,
1095+ ) ;
1096+ } ) ;
9941097 } ) ;
9951098} ) ;
0 commit comments