@adobe/acc-js-sdk 1.1.47 → 1.1.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1839,100 +1839,6 @@ describe('ACC Client', function () {
1839
1839
  });
1840
1840
  });
1841
1841
 
1842
- // write unit test for client.fileUploader.uploadAemAsset method
1843
- describe("Tests for client.fileUploader.uploadAemAsset method", () => {
1844
- // Case 1: uploadAemAsset method should return a valid response
1845
- it("Should return correct response", async () => {
1846
- const client = await Mock.makeClient();
1847
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
1848
- await client.NLWS.xtkSession.logon();
1849
-
1850
- const mockResponse = {
1851
- "publishedURL" : "http://trk-inst-xyz.camp.adobe.com/res/trk-inst/409afb8798180a36591456e152b6c406.jpeg"
1852
- };
1853
- client._bearerToken = 'Bearer 1234567890';
1854
- client._transport.mockReturnValueOnce(mockResponse);
1855
- const response = await client.fileUploader.uploadAemAsset("https://author-stg.aem.adobe.com/adobe/repository/content/dam/projects/children-kids-group-eating-ice-cream-funny-party-72701973%20%281%29%20%2812%29%20%282%29%20%283%29.jpg;api=block_download");
1856
- expect(response).toBe(mockResponse);
1857
-
1858
- client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1859
- await client.NLWS.xtkSession.logoff();
1860
- });
1861
-
1862
- // Case 2: Bearertoken is not provided, but the client is already authenticated with session token
1863
- it("Should throw error for missing authentication token", async () => {
1864
- const client = await Mock.makeClient();
1865
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
1866
- await client.NLWS.xtkSession.logon();
1867
-
1868
- await (client.fileUploader.uploadAemAsset("https://author-stg.aem.adobe.com/adobe/repository/content/dam/projects/children-kids-group-eating-ice-cream-funny-party-72701973%20%281%29%20%2812%29%20%282%29%20%283%29.jpg;api=block_download"))
1869
- .catch( e => { expect(e).toMatchObject(CampaignException.AEM_ASSET_UPLOAD_FAILED('Bearer token is missing'))});
1870
-
1871
- client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1872
- await client.NLWS.xtkSession.logoff();
1873
- });
1874
-
1875
- // Case 3: 200 response but publishedURL is not returned
1876
- // It shouldn't occur as API also checks non-emptiness of publishedURL before returning the response.
1877
- it("Should throw error for missing publishedURL", async () => {
1878
- const client = await Mock.makeClient();
1879
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
1880
- await client.NLWS.xtkSession.logon();
1881
-
1882
- const mockResponse = {};
1883
- client._transport.mockReturnValueOnce(mockResponse);
1884
- client._bearerToken = 'Bearer 1234567890';
1885
-
1886
- await (client.fileUploader.uploadAemAsset("https://author-stg.aem.adobe.com/adobe/repository/content/dam/projects/children-kids-group-eating-ice-cream-funny-party-72701973%20%281%29%20%2812%29%20%282%29%20%283%29.jpg;api=block_download"))
1887
- .catch( e => { console.log(e); expect(e).toMatchObject(CampaignException.AEM_ASSET_UPLOAD_FAILED('Publishing failed'))});
1888
-
1889
- client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1890
- await client.NLWS.xtkSession.logoff();
1891
- });
1892
-
1893
- // Case 4: AEM Asset upload failed (reason maybe aem not reachable, assetlink not reachable, etc)
1894
- it("Should throw error when AEM Asset upload failed", async () => {
1895
- const client = await Mock.makeClient();
1896
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
1897
- await client.NLWS.xtkSession.logon();
1898
-
1899
- //const mockResponse = {};
1900
- client._transport.mockImplementation(() => {
1901
- var ex = CampaignException.AEM_ASSET_UPLOAD_FAILED('The requested content does not exist', 400);
1902
- ex.faultString = 'The requested content does not exist';
1903
- ex.detail = 'Failed to upload AEM asset';
1904
- throw ex;
1905
- });
1906
- client._bearerToken = 'Bearer 1234567890';
1907
-
1908
- var ex = CampaignException.AEM_ASSET_UPLOAD_FAILED('The requested content does not exist', 400);
1909
- await (client.fileUploader.uploadAemAsset("https://author-stg.aem.adobe.com/adobe/repository/content/dam/projects/children-kids-group-eating-ice-cream-funny-party-72701973%20%281%29%20%2812%29%20%282%29%20%283%29.jpg;api=block_download"))
1910
- .catch( e => { console.log(e); expect(e).toMatchObject(ex)});
1911
-
1912
- client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1913
- await client.NLWS.xtkSession.logoff();
1914
- });
1915
-
1916
- // Case 5: transport layer returns response as string instead of json when sdk
1917
- // used as package inside browser(Fetch API is used in browser while axios in node).
1918
- it("Should return correct response", async () => {
1919
- const client = await Mock.makeClient();
1920
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
1921
- await client.NLWS.xtkSession.logon();
1922
-
1923
- const mockResponse = {
1924
- "publishedURL" : "http://trk-inst-xyz.camp.adobe.com/res/trk-inst/409afb8798180a36591456e152b6c406.jpeg"
1925
- };
1926
- const mockResponseString = JSON.stringify(mockResponse);
1927
- client._bearerToken = 'Bearer 1234567890';
1928
- client._transport.mockReturnValueOnce(mockResponseString);
1929
- const response = await client.fileUploader.uploadAemAsset("https://author-stg.aem.adobe.com/adobe/repository/content/dam/projects/children-kids-group-eating-ice-cream-funny-party-72701973%20%281%29%20%2812%29%20%282%29%20%283%29.jpg;api=block_download");
1930
- expect(response).toMatchObject(mockResponse);
1931
-
1932
- client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1933
- await client.NLWS.xtkSession.logoff();
1934
- });
1935
- });
1936
1842
 
1937
1843
 
1938
1844
  describe("Observers", () => {
@@ -3652,415 +3558,889 @@ describe('ACC Client', function () {
3652
3558
  client._unregisterCacheChangeListener(listener);
3653
3559
  });
3654
3560
  });
3655
- describe('File uploader - on server', () => {
3656
- it('is not supported', async ()=> {
3657
- const client = await Mock.makeClient();
3658
- expect(client.fileUploader).toBeDefined()
3659
- await expect(client.fileUploader.upload({name: 'abcd.txt'})).rejects.toMatchObject({"cause": undefined, "detail": "File uploading is only supported in browser based calls.", "errorCode": "SDK-000013", "faultCode": 16384, "faultString": "\"Failed to upload file abcd.txt", "message": "500 - Error 16384: SDK-000013 \"Failed to upload file abcd.txt. File uploading is only supported in browser based calls.", "methodCall": undefined, "name": "CampaignException", "statusCode": 500})
3660
- })
3661
- })
3662
3561
 
