@adobe/acc-js-sdk 1.1.46 → 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.
@@ -1061,26 +1061,44 @@ describe('ACC Client', function () {
1061
1061
  await client.NLWS.xtkSession.logon();
1062
1062
 
1063
1063
  client._transport.mockReturnValueOnce(Mock.GET_XTK_WORKFLOW_SCHEMA_RESPONSE);
1064
- client._transport.mockImplementationOnce(options => {
1065
- const doc = DomUtil.parse(options.data);
1066
- const body = DomUtil.findElement(doc.documentElement, "SOAP-ENV:Body");
1067
- const method = DomUtil.getFirstChildElement(body);
1068
- const parameters = DomUtil.findElement(method, "parameters");
1069
- const variables = DomUtil.getFirstChildElement(parameters, "variables");
1070
- if (!variables)
1071
- throw new Error("Did not find 'variables' node");
1072
- if (variables.getAttribute("hello") != "world")
1073
- throw new Error("Did not find 'hello' variable");
1074
1064
 
1075
- return Promise.resolve(`<?xml version='1.0'?>
1076
- <SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns='urn:xtk:workflow' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
1077
- <SOAP-ENV:Body>
1078
- <StartWithParametersResponse xmlns='urn:xtk:workflow' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
1079
- </StartWithParametersResponse>
1080
- </SOAP-ENV:Body>
1081
- </SOAP-ENV:Envelope>`);
1082
- });
1083
- await client.NLWS.xtkWorkflow.startWithParameters(4900, { "hello": "world" });
1065
+ const mockImpl = (methodName) => {
1066
+ client._transport.mockImplementationOnce(options => {
1067
+ const doc = DomUtil.parse(options.data);
1068
+ const body = DomUtil.findElement(doc.documentElement, "SOAP-ENV:Body");
1069
+ const method = DomUtil.getFirstChildElement(body);
1070
+ const parameters = DomUtil.findElement(method, "parameters");
1071
+ const variables = DomUtil.getFirstChildElement(parameters, "variables");
1072
+ if (!variables)
1073
+ throw new Error("Did not find 'variables' node");
1074
+ if (variables.getAttribute("hello") != "world")
1075
+ throw new Error("Did not find 'hello' variable");
1076
+
1077
+ const tagName = methodName + "Response";
1078
+ return Promise.resolve(`<?xml version='1.0'?>
1079
+ <SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns='urn:xtk:workflow' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
1080
+ <SOAP-ENV:Body>
1081
+ <${tagName} xmlns='urn:xtk:workflow' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
1082
+ </${tagName}>
1083
+ </SOAP-ENV:Body>
1084
+ </SOAP-ENV:Envelope>`);
1085
+ });
1086
+ };
1087
+
1088
+ const variables = { "hello": "world" };
1089
+
1090
+ mockImpl("StartWithParameters");
1091
+ await client.NLWS.xtkWorkflow.startWithParameters(4900, variables);
1092
+ mockImpl("SimulateWithParameters");
1093
+ await client.NLWS.xtkWorkflow.simulateWithParameters(4900, variables);
1094
+ mockImpl("PostEvent");
1095
+ await client.NLWS.xtkWorkflow.postEvent("WFK123", "signal", "", variables, false);
1096
+
1097
+ // return parameter instanceId has been removed from the mock method definition to simplify the unit test
1098
+ mockImpl("SpawnWithParameters");
1099
+ await client.NLWS.xtkWorkflow.spawnWithParameters(4900, variables);
1100
+ mockImpl("SpawnWithParametersEx");
1101
+ await client.NLWS.xtkWorkflow.spawnWithParametersEx(4900, true, variables);
1084
1102
 
1085
1103
  client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1086
1104
  await client.NLWS.xtkSession.logoff();
@@ -1821,100 +1839,6 @@ describe('ACC Client', function () {
1821
1839
  });
1822
1840
  });
1823
1841
 
