-
Notifications
You must be signed in to change notification settings - Fork 2
/
toctl_test.ts
130 lines (120 loc) · 4.23 KB
/
toctl_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { testingAsserts as ta } from "./deps-test.ts";
import { govnSvcHealth as gsh, path, shell } from "./deps.ts";
// This unit test can be run two ways:
// 1. With an auto-started child server (default)
// 2. Against an externally started server (set using GTT_TEST_BASE_URL env var)
// export GTT_TEST_BASE_URL=http://localhost:8179
// deno test -A --unstable
let baseURL = Deno.env.get("GTT_TEST_BASE_URL");
let childHttpServer: shell.RunListenableServiceResult | undefined = undefined;
let httpServerCaption = baseURL;
if (!baseURL) {
const port = 8178;
baseURL = `http://localhost:${port}`;
childHttpServer = shell.startListenableService({
port: port,
command: [
Deno.execPath(),
"run",
"-A",
"--unstable",
"toctl.ts",
"server",
`--port=${port}`,
"--module=./mod_test-html-email-messages.tmpl.ts,medigy-email",
"--module=./mod_test.single-tmpl.ts",
"--module=./mod_test.multiple-tmpl.ts",
"--allow-arbitrary-modules",
"--default-module=./template-module-debug.ts",
"--verbose",
],
cwd: path.dirname(path.fromFileUrl(import.meta.url)),
});
ta.assert(childHttpServer.serviceIsRunning, `Server must be started`);
const started = await childHttpServer.waitForListener(10000);
ta.assert(
started,
`Server must start listening at ${baseURL} within 10 seconds:\n ==> ${
childHttpServer.denoRunOpts.cmd.join(" ")
}`,
);
httpServerCaption = `${baseURL} PID ${childHttpServer.process.pid}`;
}
Deno.test(`toctl.ts GET service home page (${httpServerCaption})`, async () => {
const resp = await fetch(`${baseURL}`);
ta.assertEquals(await resp.text(), "Template Orchestration Controller");
});
Deno.test(`toctl.ts GET service health (${httpServerCaption})`, async () => {
const resp = await fetch(`${baseURL}/health`);
const health = await resp.json();
ta.assert(gsh.isHealthy(health));
ta.assert(gsh.isServiceHealthComponents(health));
ta.assertArrayIncludes(
Object.keys(health.details),
[
"template:medigy-email",
"template:mod_test.single-tmpl.ts",
"template:mod_test.multiple-tmpl.ts",
],
);
});
Deno.test(`toctl.ts GET mod_test.single-tmpl.ts with two properties (${httpServerCaption})`, async () => {
const resp = await fetch(
`${baseURL}/transform/mod_test.single-tmpl.ts?body=TestBody&heading=TestHeading`,
);
ta.assertEquals(
await resp.text(),
"<html>\n\n<head>\n TestHeading\n</head>\n\n<body>\n TestBody\n</body>\n\n</html>",
);
});
Deno.test(`toctl.ts GET mod_test.multiple-tmpl.ts 'content1' template with two properties (${httpServerCaption})`, async () => {
const resp = await fetch(
`${baseURL}/transform/mod_test.multiple-tmpl.ts/content1?heading1=TestHeading&body1=TestBody`,
);
ta.assertEquals(await resp.text(), "Template 1: TestHeading, TestBody");
});
Deno.test(`toctl.ts POST medigy-email 'create-password' template with one property (${httpServerCaption})`, async () => {
const resp = await fetch(`${baseURL}/transform`, {
method: "POST",
body: JSON.stringify({
templateName: "medigy-email",
templateIdentity: "create-password",
content: {
authnUrl: "https://www.medigy.com/x/reset-password",
},
}),
});
ta.assertEquals(
await resp.text(),
Deno.readTextFileSync("mod_test-email-message-01.html-output.golden"),
);
});
Deno.test(`toctl.ts POST medigy-email 'create-password' template with invalid property (${httpServerCaption})`, async () => {
const resp = await fetch(`${baseURL}/transform`, {
method: "POST",
body: JSON.stringify({
templateName: "medigy-email",
templateIdentity: "create-password",
content: {
badData: 1,
},
}),
});
ta.assertEquals(resp.status, 400, "Expected error HTTP status");
ta.assertEquals(
await resp.text(),
'unexpected content for template create-password: {"badData":1}',
);
});
if (childHttpServer) {
Deno.test({
name: `toctl.ts stop server (${httpServerCaption})`,
fn: async () => {
await childHttpServer!.stop();
},
// because httpServer is started outside of this method, we need to let Deno know
// not to check for resource leaks
sanitizeOps: false,
sanitizeResources: false,
});
}