Skip to content

Commit b705e00

Browse files
Merge pull request #16 from precise-alloy/features/basic-auth
feat: basic auth
2 parents f45eceb + 8c38a4f commit b705e00

10 files changed

Lines changed: 510 additions & 30 deletions

File tree

.engine_scripts/playwright/onBefore.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
module.exports = async (page, scenario, viewport, isReference, browserContext) => {
2+
if (scenario.basicAuth) {
3+
const entries = (Array.isArray(scenario.basicAuth) ? scenario.basicAuth : [scenario.basicAuth]).filter(
4+
(entry) => entry && entry.origin && entry.username && entry.password
5+
);
6+
const targetUrl = isReference && scenario.referenceUrl ? scenario.referenceUrl : scenario.url;
7+
const targetOrigin = new URL(targetUrl).origin;
8+
const match = entries.find((entry) => entry.origin === targetOrigin);
9+
10+
if (match) {
11+
// Backstop creates the browser context before this hook runs. Set
12+
// Playwright HTTP credentials on that fresh per-scenario context so the
13+
// browser responds only to Basic-auth challenges for the configured
14+
// origin, without broadcasting Authorization headers to subresources or
15+
// redirects.
16+
await browserContext.setHTTPCredentials({
17+
origin: match.origin,
18+
username: match.username,
19+
password: match.password,
20+
});
21+
} else if (entries.length > 0) {
22+
const configuredOrigins = entries.map((entry) => entry.origin).join(', ');
23+
console.warn(
24+
`No basicAuth entry matches scenario origin ${targetOrigin} (configured: ${configuredOrigins}); skipping Basic auth credentials for ${scenario.label}`
25+
);
26+
}
27+
}
28+
229
if (scenario.bypassCsp) {
330
const browser = browserContext.browser();
431
const browserName = browser ? browser.browserType().name() : undefined;

common/regressify-schema.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@
9898
},
9999
"requiredLogin": {
100100
"type": "boolean"
101+
},
102+
"basicAuth": {
103+
"$ref": "#/definitions/BasicAuthConfig"
101104
}
102105
},
103106
"title": "WorkspaceConfig"
@@ -124,6 +127,43 @@
124127
"test"
125128
],
126129
"title": "UrlReplacements"
130+
},
131+
"BasicAuthConfig": {
132+
"description": "HTTP Basic Authentication credentials. Provide a single entry to protect one origin, or an array of entries to protect several origins; the entry matching the scenario's origin is selected at runtime.",
133+
"oneOf": [
134+
{
135+
"$ref": "#/definitions/BasicAuth"
136+
},
137+
{
138+
"type": "array",
139+
"items": {
140+
"$ref": "#/definitions/BasicAuth"
141+
}
142+
}
143+
]
144+
},
145+
"BasicAuth": {
146+
"type": "object",
147+
"additionalProperties": false,
148+
"description": "HTTP Basic Authentication credentials applied before navigation. Each value may reference an environment variable with ${VAR} or $VAR so secrets stay out of committed YAML. Origin must be the protected origin (scheme://host:port).",
149+
"properties": {
150+
"origin": {
151+
"type": "string",
152+
"description": "Protected origin (scheme://host:port). May contain ${VAR}/$VAR references (including embedded, e.g. https://${HOST}); it is expanded and normalized to an origin at runtime."
153+
},
154+
"username": {
155+
"type": "string"
156+
},
157+
"password": {
158+
"type": "string"
159+
}
160+
},
161+
"required": [
162+
"origin",
163+
"username",
164+
"password"
165+
],
166+
"title": "BasicAuth"
127167
}
128168
}
129169
}

common/test-schema.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
},
9797
"ignoreSslErrors": {
9898
"type": "boolean"
99+
},
100+
"basicAuth": {
101+
"$ref": "#/definitions/BasicAuthConfig"
99102
}
100103
},
101104
"required": [
@@ -259,6 +262,9 @@
259262
},
260263
"bypassCsp": {
261264
"type": "boolean"
265+
},
266+
"basicAuth": {
267+
"$ref": "#/definitions/BasicAuthConfig"
262268
}
263269
},
264270
"required": [
@@ -291,6 +297,43 @@
291297
],
292298
"title": "URL Replacements"
293299
},
300+
"BasicAuthConfig": {
301+
"description": "HTTP Basic Authentication credentials. Provide a single entry to protect one origin, or an array of entries to protect several origins; the entry matching the scenario's origin is selected at runtime.",
302+
"oneOf": [
303+
{
304+
"$ref": "#/definitions/BasicAuth"
305+
},
306+
{
307+
"type": "array",
308+
"items": {
309+
"$ref": "#/definitions/BasicAuth"
310+
}
311+
}
312+
]
313+
},
314+
"BasicAuth": {
315+
"type": "object",
316+
"additionalProperties": false,
317+
"description": "HTTP Basic Authentication credentials applied before navigation. Each value may reference an environment variable with ${VAR} or $VAR so secrets stay out of committed YAML. Origin must be the protected origin (scheme://host:port).",
318+
"properties": {
319+
"origin": {
320+
"type": "string",
321+
"description": "Protected origin (scheme://host:port). May contain ${VAR}/$VAR references (including embedded, e.g. https://${HOST}); it is expanded and normalized to an origin at runtime."
322+
},
323+
"username": {
324+
"type": "string"
325+
},
326+
"password": {
327+
"type": "string"
328+
}
329+
},
330+
"required": [
331+
"origin",
332+
"username",
333+
"password"
334+
],
335+
"title": "BasicAuth"
336+
},
294337
"CheckAction": {
295338
"type": "object",
296339
"description": "Documentation: https://tuyen.blog/optimizely-cms/testing/create-action/#spoiler--check",

0 commit comments

Comments
 (0)