-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigFindAndLoad.test.ts
More file actions
188 lines (172 loc) · 5.09 KB
/
Copy pathconfigFindAndLoad.test.ts
File metadata and controls
188 lines (172 loc) · 5.09 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { loadAuthConfig } from "../config/loadAuthConfig";
import { isUserError } from "../internal/userError";
async function makeTempDir(): Promise<string> {
return fs.mkdtemp(path.join(os.tmpdir(), "playwright-kit-auth-"));
}
test("loadAuthConfig finds config by walking up from cwd", async () => {
const root = await makeTempDir();
const nested = path.join(root, "a", "b");
await fs.mkdir(nested, { recursive: true });
const configPath = path.join(root, "playwright.auth.config.ts");
await fs.writeFile(
configPath,
[
"export default {",
" profiles: {",
" admin: {",
" baseURL: 'https://example.com',",
" validateUrl: '/',",
" async login() {},",
" async validate() { return { ok: true }; },",
" },",
" },",
"};",
"",
].join("\n"),
"utf8",
);
const loaded = await loadAuthConfig({ cwd: nested });
assert.equal(loaded.configFilePath, configPath);
assert.equal(loaded.projectRoot, root);
assert.equal(typeof loaded.config.profiles.admin.login, "function");
assert.equal(typeof loaded.config.profiles.admin.validate, "function");
});
test("loadAuthConfig allows webServer.url to be omitted when baseURL is set", async () => {
const root = await makeTempDir();
const configPath = path.join(root, "playwright.auth.config.ts");
await fs.writeFile(
configPath,
[
"export default {",
" baseURL: 'http://127.0.0.1:3000',",
" webServer: { command: 'node', args: ['server.js'], env: { FOO: 'bar' } },",
" profiles: {",
" admin: {",
" validateUrl: '/',",
" async login() {},",
" async validate() { return { ok: true }; },",
" },",
" },",
"};",
"",
].join("\n"),
"utf8",
);
const loaded = await loadAuthConfig({ cwd: root });
assert.equal(loaded.configFilePath, configPath);
assert.ok(loaded.config.webServer);
assert.equal(loaded.config.webServer.command, "node");
assert.deepEqual(loaded.config.webServer.env, { FOO: "bar" });
});
test("loadAuthConfig requires baseURL when webServer.url is omitted", async () => {
const root = await makeTempDir();
const configPath = path.join(root, "playwright.auth.config.ts");
await fs.writeFile(
configPath,
[
"export default {",
" webServer: { command: 'node', args: ['server.js'] },",
" profiles: {",
" admin: {",
" validateUrl: '/',",
" async login() {},",
" async validate() { return { ok: true }; },",
" },",
" },",
"};",
"",
].join("\n"),
"utf8",
);
await assert.rejects(
() => loadAuthConfig({ cwd: root }),
(error) => isUserError(error),
);
});
test("loadAuthConfig rejects non-string webServer.env values", async () => {
const root = await makeTempDir();
const configPath = path.join(root, "playwright.auth.config.ts");
await fs.writeFile(
configPath,
[
"export default {",
" baseURL: 'http://127.0.0.1:3000',",
" webServer: { command: 'node', url: 'http://127.0.0.1:3000', env: { PORT: 3000 } },",
" profiles: {",
" admin: {",
" validateUrl: '/',",
" async login() {},",
" async validate() { return { ok: true }; },",
" },",
" },",
"};",
"",
].join("\n"),
"utf8",
);
await assert.rejects(
() => loadAuthConfig({ cwd: root }),
(error) => isUserError(error),
);
});
test("loadAuthConfig throws a user error when config is missing", async () => {
const root = await makeTempDir();
await assert.rejects(
() => loadAuthConfig({ cwd: root }),
(error) => isUserError(error),
);
});
test("loadAuthConfig rejects unsafe profile names", async () => {
const root = await makeTempDir();
const configPath = path.join(root, "playwright.auth.config.ts");
await fs.writeFile(
configPath,
[
"export default {",
" baseURL: 'https://example.com',",
" profiles: {",
" ' admin ': {",
" validateUrl: '/',",
" async login() {},",
" async validate() { return { ok: true }; },",
" },",
" },",
"};",
"",
].join("\n"),
"utf8",
);
await assert.rejects(
() => loadAuthConfig({ cwd: root }),
(error) => isUserError(error),
);
});
test("loadAuthConfig requires baseURL when validateUrl is relative", async () => {
const root = await makeTempDir();
const configPath = path.join(root, "playwright.auth.config.ts");
await fs.writeFile(
configPath,
[
"export default {",
" profiles: {",
" admin: {",
" validateUrl: '/admin',",
" async login() {},",
" async validate() { return { ok: true }; },",
" },",
" },",
"};",
"",
].join("\n"),
"utf8",
);
await assert.rejects(
() => loadAuthConfig({ cwd: root }),
(error) => isUserError(error),
);
});