1824
- // write unit test for client.fileUploader.uploadAemAsset method
1825
- describe("Tests for client.fileUploader.uploadAemAsset method", () => {
1826
- // Case 1: uploadAemAsset method should return a valid response
1827
- it("Should return correct response", async () => {
1828
- const client = await Mock.makeClient();
1829
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
1830
- await client.NLWS.xtkSession.logon();
1831
-
1832
- const mockResponse = {
1833
- "publishedURL" : "http://trk-inst-xyz.camp.adobe.com/res/trk-inst/409afb8798180a36591456e152b6c406.jpeg"
1834
- };
1835
- client._bearerToken = 'Bearer 1234567890';
1836
- client._transport.mockReturnValueOnce(mockResponse);
1837
- 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");
1838
- expect(response).toBe(mockResponse);
1839
-
1840
- client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1841
- await client.NLWS.xtkSession.logoff();
1842
- });
1843
-
1844
- // Case 2: Bearertoken is not provided, but the client is already authenticated with session token
1845
- it("Should throw error for missing authentication token", async () => {
1846
- const client = await Mock.makeClient();
1847
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
1848
- await client.NLWS.xtkSession.logon();
1849
-
1850
- 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"))
1851
- .catch( e => { expect(e).toMatchObject(CampaignException.AEM_ASSET_UPLOAD_FAILED('Bearer token is missing'))});
1852
-
1853
- client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1854
- await client.NLWS.xtkSession.logoff();
1855
- });
1856
-
1857
- // Case 3: 200 response but publishedURL is not returned
1858
- // It shouldn't occur as API also checks non-emptiness of publishedURL before returning the response.
1859
- it("Should throw error for missing publishedURL", async () => {
1860
- const client = await Mock.makeClient();
1861
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
1862
- await client.NLWS.xtkSession.logon();
1863
-
1864
- const mockResponse = {};
1865
- client._transport.mockReturnValueOnce(mockResponse);
1866
- client._bearerToken = 'Bearer 1234567890';
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 => { console.log(e); expect(e).toMatchObject(CampaignException.AEM_ASSET_UPLOAD_FAILED('Publishing failed'))});
1870
-
1871
- client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1872
- await client.NLWS.xtkSession.logoff();
1873
- });
1874
-
1875
- // Case 4: AEM Asset upload failed (reason maybe aem not reachable, assetlink not reachable, etc)
1876
- it("Should throw error when AEM Asset upload failed", async () => {
1877
- const client = await Mock.makeClient();
1878
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
1879
- await client.NLWS.xtkSession.logon();
1880
-
1881
- //const mockResponse = {};
1882
- client._transport.mockImplementation(() => {
1883
- var ex = CampaignException.AEM_ASSET_UPLOAD_FAILED('The requested content does not exist', 400);
1884
- ex.faultString = 'The requested content does not exist';
1885
- ex.detail = 'Failed to upload AEM asset';
1886
- throw ex;
1887
- });
1888
- client._bearerToken = 'Bearer 1234567890';
1889
-
1890
- var ex = CampaignException.AEM_ASSET_UPLOAD_FAILED('The requested content does not exist', 400);
1891
- 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"))
1892
- .catch( e => { console.log(e); expect(e).toMatchObject(ex)});
1893
-
1894
- client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1895
- await client.NLWS.xtkSession.logoff();
1896
- });
1897
-
1898
- // Case 5: transport layer returns response as string instead of json when sdk
1899
- // used as package inside browser(Fetch API is used in browser while axios in node).
1900
- it("Should return correct response", async () => {
1901
- const client = await Mock.makeClient();
1902
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
1903
- await client.NLWS.xtkSession.logon();
1904
-
1905
- const mockResponse = {
1906
- "publishedURL" : "http://trk-inst-xyz.camp.adobe.com/res/trk-inst/409afb8798180a36591456e152b6c406.jpeg"
1907
- };
1908
- const mockResponseString = JSON.stringify(mockResponse);
1909
- client._bearerToken = 'Bearer 1234567890';
1910
- client._transport.mockReturnValueOnce(mockResponseString);
1911
- 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");
1912
- expect(response).toMatchObject(mockResponse);
1913
-
1914
- client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1915
- await client.NLWS.xtkSession.logoff();
1916
- });
1917
- });
1918
1842
 
