@@ -1029,6 +1029,65 @@ describe('RecordsWriteHandler.handle()', () => {
10291029 expect ( reply . status . detail ) . to . contain ( DwnErrorCode . ProtocolAuthorizationIncorrectProtocolPath ) ;
10301030 } ) ;
10311031
1032+ it ( 'should fail authorization if given `dataFormat` is mismatching with the dataFormats in protocol definition' , async ( ) => {
1033+ const alice = await DidKeyResolver . generate ( ) ;
1034+
1035+ const protocolDefinition = {
1036+ labels : {
1037+ image : {
1038+ schema : 'https://example.com/schema' ,
1039+ dataFormats : [ 'image/jpeg' , 'image/png' ]
1040+ }
1041+ } ,
1042+ records : {
1043+ image : {
1044+ allow : {
1045+ anyone : { to : [ 'write' ] }
1046+ }
1047+ }
1048+ }
1049+ } ;
1050+
1051+ const protocol = 'https://tbd.website/decentralized-web-node/protocols/social-media' ;
1052+ const protocolConfig = await TestDataGenerator . generateProtocolsConfigure ( {
1053+ requester : alice ,
1054+ protocol,
1055+ protocolDefinition : protocolDefinition ,
1056+ } ) ;
1057+
1058+ const protocolConfigureReply = await dwn . processMessage ( alice . did , protocolConfig . message , protocolConfig . dataStream ) ;
1059+ expect ( protocolConfigureReply . status . code ) . to . equal ( 202 ) ;
1060+
1061+ // write record with matching dataFormat
1062+ const data = Encoder . stringToBytes ( 'any data' ) ;
1063+ const recordsWriteMatch = await TestDataGenerator . generateRecordsWrite ( {
1064+ requester : alice ,
1065+ recipientDid : alice . did ,
1066+ protocol,
1067+ protocolPath : 'image' ,
1068+ schema : protocolDefinition . labels . image . schema ,
1069+ dataFormat : 'image/jpeg' ,
1070+ data
1071+ } ) ;
1072+ const replyMatch = await dwn . processMessage ( alice . did , recordsWriteMatch . message , recordsWriteMatch . dataStream ) ;
1073+ expect ( replyMatch . status . code ) . to . equal ( 202 ) ;
1074+
1075+ // write record with mismatch dataFormat
1076+ const recordsWriteMismatch = await TestDataGenerator . generateRecordsWrite ( {
1077+ requester : alice ,
1078+ recipientDid : alice . did ,
1079+ protocol,
1080+ protocolPath : 'image' ,
1081+ schema : protocolDefinition . labels . image . schema ,
1082+ dataFormat : 'not/allowed/dataFormat' ,
1083+ data
1084+ } ) ;
1085+
1086+ const replyMismatch = await dwn . processMessage ( alice . did , recordsWriteMismatch . message , recordsWriteMismatch . dataStream ) ;
1087+ expect ( replyMismatch . status . code ) . to . equal ( 401 ) ;
1088+ expect ( replyMismatch . status . detail ) . to . contain ( DwnErrorCode . ProtocolAuthorizationIncorrectDataFormat ) ;
1089+ } ) ;
1090+
10321091 it ( 'should fail authorization if record schema is not allowed at the hierarchical level attempted for the RecordsWrite' , async ( ) => {
10331092 const alice = await DidKeyResolver . generate ( ) ;
10341093
0 commit comments