3663
- describe('File uploader - on browser', () => {
3664
- beforeEach(() => {
3665
- global.document = dom.window.document
3666
- global.window = dom.window
3667
- global.FormData = function () {
3668
- this.append = jest.fn()
3669
- }
3562
+ describe('File uploader', () => {
3563
+ describe('upload', () => {
3564
+ describe("File uploader - on server", () => {
3565
+ it("is not supported", async () => {
3566
+ const client = await Mock.makeClient();
3567
+ expect(client.fileUploader).toBeDefined();
3568
+ await expect(
3569
+ client.fileUploader.upload({ name: "abcd.txt" })
3570
+ ).rejects.toMatchObject({
3571
+ cause: undefined,
3572
+ detail: "File uploading is only supported in browser based calls.",
3573
+ errorCode: "SDK-000013",
3574
+ faultCode: 16384,
3575
+ faultString: '"Failed to upload file abcd.txt',
3576
+ message:
3577
+ '500 - Error 16384: SDK-000013 "Failed to upload file abcd.txt. File uploading is only supported in browser based calls.',
3578
+ methodCall: undefined,
3579
+ name: "CampaignException",
3580
+ statusCode: 500,
3581
+ });
3582
+ });
3583
+ });
3670
3584
 
3671
- // Evaluates JavaScript code returned by the upload.jsp. Evaluation is done in the context
3672
- // of an iframe and will call the parent window document.controller uploadFileCallBack
3673
- // function
3674
- function evalJSReturnedByUploadJSP(js) {
3585
+ describe("File uploader - on browser", () => {
3586
+ beforeEach(() => {
3587
+ global.document = dom.window.document;
3588
+ global.window = dom.window;
3589
+ global.FormData = function () {
3590
+ this.append = jest.fn();
3591
+ };
3592
+
3593
+ // Evaluates JavaScript code returned by the upload.jsp. Evaluation is done in the context
3594
+ // of an iframe and will call the parent window document.controller uploadFileCallBack
3595
+ // function
3596
+ function evalJSReturnedByUploadJSP(js) {
3675
3597
  const data = eval(`
3676
- (function () {
3677
- var result = undefined;
3678
- window = {
3679
- parent: {
3680
- document: {
3681
- controller: {
3682
- uploadFileCallBack: (data) => {
3683
- result = data;
3598
+ (function () {
3599
+ var result = undefined;
3600
+ window = {
3601
+ parent: {
3602
+ document: {
3603
+ controller: {
3604
+ uploadFileCallBack: (data) => {
3605
+ result = data;
3606
+ }
3607
+ }
3684
3608
  }
3685
3609
  }
3686
- }
3687
- }
3688
- };
3689
- ${js};
3690
- return result;
3691
- }())
3692
- `);
3610
+ };
3611
+ ${js};
3612
+ return result;
3613
+ }())
3614
+ `);
3693
3615
  // Call real callback
3694
3616
  global.document.controller.uploadFileCallBack(data);
3695
- }
3617
+ }
3696
3618
 
3697
- // Dynamically mock the iframe.contentWindow.document.close(); function
3698
- const handler = {
3619
+ // Dynamically mock the iframe.contentWindow.document.close(); function
3620
+ const handler = {
3699
3621
  get: function (target, prop) {
3700
- if (prop === 'contentWindow') {
3701
- target.contentWindow.document.close = () => {
3702
- var scripts = target.contentWindow.document.getElementsByTagName('script');
3703
- for (let i = 0; i < scripts.length; i++) {
3704
- const script = scripts[i];
3705
- const js = DomUtil.elementValue(script);
3706
- evalJSReturnedByUploadJSP(js);
3707
- }
3622
+ if (prop === "contentWindow") {
3623
+ target.contentWindow.document.close = () => {
3624
+ var scripts =
3625
+ target.contentWindow.document.getElementsByTagName("script");
3626
+ for (let i = 0; i < scripts.length; i++) {
3627
+ const script = scripts[i];
3628
+ const js = DomUtil.elementValue(script);
3629
+ evalJSReturnedByUploadJSP(js);
3708
3630
  }
3631
+ };
3709
3632
  }
3710
3633
  return Reflect.get(...arguments);
3711
- }
3712
- };
3713
-
3634
+ },
3635
+ };
3714
3636
 
3715
- // Intercept creation of iframe. returns a proxy which will intercept the iframe.contentWindow.document.close(); function
3716
- const _origiinalCreateElement = document.createElement;
3717
- global.document.createElement = (tagName) => {
3637
+ // Intercept creation of iframe. returns a proxy which will intercept the iframe.contentWindow.document.close(); function
3638
+ const _origiinalCreateElement = document.createElement;
3639
+ global.document.createElement = (tagName) => {
3718
3640
  const r = _origiinalCreateElement.call(document, tagName);
3719
- if (tagName === 'iframe') {
3641
+ if (tagName === "iframe") {
3720
3642
  const p = new Proxy(r, handler);
3721
3643
  return p;
3722
3644
  }
3723
3645
  return r;
3724
- };
3725
-
3726
- });
3727
-
3728
- it('works with successful post upload calls', async () => {
3729
- // Create a mock client and logon
3730
- const client = await Mock.makeClient();
3731
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3732
- await client.NLWS.xtkSession.logon();
3733
-
3734
- // Mock the upload protocol
3735
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3736
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3737
- // - call to xtk:session#Write
3738
- // - call to xtk:fileRes#PublishIfNeeded
3739
- // - call to xtk:fileRes#GetURL
3740
-
3741
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
3742
- <html xmlns="http://www.w3.org/1999/xhtml">
3743
- <head>
3744
- <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){var aFilesInfo=new Array;aFilesInfo.push({paramName:"file",fileName:"test.txt",newFileName:"d8e8fca2dc0f896fd7cb4cb0031ba249.txt",md5:"d8e8fca2dc0f896fd7cb4cb0031ba249"}),window.parent.document.controller.uploadFileCallBack(aFilesInfo)}</script>
3745
- </head>
3746
- <body></body>
3747
- </html>`)); // upload.jsp
3748
-
3749
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3750
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
3751
-
3752
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3753
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3754
-
3755
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3756
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)); // xtk:fileRes#PublishIfNeeded
3646
+ };
3647
+ });
3757
3648
 
3758
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_URL_RESPONSE)); // xtk:fileRes#GetURL
3649
+ it("works with successful post upload calls", async () => {
3650
+ // Create a mock client and logon
3651
+ const client = await Mock.makeClient();
3652
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3653
+ await client.NLWS.xtkSession.logon();
3759
3654
 
3760
- // Call upload
3761
- const result = await client.fileUploader.upload({
3762
- type: 'text/html',
3763
- size: 12345
3764
- })
3765
- expect(client._transport).toHaveBeenNthCalledWith(2,
3655
+ // Mock the upload protocol
3656
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
3657
+ // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3658
+ // - call to xtk:session#Write
3659
+ // - call to xtk:fileRes#PublishIfNeeded
3660
+ // - call to xtk:fileRes#GetURL
3661
+
3662
+ client._transport.mockReturnValueOnce(
3663
+ Promise.resolve(`Ok
3664
+ <html xmlns="http://www.w3.org/1999/xhtml">
3665
+ <head>
3666
+ <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){var aFilesInfo=new Array;aFilesInfo.push({paramName:"file",fileName:"test.txt",newFileName:"d8e8fca2dc0f896fd7cb4cb0031ba249.txt",md5:"d8e8fca2dc0f896fd7cb4cb0031ba249"}),window.parent.document.controller.uploadFileCallBack(aFilesInfo)}</script>
3667
+ </head>
3668
+ <body></body>
3669
+ </html>`)
3670
+ ); // upload.jsp
3671
+
3672
+ client._transport.mockReturnValueOnce(
3673
+ Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)
3674
+ ); // GetEntityIfMoreRecentResponse - counter
3675
+ client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
3676
+
3677
+ client._transport.mockReturnValueOnce(
3678
+ Mock.GET_XTK_SESSION_SCHEMA_RESPONSE
3679
+ ); // GetEntityIfMoreRecentResponse - session
3680
+ client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3681
+
3682
+ client._transport.mockReturnValueOnce(
3683
+ Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)
3684
+ ); // GetEntityIfMoreRecentResponse - fileRes
3685
+ client._transport.mockReturnValueOnce(
3686
+ Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)
3687
+ ); // xtk:fileRes#PublishIfNeeded
3688
+
3689
+ client._transport.mockReturnValueOnce(
3690
+ Promise.resolve(Mock.GET_URL_RESPONSE)
3691
+ ); // xtk:fileRes#GetURL
3692
+
3693
+ // Call upload
3694
+ const result = await client.fileUploader.upload({
3695
+ type: "text/html",
3696
+ size: 12345,
3697
+ });
3698
+ expect(client._transport).toHaveBeenNthCalledWith(
3699
+ 2,
3766
3700
  expect.objectContaining({
3767
3701
  data: expect.anything(),
3768
3702
  url: expect.any(String),
3769
- method: 'POST',
3703
+ method: "POST",
3770
3704
  processData: false,
3771
3705
  headers: expect.objectContaining({
3772
- 'X-Security-Token': expect.any(String),
3773
- 'X-Session-Token': expect.any(String),
3706
+ "X-Security-Token": expect.any(String),
3707
+ "X-Session-Token": expect.any(String),
3774
3708
  }),
3775
3709
  })
3776
- );
3710
+ );
3777
3711
 
3778
- expect(result).toMatchObject({
3712
+ expect(result).toMatchObject({
3779
3713
  md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
3780
3714
  name: "test.txt",
3781
3715
  size: 12345,
3782
3716
  type: "text/html",
3783
- url: "http://hello.com"
3717
+ url: "http://hello.com",
3718
+ });
3784
3719
  });
3785
- })
3720
+ it("Should add extraHttpHeaders in the headers", async () => {
3721
+ // Create a mock client and logon
3722
+ const extraHttpHeaders = {'x-api-key': 'check'};
3723
+ const client = await Mock.makeClient({extraHttpHeaders});
3724
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3725
+ await client.NLWS.xtkSession.logon();
3786
3726
 
3787
- it('throws error with dependant failures', async () => {
3788
- // Create a mock client and logon
3789
- const client = await Mock.makeClient();
3790
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3791
- await client.NLWS.xtkSession.logon();
3727
+ // Mock the upload protocol
3728
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
3729
+ // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3730
+ // - call to xtk:session#Write
3731
+ // - call to xtk:fileRes#PublishIfNeeded
3732
+ // - call to xtk:fileRes#GetURL
3733
+
3734
+ client._transport.mockReturnValueOnce(
3735
+ Promise.resolve(`Ok
3736
+ <html xmlns="http://www.w3.org/1999/xhtml">
3737
+ <head>
3738
+ <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){var aFilesInfo=new Array;aFilesInfo.push({paramName:"file",fileName:"test.txt",newFileName:"d8e8fca2dc0f896fd7cb4cb0031ba249.txt",md5:"d8e8fca2dc0f896fd7cb4cb0031ba249"}),window.parent.document.controller.uploadFileCallBack(aFilesInfo)}</script>
3739
+ </head>
3740
+ <body></body>
3741
+ </html>`)
3742
+ ); // upload.jsp
3743
+
3744
+ client._transport.mockReturnValueOnce(
3745
+ Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)
3746
+ ); // GetEntityIfMoreRecentResponse - counter
3747
+ client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
3748
+
3749
+ client._transport.mockReturnValueOnce(
3750
+ Mock.GET_XTK_SESSION_SCHEMA_RESPONSE
3751
+ ); // GetEntityIfMoreRecentResponse - session
3752
+ client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3753
+
3754
+ client._transport.mockReturnValueOnce(
3755
+ Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)
3756
+ ); // GetEntityIfMoreRecentResponse - fileRes
3757
+ client._transport.mockReturnValueOnce(
3758
+ Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)
3759
+ ); // xtk:fileRes#PublishIfNeeded
3760
+
3761
+ client._transport.mockReturnValueOnce(
3762
+ Promise.resolve(Mock.GET_URL_RESPONSE)
3763
+ ); // xtk:fileRes#GetURL
3764
+
3765
+ // Call upload
3766
+ await client.fileUploader.upload({
3767
+ type: "text/html",
3768
+ size: 12345,
3769
+ });
3770
+ expect(client._transport.mock.calls[0][0]).toMatchObject(expect.objectContaining({
3771
+ headers: expect.objectContaining(extraHttpHeaders),
3772
+ }));
3773
+ });
3774
+ it("throws error with dependant failures", async () => {
3775
+ // Create a mock client and logon
3776
+ const client = await Mock.makeClient();
3777
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3778
+ await client.NLWS.xtkSession.logon();
3779
+
3780
+ // Mock the upload protocol
3781
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
3782
+ // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3783
+ // - call to xtk:session#Write
3784
+ // - call to xtk:fileRes#PublishIfNeeded
3785
+ // - call to xtk:fileRes#GetURL
3786
+
3787
+ client._transport.mockReturnValueOnce(
3788
+ Promise.reject(`Some error occurred!!!`)
3789
+ ); // upload.jsp
3790
+
3791
+ client._transport.mockReturnValueOnce(
3792
+ Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)
3793
+ ); // GetEntityIfMoreRecentResponse - counter
3794
+ client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE);
3795
+
3796
+ client._transport.mockReturnValueOnce(
3797
+ Mock.GET_XTK_SESSION_SCHEMA_RESPONSE
3798
+ ); // GetEntityIfMoreRecentResponse - session
3799
+ client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3800
+
3801
+ client._transport.mockReturnValueOnce(
3802
+ Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)
3803
+ ); // GetEntityIfMoreRecentResponse - fileRes
3804
+ client._transport.mockReturnValueOnce(
3805
+ Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)
3806
+ ); // xtk:fileRes#PublishIfNeeded
3807
+
3808
+ client._transport.mockReturnValueOnce(
3809
+ Promise.resolve(Mock.GET_URL_RESPONSE)
3810
+ ); // xtk:fileRes#GetURL
3811
+ // For async handling
3812
+ expect.assertions(1);
3813
+ // Call upload
3814
+ await client.fileUploader
3815
+ .upload({
3816
+ type: "text/html",
3817
+ size: 12345,
3818
+ name: "abcd.txt",
3819
+ })
3820
+ .catch((ex) => {
3821
+ expect(ex.message).toMatch(
3822
+ "500 - Error 16384: SDK-000013 \"Failed to upload file abcd.txt. 500 - Error calling method '/nl/jsp/uploadFile.jsp': Some error occurred!!!"
3823
+ );
3824
+ });
3825
+ });
3826
+
3827
+ it("throws error with not okay response", async () => {
3828
+ // Create a mock client and logon
3829
+ const client = await Mock.makeClient();
3830
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3831
+ await client.NLWS.xtkSession.logon();
3792
3832
 
3793
- // Mock the upload protocol
3794
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3795
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3796
- // - call to xtk:session#Write
3797
- // - call to xtk:fileRes#PublishIfNeeded
3798
- // - call to xtk:fileRes#GetURL
3833
+ // Mock the upload protocol
3834
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
3835
+ // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3836
+ // - call to xtk:session#Write
3837
+ // - call to xtk:fileRes#PublishIfNeeded
3838
+ // - call to xtk:fileRes#GetURL
3839
+
3840
+ client._transport.mockReturnValueOnce(
3841
+ Promise.resolve(`Some error occurred!!!`)
3842
+ ); // upload.jsp
3843
+
3844
+ client._transport.mockReturnValueOnce(
3845
+ Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)
3846
+ ); // GetEntityIfMoreRecentResponse - counter
3847
+ client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE);
3848
+
3849
+ client._transport.mockReturnValueOnce(
3850
+ Mock.GET_XTK_SESSION_SCHEMA_RESPONSE
3851
+ ); // GetEntityIfMoreRecentResponse - session
3852
+ client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3853
+
3854
+ client._transport.mockReturnValueOnce(
3855
+ Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)
3856
+ ); // GetEntityIfMoreRecentResponse - fileRes
3857
+ client._transport.mockReturnValueOnce(
3858
+ Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)
3859
+ ); // xtk:fileRes#PublishIfNeeded
3860
+
3861
+ client._transport.mockReturnValueOnce(
3862
+ Promise.resolve(Mock.GET_URL_RESPONSE)
3863
+ ); // xtk:fileRes#GetURL
3864
+ // For async handling
3865
+ expect.assertions(1);
3866
+ // Call upload
3867
+ await client.fileUploader
3868
+ .upload({
3869
+ type: "text/html",
3870
+ size: 12345,
3871
+ name: "abcd.txt",
3872
+ })
3873
+ .catch((ex) => {
3874
+ expect(ex.message).toMatch(
3875
+ '500 - Error 16384: SDK-000013 "Failed to upload file abcd.txt. Some error occurred!!!'
3876
+ );
3877
+ });
3878
+ });
3799
3879
 
