@adobe/acc-js-sdk 1.0.3 → 1.0.7

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.
@@ -22,12 +22,12 @@ const { Client, ConnectionParameters } = require('../src/client.js');
22
22
  const DomUtil = require('../src/domUtil.js').DomUtil;
23
23
  const Mock = require('./mock.js').Mock;
24
24
  const { HttpError } = require('../src/transport.js');
25
+ const { Cipher } = require('../src/crypto.js');
25
26
 
26
27
 
27
28
  describe('ACC Client', function () {
28
29
 
29
30
  describe('Init', function () {
30
-
31
31
  it('Should create client', async function () {
32
32
  const client = await Mock.makeClient();
33
33
  const NLWS = client.NLWS;
@@ -605,11 +605,13 @@ describe('ACC Client', function () {
605
605
  const client = await Mock.makeClient();
606
606
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
607
607
  await client.NLWS.xtkSession.logon();
608
+ const key = Mock.makeKey();
609
+ const encrypted = new Cipher(key).encryptPassword("mid");
608
610
 
609
611
  client._transport.mockReturnValueOnce(Mock.GET_XTK_QUERY_SCHEMA_RESPONSE);
610
- client._transport.mockReturnValueOnce(Mock.GET_MID_EXT_ACCOUNT_RESPONSE);
612
+ client._transport.mockReturnValueOnce(Mock.GET_MID_EXT_ACCOUNT_RESPONSE(encrypted));
611
613
  client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
612
- client._transport.mockReturnValueOnce(Mock.GET_SECRET_KEY_OPTION_RESPONSE);
614
+ client._transport.mockReturnValueOnce(Mock.GET_SECRET_KEY_OPTION_RESPONSE(key));
613
615
  var connectionParameters = await sdk.ConnectionParameters.ofExternalAccount(client, "defaultEmailMid");
614
616
  var midClient = await sdk.init(connectionParameters);
615
617
  midClient._transport = jest.fn();
@@ -631,11 +633,13 @@ describe('ACC Client', function () {
631
633
  const client = await Mock.makeClient();
632
634
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
633
635
  await client.NLWS.xtkSession.logon();
636
+ const key = Mock.makeKey();
637
+ const encrypted = new Cipher(key).encryptPassword("mid");
634
638
 
635
639
  client._transport.mockReturnValueOnce(Mock.GET_XTK_QUERY_SCHEMA_RESPONSE);
636
- client._transport.mockReturnValueOnce(Mock.GET_BAD_EXT_ACCOUNT_RESPONSE);
640
+ client._transport.mockReturnValueOnce(Mock.GET_BAD_EXT_ACCOUNT_RESPONSE(encrypted));
637
641
  client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
638
- client._transport.mockReturnValueOnce(Mock.GET_SECRET_KEY_OPTION_RESPONSE);
642
+ client._transport.mockReturnValueOnce(Mock.GET_SECRET_KEY_OPTION_RESPONSE(key));
639
643
  await expect(async () => {
640
644
  return sdk.ConnectionParameters.ofExternalAccount(client, "bad");
641
645
  }).rejects.toMatchObject({ errorCode: "SDK-000005" });
@@ -643,14 +647,16 @@ describe('ACC Client', function () {
643
647
 
644
648
  it("Should fail if invalid representation", async () => {
645
649
  const client = await Mock.makeClient();
650
+ const key = Mock.makeKey();
651
+ const encrypted = new Cipher(key).encryptPassword("mid");
646
652
 
647
653
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
648
654
  await client.NLWS.xtkSession.logon();
649
655
 
650
656
  client._transport.mockReturnValueOnce(Mock.GET_XTK_QUERY_SCHEMA_RESPONSE);
651
- client._transport.mockReturnValueOnce(Mock.GET_MID_EXT_ACCOUNT_RESPONSE);
657
+ client._transport.mockReturnValueOnce(Mock.GET_MID_EXT_ACCOUNT_RESPONSE(encrypted));
652
658
  client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
653
- client._transport.mockReturnValueOnce(Mock.GET_SECRET_KEY_OPTION_RESPONSE);
659
+ client._transport.mockReturnValueOnce(Mock.GET_SECRET_KEY_OPTION_RESPONSE(key));
654
660
 
655
661
  await expect(async () => {
656
662
  client._representation = "Dummy";
@@ -664,14 +670,16 @@ describe('ACC Client', function () {
664
670
  it("Should fail not fail with SimpleJson representation", async () => {
665
671
  const client = await Mock.makeClient();
666
672
  client._representation = "SimpleJson";
673
+ const key = Mock.makeKey();
674
+ const encrypted = new Cipher(key).encryptPassword("mid");
667
675
 
668
676
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
669
677
  await client.NLWS.xtkSession.logon();
670
678
 
671
679
  client._transport.mockReturnValueOnce(Mock.GET_XTK_QUERY_SCHEMA_RESPONSE);
672
- client._transport.mockReturnValueOnce(Mock.GET_MID_EXT_ACCOUNT_RESPONSE);
680
+ client._transport.mockReturnValueOnce(Mock.GET_MID_EXT_ACCOUNT_RESPONSE(encrypted));
673
681
  client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
674
- client._transport.mockReturnValueOnce(Mock.GET_SECRET_KEY_OPTION_RESPONSE);
682
+ client._transport.mockReturnValueOnce(Mock.GET_SECRET_KEY_OPTION_RESPONSE(key));
675
683
 
676
684
  var connectionParameters = await sdk.ConnectionParameters.ofExternalAccount(client, "defaultEmailMid");
677
685
  await sdk.init(connectionParameters);
@@ -681,9 +689,10 @@ describe('ACC Client', function () {
681
689
  const client = await Mock.makeClient();
682
690
  client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
683
691
  await client.NLWS.xtkSession.logon();
684
-
692
+ const key = Mock.makeKey();
693
+
685
694
  client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
686
- client._transport.mockReturnValueOnce(Mock.GET_SECRET_KEY_OPTION_RESPONSE);
695
+ client._transport.mockReturnValueOnce(Mock.GET_SECRET_KEY_OPTION_RESPONSE(key));
687
696
  var cipher = await client._getSecretKeyCipher();
688
697
  expect(cipher).not.toBeNull();
689
698
  expect(cipher.key).not.toBeNull();
@@ -1193,13 +1202,13 @@ describe('ACC Client', function () {
1193
1202
 
1194
1203
  it("from default representation", async () => {
1195
1204
  const client = await Mock.makeClient();
1196
- var xml = DomUtil.toXMLString(client.fromRepresentation("root", {}));
1205
+ var xml = DomUtil.toXMLString(client._fromRepresentation("root", {}));
1197
1206
  expect(xml).toBe("<root/>");
1198
1207
  })
1199
1208
 
1200
1209
  it("from SimpleJson representation", async () => {
1201
1210
  const client = await Mock.makeClient();
1202
- var xml = DomUtil.toXMLString(client.fromRepresentation("root", {}, "SimpleJson"));
1211
+ var xml = DomUtil.toXMLString(client._fromRepresentation("root", {}, "SimpleJson"));
1203
1212
  expect(xml).toBe("<root/>");
1204
1213
  })
1205
1214
 
@@ -1208,14 +1217,14 @@ describe('ACC Client', function () {
1208
1217
  it("Should convert from BadgerFish to BadgerFish", async () => {
1209
1218
  const client = await Mock.makeClient();
1210
1219
  var from = { "@id": "1", "child": {} };
1211
- var to = client.convertToRepresentation(from, "BadgerFish", "BadgerFish");
1220
+ var to = client._convertToRepresentation(from, "BadgerFish", "BadgerFish");
1212
1221
  expect(to).toStrictEqual(from);
1213
1222
  })
1214
1223
 
1215
1224
  it("Should convert from BadgerFish to SimpleJson", async () => {
1216
1225
  const client = await Mock.makeClient();
1217
1226
  var from = { "@id": "1", "child": {} };
1218
- var to = client.convertToRepresentation(from, "BadgerFish", "SimpleJson");
1227
+ var to = client._convertToRepresentation(from, "BadgerFish", "SimpleJson");
1219
1228
  expect(to).toStrictEqual({ id: "1", child: {} });
1220
1229
  })
1221
1230
 
@@ -1224,20 +1233,20 @@ describe('ACC Client', function () {
1224
1233
 
1225
1234
  it("Compare representations", async () => {
1226
1235
  const client = await Mock.makeClient();
1227
- expect(() => { client.isSameRepresentation("json", "json") }).toThrow("SDK-000004");
1228
- expect(() => { client.isSameRepresentation("json", "BadgerFish") }).toThrow("SDK-000004");
1229
- expect(() => { client.isSameRepresentation("BadgerFish", "json") }).toThrow("SDK-000004");
1230
- expect(client.isSameRepresentation("SimpleJson", "SimpleJson")).toBeTruthy();
1231
- expect(client.isSameRepresentation("BadgerFish", "SimpleJson")).toBeFalsy();
1232
- expect(client.isSameRepresentation("SimpleJson", "BadgerFish")).toBeFalsy();
1233
- expect(client.isSameRepresentation("xml", "BadgerFish")).toBeFalsy();
1234
- expect(client.isSameRepresentation("SimpleJson", "xml")).toBeFalsy();
1235
- expect(() => { client.isSameRepresentation("Xml", "Xml") }).toThrow("SDK-000004");
1236
- expect(() => { client.isSameRepresentation("xml", "Xml") }).toThrow("SDK-000004");
1237
- expect(() => { client.isSameRepresentation("Xml", "xml") }).toThrow("SDK-000004");
1238
- expect(() => { client.isSameRepresentation("", "xml") }).toThrow("SDK-000004");
1239
- expect(() => { client.isSameRepresentation("xml", "") }).toThrow("SDK-000004");
1240
- expect(() => { client.isSameRepresentation("xml", null) }).toThrow("SDK-000004");
1236
+ expect(() => { client._isSameRepresentation("json", "json") }).toThrow("SDK-000004");
1237
+ expect(() => { client._isSameRepresentation("json", "BadgerFish") }).toThrow("SDK-000004");
1238
+ expect(() => { client._isSameRepresentation("BadgerFish", "json") }).toThrow("SDK-000004");
1239
+ expect(client._isSameRepresentation("SimpleJson", "SimpleJson")).toBeTruthy();
1240
+ expect(client._isSameRepresentation("BadgerFish", "SimpleJson")).toBeFalsy();
1241
+ expect(client._isSameRepresentation("SimpleJson", "BadgerFish")).toBeFalsy();
1242
+ expect(client._isSameRepresentation("xml", "BadgerFish")).toBeFalsy();
1243
+ expect(client._isSameRepresentation("SimpleJson", "xml")).toBeFalsy();
1244
+ expect(() => { client._isSameRepresentation("Xml", "Xml") }).toThrow("SDK-000004");
1245
+ expect(() => { client._isSameRepresentation("xml", "Xml") }).toThrow("SDK-000004");
1246
+ expect(() => { client._isSameRepresentation("Xml", "xml") }).toThrow("SDK-000004");
1247
+ expect(() => { client._isSameRepresentation("", "xml") }).toThrow("SDK-000004");
1248
+ expect(() => { client._isSameRepresentation("xml", "") }).toThrow("SDK-000004");
1249
+ expect(() => { client._isSameRepresentation("xml", null) }).toThrow("SDK-000004");
1241
1250
  })
1242
1251
  });
1243
1252
 
@@ -1310,7 +1319,7 @@ describe('ACC Client', function () {
1310
1319
 
1311
1320
  it("User agent string", async () => {
1312
1321
  const client = await Mock.makeClient();
1313
- const ua = client.getUserAgentString();
1322
+ const ua = client._getUserAgentString();
1314
1323
  expect(ua.startsWith("@adobe/acc-js-sdk/")).toBeTruthy();
1315
1324
  expect(ua.endsWith(" ACC Javascript SDK")).toBeTruthy();
1316
1325
  })
@@ -1896,4 +1905,450 @@ describe('ACC Client', function () {
1896
1905
  expect(logoff.mock.calls.length).toBe(1);
1897
1906
  })
1898
1907
  })
1908
+
1909
+ describe("Bearer token authentication", () => {
1910
+ // Bearer token authentication is used when embedding IMS for authentication
1911
+ it("Should create logged client", async() => {
1912
+ const connectionParameters = sdk.ConnectionParameters.ofBearerToken("http://acc-sdk:8080");
1913
+ const client = await sdk.init(connectionParameters);
1914
+ client._transport = jest.fn();
1915
+ client._transport.mockReturnValueOnce(Mock.BEARER_LOGON_RESPONSE);
1916
+ expect(client.isLogged()).toBeFalsy();
1917
+ await client.logon();
1918
+ expect(client.isLogged()).toBeTruthy();
1919
+ const transport = client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
1920
+ await client.logoff();
1921
+ expect(client.isLogged()).toBeFalsy();
1922
+ // Ensure logoff has been called
1923
+ expect(transport.mock.calls.length).toBe(2);
1924
+ })
1925
+
1926
+ it("Call SAOP method", async () => {
1927
+ const connectionParameters = sdk.ConnectionParameters.ofBearerToken("http://acc-sdk:8080", "$token$");
1928
+ const client = await sdk.init(connectionParameters);
1929
+ client._transport = jest.fn();
1930
+ client._transport.mockReturnValueOnce(Mock.BEARER_LOGON_RESPONSE);
1931
+ await client.logon();
1932
+ client._transport.mockReturnValueOnce(Mock.GET_XTK_QUERY_SCHEMA_RESPONSE);
1933
+ var queryDef = {
1934
+ "schema": "nms:extAccount",
1935
+ "operation": "select",
1936
+ "select": {
1937
+ "node": [
1938
+ { "expr": "@id" },
1939
+ { "expr": "@name" }
1940
+ ]
1941
+ }
1942
+ };
1943
+
1944
+ client._transport.mockReturnValueOnce(Promise.resolve(`<?xml version='1.0'?>
1945
+ <SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns='urn:xtk:queryDef' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
1946
+ <SOAP-ENV:Body>
1947
+ <ExecuteQueryResponse xmlns='urn:xtk:queryDef' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
1948
+ <pdomOutput xsi:type='ns:Element' SOAP-ENV:encodingStyle='http://xml.apache.org/xml-soap/literalxml'>
1949
+ <extAccount-collection/>
1950
+ </pdomOutput></ExecuteQueryResponse>
1951
+ </SOAP-ENV:Body>
1952
+ </SOAP-ENV:Envelope>`));
1953
+
1954
+ // Select should return empty array
1955
+ var query = client.NLWS.xtkQueryDef.create(queryDef);
1956
+ var extAccount = await query.executeQuery();
1957
+ expect(extAccount).toEqual({ extAccount: [] });
1958
+ });
1959
+
1960
+ it("Expired session refresh client callback", async () => {
1961
+ let refreshClient = async (client) => {
1962
+ const connectionParameters = sdk.ConnectionParameters.ofSecurityToken("http://acc-sdk:8080",
1963
+ "$security_token$", {refreshClient: refreshClient});
1964
+ const newClient = await sdk.init(connectionParameters);
1965
+ newClient._transport = jest.fn();
1966
+ newClient._transport.mockReturnValueOnce(Mock.BEARER_LOGON_RESPONSE);
1967
+ await newClient.logon();
1968
+ return newClient;
1969
+ }
1970
+ const connectionParameters = sdk.ConnectionParameters.ofBearerToken("http://acc-sdk:8080",
1971
+ "$token$", {refreshClient: refreshClient});
1972
+ const client = await sdk.init(connectionParameters);
1973
+ client.traceAPICalls(true);
1974
+ client._transport = jest.fn();
1975
+ client._transport.mockReturnValueOnce(Mock.BEARER_LOGON_RESPONSE);
1976
+ client._transport.mockReturnValueOnce(Promise.resolve(`XSV-350008 Session has expired or is invalid. Please reconnect.`));
1977
+ client._transport.mockReturnValueOnce(Mock.GET_XTK_QUERY_SCHEMA_RESPONSE);
1978
+ client._transport.mockReturnValueOnce(Promise.resolve(`<?xml version='1.0'?>
1979
+ <SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns='urn:xtk:queryDef' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
1980
+ <SOAP-ENV:Body>
1981
+ <ExecuteQueryResponse xmlns='urn:xtk:queryDef' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
1982
+ <pdomOutput xsi:type='ns:Element' SOAP-ENV:encodingStyle='http://xml.apache.org/xml-soap/literalxml'>
1983
+ <extAccount-collection/>
1984
+ </pdomOutput></ExecuteQueryResponse>
1985
+ </SOAP-ENV:Body>
1986
+ </SOAP-ENV:Envelope>`));
1987
+ await client.logon();
1988
+ var queryDef = {
1989
+ "schema": "nms:extAccount",
1990
+ "operation": "select",
1991
+ "select": {
1992
+ "node": [
1993
+ { "expr": "@id" },
1994
+ { "expr": "@name" }
1995
+ ]
1996
+ }
1997
+ };
1998
+ var query = client.NLWS.xtkQueryDef.create(queryDef);
1999
+ var extAccount = await query.executeQuery();
2000
+ expect(extAccount).toEqual({ extAccount: [] });
2001
+ // Same test as before traceAPICalls = false for code coverage
2002
+ client._transport.mockReturnValueOnce(Promise.resolve(`XSV-350008 Session has expired or is invalid. Please reconnect.`));
2003
+ client._transport.mockReturnValueOnce(Promise.resolve(`<?xml version='1.0'?>
2004
+ <SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns='urn:xtk:queryDef' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
2005
+ <SOAP-ENV:Body>
2006
+ <ExecuteQueryResponse xmlns='urn:xtk:queryDef' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
2007
+ <pdomOutput xsi:type='ns:Element' SOAP-ENV:encodingStyle='http://xml.apache.org/xml-soap/literalxml'>
2008
+ <extAccount-collection/>
2009
+ </pdomOutput></ExecuteQueryResponse>
2010
+ </SOAP-ENV:Body>
2011
+ </SOAP-ENV:Envelope>`));
2012
+ client.traceAPICalls(false);
2013
+ var query1 = client.NLWS.xtkQueryDef.create(queryDef);
2014
+ extAccount1 = await query1.executeQuery();
2015
+ expect(extAccount1).toEqual({ extAccount: [] });
2016
+ });
2017
+
2018
+ it("Expired session refresh client callback for code coverage", async () => {
2019
+ let refreshClient = async (client) => {
2020
+ const connectionParameters = sdk.ConnectionParameters.ofSessionToken("http://acc-sdk:8080", "$session_token$");
2021
+ const newClient = await sdk.init(connectionParameters);
2022
+ newClient._transport = jest.fn();
2023
+ newClient._transport.mockReturnValueOnce(Mock.BEARER_LOGON_RESPONSE);
2024
+ await newClient.logon();
2025
+ return newClient;
2026
+ }
2027
+ const connectionParameters = sdk.ConnectionParameters.ofBearerToken("http://acc-sdk:8080",
2028
+ "$token$", {refreshClient: refreshClient});
2029
+ const client = await sdk.init(connectionParameters);
2030
+ client.traceAPICalls(true);
2031
+ client._transport = jest.fn();
2032
+ client._transport.mockReturnValueOnce(Mock.BEARER_LOGON_RESPONSE);
2033
+ client._transport.mockReturnValueOnce(Promise.resolve(`XSV-350008 Session has expired or is invalid. Please reconnect.`));
2034
+ client._transport.mockReturnValueOnce(Mock.GET_XTK_QUERY_SCHEMA_RESPONSE);
2035
+ client._transport.mockReturnValueOnce(Promise.resolve(`<?xml version='1.0'?>
2036
+ <SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns='urn:xtk:queryDef' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
2037
+ <SOAP-ENV:Body>
2038
+ <ExecuteQueryResponse xmlns='urn:xtk:queryDef' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
2039
+ <pdomOutput xsi:type='ns:Element' SOAP-ENV:encodingStyle='http://xml.apache.org/xml-soap/literalxml'>
2040
+ <extAccount-collection/>
2041
+ </pdomOutput></ExecuteQueryResponse>
2042
+ </SOAP-ENV:Body>
2043
+ </SOAP-ENV:Envelope>`));
2044
+ await client.logon();
2045
+ var queryDef = {
2046
+ "schema": "nms:extAccount",
2047
+ "operation": "select",
2048
+ "select": {
2049
+ "node": [
2050
+ { "expr": "@id" },
2051
+ { "expr": "@name" }
2052
+ ]
2053
+ }
2054
+ };
2055
+ var query = client.NLWS.xtkQueryDef.create(queryDef);
2056
+ var extAccount = await query.executeQuery();
2057
+ expect(extAccount).toEqual({ extAccount: [] });
2058
+ });
2059
+
2060
+ it("Expired session refresh client callback retry failure", async () => {
2061
+ let refreshClient = async (client) => {
2062
+ const connectionParameters = sdk.ConnectionParameters.ofBearerToken("http://acc-sdk:8080",
2063
+ "$token$", {refreshClient: refreshClient});
2064
+ const newClient = await sdk.init(connectionParameters);
2065
+ newClient._transport = jest.fn();
2066
+ newClient._transport.mockReturnValueOnce(Mock.BEARER_LOGON_RESPONSE);
2067
+ await newClient.logon();
2068
+ return newClient;
2069
+ }
2070
+ const connectionParameters = sdk.ConnectionParameters.ofSecurityToken("http://acc-sdk:8080",
2071
+ "$security_token$", {refreshClient: refreshClient});
2072
+ const client = await sdk.init(connectionParameters);
2073
+ client._transport = jest.fn();
2074
+ client._transport.mockReturnValueOnce(Promise.resolve(`XSV-350008 Session has expired or is invalid. Please reconnect.`));
2075
+ client._transport.mockReturnValueOnce(Promise.resolve(`XSV-350008 Session has expired or is invalid. Please reconnect.`));
2076
+ client._transport.mockReturnValueOnce(Mock.GET_XTK_QUERY_SCHEMA_RESPONSE);
2077
+ client._transport.mockReturnValueOnce(Promise.resolve(`<?xml version='1.0'?>
2078
+ <SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns='urn:xtk:queryDef' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
2079
+ <SOAP-ENV:Body>
2080
+ <ExecuteQueryResponse xmlns='urn:xtk:queryDef' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
2081
+ <pdomOutput xsi:type='ns:Element' SOAP-ENV:encodingStyle='http://xml.apache.org/xml-soap/literalxml'>
2082
+ <extAccount-collection/>
2083
+ </pdomOutput></ExecuteQueryResponse>
2084
+ </SOAP-ENV:Body>
2085
+ </SOAP-ENV:Envelope>`));
2086
+ await client.logon();
2087
+ var queryDef = {
2088
+ "schema": "nms:extAccount",
2089
+ "operation": "select",
2090
+ "select": {
2091
+ "node": [
2092
+ { "expr": "@id" },
2093
+ { "expr": "@name" }
2094
+ ]
2095
+ }
2096
+ };
2097
+ var query = client.NLWS.xtkQueryDef.create(queryDef);
2098
+ await expect(query.executeQuery()).rejects.toMatchObject({ errorCode: "SDK-000012" });
2099
+ expect(client._transport.mock.calls.length).toBe(2);
2100
+ });
2101
+ })
2102
+
2103
+ describe("Logon should always return a promise", () => {
2104
+
2105
+ it("Should return a promise with UserPassword", async () => {
2106
+ const connectionParameters = sdk.ConnectionParameters.ofUserAndPassword("http://acc-sdk:8080", "$user$", "$password$");
2107
+ const client = await sdk.init(connectionParameters);
2108
+ client._transport = jest.fn();
2109
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
2110
+ const result = client.logon();
2111
+ expect(result instanceof Promise).toBe(true);
2112
+ await result;
2113
+ })
2114
+
2115
+ it("Should return a promise with bearer token", async () => {
2116
+ const connectionParameters = sdk.ConnectionParameters.ofBearerToken("http://acc-sdk:8080", "$token$");
2117
+ const client = await sdk.init(connectionParameters);
2118
+ client._transport = jest.fn();
2119
+ client._transport.mockReturnValueOnce(Mock.BEARER_LOGON_RESPONSE);
2120
+ const result = client.logon();
2121
+ expect(result instanceof Promise).toBe(true);
2122
+ await result;
2123
+ })
2124
+
2125
+ it("Should return a promise with UserAndServiceToken", async () => {
2126
+ const connectionParameters = sdk.ConnectionParameters.ofUserAndServiceToken("http://acc-sdk:8080", "$user$", "$service_token$");
2127
+ const client = await sdk.init(connectionParameters);
2128
+ client._transport = jest.fn();
2129
+ const result = client.logon();
2130
+ expect(result instanceof Promise).toBe(true);
2131
+ try {
2132
+ await result;
2133
+ } catch(ex) { /* result or exception is not handled */ }
2134
+ })
2135
+
2136
+ it("Should return a promise with SessionToken", async () => {
2137
+ const connectionParameters = sdk.ConnectionParameters.ofSessionToken("http://acc-sdk:8080", "$session_token$");
2138
+ const client = await sdk.init(connectionParameters);
2139
+ client._transport = jest.fn();
2140
+ const result = client.logon();
2141
+ expect(result instanceof Promise).toBe(true);
2142
+ await result;
2143
+ })
2144
+
2145
+ it("Should return a promise with SecurityToken", async () => {
2146
+ const connectionParameters = sdk.ConnectionParameters.ofSecurityToken("http://acc-sdk:8080", "$security_token$");
2147
+ const client = await sdk.init(connectionParameters);
2148
+ client._transport = jest.fn();
2149
+ const result = client.logon();
2150
+ expect(result instanceof Promise).toBe(true);
2151
+ await result;
2152
+ })
2153
+
2154
+ it("Should return a promise with AnonymousUser", async () => {
2155
+ const connectionParameters = sdk.ConnectionParameters.ofAnonymousUser("http://acc-sdk:8080");
2156
+ const client = await sdk.init(connectionParameters);
2157
+ client._transport = jest.fn();
2158
+ const result = client.logon();
2159
+ expect(result instanceof Promise).toBe(true);
2160
+ await result;
2161
+ })
2162
+ })
2163
+
2164
+
2165
+ describe("Should simulate server down", () => {
2166
+ it("Should simulate server down and up again", async () => {
2167
+ // Server is up and getOption
2168
+ const client = await Mock.makeClient();
2169
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
2170
+ client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
2171
+ await client.NLWS.xtkSession.logon();
2172
+ client._transport.mockReturnValueOnce(Mock.GET_DATABASEID_RESPONSE);
2173
+ var databaseId = await client.getOption("XtkDatabaseId", false);
2174
+ expect(databaseId).toBe("uFE80000000000000F1FA913DD7CC7C480041161C");
2175
+
2176
+ // Now simulate a connection error (server is down)
2177
+ const error = new Error("connect ECONNREFUSED 3.225.73.178:8080");
2178
+ error.code="ECONNREFUSED";
2179
+ error.errno="ECONNREFUSED";
2180
+ client._transport.mockReturnValueOnce(Promise.reject(error));
2181
+ await expect(client.getOption("XtkDatabaseId", false)).rejects.toMatchObject({
2182
+ message: "500 - Error calling method 'xtk:session#GetOption': Error (connect ECONNREFUSED 3.225.73.178:8080)"
2183
+ });
2184
+
2185
+ // Server is back up again
2186
+ client._transport.mockReturnValueOnce(Mock.GET_DATABASEID_RESPONSE);
2187
+ databaseId = await client.getOption("XtkDatabaseId", false);
2188
+ expect(databaseId).toBe("uFE80000000000000F1FA913DD7CC7C480041161C")
2189
+ });
2190
+ })
2191
+
2192
+ describe("Connection options", () => {
2193
+ it("Should set options cache TTL", async () => {
2194
+ const client = await Mock.makeClient({ optionCacheTTL: -1 });
2195
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
2196
+ client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
2197
+ await client.NLWS.xtkSession.logon();
2198
+ // Get Option and cache result. Check the value is in cache
2199
+ client._transport.mockReturnValueOnce(Mock.GET_DATABASEID_RESPONSE);
2200
+ await client.getOption("XtkDatabaseId", true);
2201
+ expect(client._optionCache._cache["XtkDatabaseId"].value).toMatchObject({ type: 6, rawValue: "uFE80000000000000F1FA913DD7CC7C480041161C" });
2202
+ // Get it again, it should not use the cache (=> it should make a SOAP call)
2203
+ // To test if the SOAP call is made, we mock the SOAP call answer with a different result
2204
+ client._transport.mockReturnValueOnce(Promise.resolve(`<?xml version='1.0'?>
2205
+ <SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns='urn:xtk:session' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
2206
+ <SOAP-ENV:Body>
2207
+ <GetOptionResponse xmlns='urn:xtk:session' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
2208
+ <pstrValue xsi:type='xsd:string'>uFE80000000000000F1FA913DD7CC7C48004116FF</pstrValue>
2209
+ <pbtType xsi:type='xsd:byte'>6</pbtType>
2210
+ </GetOptionResponse>
2211
+ </SOAP-ENV:Body>
2212
+ </SOAP-ENV:Envelope>`));
2213
+ await client.getOption("XtkDatabaseId", true);
2214
+ expect(client._optionCache._cache["XtkDatabaseId"].value).toMatchObject({ type: 6, rawValue: "uFE80000000000000F1FA913DD7CC7C48004116FF" });
2215
+ })
2216
+
2217
+ it("Should set default value for traceAPICalls", async () => {
2218
+ var client = await Mock.makeClient({ traceAPICalls: undefined });
2219
+ expect(client._traceAPICalls).toBeFalsy();
2220
+ client = await Mock.makeClient({ traceAPICalls: null });
2221
+ expect(client._traceAPICalls).toBeFalsy();
2222
+ client = await Mock.makeClient({ traceAPICalls: false });
2223
+ expect(client._traceAPICalls).toBeFalsy();
2224
+ client = await Mock.makeClient({ traceAPICalls: true });
2225
+ expect(client._traceAPICalls).toBeTruthy();
2226
+ })
2227
+
2228
+ it("Should set default transport", async () => {
2229
+ var client = await Mock.makeClient({ transport: async () => {
2230
+ return "Hello";
2231
+ }});
2232
+ await expect(client._transport()).resolves.toBe("Hello");
2233
+ });
2234
+ })
2235
+
2236
+
2237
+ describe("Local storage", () => {
2238
+ it("Shoud read from local storage", async () => {
2239
+ const storage = {
2240
+ getItem: jest.fn(),
2241
+ setItem: jest.fn(),
2242
+ }
2243
+ const client = await Mock.makeClient({ storage: storage });
2244
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
2245
+ client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
2246
+ await client.NLWS.xtkSession.logon();
2247
+ storage.getItem.mockReturnValueOnce(JSON.stringify({value: { value: "Hello", type: 6 }, cachedAt: 1633715996021 }));
2248
+ const value = await client.getOption("XtkDatabaseId");
2249
+ expect(value).toBe("Hello");
2250
+ })
2251
+
2252
+ it("Should write to local storage", async () => {
2253
+ const storage = {
2254
+ getItem: jest.fn(),
2255
+ setItem: jest.fn(),
2256
+ }
2257
+ const client = await Mock.makeClient({ storage: storage });
2258
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
2259
+ client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
2260
+ await client.NLWS.xtkSession.logon();
2261
+ storage.getItem.mockReturnValueOnce(JSON.stringify({value: { value: "Hello", type: 6 }, cachedAt: 1633715996021 }));
2262
+ client._transport.mockReturnValueOnce(Promise.resolve(`<?xml version='1.0'?>
2263
+ <SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns='urn:wpp:default' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
2264
+ <SOAP-ENV:Body>
2265
+ <WriteResponse xmlns='urn:wpp:default' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
2266
+ </WriteResponse>
2267
+ </SOAP-ENV:Body>
2268
+ </SOAP-ENV:Envelope>`));
2269
+ await client.setOption("XtkDatabaseId", "World");
2270
+ var call = undefined;
2271
+ for (var i=0; i<storage.setItem.mock.calls.length; i++) {
2272
+ if (storage.setItem.mock.calls[i][0].endsWith("OptionCache$XtkDatabaseId")) {
2273
+ call = storage.setItem.mock.calls[i];
2274
+ break;
2275
+ }
2276
+ }
2277
+ expect(JSON.parse(call[1])).toMatchObject({
2278
+ value: { value: "World", type: 6 }
2279
+ })
2280
+ });
2281
+
2282
+ it("Should ignore protocol for local storage root key", async () => {
2283
+ var connectionParameters = sdk.ConnectionParameters.ofUserAndPassword("http://acc-sdk:8080", "admin", "admin", {});
2284
+ var client = await sdk.init(connectionParameters);
2285
+ expect(client._optionCache._storage._rootKey).toBe("acc.js.sdk.1.0.7.acc-sdk:8080.cache.OptionCache$");
2286
+
2287
+ connectionParameters = sdk.ConnectionParameters.ofUserAndPassword("https://acc-sdk:8080", "admin", "admin", {});
2288
+ client = await sdk.init(connectionParameters);
2289
+ expect(client._optionCache._storage._rootKey).toBe("acc.js.sdk.1.0.7.acc-sdk:8080.cache.OptionCache$");
2290
+
2291
+ connectionParameters = sdk.ConnectionParameters.ofUserAndPassword("acc-sdk:8080", "admin", "admin", {});
2292
+ client = await sdk.init(connectionParameters);
2293
+ expect(client._optionCache._storage._rootKey).toBe("acc.js.sdk.1.0.7.acc-sdk:8080.cache.OptionCache$");
2294
+ })
2295
+
2296
+ it("Should support no storage", async () => {
2297
+ const storage = {
2298
+ getItem: jest.fn(),
2299
+ setItem: jest.fn(),
2300
+ }
2301
+ const client = await Mock.makeClient({ storage: storage, noStorage: true });
2302
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
2303
+ client._transport.mockReturnValueOnce(Mock.GET_XTK_SESSION_SCHEMA_RESPONSE);
2304
+ await client.NLWS.xtkSession.logon();
2305
+ client._transport.mockReturnValueOnce(Mock.GET_DATABASEID_RESPONSE);
2306
+ const value = await client.getOption("XtkDatabaseId");
2307
+ expect(value).toBe('uFE80000000000000F1FA913DD7CC7C480041161C');
2308
+ expect(storage.getItem.mock.calls.length).toBe(0); // storage is disabled and should not have been called
2309
+ })
2310
+
2311
+ it("Should cache XML in storage", async () => {
2312
+ const map = {};
2313
+ const storage = {
2314
+ getItem: jest.fn((key) => map[key]),
2315
+ setItem: jest.fn((key, value) => map[key] = value)
2316
+ }
2317
+ let client = await Mock.makeClient({ storage: storage });
2318
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
2319
+ await client.NLWS.xtkSession.logon();
2320
+ client._transport.mockReturnValueOnce(Mock.GET_NMS_EXTACCOUNT_SCHEMA_RESPONSE);
2321
+ await client.getSchema("nms:extAccount");
2322
+ // Schema should have been cached to local storage
2323
+ expect(storage.setItem.mock.calls.length).toBe(1);
2324
+ expect(storage.setItem.mock.calls[0][0]).toMatch("cache.XtkEntityCache$xtk:schema|nms:extAccount");
2325
+ // Value is the cached object, it should not be an empty object
2326
+ const cached = JSON.parse(storage.setItem.mock.calls[0][1]);
2327
+ expect(Object.keys(cached.value).length).toBeGreaterThan(0);
2328
+ expect(cached.value).toMatch("<schema");
2329
+
2330
+ // Now simulate reusing the local storage. We need a new client to make sure we do not reuse
2331
+ // the in-memory cache of the client.
2332
+ client = await Mock.makeClient({ storage: storage });
2333
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
2334
+ await client.NLWS.xtkSession.logon();
2335
+ client._transport.mockReturnValueOnce(Mock.GET_NMS_EXTACCOUNT_SCHEMA_RESPONSE);
2336
+ await client.getSchema("nms:extAccount");
2337
+ })
2338
+ })
2339
+
2340
+ describe("Get Schema, cache and representations", () => {
2341
+ it("Should get schema with no cache", async () => {
2342
+ const client = await Mock.makeClient();
2343
+ client.clearAllCaches();
2344
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
2345
+ await client.NLWS.xtkSession.logon();
2346
+
2347
+ client._transport.mockReturnValueOnce(Mock.GET_NMS_EXTACCOUNT_SCHEMA_RESPONSE);
2348
+ var schema = await client.getSchema("nms:extAccount");
2349
+ expect(schema["namespace"]).toBe("nms");
2350
+ expect(schema["name"]).toBe("extAccount");
2351
+ });
2352
+
2353
+ });
1899
2354
  });