@adobe/acc-js-sdk 1.1.47 → 1.1.48

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,796 @@ 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
- })
3786
3720
 
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();
3721
+ it("throws error with dependant failures", async () => {
3722
+ // Create a mock client and logon
3723
+ const client = await Mock.makeClient();
3724
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3725
+ await client.NLWS.xtkSession.logon();
3792
3726
 
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
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.reject(`Some error occurred!!!`)
3736
+ ); // upload.jsp
3737
+
3738
+ client._transport.mockReturnValueOnce(
3739
+ Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)
3740
+ ); // GetEntityIfMoreRecentResponse - counter
3741
+ client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE);
3742
+
3743
+ client._transport.mockReturnValueOnce(
3744
+ Mock.GET_XTK_SESSION_SCHEMA_RESPONSE
3745
+ ); // GetEntityIfMoreRecentResponse - session
3746
+ client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3747
+
3748
+ client._transport.mockReturnValueOnce(
3749
+ Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)
3750
+ ); // GetEntityIfMoreRecentResponse - fileRes
3751
+ client._transport.mockReturnValueOnce(
3752
+ Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)
3753
+ ); // xtk:fileRes#PublishIfNeeded
3754
+
3755
+ client._transport.mockReturnValueOnce(
3756
+ Promise.resolve(Mock.GET_URL_RESPONSE)
3757
+ ); // xtk:fileRes#GetURL
3758
+ // For async handling
3759
+ expect.assertions(1);
3760
+ // Call upload
3761
+ await client.fileUploader
3762
+ .upload({
3763
+ type: "text/html",
3764
+ size: 12345,
3765
+ name: "abcd.txt",
3766
+ })
3767
+ .catch((ex) => {
3768
+ expect(ex.message).toMatch(
3769
+ "500 - Error 16384: SDK-000013 \"Failed to upload file abcd.txt. 500 - Error calling method '/nl/jsp/uploadFile.jsp': Some error occurred!!!"
3770
+ );
3771
+ });
3772
+ });
3799
3773
 