3800
- client._transport.mockReturnValueOnce(Promise.reject(`Some error occurred!!!`)); // upload.jsp
3880
+ it("throws error with malformed response", async () => {
3881
+ // Create a mock client and logon
3882
+ const client = await Mock.makeClient();
3883
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3884
+ await client.NLWS.xtkSession.logon();
3801
3885
 
3802
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3803
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE);
3886
+ // Mock the upload protocol
3887
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
3888
+ // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3889
+ // - call to xtk:session#Write
3890
+ // - call to xtk:fileRes#PublishIfNeeded
3891
+ // - call to xtk:fileRes#GetURL
3892
+
3893
+ client._transport.mockReturnValueOnce(
3894
+ Promise.resolve(`Ok
3895
+ <html xmlns="http://www.w3.org/1999/xhtml">
3896
+ <head>
3897
+ <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){window.parent.document.controller.uploadFileCallBack([])}</script>
3898
+ </head>
3899
+ <body></body>
3900
+ </html>`)
3901
+ ); // upload.jsp
3902
+
3903
+ client._transport.mockReturnValueOnce(
3904
+ Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)
3905
+ ); // GetEntityIfMoreRecentResponse - counter
3906
+ client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE);
3907
+
3908
+ client._transport.mockReturnValueOnce(
3909
+ Mock.GET_XTK_SESSION_SCHEMA_RESPONSE
3910
+ ); // GetEntityIfMoreRecentResponse - session
3911
+ client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3912
+
3913
+ client._transport.mockReturnValueOnce(
3914
+ Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)
3915
+ ); // GetEntityIfMoreRecentResponse - fileRes
3916
+ client._transport.mockReturnValueOnce(
3917
+ Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)
3918
+ ); // xtk:fileRes#PublishIfNeeded
3919
+
3920
+ client._transport.mockReturnValueOnce(
3921
+ Promise.resolve(Mock.GET_URL_RESPONSE)
3922
+ ); // xtk:fileRes#GetURL
3923
+ // For async handling
3924
+ expect.assertions(1);
3925
+ // Call upload
3926
+ await client.fileUploader
3927
+ .upload({
3928
+ type: "text/html",
3929
+ size: 12345,
3930
+ name: "abcd.txt",
3931
+ })
3932
+ .catch((ex) => {
3933
+ expect(ex.message).toMatch(
3934
+ '500 - Error 16384: SDK-000013 "Failed to upload file abcd.txt. Malformed data:'
3935
+ );
3936
+ });
3937
+ });
3804
3938
 