1919
1843
 
1920
1844
  describe("Observers", () => {
@@ -3634,415 +3558,796 @@ describe('ACC Client', function () {
3634
3558
  client._unregisterCacheChangeListener(listener);
3635
3559
  });
3636
3560
  });
3637
- describe('File uploader - on server', () => {
3638
- it('is not supported', async ()=> {
3639
- const client = await Mock.makeClient();
3640
- expect(client.fileUploader).toBeDefined()
3641
- 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})
3642
- })
3643
- })
3644
3561
 
3645
- describe('File uploader - on browser', () => {
3646
- beforeEach(() => {
3647
- global.document = dom.window.document
3648
- global.window = dom.window
3649
- global.FormData = function () {
3650
- this.append = jest.fn()
3651
- }
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
+ });
3652
3584
 
3653
- // Evaluates JavaScript code returned by the upload.jsp. Evaluation is done in the context
3654
- // of an iframe and will call the parent window document.controller uploadFileCallBack
3655
- // function
3656
- 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) {
3657
3597
  const data = eval(`
3658
- (function () {
3659
- var result = undefined;
3660
- window = {
3661
- parent: {
3662
- document: {
3663
- controller: {
3664
- uploadFileCallBack: (data) => {
3665
- result = data;
3598
+ (function () {
3599
+ var result = undefined;
3600
+ window = {
3601
+ parent: {
3602
+ document: {
3603
+ controller: {
3604
+ uploadFileCallBack: (data) => {
3605
+ result = data;
3606
+ }
3607
+ }
3666
3608
  }
3667
3609
  }
3668
- }
3669
- }
3670
- };
3671
- ${js};
3672
- return result;
3673
- }())
3674
- `);
3610
+ };
3611
+ ${js};
3612
+ return result;
3613
+ }())
3614
+ `);
3675
3615
  // Call real callback
3676
3616
  global.document.controller.uploadFileCallBack(data);
3677
- }
3617
+ }
3678
3618
 
3679
- // Dynamically mock the iframe.contentWindow.document.close(); function
3680
- const handler = {
3619
+ // Dynamically mock the iframe.contentWindow.document.close(); function
3620
+ const handler = {
3681
3621
  get: function (target, prop) {
3682
- if (prop === 'contentWindow') {
3683
- target.contentWindow.document.close = () => {
3684
- var scripts = target.contentWindow.document.getElementsByTagName('script');
3685
- for (let i = 0; i < scripts.length; i++) {
3686
- const script = scripts[i];
3687
- const js = DomUtil.elementValue(script);
3688
- evalJSReturnedByUploadJSP(js);
3689
- }
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);
3690
3630
  }
3631
+ };
3691
3632
  }
3692
3633
  return Reflect.get(...arguments);
3693
- }
3694
- };
3695
-
3634
+ },
3635
+ };
3696
3636
 
3697
- // Intercept creation of iframe. returns a proxy which will intercept the iframe.contentWindow.document.close(); function
3698
- const _origiinalCreateElement = document.createElement;
3699
- 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) => {
3700
3640
  const r = _origiinalCreateElement.call(document, tagName);
3701
- if (tagName === 'iframe') {
3641
+ if (tagName === "iframe") {
3702
3642
  const p = new Proxy(r, handler);
3703
3643
  return p;
3704
3644
  }
3705
3645
  return r;
3706
- };
3707
-
3708
- });
3709
-
3710
- it('works with successful post upload calls', async () => {
3711
- // Create a mock client and logon
3712
- const client = await Mock.makeClient();
3713
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3714
- await client.NLWS.xtkSession.logon();
3715
-
3716
- // Mock the upload protocol
3717
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3718
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3719
- // - call to xtk:session#Write
3720
- // - call to xtk:fileRes#PublishIfNeeded
3721
- // - call to xtk:fileRes#GetURL
3722
-
3723
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
3724
- <html xmlns="http://www.w3.org/1999/xhtml">
3725
- <head>
3726
- <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>
3727
- </head>
3728
- <body></body>
3729
- </html>`)); // upload.jsp
3730
-
3731
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3732
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
3733
-
3734
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3735
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3736
-
3737
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3738
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)); // xtk:fileRes#PublishIfNeeded
3646
+ };
3647
+ });
3739
3648
 
