@nsshunt/stsdevtools 1.0.67 → 1.0.68

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.
@@ -1,14 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __typeError = (msg) => {
3
- throw TypeError(msg);
4
- };
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
8
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
9
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
10
- var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
11
- var _regexURLSafeStringComponent, _regexSTSBase64, _regexJWT, _network, _databaseContainer, _stsAuthContainer, _stsRest01Container, _ioRedisContainer, _stsFhirContainer, _ioRedisMessageProcessorUrl, _authEndpoint, _authPort, _authHost, _rest01Host, _rest01Port, _rest01Endpoint, _fhirHost, _fhirPort, _fhirEndpoint;
12
1
  import { expect } from "vitest";
13
2
  import chalk from "chalk";
14
3
  import * as tough from "tough-cookie";
@@ -20,343 +9,324 @@ import { defaultLogger, AgentManager, STSAxiosConfig, Sleep } from "@nsshunt/sts
20
9
  import http from "node:http";
21
10
  import https from "node:https";
22
11
  class TestHelper {
12
+ //#regexBase64URL = /^[A-Za-z0-9_-]+$/ // Base64URL - https://base64.guru/standards/base64url
13
+ #regexURLSafeStringComponent = /[-a-zA-Z0-9@:%._+~#=]{1,256}/;
14
+ // URL safe string component
15
+ //#regexBase64 = /(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?/ // Base64 - https://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data
16
+ #regexSTSBase64 = /SES_(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?/;
17
+ // Base64
18
+ #regexJWT = /[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+/;
19
+ // JWT (Base64URL.Base64URL.Base64URL)
20
+ #network;
21
+ #databaseContainer;
22
+ #stsAuthContainer;
23
+ #stsRest01Container;
24
+ #ioRedisContainer;
25
+ #stsFhirContainer;
26
+ #ioRedisMessageProcessorUrl = "";
27
+ #authEndpoint = "";
28
+ #authPort = "";
29
+ #authHost = "";
30
+ #rest01Host = "";
31
+ #rest01Port = "";
32
+ #rest01Endpoint = "";
33
+ #fhirHost = "";
34
+ #fhirPort = "";
35
+ #fhirEndpoint = "";
23
36
  constructor() {
24
- //#regexBase64URL = /^[A-Za-z0-9_-]+$/ // Base64URL - https://base64.guru/standards/base64url
25
- __privateAdd(this, _regexURLSafeStringComponent, /[-a-zA-Z0-9@:%._+~#=]{1,256}/);
26
- // URL safe string component
27
- //#regexBase64 = /(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?/ // Base64 - https://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data
28
- __privateAdd(this, _regexSTSBase64, /SES_(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?/);
29
- // Base64
30
- __privateAdd(this, _regexJWT, /[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+/);
31
- // JWT (Base64URL.Base64URL.Base64URL)
32
- __privateAdd(this, _network);
33
- __privateAdd(this, _databaseContainer);
34
- __privateAdd(this, _stsAuthContainer);
35
- __privateAdd(this, _stsRest01Container);
36
- __privateAdd(this, _ioRedisContainer);
37
- __privateAdd(this, _stsFhirContainer);
38
- __privateAdd(this, _ioRedisMessageProcessorUrl, "");
39
- __privateAdd(this, _authEndpoint, "");
40
- __privateAdd(this, _authPort, "");
41
- __privateAdd(this, _authHost, "");
42
- __privateAdd(this, _rest01Host, "");
43
- __privateAdd(this, _rest01Port, "");
44
- __privateAdd(this, _rest01Endpoint, "");
45
- __privateAdd(this, _fhirHost, "");
46
- __privateAdd(this, _fhirPort, "");
47
- __privateAdd(this, _fhirEndpoint, "");
48
- __publicField(this, "StartNetwork", async () => {
49
- __privateSet(this, _network, await new Network().start());
50
- });
51
- __publicField(this, "StopNetwork", async () => {
52
- await __privateGet(this, _network).stop();
53
- });
54
- __publicField(this, "CreateRandomString", () => {
55
- const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_~.";
56
- let random = "";
57
- const randomValues = Array.from(crypto.getRandomValues(new Uint8Array(43)));
58
- randomValues.forEach((v) => random += charset[v % charset.length]);
59
- return random;
60
- });
61
- __publicField(this, "Login", async (username, password) => {
62
- const client_id = process.env.CLIENT_ID;
63
- const nonce = crypto.randomBytes(43).toString("base64");
64
- const response_type = "code";
65
- const redirect_uri = process.env.REDIRECT_URI;
66
- const response_mode = "query";
67
- const scope = process.env.SCOPE;
68
- const state = crypto.randomBytes(43).toString("base64");
69
- const code_verifier = this.CreateRandomString();
70
- const code_challenge = crypto.createHash("sha256").update(code_verifier).digest("base64");
71
- const code_challenge_method = "S256";
72
- const authoriseOptions = {
73
- email: username,
74
- password,
75
- client_id,
76
- nonce,
77
- response_type,
78
- redirect_uri,
79
- response_mode,
80
- scope,
81
- state,
82
- code_challenge,
83
- code_challenge_method
84
- };
85
- const url = `${__privateGet(this, _authEndpoint)}${goptions.asapiroot}/login`;
86
- console.log(`Login ---------------------------------------------------------------------------`);
87
- console.log(`url: [${url}]`);
88
- console.log(`authoriseOptions: [${JSON.stringify(authoriseOptions)}]`);
89
- const agentManager = new AgentManager({
90
- httpAgentFactory(options) {
91
- return new http.Agent(options);
92
- },
93
- httpsAgentFactory(options) {
94
- return new https.Agent(options);
95
- }
96
- });
97
- const retVal = await axios(new STSAxiosConfig(url, "post").withDefaultHeaders().withData(authoriseOptions).withAgentManager(agentManager).config);
98
- return retVal;
99
- });
100
- /*
101
- GetAuthServerAPITokenFromServer = async (): Promise<string> => {
102
- return await this.#authUtilsNode.GetAPITokenFromAuthServer(STSClientID.STSTestingService,
103
- "eN9u0mHZLGWZrdnE1zit2vL6xwUFW466sTZcbkXDml5KWxlvKaZ1uiOZmA==",
104
- goptions.asapiidentifier, this.#authEndpoint)
105
- }
106
- */
107
- __publicField(this, "StartDatabase", async () => {
108
- __privateSet(this, _databaseContainer, await new GenericContainer("postgres").withExposedPorts(5432).withEnvironment({
109
- POSTGRES_PASSWORD: "postgres"
110
- //UV_THREADPOOL_SIZE: "64"
111
- }).withNetwork(__privateGet(this, _network)).withNetworkAliases("database").start());
112
- const httpPort = __privateGet(this, _databaseContainer).getMappedPort(5432);
113
- const host = __privateGet(this, _databaseContainer).getHost();
114
- const networkIpAddress = __privateGet(this, _databaseContainer).getIpAddress(__privateGet(this, _network).getName());
115
- process.env.DB_HOST = `${host}:${httpPort}`;
116
- $ResetOptions();
117
- this.LogDebugMessage(chalk.green(`httpPort: [${httpPort}]`));
118
- this.LogDebugMessage(chalk.green(`host: [${host}]`));
119
- this.LogDebugMessage(chalk.green(`networkIpAddress: [${networkIpAddress}]`));
120
- this.LogDebugMessage(chalk.green(`connectionString: [${goptions.connectionString}]`));
121
- this.LogDebugMessage(chalk.green(`defaultDatabaseConnectionString: [${goptions.defaultDatabaseConnectionString}]`));
122
- });
123
- __publicField(this, "StopDatabase", async () => {
124
- if (__privateGet(this, _databaseContainer)) {
125
- await __privateGet(this, _databaseContainer).stop();
126
- this.LogDebugMessage(chalk.yellow(`Used the following parameters for the database during testing:`));
127
- this.LogDebugMessage(chalk.yellow(`connectionString: [${goptions.connectionString}]`));
128
- this.LogDebugMessage(chalk.yellow(`defaultDatabaseConnectionString: [${goptions.defaultDatabaseConnectionString}]`));
129
- }
130
- });
131
- // Note: .withCopyFilesToContainer and .withCopyContentToContainer have a defect in that Jest will not close. A file handle/stream is left open
132
- // within the underlying code.
133
- __publicField(this, "InitializeDatabase", async () => {
134
- const stsAuthContainerInit = await new GenericContainer("serza/stsauth:latest").withEnvironment({
135
- DB_USER: "postgres",
136
- DB_PASSWORD: "postgres",
137
- DB_HOST: "database:5432",
138
- // "192.168.14.101",
139
- POOL_SIZE: "50",
140
- MAX_CPU: "2",
141
- DEBUG: "proc*",
142
- HTTPS_SERVER_KEY_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.key",
143
- HTTPS_SERVER_CERT_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.cert",
144
- AS_ENDPOINT: "https://stscore.stsmda.org"
145
- }).withCommand(["node", "dist/app", "create"]).withNetwork(__privateGet(this, _network)).withNetworkAliases("stsauthrunnerinit").withWaitStrategy(Wait.forLogMessage(`User registered: {"status":200,"detail":{"id":"USR_STSGlobalAdminUser@stsmda.com","name":"STSGlobalAdminUser@stsmda.com","email":"STSGlobalAdminUser@stsmda.com","roles":[]}}`)).start();
146
- await Sleep(500);
147
- await stsAuthContainerInit.stop();
148
- });
149
- __publicField(this, "StartRedis", async () => {
150
- __privateSet(this, _ioRedisContainer, await new GenericContainer("redis/redis-stack-server").withEnvironment({
151
- REDIS_ARGS: '--save "" --appendonly no'
152
- }).withExposedPorts(6379).withNetwork(__privateGet(this, _network)).withNetworkAliases("redisstackserver").start());
153
- __privateSet(this, _ioRedisMessageProcessorUrl, `redis://${__privateGet(this, _ioRedisContainer).getHost()}:${__privateGet(this, _ioRedisContainer).getMappedPort(6379)}`);
154
- await Sleep(2e3);
155
- this.LogDebugMessage(chalk.green(`redis/redis-stack-server Started. Server Details: ioRedisMessageProcessorUrl: [${__privateGet(this, _ioRedisMessageProcessorUrl)}]`));
156
- });
157
- __publicField(this, "StopRedis", async () => {
158
- await __privateGet(this, _ioRedisContainer).stop();
159
- await Sleep(200);
160
- });
161
- __publicField(this, "StartAuthService", async () => {
162
- __privateSet(this, _stsAuthContainer, await new GenericContainer("serza/stsauth:latest").withExposedPorts(3002).withEnvironment({
163
- DB_USER: "postgres",
164
- DB_PASSWORD: "postgres",
165
- DB_HOST: "database:5432",
166
- POOL_SIZE: "50",
167
- MAX_CPU: "2",
168
- DEBUG: "proc*",
169
- HTTPS_SERVER_KEY_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.key",
170
- HTTPS_SERVER_CERT_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.cert",
171
- AS_ENDPOINT: "https://stscore.stsmda.org",
172
- IM_REDIS_MESSAGE_PROCESSOR_URL: "redis://redisstackserver:6379"
173
- }).withNetwork(__privateGet(this, _network)).withNetworkAliases("stsauthrunner").withWaitStrategy(Wait.forHttp("/stsauth/v1.0/latency", 3002).usingTls().allowInsecure()).start());
174
- const httpAuthPort = __privateGet(this, _stsAuthContainer).getMappedPort(3002);
175
- await Sleep(200);
176
- __privateSet(this, _authHost, "https://localhost");
177
- __privateSet(this, _authPort, httpAuthPort);
178
- __privateSet(this, _authEndpoint, `${__privateGet(this, _authHost)}:${__privateGet(this, _authPort)}`);
179
- this.LogDebugMessage(chalk.green(`STSAuth Started. Server Details: Host: [${__privateGet(this, _authHost)}], Port: [${__privateGet(this, _authPort)}]`));
180
- });
181
- __publicField(this, "StartFhirService", async () => {
182
- __privateSet(this, _stsFhirContainer, await new GenericContainer("serza/stsfhir:latest").withExposedPorts(3005).withEnvironment({
183
- DB_USER: "postgres",
184
- DB_PASSWORD: "postgres",
185
- DB_HOST: "database:5432",
186
- POOL_SIZE: "50",
187
- MAX_CPU: "2",
188
- DEBUG: "proc*",
189
- HTTPS_SERVER_KEY_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.key",
190
- HTTPS_SERVER_CERT_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.cert",
191
- AS_ENDPOINT: "https://stsauthrunner",
192
- AS_HOST_PORT: "3002",
193
- AS_PORT: "3002",
194
- IM_REDIS_MESSAGE_PROCESSOR_URL: "redis://redisstackserver:6379",
195
- SOCKET_IO_REDIS_ADAPTOR_URL: "redis://redisstackserver:6379",
196
- STSFHIR_PORT: "3005",
197
- STSFHIR_HOST_PORT: "3005",
198
- STSFHIR_ENDPOINT: "https://stsfhirserver",
199
- // "https://stscore.stsmda.org",
200
- UV_THREADPOOL_SIZE: "64",
201
- FORCE_COLOR: "3"
202
- //node dist/app usedb postgresResources
203
- }).withNetwork(__privateGet(this, _network)).withNetworkAliases("stsfhirserver").start());
204
- __privateGet(this, _stsFhirContainer).logs().then((stream) => {
205
- stream.on("data", (line) => process.stdout.write(line.toString())).on("err", (line) => process.stderr.write(line.toString())).on("end", () => console.log("Stream ended."));
206
- });
207
- const httpFhirPort = __privateGet(this, _stsFhirContainer).getMappedPort(3005);
208
- await Sleep(200);
209
- __privateSet(this, _fhirHost, "https://localhost");
210
- __privateSet(this, _fhirPort, httpFhirPort);
211
- __privateSet(this, _fhirEndpoint, `${__privateGet(this, _fhirHost)}:${__privateGet(this, _fhirPort)}`);
212
- this.LogDebugMessage(chalk.green(`STSFhir Started. Server Details: Host: [${__privateGet(this, _fhirHost)}], Port: [${__privateGet(this, _fhirPort)}]`));
213
- });
214
- __publicField(this, "StopAuthService", async () => {
215
- if (__privateGet(this, _stsAuthContainer)) {
216
- await __privateGet(this, _stsAuthContainer).stop();
217
- await Sleep(200);
218
- }
219
- });
220
- __publicField(this, "StopRest01Service", async () => {
221
- if (__privateGet(this, _stsRest01Container)) {
222
- await __privateGet(this, _stsRest01Container).stop();
223
- await Sleep(200);
224
- }
225
- });
226
- __publicField(this, "StopFhirService", async () => {
227
- if (__privateGet(this, _stsFhirContainer)) {
228
- await __privateGet(this, _stsFhirContainer).stop();
229
- await Sleep(200);
230
- }
231
- });
232
- __publicField(this, "TestLoginAndVerify", async () => {
233
- expect.assertions(4);
234
- console.log(`TestLoginAndVerify:1`);
235
- const retVal = await this.Login("user01@stsmda.com.au", "user01password");
236
- expect(retVal.status).toEqual(200);
237
- console.log(`TestLoginAndVerify:2`);
238
- this.LogDebugMessage(chalk.red(`${JSON.stringify(retVal.data)}`));
239
- this.LogDebugMessage(chalk.magenta(`${JSON.stringify(retVal.headers)}`));
240
- this.LogDebugMessage(chalk.yellow(`${JSON.stringify(retVal.headers["set-cookie"])}`));
241
- const cookies = retVal.headers["set-cookie"];
242
- this.LogDebugMessage(chalk.yellow(`${cookies[0]}`));
243
- this.LogDebugMessage(chalk.green(`${JSON.stringify(tough.Cookie.parse(cookies[0]))}`));
244
- console.log(`TestLoginAndVerify:3`);
245
- const cookie = tough.Cookie.parse(cookies[0]);
246
- console.log(`TestLoginAndVerify:4`);
247
- const desiredCookieResultAxios = {
248
- key: "consent_cookie",
249
- value: expect.stringMatching(__privateGet(this, _regexURLSafeStringComponent)),
250
- path: "/",
251
- secure: true,
252
- httpOnly: true,
253
- sameSite: "strict"
254
- };
255
- const cookieResult = JSON.parse(JSON.stringify(cookie));
256
- expect(cookieResult).toMatchObject(desiredCookieResultAxios);
257
- console.log(`TestLoginAndVerify:5`);
258
- const cookieExpireDate = new Date(cookie.expires);
259
- expect(cookieExpireDate.getTime()).toBeGreaterThan((/* @__PURE__ */ new Date()).getTime());
260
- const consentRequired = process.env.CONSENT_REQUIRED.split(" ").sort();
261
- retVal.data.detail.consentRequired = retVal.data.detail.consentRequired.sort();
262
- console.log(`TestLoginAndVerify:6`);
263
- const desiredResult = {
264
- sessionId: expect.stringMatching(__privateGet(this, _regexSTSBase64)),
265
- id_token: expect.stringMatching(__privateGet(this, _regexJWT)),
266
- consentRequired
267
- };
268
- console.log(`TestLoginAndVerify:7`);
269
- expect(retVal.data.detail).toMatchObject(desiredResult);
270
- console.log(`TestLoginAndVerify:9`);
271
- });
272
- __publicField(this, "StartRest01Service", async () => {
273
- __privateSet(this, _stsRest01Container, await new GenericContainer("serza/stsrest01:latest").withExposedPorts(3003).withEnvironment({
274
- DB_USER: "postgres",
275
- DB_PASSWORD: "postgres",
276
- DB_HOST: "database:5432",
277
- POOL_SIZE: "50",
278
- MAX_CPU: "2",
279
- DEBUG: "proc*",
280
- HTTPS_SERVER_KEY_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.key",
281
- HTTPS_SERVER_CERT_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.cert",
282
- //AS_ENDPOINT: "https://stscore.stsmda.org",
283
- AS_ENDPOINT: "https://stsauthrunner",
284
- // this.authHost,
285
- //AS_ENDPOINT: 'https://localhost', // this.authHost,
286
- AS_HOST_PORT: "3002",
287
- AS_PORT: "3002",
288
- IM_REDIS_MESSAGE_PROCESSOR_URL: "redis://redisstackserver:6379"
289
- //AS_HOST_PORT: this.authPort,
290
- //AS_PORT: this.authPort,
291
- //REST01_PORT: '3003',
292
- //REST01_HOST_PORT: '3003',
293
- //REST01_ENDPOINT: 'https://localhost' // 'https://stscore.stsmda.org'
294
- }).withNetwork(__privateGet(this, _network)).withNetworkAliases("rest01").withWaitStrategy(Wait.forHttp("/stsrest01/v1/latency", 3003).usingTls().allowInsecure()).start());
295
- const httpRest01Port = __privateGet(this, _stsRest01Container).getMappedPort(3003);
296
- await Sleep(200);
297
- __privateSet(this, _rest01Host, "https://localhost");
298
- __privateSet(this, _rest01Port, httpRest01Port);
299
- __privateSet(this, _rest01Endpoint, `${__privateGet(this, _rest01Host)}:${__privateGet(this, _rest01Port)}`);
300
- this.LogDebugMessage(chalk.green(`STSRest01 Started. Server Details: Host: [${this.rest01Host}], Port: [${this.rest01Port}]`));
301
- });
302
- __privateSet(this, _authEndpoint, "https://localhost:3002");
37
+ this.#authEndpoint = "https://localhost:3002";
303
38
  }
304
39
  LogDebugMessage(message) {
305
40
  defaultLogger.debug(message);
306
41
  }
42
+ StartNetwork = async () => {
43
+ this.#network = await new Network().start();
44
+ };
45
+ StopNetwork = async () => {
46
+ await this.#network.stop();
47
+ };
307
48
  get network() {
308
- return __privateGet(this, _network);
49
+ return this.#network;
309
50
  }
310
51
  get authPort() {
311
- return __privateGet(this, _authPort);
52
+ return this.#authPort;
312
53
  }
313
54
  get authHost() {
314
- return __privateGet(this, _authHost);
55
+ return this.#authHost;
315
56
  }
316
57
  get authEndpoint() {
317
- return __privateGet(this, _authEndpoint);
58
+ return this.#authEndpoint;
318
59
  }
319
60
  get rest01Port() {
320
- return __privateGet(this, _rest01Port);
61
+ return this.#rest01Port;
321
62
  }
322
63
  get rest01Host() {
323
- return __privateGet(this, _rest01Host);
64
+ return this.#rest01Host;
324
65
  }
325
66
  get rest01Endpoint() {
326
- return __privateGet(this, _rest01Endpoint);
67
+ return this.#rest01Endpoint;
327
68
  }
328
69
  get fhirPort() {
329
- return __privateGet(this, _fhirPort);
70
+ return this.#fhirPort;
330
71
  }
331
72
  get fhirHost() {
332
- return __privateGet(this, _fhirHost);
73
+ return this.#fhirHost;
333
74
  }
334
75
  get fhirEndpoint() {
335
- return __privateGet(this, _fhirEndpoint);
76
+ return this.#fhirEndpoint;
336
77
  }
337
78
  get ioRedisMessageProcessorUrl() {
338
- return __privateGet(this, _ioRedisMessageProcessorUrl);
79
+ return this.#ioRedisMessageProcessorUrl;
80
+ }
81
+ CreateRandomString = () => {
82
+ const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_~.";
83
+ let random = "";
84
+ const randomValues = Array.from(crypto.getRandomValues(new Uint8Array(43)));
85
+ randomValues.forEach((v) => random += charset[v % charset.length]);
86
+ return random;
87
+ };
88
+ Login = async (username, password) => {
89
+ const client_id = process.env.CLIENT_ID;
90
+ const nonce = crypto.randomBytes(43).toString("base64");
91
+ const response_type = "code";
92
+ const redirect_uri = process.env.REDIRECT_URI;
93
+ const response_mode = "query";
94
+ const scope = process.env.SCOPE;
95
+ const state = crypto.randomBytes(43).toString("base64");
96
+ const code_verifier = this.CreateRandomString();
97
+ const code_challenge = crypto.createHash("sha256").update(code_verifier).digest("base64");
98
+ const code_challenge_method = "S256";
99
+ const authoriseOptions = {
100
+ email: username,
101
+ password,
102
+ client_id,
103
+ nonce,
104
+ response_type,
105
+ redirect_uri,
106
+ response_mode,
107
+ scope,
108
+ state,
109
+ code_challenge,
110
+ code_challenge_method
111
+ };
112
+ const url = `${this.#authEndpoint}${goptions.asapiroot}/login`;
113
+ console.log(`Login ---------------------------------------------------------------------------`);
114
+ console.log(`url: [${url}]`);
115
+ console.log(`authoriseOptions: [${JSON.stringify(authoriseOptions)}]`);
116
+ const agentManager = new AgentManager({
117
+ httpAgentFactory(options) {
118
+ return new http.Agent(options);
119
+ },
120
+ httpsAgentFactory(options) {
121
+ return new https.Agent(options);
122
+ }
123
+ });
124
+ const retVal = await axios(new STSAxiosConfig(url, "post").withDefaultHeaders().withData(authoriseOptions).withAgentManager(agentManager).config);
125
+ return retVal;
126
+ };
127
+ /*
128
+ GetAuthServerAPITokenFromServer = async (): Promise<string> => {
129
+ return await this.#authUtilsNode.GetAPITokenFromAuthServer(STSClientID.STSTestingService,
130
+ "eN9u0mHZLGWZrdnE1zit2vL6xwUFW466sTZcbkXDml5KWxlvKaZ1uiOZmA==",
131
+ goptions.asapiidentifier, this.#authEndpoint)
339
132
  }
133
+ */
134
+ StartDatabase = async () => {
135
+ this.#databaseContainer = await new GenericContainer("postgres").withExposedPorts(5432).withEnvironment({
136
+ POSTGRES_PASSWORD: "postgres"
137
+ //UV_THREADPOOL_SIZE: "64"
138
+ }).withNetwork(this.#network).withNetworkAliases("database").start();
139
+ const httpPort = this.#databaseContainer.getMappedPort(5432);
140
+ const host = this.#databaseContainer.getHost();
141
+ const networkIpAddress = this.#databaseContainer.getIpAddress(this.#network.getName());
142
+ process.env.DB_HOST = `${host}:${httpPort}`;
143
+ $ResetOptions();
144
+ this.LogDebugMessage(chalk.green(`httpPort: [${httpPort}]`));
145
+ this.LogDebugMessage(chalk.green(`host: [${host}]`));
146
+ this.LogDebugMessage(chalk.green(`networkIpAddress: [${networkIpAddress}]`));
147
+ this.LogDebugMessage(chalk.green(`connectionString: [${goptions.connectionString}]`));
148
+ this.LogDebugMessage(chalk.green(`defaultDatabaseConnectionString: [${goptions.defaultDatabaseConnectionString}]`));
149
+ };
150
+ StopDatabase = async () => {
151
+ if (this.#databaseContainer) {
152
+ await this.#databaseContainer.stop();
153
+ this.LogDebugMessage(chalk.yellow(`Used the following parameters for the database during testing:`));
154
+ this.LogDebugMessage(chalk.yellow(`connectionString: [${goptions.connectionString}]`));
155
+ this.LogDebugMessage(chalk.yellow(`defaultDatabaseConnectionString: [${goptions.defaultDatabaseConnectionString}]`));
156
+ }
157
+ };
158
+ // Note: .withCopyFilesToContainer and .withCopyContentToContainer have a defect in that Jest will not close. A file handle/stream is left open
159
+ // within the underlying code.
160
+ InitializeDatabase = async () => {
161
+ const stsAuthContainerInit = await new GenericContainer("serza/stsauth:latest").withEnvironment({
162
+ DB_USER: "postgres",
163
+ DB_PASSWORD: "postgres",
164
+ DB_HOST: "database:5432",
165
+ // "192.168.14.101",
166
+ POOL_SIZE: "50",
167
+ MAX_CPU: "2",
168
+ DEBUG: "proc*",
169
+ HTTPS_SERVER_KEY_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.key",
170
+ HTTPS_SERVER_CERT_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.cert",
171
+ AS_ENDPOINT: "https://stscore.stsmda.org"
172
+ }).withCommand(["node", "dist/app", "create"]).withNetwork(this.#network).withNetworkAliases("stsauthrunnerinit").withWaitStrategy(Wait.forLogMessage(`User registered: {"status":200,"detail":{"id":"USR_STSGlobalAdminUser@stsmda.com","name":"STSGlobalAdminUser@stsmda.com","email":"STSGlobalAdminUser@stsmda.com","roles":[]}}`)).start();
173
+ await Sleep(500);
174
+ await stsAuthContainerInit.stop();
175
+ };
176
+ StartRedis = async () => {
177
+ this.#ioRedisContainer = await new GenericContainer("redis/redis-stack-server").withEnvironment({
178
+ REDIS_ARGS: '--save "" --appendonly no'
179
+ }).withExposedPorts(6379).withNetwork(this.#network).withNetworkAliases("redisstackserver").start();
180
+ this.#ioRedisMessageProcessorUrl = `redis://${this.#ioRedisContainer.getHost()}:${this.#ioRedisContainer.getMappedPort(6379)}`;
181
+ await Sleep(2e3);
182
+ this.LogDebugMessage(chalk.green(`redis/redis-stack-server Started. Server Details: ioRedisMessageProcessorUrl: [${this.#ioRedisMessageProcessorUrl}]`));
183
+ };
184
+ StopRedis = async () => {
185
+ await this.#ioRedisContainer.stop();
186
+ await Sleep(200);
187
+ };
188
+ StartAuthService = async () => {
189
+ this.#stsAuthContainer = await new GenericContainer("serza/stsauth:latest").withExposedPorts(3002).withEnvironment({
190
+ DB_USER: "postgres",
191
+ DB_PASSWORD: "postgres",
192
+ DB_HOST: "database:5432",
193
+ POOL_SIZE: "50",
194
+ MAX_CPU: "2",
195
+ DEBUG: "proc*",
196
+ HTTPS_SERVER_KEY_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.key",
197
+ HTTPS_SERVER_CERT_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.cert",
198
+ AS_ENDPOINT: "https://stscore.stsmda.org",
199
+ IM_REDIS_MESSAGE_PROCESSOR_URL: "redis://redisstackserver:6379"
200
+ }).withNetwork(this.#network).withNetworkAliases("stsauthrunner").withWaitStrategy(Wait.forHttp("/stsauth/v1.0/latency", 3002).usingTls().allowInsecure()).start();
201
+ const httpAuthPort = this.#stsAuthContainer.getMappedPort(3002);
202
+ await Sleep(200);
203
+ this.#authHost = "https://localhost";
204
+ this.#authPort = httpAuthPort;
205
+ this.#authEndpoint = `${this.#authHost}:${this.#authPort}`;
206
+ this.LogDebugMessage(chalk.green(`STSAuth Started. Server Details: Host: [${this.#authHost}], Port: [${this.#authPort}]`));
207
+ };
208
+ StartFhirService = async () => {
209
+ this.#stsFhirContainer = await new GenericContainer("serza/stsfhir:latest").withExposedPorts(3005).withEnvironment({
210
+ DB_USER: "postgres",
211
+ DB_PASSWORD: "postgres",
212
+ DB_HOST: "database:5432",
213
+ POOL_SIZE: "50",
214
+ MAX_CPU: "2",
215
+ DEBUG: "proc*",
216
+ HTTPS_SERVER_KEY_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.key",
217
+ HTTPS_SERVER_CERT_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.cert",
218
+ AS_ENDPOINT: "https://stsauthrunner",
219
+ AS_HOST_PORT: "3002",
220
+ AS_PORT: "3002",
221
+ IM_REDIS_MESSAGE_PROCESSOR_URL: "redis://redisstackserver:6379",
222
+ SOCKET_IO_REDIS_ADAPTOR_URL: "redis://redisstackserver:6379",
223
+ STSFHIR_PORT: "3005",
224
+ STSFHIR_HOST_PORT: "3005",
225
+ STSFHIR_ENDPOINT: "https://stsfhirserver",
226
+ // "https://stscore.stsmda.org",
227
+ UV_THREADPOOL_SIZE: "64",
228
+ FORCE_COLOR: "3"
229
+ //node dist/app usedb postgresResources
230
+ }).withNetwork(this.#network).withNetworkAliases("stsfhirserver").start();
231
+ this.#stsFhirContainer.logs().then((stream) => {
232
+ stream.on("data", (line) => process.stdout.write(line.toString())).on("err", (line) => process.stderr.write(line.toString())).on("end", () => console.log("Stream ended."));
233
+ });
234
+ const httpFhirPort = this.#stsFhirContainer.getMappedPort(3005);
235
+ await Sleep(200);
236
+ this.#fhirHost = "https://localhost";
237
+ this.#fhirPort = httpFhirPort;
238
+ this.#fhirEndpoint = `${this.#fhirHost}:${this.#fhirPort}`;
239
+ this.LogDebugMessage(chalk.green(`STSFhir Started. Server Details: Host: [${this.#fhirHost}], Port: [${this.#fhirPort}]`));
240
+ };
241
+ StopAuthService = async () => {
242
+ if (this.#stsAuthContainer) {
243
+ await this.#stsAuthContainer.stop();
244
+ await Sleep(200);
245
+ }
246
+ };
247
+ StopRest01Service = async () => {
248
+ if (this.#stsRest01Container) {
249
+ await this.#stsRest01Container.stop();
250
+ await Sleep(200);
251
+ }
252
+ };
253
+ StopFhirService = async () => {
254
+ if (this.#stsFhirContainer) {
255
+ await this.#stsFhirContainer.stop();
256
+ await Sleep(200);
257
+ }
258
+ };
259
+ TestLoginAndVerify = async () => {
260
+ expect.assertions(4);
261
+ console.log(`TestLoginAndVerify:1`);
262
+ const retVal = await this.Login("user01@stsmda.com.au", "user01password");
263
+ expect(retVal.status).toEqual(200);
264
+ console.log(`TestLoginAndVerify:2`);
265
+ this.LogDebugMessage(chalk.red(`${JSON.stringify(retVal.data)}`));
266
+ this.LogDebugMessage(chalk.magenta(`${JSON.stringify(retVal.headers)}`));
267
+ this.LogDebugMessage(chalk.yellow(`${JSON.stringify(retVal.headers["set-cookie"])}`));
268
+ const cookies = retVal.headers["set-cookie"];
269
+ this.LogDebugMessage(chalk.yellow(`${cookies[0]}`));
270
+ this.LogDebugMessage(chalk.green(`${JSON.stringify(tough.Cookie.parse(cookies[0]))}`));
271
+ console.log(`TestLoginAndVerify:3`);
272
+ const cookie = tough.Cookie.parse(cookies[0]);
273
+ console.log(`TestLoginAndVerify:4`);
274
+ const desiredCookieResultAxios = {
275
+ key: "consent_cookie",
276
+ value: expect.stringMatching(this.#regexURLSafeStringComponent),
277
+ path: "/",
278
+ secure: true,
279
+ httpOnly: true,
280
+ sameSite: "strict"
281
+ };
282
+ const cookieResult = JSON.parse(JSON.stringify(cookie));
283
+ expect(cookieResult).toMatchObject(desiredCookieResultAxios);
284
+ console.log(`TestLoginAndVerify:5`);
285
+ const cookieExpireDate = new Date(cookie.expires);
286
+ expect(cookieExpireDate.getTime()).toBeGreaterThan((/* @__PURE__ */ new Date()).getTime());
287
+ const consentRequired = process.env.CONSENT_REQUIRED.split(" ").sort();
288
+ retVal.data.detail.consentRequired = retVal.data.detail.consentRequired.sort();
289
+ console.log(`TestLoginAndVerify:6`);
290
+ const desiredResult = {
291
+ sessionId: expect.stringMatching(this.#regexSTSBase64),
292
+ id_token: expect.stringMatching(this.#regexJWT),
293
+ consentRequired
294
+ };
295
+ console.log(`TestLoginAndVerify:7`);
296
+ expect(retVal.data.detail).toMatchObject(desiredResult);
297
+ console.log(`TestLoginAndVerify:9`);
298
+ };
299
+ StartRest01Service = async () => {
300
+ this.#stsRest01Container = await new GenericContainer("serza/stsrest01:latest").withExposedPorts(3003).withEnvironment({
301
+ DB_USER: "postgres",
302
+ DB_PASSWORD: "postgres",
303
+ DB_HOST: "database:5432",
304
+ POOL_SIZE: "50",
305
+ MAX_CPU: "2",
306
+ DEBUG: "proc*",
307
+ HTTPS_SERVER_KEY_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.key",
308
+ HTTPS_SERVER_CERT_PATH: "/var/lib/sts/stsglobalresources/keys-tmp/server.cert",
309
+ //AS_ENDPOINT: "https://stscore.stsmda.org",
310
+ AS_ENDPOINT: "https://stsauthrunner",
311
+ // this.authHost,
312
+ //AS_ENDPOINT: 'https://localhost', // this.authHost,
313
+ AS_HOST_PORT: "3002",
314
+ AS_PORT: "3002",
315
+ IM_REDIS_MESSAGE_PROCESSOR_URL: "redis://redisstackserver:6379"
316
+ //AS_HOST_PORT: this.authPort,
317
+ //AS_PORT: this.authPort,
318
+ //REST01_PORT: '3003',
319
+ //REST01_HOST_PORT: '3003',
320
+ //REST01_ENDPOINT: 'https://localhost' // 'https://stscore.stsmda.org'
321
+ }).withNetwork(this.#network).withNetworkAliases("rest01").withWaitStrategy(Wait.forHttp("/stsrest01/v1/latency", 3003).usingTls().allowInsecure()).start();
322
+ const httpRest01Port = this.#stsRest01Container.getMappedPort(3003);
323
+ await Sleep(200);
324
+ this.#rest01Host = "https://localhost";
325
+ this.#rest01Port = httpRest01Port;
326
+ this.#rest01Endpoint = `${this.#rest01Host}:${this.#rest01Port}`;
327
+ this.LogDebugMessage(chalk.green(`STSRest01 Started. Server Details: Host: [${this.rest01Host}], Port: [${this.rest01Port}]`));
328
+ };
340
329
  }
341
- _regexURLSafeStringComponent = new WeakMap();
342
- _regexSTSBase64 = new WeakMap();
343
- _regexJWT = new WeakMap();
344
- _network = new WeakMap();
345
- _databaseContainer = new WeakMap();
346
- _stsAuthContainer = new WeakMap();
347
- _stsRest01Container = new WeakMap();
348
- _ioRedisContainer = new WeakMap();
349
- _stsFhirContainer = new WeakMap();
350
- _ioRedisMessageProcessorUrl = new WeakMap();
351
- _authEndpoint = new WeakMap();
352
- _authPort = new WeakMap();
353
- _authHost = new WeakMap();
354
- _rest01Host = new WeakMap();
355
- _rest01Port = new WeakMap();
356
- _rest01Endpoint = new WeakMap();
357
- _fhirHost = new WeakMap();
358
- _fhirPort = new WeakMap();
359
- _fhirEndpoint = new WeakMap();
360
330
  export {
361
331
  TestHelper
362
332
  };