3805
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3806
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3939
+ it("Should support 'publishIfNeeded' action", async () => {
3940
+ // Create a mock client and logon
3941
+ const client = await Mock.makeClient();
3942
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3943
+ await client.NLWS.xtkSession.logon();
3807
3944
 
3808
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3809
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)); // xtk:fileRes#PublishIfNeeded
3945
+ // Mock the upload protocol
3946
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
3947
+ // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3948
+ // - call to xtk:session#Write
3949
+ // - call to xtk:fileRes#PublishIfNeeded
3950
+ // - call to xtk:fileRes#GetURL
3951
+
3952
+ client._transport.mockReturnValueOnce(
3953
+ Promise.resolve(`Ok
3954
+ <html xmlns="http://www.w3.org/1999/xhtml">
3955
+ <head>
3956
+ <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){var aFilesInfo=new Array;aFilesInfo.push({paramName:"file",fileName:"test.txt",newFileName:"d8e8fca2dc0f896fd7cb4cb0031ba249.txt",md5:"d8e8fca2dc0f896fd7cb4cb0031ba249"}),window.parent.document.controller.uploadFileCallBack(aFilesInfo)}</script>
3957
+ </head>
3958
+ <body></body>
3959
+ </html>`)
3960
+ ); // upload.jsp
3961
+
3962
+ client._transport.mockReturnValueOnce(
3963
+ Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)
3964
+ ); // GetEntityIfMoreRecentResponse - counter
3965
+ client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
3966
+
3967
+ client._transport.mockReturnValueOnce(
3968
+ Mock.GET_XTK_SESSION_SCHEMA_RESPONSE
3969
+ ); // GetEntityIfMoreRecentResponse - session
3970
+ client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3971
+
3972
+ client._transport.mockReturnValueOnce(
3973
+ Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)
3974
+ ); // GetEntityIfMoreRecentResponse - fileRes
3975
+ client._transport.mockReturnValueOnce(
3976
+ Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)
3977
+ ); // xtk:fileRes#PublishIfNeeded
3978
+
3979
+ client._transport.mockReturnValueOnce(
3980
+ Promise.resolve(Mock.GET_URL_RESPONSE)
3981
+ ); // xtk:fileRes#GetURL
3982
+
3983
+ // Call upload
3984
+ const result = await client.fileUploader.upload(
3985
+ {
3986
+ type: "text/html",
3987
+ size: 12345,
3988
+ },
3989
+ { action: "publishIfNeeded" }
3990
+ );
3810
3991
 