3740
- 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();
3741
3654
 
3742
- // Call upload
3743
- const result = await client.fileUploader.upload({
3744
- type: 'text/html',
3745
- size: 12345
3746
- })
3747
- 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,
3748
3700
  expect.objectContaining({
3749
3701
  data: expect.anything(),
3750
3702
  url: expect.any(String),
3751
- method: 'POST',
3703
+ method: "POST",
3752
3704
  processData: false,
3753
3705
  headers: expect.objectContaining({
3754
- 'X-Security-Token': expect.any(String),
3755
- 'X-Session-Token': expect.any(String),
3706
+ "X-Security-Token": expect.any(String),
3707
+ "X-Session-Token": expect.any(String),
3756
3708
  }),
3757
3709
  })
3758
- );
3710
+ );
3759
3711
 
3760
- expect(result).toMatchObject({
3712
+ expect(result).toMatchObject({
3761
3713
  md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
3762
3714
  name: "test.txt",
3763
3715
  size: 12345,
3764
3716
  type: "text/html",
3765
- url: "http://hello.com"
3717
+ url: "http://hello.com",
3718
+ });
3766
3719
  });
3767
- })
3768
3720
 
3769
- it('throws error with dependant failures', async () => {
3770
- // Create a mock client and logon
3771
- const client = await Mock.makeClient();
3772
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3773
- 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();
3774
3726
 
3775
- // Mock the upload protocol
3776
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3777
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3778
- // - call to xtk:session#Write
3779
- // - call to xtk:fileRes#PublishIfNeeded
3780
- // - 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
+ });
3781
3773
 
3782
- 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();
3783
3779
 
3784
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3785
- 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
+ });
3786
3826
 
3787
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3788
- 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();
3789
3832
 
3790
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3791
- 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
+ });
3792
3885
 
3793
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_URL_RESPONSE)); // xtk:fileRes#GetURL
3794
- // For async handling
3795
- expect.assertions(1)
3796
- // Call upload
3797
- await client.fileUploader.upload({
3798
- type: 'text/html',
3799
- size: 12345,
3800
- name: 'abcd.txt'
3801
- }).catch((ex) => {
3802
- 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!!!');
3803
- })
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();
3804
3891
 
3805
- })
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
+ );
3806
3938
 
3807
- it('throws error with not okay response', async () => {
3808
- // Create a mock client and logon
3809
- const client = await Mock.makeClient();
3810
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3811
- 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
+ });
3812
3947
 
3813
- // Mock the upload protocol
3814
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3815
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3816
- // - call to xtk:session#Write
3817
- // - call to xtk:fileRes#PublishIfNeeded
3818
- // - 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();
3819
3953
 
3820
- 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
+ );
3821
4000
 
3822
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3823
- 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
+ });
3824
4010
 
3825
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3826
- 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();
3827
4016
 
3828
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3829
- 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
+ );
3830
4038
 
3831
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_URL_RESPONSE)); // xtk:fileRes#GetURL
3832
- // For async handling
3833
- expect.assertions(1)
3834
- // Call upload
3835
- await client.fileUploader.upload({
3836
- type: 'text/html',
4039
+ expect(result).toMatchObject({
4040
+ md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
4041
+ name: "test.txt",
3837
4042
  size: 12345,
3838
- name: 'abcd.txt'
3839
- }).catch((ex) => {
3840
- expect(ex.message).toMatch('500 - Error 16384: SDK-000013 "Failed to upload file abcd.txt. Some error occurred!!!');
3841
- })
3842
-
3843
- })
4043
+ type: "text/html",
4044
+ });
4045
+ expect(result.url).toBeUndefined();
4046
+ });
3844
4047
 
