@@ -2433,23 +2433,32 @@ function testDocApi(settings: {
2433
2433
2434
2434
} ) ;
2435
2435
2436
+ async function addAttachmentsToDoc ( docId : string , attachments : { name : string , contents : string } [ ] ,
2437
+ user : AxiosRequestConfig = chimpy ) {
2438
+ const formData = new FormData ( ) ;
2439
+ for ( const attachment of attachments ) {
2440
+ formData . append ( 'upload' , attachment . contents , attachment . name ) ;
2441
+ }
2442
+ const resp = await axios . post ( `${ serverUrl } /api/docs/${ docId } /attachments` , formData ,
2443
+ defaultsDeep ( { headers : formData . getHeaders ( ) } , user ) ) ;
2444
+ assert . equal ( resp . status , 200 ) ;
2445
+ assert . equal ( resp . data . length , attachments . length ) ;
2446
+ return resp ;
2447
+ }
2448
+
2436
2449
describe ( 'attachments' , function ( ) {
2437
2450
it ( "POST /docs/{did}/attachments adds attachments" , async function ( ) {
2438
- let formData = new FormData ( ) ;
2439
- formData . append ( 'upload' , 'foobar' , "hello.doc" ) ;
2440
- formData . append ( 'upload' , '123456' , "world.jpg" ) ;
2441
- let resp = await axios . post ( `${ serverUrl } /api/docs/${ docIds . TestDoc } /attachments` , formData ,
2442
- defaultsDeep ( { headers : formData . getHeaders ( ) } , chimpy ) ) ;
2443
- assert . equal ( resp . status , 200 ) ;
2444
- assert . deepEqual ( resp . data , [ 1 , 2 ] ) ;
2451
+ const uploadResp = await addAttachmentsToDoc ( docIds . TestDoc , [
2452
+ { name : 'hello.doc' , contents : 'foobar' } ,
2453
+ { name : 'world.jpg' , contents : '123456' } ,
2454
+ ] , chimpy ) ;
2455
+ assert . deepEqual ( uploadResp . data , [ 1 , 2 ] ) ;
2445
2456
2446
2457
// Another upload gets the next number.
2447
- formData = new FormData ( ) ;
2448
- formData . append ( 'upload' , 'abcdef' , "hello.png" ) ;
2449
- resp = await axios . post ( `${ serverUrl } /api/docs/${ docIds . TestDoc } /attachments` , formData ,
2450
- defaultsDeep ( { headers : formData . getHeaders ( ) } , chimpy ) ) ;
2451
- assert . equal ( resp . status , 200 ) ;
2452
- assert . deepEqual ( resp . data , [ 3 ] ) ;
2458
+ const upload2Resp = await addAttachmentsToDoc ( docIds . TestDoc , [
2459
+ { name : 'hello.png' , contents : 'abcdef' } ,
2460
+ ] , chimpy ) ;
2461
+ assert . deepEqual ( upload2Resp . data , [ 3 ] ) ;
2453
2462
} ) ;
2454
2463
2455
2464
it ( "GET /docs/{did}/attachments lists attachment metadata" , async function ( ) {
@@ -2753,13 +2762,11 @@ function testDocApi(settings: {
2753
2762
docId = await userApi . newDoc ( { name : 'TestDocExternalAttachments' } , wid ) ;
2754
2763
docUrl = `${ serverUrl } /api/docs/${ docId } ` ;
2755
2764
2756
- const formData = new FormData ( ) ;
2757
- formData . append ( 'upload' , 'foobar' , "hello.doc" ) ;
2758
- formData . append ( 'upload' , '123456' , "world.jpg" ) ;
2759
- formData . append ( 'upload' , 'foobar' , "hello2.doc" ) ;
2760
- const resp = await axios . post ( `${ docUrl } /attachments` , formData ,
2761
- defaultsDeep ( { headers : formData . getHeaders ( ) } , chimpy ) ) ;
2762
- assert . equal ( resp . status , 200 ) ;
2765
+ const resp = await addAttachmentsToDoc ( docId , [
2766
+ { name : 'hello.doc' , contents : 'foobar' } ,
2767
+ { name : 'world.jpg' , contents : '123456' } ,
2768
+ { name : 'hello2.doc' , contents : 'foobar' }
2769
+ ] , chimpy ) ;
2763
2770
assert . deepEqual ( resp . data , [ 1 , 2 , 3 ] ) ;
2764
2771
} ) ;
2765
2772
@@ -2804,6 +2811,23 @@ function testDocApi(settings: {
2804
2811
locationSummary : "internal" ,
2805
2812
} ) ;
2806
2813
} ) ;
2814
+
2815
+ it ( "POST /docs/{did}/copy fails when the document has external attachments" , async function ( ) {
2816
+ const worker1 = await userApi . getWorkerAPI ( docId ) ;
2817
+ await assert . isRejected ( worker1 . copyDoc ( docId , undefined , 'copy' ) , / s t a t u s 4 0 0 / ) ;
2818
+ } ) ;
2819
+
2820
+ it ( "POST /docs/{did} with sourceDocId fails to copy a document with external attachments" , async function ( ) {
2821
+ const chimpyWs = await userApi . newWorkspace ( { name : "Chimpy's Workspace" } , ORG_NAME ) ;
2822
+ const resp = await axios . post ( `${ serverUrl } /api/docs` , {
2823
+ sourceDocumentId : docId ,
2824
+ documentName : 'copy of TestDocExternalAttachments' ,
2825
+ asTemplate : false ,
2826
+ workspaceId : chimpyWs
2827
+ } , chimpy ) ;
2828
+ assert . equal ( resp . status , 400 ) ;
2829
+ assert . match ( resp . data . error , / e x t e r n a l a t t a c h m e n t s / ) ;
2830
+ } ) ;
2807
2831
} ) ;
2808
2832
} ) ;
2809
2833
0 commit comments