3811
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_URL_RESPONSE)); // xtk:fileRes#GetURL
3812
- // For async handling
3813
- expect.assertions(1)
3814
- // Call upload
3815
- await client.fileUploader.upload({
3816
- type: 'text/html',
3992
+ expect(result).toMatchObject({
3993
+ md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
3994
+ name: "test.txt",
3817
3995
  size: 12345,
3818
- name: 'abcd.txt'
3819
- }).catch((ex) => {
3820
- expect(ex.message).toMatch('500 - Error 16384: SDK-000013 "Failed to upload file abcd.txt. 500 - Error calling method \'/nl/jsp/uploadFile.jsp\': Some error occurred!!!');
3821
- })
3996
+ type: "text/html",
3997
+ url: "http://hello.com",
3998
+ });
3999
+ });
3822
4000
 
3823
- })
4001
+ it("Should support 'className' action", async () => {
4002
+ // Create a mock client and logon
4003
+ const client = await Mock.makeClient();
4004
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4005
+ await client.NLWS.xtkSession.logon();
3824
4006
 
3825
- it('throws error with not okay response', async () => {
3826
- // Create a mock client and logon
3827
- const client = await Mock.makeClient();
3828
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3829
- await client.NLWS.xtkSession.logon();
4007
+ // Mock the upload protocol
4008
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
4009
+ // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
4010
+ // - call to xtk:session#Write
4011
+ // - call to xtk:fileRes#PublishIfNeeded
4012
+ // - call to xtk:fileRes#GetURL
4013
+
4014
+ client._transport.mockReturnValueOnce(
4015
+ Promise.resolve(`Ok
4016
+ <html xmlns="http://www.w3.org/1999/xhtml">
4017
+ <head>
4018
+ <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){var aFilesInfo=new Array;aFilesInfo.push({paramName:"file",fileName:"test.txt",newFileName:"d8e8fca2dc0f896fd7cb4cb0031ba249.txt",md5:"d8e8fca2dc0f896fd7cb4cb0031ba249"}),window.parent.document.controller.uploadFileCallBack(aFilesInfo)}</script>
4019
+ </head>
4020
+ <body></body>
4021
+ </html>`)
4022
+ ); // upload.jsp
4023
+
4024
+ client._transport.mockReturnValueOnce(
4025
+ Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)
4026
+ ); // GetEntityIfMoreRecentResponse - counter
4027
+ client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
4028
+
4029
+ client._transport.mockReturnValueOnce(
4030
+ Mock.GET_XTK_SESSION_SCHEMA_RESPONSE
4031
+ ); // GetEntityIfMoreRecentResponse - session
4032
+ client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
4033
+
4034
+ client._transport.mockReturnValueOnce(
4035
+ Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)
4036
+ ); // GetEntityIfMoreRecentResponse - fileRes
4037
+ client._transport.mockReturnValueOnce(
4038
+ Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)
4039
+ ); // xtk:fileRes#PublishIfNeeded
4040
+
4041
+ client._transport.mockReturnValueOnce(
4042
+ Promise.resolve(Mock.GET_URL_RESPONSE)
4043
+ ); // xtk:fileRes#GetURL
4044
+
4045
+ // Call upload
4046
+ const result = await client.fileUploader.upload(
4047
+ {
4048
+ type: "text/html",
4049
+ size: 12345,
4050
+ },
4051
+ { action: "publishIfNeeded", className: "myClass" }
4052
+ );
3830
4053
 
3831
- // Mock the upload protocol
3832
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3833
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3834
- // - call to xtk:session#Write
3835
- // - call to xtk:fileRes#PublishIfNeeded
3836
- // - call to xtk:fileRes#GetURL
4054
+ const body = global.document.body;
4055
+ const iframes = body.getElementsByTagName("iframe");
4056
+ let found = false;
4057
+ for (let i = 0; i < iframes.length; i++) {
4058
+ const iframe = iframes[i];
4059
+ if (iframe.className === "myClass") found = true;
4060
+ }
4061
+ expect(found).toBe(true);
4062
+ });
3837
4063
 
3838
- client._transport.mockReturnValueOnce(Promise.resolve(`Some error occurred!!!`)); // upload.jsp
4064
+ it("Should support 'none' action", async () => {
4065
+ // Create a mock client and logon
4066
+ const client = await Mock.makeClient();
4067
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4068
+ await client.NLWS.xtkSession.logon();
3839
4069
 
3840
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3841
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE);
4070
+ // Mock the upload protocol
4071
+ // With the "none" action, we skip the counter & publication
4072
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
4073
+ client._transport.mockReturnValueOnce(
4074
+ Promise.resolve(`Ok
4075
+ <html xmlns="http://www.w3.org/1999/xhtml">
4076
+ <head>
4077
+ <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){var aFilesInfo=new Array;aFilesInfo.push({paramName:"file",fileName:"test.txt",newFileName:"d8e8fca2dc0f896fd7cb4cb0031ba249.txt",md5:"d8e8fca2dc0f896fd7cb4cb0031ba249"}),window.parent.document.controller.uploadFileCallBack(aFilesInfo)}</script>
4078
+ </head>
4079
+ <body></body>
4080
+ </html>`)
4081
+ ); // upload.jsp
4082
+
4083
+ // Call upload
4084
+ const result = await client.fileUploader.upload(
4085
+ {
4086
+ type: "text/html",
4087
+ size: 12345,
4088
+ },
4089
+ { action: "none" }
4090
+ );
3842
4091
 
3843
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3844
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
4092
+ expect(result).toMatchObject({
4093
+ md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
4094
+ name: "test.txt",
4095
+ size: 12345,
4096
+ type: "text/html",
4097
+ });
4098
+ expect(result.url).toBeUndefined();
4099
+ });
3845
4100
 
3846
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3847
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)); // xtk:fileRes#PublishIfNeeded
4101
+ it("Should failed with invalid action", async () => {
4102
+ // Create a mock client and logon
4103
+ const client = await Mock.makeClient();
4104
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4105
+ await client.NLWS.xtkSession.logon();
3848
4106
 
3849
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_URL_RESPONSE)); // xtk:fileRes#GetURL
3850
- // For async handling
3851
- expect.assertions(1)
3852
- // Call upload
3853
- await client.fileUploader.upload({
3854
- type: 'text/html',
3855
- size: 12345,
3856
- name: 'abcd.txt'
3857
- }).catch((ex) => {
3858
- expect(ex.message).toMatch('500 - Error 16384: SDK-000013 "Failed to upload file abcd.txt. Some error occurred!!!');
3859
- })
4107
+ // Mock the upload protocol
4108
+ // With the "none" action, we skip the counter & publication
4109
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
4110
+ client._transport.mockReturnValueOnce(
4111
+ Promise.resolve(`Ok
4112
+ <html xmlns="http://www.w3.org/1999/xhtml">
4113
+ <head>
4114
+ <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){var aFilesInfo=new Array;aFilesInfo.push({paramName:"file",fileName:"test.txt",newFileName:"d8e8fca2dc0f896fd7cb4cb0031ba249.txt",md5:"d8e8fca2dc0f896fd7cb4cb0031ba249"}),window.parent.document.controller.uploadFileCallBack(aFilesInfo)}</script>
4115
+ </head>
4116
+ <body></body>
4117
+ </html>`)
4118
+ ); // upload.jsp
4119
+
4120
+ // Call upload
4121
+ await expect(
4122
+ client.fileUploader.upload(
4123
+ {
4124
+ type: "text/html",
4125
+ size: 12345,
4126
+ },
4127
+ { action: "invalid" }
4128
+ )
4129
+ ).rejects.toMatchObject({
4130
+ errorCode: "SDK-000006",
4131
+ faultCode: 16384,
4132
+ faultString: "Bad parameter 'action' with value 'invalid'",
4133
+ statusCode: 400,
4134
+ });
4135
+ });
4136
+ });
4137
+ });
3860
4138
 