3845
- it('throws error with malformed response', async () => {
3846
- // Create a mock client and logon
3847
- const client = await Mock.makeClient();
3848
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3849
- 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();
3850
4053
 
3851
- // Mock the upload protocol
3852
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3853
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3854
- // - call to xtk:session#Write
3855
- // - call to xtk:fileRes#PublishIfNeeded
3856
- // - call to xtk:fileRes#GetURL
3857
-
3858
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
3859
- <html xmlns="http://www.w3.org/1999/xhtml">
3860
- <head>
3861
- <script type="text/javascript">if(window.parent&&window.parent.document.controller&&"function"==typeof window.parent.document.controller.uploadFileCallBack){window.parent.document.controller.uploadFileCallBack([])}</script>
3862
- </head>
3863
- <body></body>
3864
- </html>`)); // upload.jsp
3865
-
3866
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3867
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE);
3868
-
3869
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3870
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3871
-
3872
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3873
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)); // xtk:fileRes#PublishIfNeeded
3874
-
3875
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_URL_RESPONSE)); // xtk:fileRes#GetURL
3876
- // For async handling
3877
- expect.assertions(1)
3878
- // Call upload
3879
- await client.fileUploader.upload({
3880
- type: 'text/html',
3881
- size: 12345,
3882
- name: 'abcd.txt'
3883
- }).catch((ex) => {
3884
- expect(ex.message).toMatch('500 - Error 16384: SDK-000013 "Failed to upload file abcd.txt. Malformed data:');
3885
- })
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
+ });
3886
4084
  });
3887
4085
 
3888
- it("Should support 'publishIfNeeded' action", async () => {
3889
- // Create a mock client and logon
3890
- const client = await Mock.makeClient();
3891
- client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3892
- await client.NLWS.xtkSession.logon();
3893
-
3894
- // Mock the upload protocol
3895
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3896
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3897
- // - call to xtk:session#Write
3898
- // - call to xtk:fileRes#PublishIfNeeded
3899
- // - 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
+ });
3900
4109
 
3901
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
3902
- <html xmlns="http://www.w3.org/1999/xhtml">
3903
- <head>
3904
- <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>
3905
- </head>
3906
- <body></body>
3907
- </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
+ });
3908
4127
 
3909
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3910
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
4128
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4129
+ await client.NLWS.xtkSession.logoff();
4130
+ });
3911
4131
 
3912
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3913
- 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
+ });
3914
4154
 
3915
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3916
- 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
+ });
3917
4158
 
3918
- 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
+ });
3919
4188
 
3920
- // Call upload
3921
- const result = await client.fileUploader.upload({
3922
- type: 'text/html',
3923
- size: 12345
3924
- }, { action: "publishIfNeeded" });
4189
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
4190
+ await client.NLWS.xtkSession.logoff();
4191
+ });
3925
4192
 
3926
- expect(result).toMatchObject({
3927
- md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
3928
- name: "test.txt",
3929
- size: 12345,
3930
- type: "text/html",
3931
- 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();
3932
4214
  });
4215
+ });
3933
4216
  });
3934
4217
 
3935
- it("Should support 'className' action", async () => {
3936
- // 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 () => {
3937
4231
  const client = await Mock.makeClient();
3938
4232
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3939
4233
  await client.NLWS.xtkSession.logon();
3940
4234
 
3941
- // Mock the upload protocol
3942
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3943
- // - call to xtk:counter#IncreaseValue (first, retrieve the schema xtk:counter then call the function)
3944
- // - call to xtk:session#Write
3945
- // - call to xtk:fileRes#PublishIfNeeded
3946
- // - call to xtk:fileRes#GetURL
3947
-
3948
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
3949
- <html xmlns="http://www.w3.org/1999/xhtml">
3950
- <head>
3951
- <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>
3952
- </head>
3953
- <body></body>
3954
- </html>`)); // upload.jsp
3955
-
3956
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_XTK_COUNTER_RESPONSE)); // GetEntityIfMoreRecentResponse - counter
3957
- client._transport.mockReturnValueOnce(Mock.INCREASE_VALUE_RESPONSE); // xtk:counter#IncreaseValue
3958
-
3959
- client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE); // GetEntityIfMoreRecentResponse - session
3960
- client._transport.mockReturnValueOnce(Mock.FILE_RES_WRITE_RESPONSE); // xtk:session#Write
3961
-
3962
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_FILERES_QUERY_SCHEMA_RESPONSE)); // GetEntityIfMoreRecentResponse - fileRes
3963
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.PUBLISH_IF_NEEDED_RESPONSE)); // xtk:fileRes#PublishIfNeeded
3964
-
3965
- client._transport.mockReturnValueOnce(Promise.resolve(Mock.GET_URL_RESPONSE)); // xtk:fileRes#GetURL
3966
-
3967
- // Call upload
3968
- const result = await client.fileUploader.upload({
3969
- type: 'text/html',
3970
- size: 12345
3971
- }, { action: "publishIfNeeded", className: "myClass" });
3972
-
3973
- const body = global.document.body;
3974
- const iframes = body.getElementsByTagName("iframe");
3975
- let found = false;
3976
- for (let i=0; i<iframes.length; i++) {
3977
- const iframe = iframes[i];
3978
- if (iframe.className === "myClass")
3979
- found = true;
3980
- }
3981
- expect(found).toBe(true);
3982
- });
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
+ );
4243
+
4244
+ expect(response).toStrictEqual(mockResponse);
4245
+ });
3983
4246
 