3800
- client._transport.mockReturnValueOnce(Promise.reject(`Some error occurred!!!`)); // upload.jsp
3774
+ it("throws error with not okay response", 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();
3801
3779
 
3802
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3803
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE);
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.resolve(`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. Some error occurred!!!'
3823
+ );
3824
+ });
3825
+ });
3804
3826
 
3805
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3806
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3827
+ it("throws error with malformed 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();
3807
3832
 
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
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(`Ok
3842
+ <html xmlns="http://www.w3.org/1999/xhtml">
3843
+ <head>
3844
+ <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){window.parent.document.controller.uploadFileCallBack([])}</script>
3845
+ </head>
3846
+ <body></body>
3847
+ </html>`)
3848
+ ); // upload.jsp
3849
+
3850
+ client._transport.mockReturnValueOnce(
3851
+ Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)
3852
+ ); // GetEntityIfMoreRecentResponse - counter
3853
+ client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE);
3854
+
3855
+ client._transport.mockReturnValueOnce(
3856
+ Mock.GET_XTK_SESSION_SCHEMA_RESPONSE
3857
+ ); // GetEntityIfMoreRecentResponse - session
3858
+ client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3859
+
3860
+ client._transport.mockReturnValueOnce(
3861
+ Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)
3862
+ ); // GetEntityIfMoreRecentResponse - fileRes
3863
+ client._transport.mockReturnValueOnce(
3864
+ Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)
3865
+ ); // xtk:fileRes#PublishIfNeeded
3866
+
3867
+ client._transport.mockReturnValueOnce(
3868
+ Promise.resolve(Mock.GET_URL_RESPONSE)
3869
+ ); // xtk:fileRes#GetURL
3870
+ // For async handling
3871
+ expect.assertions(1);
3872
+ // Call upload
3873
+ await client.fileUploader
3874
+ .upload({
3875
+ type: "text/html",
3876
+ size: 12345,
3877
+ name: "abcd.txt",
3878
+ })
3879
+ .catch((ex) => {
3880
+ expect(ex.message).toMatch(
3881
+ '500 - Error 16384: SDK-000013 "Failed to upload file abcd.txt. Malformed data:'
3882
+ );
3883
+ });
3884
+ });
3810
3885
 
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',
3817
- 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
- })
3886
+ it("Should support 'publishIfNeeded' action", async () => {
3887
+ // Create a mock client and logon
3888
+ const client = await Mock.makeClient();
3889
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3890
+ await client.NLWS.xtkSession.logon();
3822
3891
 
3823
- })
3892
+ // Mock the upload protocol
3893
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
3894
+ // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3895
+ // - call to xtk:session#Write
3896
+ // - call to xtk:fileRes#PublishIfNeeded
3897
+ // - call to xtk:fileRes#GetURL
3898
+
3899
+ client._transport.mockReturnValueOnce(
3900
+ Promise.resolve(`Ok
3901
+ <html xmlns="http://www.w3.org/1999/xhtml">
3902
+ <head>
3903
+ <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>
3904
+ </head>
3905
+ <body></body>
3906
+ </html>`)
3907
+ ); // upload.jsp
3908
+
3909
+ client._transport.mockReturnValueOnce(
3910
+ Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)
3911
+ ); // GetEntityIfMoreRecentResponse - counter
3912
+ client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
3913
+
3914
+ client._transport.mockReturnValueOnce(
3915
+ Mock.GET_XTK_SESSION_SCHEMA_RESPONSE
3916
+ ); // GetEntityIfMoreRecentResponse - session
3917
+ client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3918
+
3919
+ client._transport.mockReturnValueOnce(
3920
+ Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)
3921
+ ); // GetEntityIfMoreRecentResponse - fileRes
3922
+ client._transport.mockReturnValueOnce(
3923
+ Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)
3924
+ ); // xtk:fileRes#PublishIfNeeded
3925
+
3926
+ client._transport.mockReturnValueOnce(
3927
+ Promise.resolve(Mock.GET_URL_RESPONSE)
3928
+ ); // xtk:fileRes#GetURL
3929
+
3930
+ // Call upload
3931
+ const result = await client.fileUploader.upload(
3932
+ {
3933
+ type: "text/html",
3934
+ size: 12345,
3935
+ },
3936
+ { action: "publishIfNeeded" }
3937
+ );
3824
3938
 
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();
3939
+ expect(result).toMatchObject({
3940
+ md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
3941
+ name: "test.txt",
3942
+ size: 12345,
3943
+ type: "text/html",
3944
+ url: "http://hello.com",
3945
+ });
3946
+ });
3830
3947
 
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
3948
+ it("Should support 'className' action", async () => {
3949
+ // Create a mock client and logon
3950
+ const client = await Mock.makeClient();
3951
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3952
+ await client.NLWS.xtkSession.logon();
3837
3953
 
3838
- client._transport.mockReturnValueOnce(Promise.resolve(`Some error occurred!!!`)); // upload.jsp
3954
+ // Mock the upload protocol
3955
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
3956
+ // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3957
+ // - call to xtk:session#Write
3958
+ // - call to xtk:fileRes#PublishIfNeeded
3959
+ // - call to xtk:fileRes#GetURL
3960
+
3961
+ client._transport.mockReturnValueOnce(
3962
+ Promise.resolve(`Ok
3963
+ <html xmlns="http://www.w3.org/1999/xhtml">
3964
+ <head>
3965
+ <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>
3966
+ </head>
3967
+ <body></body>
3968
+ </html>`)
3969
+ ); // upload.jsp
3970
+
3971
+ client._transport.mockReturnValueOnce(
3972
+ Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)
3973
+ ); // GetEntityIfMoreRecentResponse - counter
3974
+ client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
3975
+
3976
+ client._transport.mockReturnValueOnce(
3977
+ Mock.GET_XTK_SESSION_SCHEMA_RESPONSE
3978
+ ); // GetEntityIfMoreRecentResponse - session
3979
+ client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3980
+
3981
+ client._transport.mockReturnValueOnce(
3982
+ Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)
3983
+ ); // GetEntityIfMoreRecentResponse - fileRes
3984
+ client._transport.mockReturnValueOnce(
3985
+ Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)
3986
+ ); // xtk:fileRes#PublishIfNeeded
3987
+
3988
+ client._transport.mockReturnValueOnce(
3989
+ Promise.resolve(Mock.GET_URL_RESPONSE)
3990
+ ); // xtk:fileRes#GetURL
3991
+
3992
+ // Call upload
3993
+ const result = await client.fileUploader.upload(
3994
+ {
3995
+ type: "text/html",
3996
+ size: 12345,
3997
+ },
3998
+ { action: "publishIfNeeded", className: "myClass" }
3999
+ );
3839
4000
 
3840
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3841
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE);
4001
+ const body = global.document.body;
4002
+ const iframes = body.getElementsByTagName("iframe");
4003
+ let found = false;
4004
+ for (let i = 0; i < iframes.length; i++) {
4005
+ const iframe = iframes[i];
4006
+ if (iframe.className === "myClass") found = true;
4007
+ }
4008
+ expect(found).toBe(true);
4009
+ });
3842
4010
 
3843
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3844
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
4011
+ it("Should support 'none' action", async () => {
4012
+ // Create a mock client and logon
4013
+ const client = await Mock.makeClient();
4014
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4015
+ await client.NLWS.xtkSession.logon();
3845
4016
 
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
4017
+ // Mock the upload protocol
4018
+ // With the "none" action, we skip the counter & publication
4019
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
4020
+ client._transport.mockReturnValueOnce(
4021
+ Promise.resolve(`Ok
4022
+ <html xmlns="http://www.w3.org/1999/xhtml">
4023
+ <head>
4024
+ <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>
4025
+ </head>
4026
+ <body></body>
4027
+ </html>`)
4028
+ ); // upload.jsp
4029
+
4030
+ // Call upload
4031
+ const result = await client.fileUploader.upload(
4032
+ {
4033
+ type: "text/html",
4034
+ size: 12345,
4035
+ },
4036
+ { action: "none" }
4037
+ );
3848
4038
 
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',
4039
+ expect(result).toMatchObject({
4040
+ md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
4041
+ name: "test.txt",
3855
4042
  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
- })
3860
-
3861
- })
4043
+ type: "text/html",
4044
+ });
4045
+ expect(result.url).toBeUndefined();
4046
+ });
3862
4047
 
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();
4048
+ it("Should failed with invalid action", async () => {
4049
+ // Create a mock client and logon
4050
+ const client = await Mock.makeClient();
4051
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4052
+ await client.NLWS.xtkSession.logon();
3868
4053
 
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
- })
4054
+ // Mock the upload protocol
4055
+ // With the "none" action, we skip the counter & publication
4056
+ // - the upload.jsp (which returns the content of an iframe and JS to eval)
4057
+ client._transport.mockReturnValueOnce(
4058
+ Promise.resolve(`Ok
4059
+ <html xmlns="http://www.w3.org/1999/xhtml">
4060
+ <head>
4061
+ <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>
4062
+ </head>
4063
+ <body></body>
4064
+ </html>`)
4065
+ ); // upload.jsp
4066
+
4067
+ // Call upload
4068
+ await expect(
4069
+ client.fileUploader.upload(
4070
+ {
4071
+ type: "text/html",
4072
+ size: 12345,
4073
+ },
4074
+ { action: "invalid" }
4075
+ )
4076
+ ).rejects.toMatchObject({
4077
+ errorCode: "SDK-000006",
4078
+ faultCode: 16384,
4079
+ faultString: "Bad parameter 'action' with value 'invalid'",
4080
+ statusCode: 400,
4081
+ });
4082
+ });
4083
+ });
3904
4084
  });
3905
4085
 
3906
- it("Should support 'publishIfNeeded' action", async () => {
3907
- // Create a mock client and logon
3908
- const client = await Mock.makeClient();
3909
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3910
- await client.NLWS.xtkSession.logon();
3911
-
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
4086
+ describe('uploadAemAsset', () => {
4087
+ // write unit test for client.fileUploader.uploadAemAsset method
4088
+ describe("Tests for client.fileUploader.uploadAemAsset method", () => {
4089
+ // Case 1: uploadAemAsset method should return a valid response
4090
+ it("Should return correct response", async () => {
4091
+ const client = await Mock.makeClient();
4092
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4093
+ await client.NLWS.xtkSession.logon();
4094
+
4095
+ const mockResponse = {
4096
+ publishedURL:
4097
+ "http://trk-inst-xyz.camp.adobe.com/res/trk-inst/409afb8798180a36591456e152b6c406.jpeg",
4098
+ };
4099
+ client._bearerToken = "Bearer 1234567890";
4100
+ client._transport.mockReturnValueOnce(mockResponse);
4101
+ const response = await client.fileUploader.uploadAemAsset(
4102
+ "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"
4103
+ );
4104
+ expect(response).toBe(mockResponse);
4105
+
4106
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4107
+ await client.NLWS.xtkSession.logoff();
4108
+ });
3918
4109
 
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
4110
+ // Case 2: Bearertoken is not provided, but the client is already authenticated with session token
4111
+ it("Should throw error for missing authentication token", async () => {
4112
+ const client = await Mock.makeClient();
4113
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4114
+ await client.NLWS.xtkSession.logon();
4115
+
4116
+ await client.fileUploader
4117
+ .uploadAemAsset(
4118
+ "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"
4119
+ )
4120
+ .catch((e) => {
4121
+ expect(e).toMatchObject(
4122
+ CampaignException.AEM_ASSET_UPLOAD_FAILED(
4123
+ "Bearer token is missing"
4124
+ )
4125
+ );
4126
+ });
3926
4127
 
3927
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3928
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
4128
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4129
+ await client.NLWS.xtkSession.logoff();
4130
+ });
3929
4131
 
3930
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3931
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
4132
+ // Case 3: 200 response but publishedURL is not returned
4133
+ // It shouldn't occur as API also checks non-emptiness of publishedURL before returning the response.
4134
+ it("Should throw error for missing publishedURL", async () => {
4135
+ const client = await Mock.makeClient();
4136
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4137
+ await client.NLWS.xtkSession.logon();
4138
+
4139
+ const mockResponse = {};
4140
+ client._transport.mockReturnValueOnce(mockResponse);
4141
+ client._bearerToken = "Bearer 1234567890";
4142
+
4143
+ await client.fileUploader
4144
+ .uploadAemAsset(
4145
+ "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"
4146
+ )
4147
+ .catch((e) => {
4148
+ expect(e).toMatchObject(
4149
+ CampaignException.AEM_ASSET_UPLOAD_FAILED(
4150
+ "Publishing failed"
4151
+ )
4152
+ );
4153
+ });
3932
4154
 
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
4155
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4156
+ await client.NLWS.xtkSession.logoff();
4157
+ });
3935
4158
 
3936
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_URL_RESPONSE)); // xtk:fileRes#GetURL
4159
+ // Case 4: AEM Asset upload failed (reason maybe aem not reachable, assetlink not reachable, etc)
4160
+ it("Should throw error when AEM Asset upload failed", async () => {
4161
+ const client = await Mock.makeClient();
4162
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4163
+ await client.NLWS.xtkSession.logon();
4164
+
4165
+ //const mockResponse = {};
4166
+ client._transport.mockImplementation(() => {
4167
+ var ex = CampaignException.AEM_ASSET_UPLOAD_FAILED(
4168
+ "The requested content does not exist",
4169
+ 400
4170
+ );
4171
+ ex.faultString = "The requested content does not exist";
4172
+ ex.detail = "Failed to upload AEM asset";
4173
+ throw ex;
4174
+ });
4175
+ client._bearerToken = "Bearer 1234567890";
4176
+
4177
+ var ex = CampaignException.AEM_ASSET_UPLOAD_FAILED(
4178
+ "The requested content does not exist",
4179
+ 400
4180
+ );
4181
+ await client.fileUploader
4182
+ .uploadAemAsset(
4183
+ "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"
4184
+ )
4185
+ .catch((e) => {
4186
+ expect(e).toMatchObject(ex);
4187
+ });
3937
4188
 
3938
- // Call upload
3939
- const result = await client.fileUploader.upload({
3940
- type: 'text/html',
3941
- size: 12345
3942
- }, { action: "publishIfNeeded" });
4189
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4190
+ await client.NLWS.xtkSession.logoff();
4191
+ });
3943
4192
 
3944
- expect(result).toMatchObject({
3945
- md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
3946
- name: "test.txt",
3947
- size: 12345,
3948
- type: "text/html",
3949
- url: "http://hello.com"
4193
+ // Case 5: transport layer returns response as string instead of json when sdk
4194
+ // used as package inside browser(Fetch API is used in browser while axios in node).
4195
+ it("Should return correct response", async () => {
4196
+ const client = await Mock.makeClient();
4197
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4198
+ await client.NLWS.xtkSession.logon();
4199
+
4200
+ const mockResponse = {
4201
+ publishedURL:
4202
+ "http://trk-inst-xyz.camp.adobe.com/res/trk-inst/409afb8798180a36591456e152b6c406.jpeg",
4203
+ };
4204
+ const mockResponseString = JSON.stringify(mockResponse);
4205
+ client._bearerToken = "Bearer 1234567890";
4206
+ client._transport.mockReturnValueOnce(mockResponseString);
4207
+ const response = await client.fileUploader.uploadAemAsset(
4208
+ "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"
4209
+ );
4210
+ expect(response).toMatchObject(mockResponse);
4211
+
4212
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4213
+ await client.NLWS.xtkSession.logoff();
3950
4214
  });
4215
+ });
3951
4216
  });
3952
4217
 
3953
- it("Should support 'className' action", async () => {
3954
- // Create a mock client and logon
4218
+ describe("download", () => {
4219
+ let fileOptions;
4220
+ const fileExt = 'csv';
4221
+ const fileMd5 = 'a88333b4e8b523a1c4fca8f3b378b8e0';
4222
+
4223
+ beforeEach(() => {
4224
+ fileOptions = {
4225
+ fileName: "myFile",
4226
+ contentType: "text/csv",
4227
+ }
4228
+ });
4229
+
4230
+ it("Should return correct response with all correct params", async () => {
3955
4231
  const client = await Mock.makeClient();
3956
4232
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3957
4233
  await client.NLWS.xtkSession.logon();
3958
4234
 
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
- });
4235
+ const mockResponse = Mock.FILE_DOWNLOAD_RESPONSE;
4236
+ client._transport.mockReturnValueOnce(mockResponse);
4237
+
4238
+ const response = await client.fileUploader.download(
4239
+ fileMd5,
4240
+ fileExt,
4241
+ fileOptions,
4242
+ );
4001
4243
 
4002
- it("Should support 'none' action", async () => {
4003
- // Create a mock client and logon
4244
+ expect(response).toStrictEqual(mockResponse);
4245
+ });
4246
+
4247
+ it("Should return correct response without fileOptions", async () => {
4004
4248
  const client = await Mock.makeClient();
4005
4249
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4006
4250
  await client.NLWS.xtkSession.logon();
4007
4251
 
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",
4252
+ const mockResponse = Mock.FILE_DOWNLOAD_RESPONSE;
4253
+ client._transport.mockReturnValueOnce(mockResponse);
4254
+
4255
+ const response = await client.fileUploader.download(
4256
+ fileMd5,
4257
+ fileExt,
4258
+ );
4259
+
4260
+ expect(response).toStrictEqual(mockResponse);
4261
+ });
4262
+
4263
+ it("Should return error of bad params", async () => {
4264
+ const client = await Mock.makeClient();
4265
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4266
+ await client.NLWS.xtkSession.logon();
4267
+
4268
+ const mockResponse = Mock.FILE_DOWNLOAD_RESPONSE;
4269
+ client._transport.mockReturnValueOnce(mockResponse);
4270
+
4271
+ // md5 is not provided in params
4272
+ await client.fileUploader.download().catch((ex) => {
4273
+ expect(ex).toMatchObject(
4274
+ CampaignException.BAD_PARAMETER(
4275
+ "md5",
4276
+ undefined,
4277
+ "'md5' is mandatory parameter with type as 'string' for download file."
4278
+ )
4279
+ );
4030
4280
  });
4031
- expect(result.url).toBeUndefined();
4032
- });
4033
4281
 
4034
- it("Should failed with invalid action", async () => {
4035
- // Create a mock client and logon
4282
+ // md5 is not a string
4283
+ await client.fileUploader.download(
4284
+ fileOptions,
4285
+ fileMd5,
4286
+ ).catch((ex) => {
4287
+ expect(ex).toMatchObject(
4288
+ CampaignException.BAD_PARAMETER(
4289
+ "md5",
4290
+ fileOptions,
4291
+ "'md5' is mandatory parameter with type as 'string' for download file."
4292
+ )
4293
+ );
4294
+ });
4295
+
4296
+ // ext is not provided in params
4297
+ await client.fileUploader.download(fileMd5).catch((ex) => {
4298
+ expect(ex).toMatchObject(
4299
+ CampaignException.BAD_PARAMETER(
4300
+ "ext",
4301
+ undefined,
4302
+ "'ext' is mandatory parameter with type as 'string' for download file."
4303
+ )
4304
+ );
4305
+ });
4306
+
4307
+ // ext is not a string
4308
+ await client.fileUploader.download(
4309
+ fileMd5,
4310
+ fileOptions,
4311
+ ).catch((ex) => {
4312
+ expect(ex).toMatchObject(
4313
+ CampaignException.BAD_PARAMETER(
4314
+ "ext",
4315
+ fileOptions,
4316
+ "'ext' is mandatory parameter with type as 'string' for download file."
4317
+ )
4318
+ );
4319
+ });
4320
+ });
4321
+
4322
+ it("Should throw CampaignException when file download fails", async () => {
4036
4323
  const client = await Mock.makeClient();
4037
4324
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4038
4325
  await client.NLWS.xtkSession.logon();
4039
4326
 
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
4327
+
4328
+ var mockException = CampaignException.FILE_DOWNLOAD_FAILED(
4329
+ "Failed to download the requested file",
4330
+ 400
4331
+ );
4332
+
4333
+ client._transport.mockImplementation(() => {
4334
+ throw 'Something went wrong!';
4060
4335
  });
4336
+
4337
+ await client.fileUploader
4338
+ .download(
4339
+ fileMd5,
4340
+ fileExt,
4341
+ fileOptions,
4342
+ )
4343
+ .catch((actualException) => {
4344
+ 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!")
4345
+ });
4346
+ });
4061
4347
  });
4062
4348
  });
4063
4349
 
4350
+
4064
4351
  describe("Setting the xtkschema attribute", () => {
4065
4352
  it("Should support setting explicitely setting the xtkschema", async() => {
4066
4353
  const client = await Mock.makeClient();