3861
- })
4139
+ describe('uploadAemAsset', () => {
4140
+ // write unit test for client.fileUploader.uploadAemAsset method
4141
+ describe("Tests for client.fileUploader.uploadAemAsset method", () => {
4142
+ // Case 1: uploadAemAsset method should return a valid response
4143
+ it("Should return correct response", async () => {
4144
+ const client = await Mock.makeClient();
4145
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4146
+ await client.NLWS.xtkSession.logon();
4147
+
4148
+ const mockResponse = {
4149
+ publishedURL:
4150
+ "http://trk-inst-xyz.camp.adobe.com/res/trk-inst/409afb8798180a36591456e152b6c406.jpeg",
4151
+ };
4152
+ client._bearerToken = "Bearer 1234567890";
4153
+ client._transport.mockReturnValueOnce(mockResponse);
4154
+ const response = await client.fileUploader.uploadAemAsset(
4155
+ "https://author-stg.aem.adobe.com/adobe/repository/content/dam/projects/children-kids-group-eating-ice-cream-funny-party-72701973%20%281%29%20%2812%29%20%282%29%20%283%29.jpg;api=block_download"
4156
+ );
4157
+ expect(response).toBe(mockResponse);
4158
+
4159
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4160
+ await client.NLWS.xtkSession.logoff();
4161
+ });
3862
4162
 
3863
- it('throws error with malformed response', async () => {
3864
- // Create a mock client and logon
3865
- const client = await Mock.makeClient();
3866
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3867
- await client.NLWS.xtkSession.logon();
4163
+ it("Should add extraHttpHeaders in the headers", async () => {
4164
+ const extraHttpHeaders = {'x-api-key': 'check'};
4165
+ const client = await Mock.makeClient({extraHttpHeaders});
4166
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4167
+ await client.NLWS.xtkSession.logon();
4168
+
4169
+ const mockResponse = {
4170
+ publishedURL:
4171
+ "http://trk-inst-xyz.camp.adobe.com/res/trk-inst/409afb8798180a36591456e152b6c406.jpeg",
4172
+ };
4173
+ client._bearerToken = "Bearer 1234567890";
4174
+ client._transport.mockReturnValueOnce(mockResponse);
4175
+ await client.fileUploader.uploadAemAsset(
4176
+ "https://author-stg.aem.adobe.com/adobe/repository/content/dam/projects/children-kids-group-eating-ice-cream-funny-party-72701973%20%281%29%20%2812%29%20%282%29%20%283%29.jpg;api=block_download"
4177
+ );
4178
+ expect(client._transport.mock.calls[0][0]).toMatchObject(expect.objectContaining({
4179
+ headers: expect.objectContaining(extraHttpHeaders),
4180
+ }));
4181
+ });
4182
+
4183
+ // Case 2: Bearertoken is not provided, but the client is already authenticated with session token
4184
+ it("Should throw error for missing authentication token", async () => {
4185
+ const client = await Mock.makeClient();
4186
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4187
+ await client.NLWS.xtkSession.logon();
4188
+
4189
+ await client.fileUploader
4190
+ .uploadAemAsset(
4191
+ "https://author-stg.aem.adobe.com/adobe/repository/content/dam/projects/children-kids-group-eating-ice-cream-funny-party-72701973%20%281%29%20%2812%29%20%282%29%20%283%29.jpg;api=block_download"
4192
+ )
4193
+ .catch((e) => {
4194
+ expect(e).toMatchObject(
4195
+ CampaignException.AEM_ASSET_UPLOAD_FAILED(
4196
+ "Bearer token is missing"
4197
+ )
4198
+ );
4199
+ });
3868
4200
 
3869
- // Mock the upload protocol
3870
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3871
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3872
- // - call to xtk:session#Write
3873
- // - call to xtk:fileRes#PublishIfNeeded
3874
- // - call to xtk:fileRes#GetURL
3875
-
3876
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
3877
- <html xmlns="http://www.w3.org/1999/xhtml">
3878
- <head>
3879
- <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){window.parent.document.controller.uploadFileCallBack([])}</script>
3880
- </head>
3881
- <body></body>
3882
- </html>`)); // upload.jsp
3883
-
3884
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3885
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE);
3886
-
3887
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3888
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3889
-
3890
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3891
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)); // xtk:fileRes#PublishIfNeeded
3892
-
3893
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_URL_RESPONSE)); // xtk:fileRes#GetURL
3894
- // For async handling
3895
- expect.assertions(1)
3896
- // Call upload
3897
- await client.fileUploader.upload({
3898
- type: 'text/html',
3899
- size: 12345,
3900
- name: 'abcd.txt'
3901
- }).catch((ex) => {
3902
- expect(ex.message).toMatch('500 - Error 16384: SDK-000013 "Failed to upload file abcd.txt. Malformed data:');
3903
- })
4201
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4202
+ await client.NLWS.xtkSession.logoff();
4203
+ });
4204
+
4205
+ // Case 3: 200 response but publishedURL is not returned
4206
+ // It shouldn't occur as API also checks non-emptiness of publishedURL before returning the response.
4207
+ it("Should throw error for missing publishedURL", async () => {
4208
+ const client = await Mock.makeClient();
4209
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4210
+ await client.NLWS.xtkSession.logon();
4211
+
4212
+ const mockResponse = {};
4213
+ client._transport.mockReturnValueOnce(mockResponse);
4214
+ client._bearerToken = "Bearer 1234567890";
4215
+
4216
+ await client.fileUploader
4217
+ .uploadAemAsset(
4218
+ "https://author-stg.aem.adobe.com/adobe/repository/content/dam/projects/children-kids-group-eating-ice-cream-funny-party-72701973%20%281%29%20%2812%29%20%282%29%20%283%29.jpg;api=block_download"
4219
+ )
4220
+ .catch((e) => {
4221
+ expect(e).toMatchObject(
4222
+ CampaignException.AEM_ASSET_UPLOAD_FAILED(
4223
+ "Publishing failed"
4224
+ )
4225
+ );
4226
+ });
4227
+
4228
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4229
+ await client.NLWS.xtkSession.logoff();
4230
+ });
4231
+
4232
+ // Case 4: AEM Asset upload failed (reason maybe aem not reachable, assetlink not reachable, etc)
4233
+ it("Should throw error when AEM Asset upload failed", async () => {
4234
+ const client = await Mock.makeClient();
4235
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4236
+ await client.NLWS.xtkSession.logon();
4237
+
4238
+ //const mockResponse = {};
4239
+ client._transport.mockImplementation(() => {
4240
+ var ex = CampaignException.AEM_ASSET_UPLOAD_FAILED(
4241
+ "The requested content does not exist",
4242
+ 400
4243
+ );
4244
+ ex.faultString = "The requested content does not exist";
4245
+ ex.detail = "Failed to upload AEM asset";
4246
+ throw ex;
4247
+ });
4248
+ client._bearerToken = "Bearer 1234567890";
4249
+
4250
+ var ex = CampaignException.AEM_ASSET_UPLOAD_FAILED(
4251
+ "The requested content does not exist",
4252
+ 400
4253
+ );
4254
+ await client.fileUploader
4255
+ .uploadAemAsset(
4256
+ "https://author-stg.aem.adobe.com/adobe/repository/content/dam/projects/children-kids-group-eating-ice-cream-funny-party-72701973%20%281%29%20%2812%29%20%282%29%20%283%29.jpg;api=block_download"
4257
+ )
4258
+ .catch((e) => {
4259
+ expect(e).toMatchObject(ex);
4260
+ });
4261
+
4262
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4263
+ await client.NLWS.xtkSession.logoff();
4264
+ });
4265
+
4266
+ // Case 5: transport layer returns response as string instead of json when sdk
4267
+ // used as package inside browser(Fetch API is used in browser while axios in node).
4268
+ it("Should return correct response", async () => {
4269
+ const client = await Mock.makeClient();
4270
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4271
+ await client.NLWS.xtkSession.logon();
4272
+
4273
+ const mockResponse = {
4274
+ publishedURL:
4275
+ "http://trk-inst-xyz.camp.adobe.com/res/trk-inst/409afb8798180a36591456e152b6c406.jpeg",
4276
+ };
4277
+ const mockResponseString = JSON.stringify(mockResponse);
4278
+ client._bearerToken = "Bearer 1234567890";
4279
+ client._transport.mockReturnValueOnce(mockResponseString);
4280
+ const response = await client.fileUploader.uploadAemAsset(
4281
+ "https://author-stg.aem.adobe.com/adobe/repository/content/dam/projects/children-kids-group-eating-ice-cream-funny-party-72701973%20%281%29%20%2812%29%20%282%29%20%283%29.jpg;api=block_download"
4282
+ );
4283
+ expect(response).toMatchObject(mockResponse);
4284
+
4285
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4286
+ await client.NLWS.xtkSession.logoff();
4287
+ });
4288
+ });
3904
4289
  });
3905
4290
 
3906
- it("Should support 'publishIfNeeded' action", async () => {
3907
- // Create a mock client and logon
4291
+ describe("download", () => {
4292
+ let fileOptions;
4293
+ const fileExt = 'csv';
4294
+ const fileMd5 = 'a88333b4e8b523a1c4fca8f3b378b8e0';
4295
+
4296
+ beforeEach(() => {
4297
+ fileOptions = {
4298
+ fileName: "myFile",
4299
+ contentType: "text/csv",
4300
+ }
4301
+ });
4302
+
4303
+ it("Should return correct response with all correct params", async () => {
3908
4304
  const client = await Mock.makeClient();
3909
4305
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3910
4306
  await client.NLWS.xtkSession.logon();
3911
4307
 
3912
- // Mock the upload protocol
3913
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3914
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3915
- // - call to xtk:session#Write
3916
- // - call to xtk:fileRes#PublishIfNeeded
3917
- // - call to xtk:fileRes#GetURL
3918
-
3919
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
3920
- <html xmlns="http://www.w3.org/1999/xhtml">
3921
- <head>
3922
- <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){var aFilesInfo=new Array;aFilesInfo.push({paramName:"file",fileName:"test.txt",newFileName:"d8e8fca2dc0f896fd7cb4cb0031ba249.txt",md5:"d8e8fca2dc0f896fd7cb4cb0031ba249"}),window.parent.document.controller.uploadFileCallBack(aFilesInfo)}</script>
3923
- </head>
3924
- <body></body>
3925
- </html>`)); // upload.jsp
4308
+ const mockResponse = Mock.FILE_DOWNLOAD_RESPONSE;
4309
+ client._transport.mockReturnValueOnce(mockResponse);
3926
4310
 
3927
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3928
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
4311
+ const response = await client.fileUploader.download(
4312
+ fileMd5,
4313
+ fileExt,
4314
+ fileOptions,
4315
+ );
3929
4316
 
3930
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3931
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
4317
+ expect(response).toStrictEqual(mockResponse);
4318
+ });
3932
4319
 
3933
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3934
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)); // xtk:fileRes#PublishIfNeeded
4320
+ it("Should add extraHttpHeaders in the headers", async () => {
4321
+ const extraHttpHeaders = {'x-api-key': 'check'}
4322
+ const client = await Mock.makeClient({extraHttpHeaders});
4323
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4324
+ await client.NLWS.xtkSession.logon();
3935
4325
 
3936
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_URL_RESPONSE)); // xtk:fileRes#GetURL
4326
+ const mockResponse = Mock.FILE_DOWNLOAD_RESPONSE;
4327
+ client._transport.mockReturnValueOnce(mockResponse);
3937
4328
 
3938
- // Call upload
3939
- const result = await client.fileUploader.upload({
3940
- type: 'text/html',
3941
- size: 12345
3942
- }, { action: "publishIfNeeded" });
4329
+ await client.fileUploader.download(
4330
+ fileMd5,
4331
+ fileExt,
4332
+ fileOptions,
4333
+ );
3943
4334
 
3944
- expect(result).toMatchObject({
3945
- md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
3946
- name: "test.txt",
3947
- size: 12345,
3948
- type: "text/html",
3949
- url: "http://hello.com"
3950
- });
3951
- });
4335
+ expect(client._transport.mock.calls[0][0]).toMatchObject(expect.objectContaining({
4336
+ headers: expect.objectContaining(extraHttpHeaders),
4337
+ }));
4338
+ });
3952
4339
 