3984
- it("Should support 'none' action", async () => {
3985
- // Create a mock client and logon
4247
+ it("Should return correct response without fileOptions", async () => {
3986
4248
  const client = await Mock.makeClient();
3987
4249
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
3988
4250
  await client.NLWS.xtkSession.logon();
3989
4251
 
3990
- // Mock the upload protocol
3991
- // With the "none" action, we skip the counter & publication
3992
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
3993
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
3994
- <html xmlns="http://www.w3.org/1999/xhtml">
3995
- <head>
3996
- <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>
3997
- </head>
3998
- <body></body>
3999
- </html>`)); // upload.jsp
4000
-
4001
- // Call upload
4002
- const result = await client.fileUploader.upload({
4003
- type: 'text/html',
4004
- size: 12345
4005
- }, { action: "none" });
4006
-
4007
- expect(result).toMatchObject({
4008
- md5: "d8e8fca2dc0f896fd7cb4cb0031ba249",
4009
- name: "test.txt",
4010
- size: 12345,
4011
- 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
+ );
4012
4280
  });
4013
- expect(result.url).toBeUndefined();
4014
- });
4015
4281
 
4016
- it("Should failed with invalid action", async () => {
4017
- // 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 () => {
4018
4323
  const client = await Mock.makeClient();
4019
4324
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
4020
4325
  await client.NLWS.xtkSession.logon();
4021
4326
 
4022
- // Mock the upload protocol
4023
- // With the "none" action, we skip the counter & publication
4024
- // - the upload.jsp (which returns the content of an iframe and JS to eval)
4025
- client._transport.mockReturnValueOnce(Promise.resolve(`Ok
4026
- <html xmlns="http://www.w3.org/1999/xhtml">
4027
- <head>
4028
- <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>
4029
- </head>
4030
- <body></body>
4031
- </html>`)); // upload.jsp
4032
-
4033
- // Call upload
4034
- await expect(client.fileUploader.upload({
4035
- type: 'text/html',
4036
- size: 12345
4037
- }, { action: "invalid" })).rejects.toMatchObject({
4038
- errorCode: "SDK-000006",
4039
- "faultCode": 16384,
4040
- "faultString": "Bad parameter 'action' with value 'invalid'",
4041
- "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!';
4042
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
+ });
4043
4347
  });
4044
4348
  });
4045
4349
 
4350
+
4046
4351
  describe("Setting the xtkschema attribute", () => {
4047
4352
  it("Should support setting explicitely setting the xtkschema", async() => {
4048
4353
  const client = await Mock.makeClient();