3953
- it("Should support 'className' action", async () => {
3954
- // Create a mock client and logon
4340
+ it("Should return correct response without fileOptions", async () => {
3955
4341
  const client = await Mock.makeClient();
3956
4342
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3957
4343
  await client.NLWS.xtkSession.logon();
3958
4344
 
3959
- // Mock the upload protocol
3960
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3961
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3962
- // - call to xtk:session#Write
3963
- // - call to xtk:fileRes#PublishIfNeeded
3964
- // - call to xtk:fileRes#GetURL
3965
-
3966
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
3967
- <html xmlns="http://www.w3.org/1999/xhtml">
3968
- <head>
3969
- <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){var aFilesInfo=new Array;aFilesInfo.push({paramName:"file",fileName:"test.txt",newFileName:"d8e8fca2dc0f896fd7cb4cb0031ba249.txt",md5:"d8e8fca2dc0f896fd7cb4cb0031ba249"}),window.parent.document.controller.uploadFileCallBack(aFilesInfo)}</script>
3970
- </head>
3971
- <body></body>
3972
- </html>`)); // upload.jsp
3973
-
3974
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3975
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
3976
-
3977
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3978
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3979
-
3980
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3981
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)); // xtk:fileRes#PublishIfNeeded
3982
-
3983
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_URL_RESPONSE)); // xtk:fileRes#GetURL
3984
-
3985
- // Call upload
3986
- const result = await client.fileUploader.upload({
3987
- type: 'text/html',
3988
- size: 12345
3989
- }, { action: "publishIfNeeded", className: "myClass" });
3990
-
3991
- const body = global.document.body;
3992
- const iframes = body.getElementsByTagName("iframe");
3993
- let found = false;
3994
- for (let i=0; i<iframes.length; i++) {
3995
- const iframe = iframes[i];
3996
- if (iframe.className === "myClass")
3997
- found = true;
3998
- }
3999
- expect(found).toBe(true);
4000
- });
4345
+ const mockResponse = Mock.FILE_DOWNLOAD_RESPONSE;
4346
+ client._transport.mockReturnValueOnce(mockResponse);
4347
+
4348
+ const response = await client.fileUploader.download(
4349
+ fileMd5,
4350
+ fileExt,
4351
+ );
4001
4352
 
4002
- it("Should support 'none' action", async () => {
4003
- // Create a mock client and logon
4353
+ expect(response).toStrictEqual(mockResponse);
4354
+ });
4355
+
4356
+ it("Should return error of bad params", async () => {
4004
4357
  const client = await Mock.makeClient();
4005
4358
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4006
4359
  await client.NLWS.xtkSession.logon();
4007
4360
 
4008
- // Mock the upload protocol
4009
- // With the "none" action, we skip the counter & publication
4010
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
4011
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
4012
- <html xmlns="http://www.w3.org/1999/xhtml">
4013
- <head>
4014
- <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){var aFilesInfo=new Array;aFilesInfo.push({paramName:"file",fileName:"test.txt",newFileName:"d8e8fca2dc0f896fd7cb4cb0031ba249.txt",md5:"d8e8fca2dc0f896fd7cb4cb0031ba249"}),window.parent.document.controller.uploadFileCallBack(aFilesInfo)}</script>
4015
- </head>
4016
- <body></body>
4017
- </html>`)); // upload.jsp
4018
-
4019
- // Call upload
4020
- const result = await client.fileUploader.upload({
4021
- type: 'text/html',
4022
- size: 12345
4023
- }, { action: "none" });
4024
-
4025
- expect(result).toMatchObject({
4026
- md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
4027
- name: "test.txt",
4028
- size: 12345,
4029
- type: "text/html",
4361
+ const mockResponse = Mock.FILE_DOWNLOAD_RESPONSE;
4362
+ client._transport.mockReturnValueOnce(mockResponse);
4363
+
4364
+ // md5 is not provided in params
4365
+ await client.fileUploader.download().catch((ex) => {
4366
+ expect(ex).toMatchObject(
4367
+ CampaignException.BAD_PARAMETER(
4368
+ "md5",
4369
+ undefined,
4370
+ "'md5' is mandatory parameter with type as 'string' for download file."
4371
+ )
4372
+ );
4030
4373
  });
4031
- expect(result.url).toBeUndefined();
4032
- });
4033
4374
 
4034
- it("Should failed with invalid action", async () => {
4035
- // Create a mock client and logon
4375
+ // md5 is not a string
4376
+ await client.fileUploader.download(
4377
+ fileOptions,
4378
+ fileMd5,
4379
+ ).catch((ex) => {
4380
+ expect(ex).toMatchObject(
4381
+ CampaignException.BAD_PARAMETER(
4382
+ "md5",
4383
+ fileOptions,
4384
+ "'md5' is mandatory parameter with type as 'string' for download file."
4385
+ )
4386
+ );
4387
+ });
4388
+
4389
+ // ext is not provided in params
4390
+ await client.fileUploader.download(fileMd5).catch((ex) => {
4391
+ expect(ex).toMatchObject(
4392
+ CampaignException.BAD_PARAMETER(
4393
+ "ext",
4394
+ undefined,
4395
+ "'ext' is mandatory parameter with type as 'string' for download file."
4396
+ )
4397
+ );
4398
+ });
4399
+
4400
+ // ext is not a string
4401
+ await client.fileUploader.download(
4402
+ fileMd5,
4403
+ fileOptions,
4404
+ ).catch((ex) => {
4405
+ expect(ex).toMatchObject(
4406
+ CampaignException.BAD_PARAMETER(
4407
+ "ext",
4408
+ fileOptions,
4409
+ "'ext' is mandatory parameter with type as 'string' for download file."
4410
+ )
4411
+ );
4412
+ });
4413
+ });
4414
+
4415
+ it("Should throw CampaignException when file download fails", async () => {
4036
4416
  const client = await Mock.makeClient();
4037
4417
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4038
4418
  await client.NLWS.xtkSession.logon();
4039
4419
 
4040
- // Mock the upload protocol
4041
- // With the "none" action, we skip the counter & publication
4042
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
4043
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
4044
- <html xmlns="http://www.w3.org/1999/xhtml">
4045
- <head>
4046
- <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){var aFilesInfo=new Array;aFilesInfo.push({paramName:"file",fileName:"test.txt",newFileName:"d8e8fca2dc0f896fd7cb4cb0031ba249.txt",md5:"d8e8fca2dc0f896fd7cb4cb0031ba249"}),window.parent.document.controller.uploadFileCallBack(aFilesInfo)}</script>
4047
- </head>
4048
- <body></body>
4049
- </html>`)); // upload.jsp
4050
-
4051
- // Call upload
4052
- await expect(client.fileUploader.upload({
4053
- type: 'text/html',
4054
- size: 12345
4055
- }, { action: "invalid" })).rejects.toMatchObject({
4056
- errorCode: "SDK-000006",
4057
- "faultCode": 16384,
4058
- "faultString": "Bad parameter 'action' with value 'invalid'",
4059
- "statusCode": 400
4420
+
4421
+ var mockException = CampaignException.FILE_DOWNLOAD_FAILED(
4422
+ "Failed to download the requested file",
4423
+ 400
4424
+ );
4425
+
4426
+ client._transport.mockImplementation(() => {
4427
+ throw 'Something went wrong!';
4060
4428
  });
4429
+
4430
+ await client.fileUploader
4431
+ .download(
4432
+ fileMd5,
4433
+ fileExt,
4434
+ fileOptions,
4435
+ )
4436
+ .catch((actualException) => {
4437
+ expect(actualException.message).toStrictEqual("500 - Error 16384: SDK-000018 \"Failed to download file a88333b4e8b523a1c4fca8f3b378b8e0. 500 - Error calling method '/nl/jsp/downloadFile.jsp?md5=a88333b4e8b523a1c4fca8f3b378b8e0&ext=csv&fileName=myFile&contentType=text%2Fcsv': Something went wrong!")
4438
+ });
4439
+ });
4061
4440
  });
4062
4441
  });
4063
4442
 
4443
+
4064
4444
  describe("Setting the xtkschema attribute", () => {
4065
4445
  it("Should support setting explicitely setting the xtkschema", async() => {
4066
4446
  const client = await Mock.makeClient();