-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathauth.setup.ts
More file actions
158 lines (133 loc) · 4.55 KB
/
auth.setup.ts
File metadata and controls
158 lines (133 loc) · 4.55 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
import { expect, test } from '@playwright/test';
import {
logInWithUsernameAndPassword,
storeStorageStateAndToken,
ensureNotInPreview,
logout,
} from 'test-utils/helpers/auth';
import { existsSync, mkdirSync } from 'fs';
import { disableTrackingAndConsent } from 'test-utils';
const authDir = '.auth';
if (!existsSync(authDir)) {
mkdirSync(authDir);
}
const ADMIN_KEY = 'admin';
const ADVISORY_REMEDIATION_KEY = 'advisory_remediation';
type UserConfig = {
key: string;
tokenEnvVar: string;
credentialEnvVars: [string, string]; // [usernameEnvVar, passwordEnvVar]
requiredFlags: {
integration?: boolean;
};
};
const ALL_USERS: UserConfig[] = [
{
key: ADMIN_KEY,
tokenEnvVar: 'ADMIN_TOKEN',
credentialEnvVars: ['ADMIN_USERNAME', 'ADMIN_PASSWORD'],
requiredFlags: {},
},
{
key: ADVISORY_REMEDIATION_KEY,
tokenEnvVar: 'ADVISORY_REMEDIATION_TOKEN',
credentialEnvVars: ['ADVISORY_REMEDIATION_USERNAME', 'ADVISORY_REMEDIATION_PASSWORD'],
requiredFlags: {},
},
];
/**
* Builds the list of users to authenticate based on environment flags.
* If AUTH_USERS is set, uses that list instead. Admin is always last.
*/
const buildActiveUsers = (): UserConfig[] => {
if (process.env.AUTH_USERS) {
const requestedKeys = process.env.AUTH_USERS.split(',').map((k) => k.trim());
const users = ALL_USERS.filter((u) => requestedKeys.includes(u.key));
if (requestedKeys.length !== users.length) {
const missingUsers = requestedKeys.filter((k) => !users.find((u) => u.key === k));
console.log(`\u001b[48;5;160mUser(s): "${missingUsers.join(', ')}" not found!\x1b[0m`);
console.log(
`\u001b[48;5;27mAvailable ones are:\x1b[0m ${ALL_USERS.map((u) => u.key).join(', ')}`,
);
throw new Error(`Couldn't find user(s): ${missingUsers.join(', ')}`);
}
const adminIndex = users.findIndex((u) => u.key === ADMIN_KEY);
if (adminIndex !== -1 && adminIndex !== users.length - 1) {
const [admin] = users.splice(adminIndex, 1);
users.push(admin);
}
return users;
}
const users = ALL_USERS.filter((user) => {
const { integration } = user.requiredFlags;
if (!integration) {
return false;
}
if (integration && !process.env.INTEGRATION) {
return false;
}
return true;
});
if (!process.env.INTEGRATION) {
users.push(ALL_USERS.find((u) => u.key === ADVISORY_REMEDIATION_KEY)!);
}
users.push(ALL_USERS.find((u) => u.key === ADMIN_KEY)!);
return users;
};
/**
* Returns the list of required environment variables based on selected users.
* Includes base vars, user credentials, and integration-specific vars.
*/
const getRequiredEnvVars = (users: UserConfig[]): string[] => {
const baseVars = ['BASE_URL'];
const userCredentials = users.flatMap((u) => u.credentialEnvVars);
const integrationVars = process.env.INTEGRATION
? [
'RH_CLIENT_PROXY',
'ORG_ID_1',
'ACTIVATION_KEY_1',
...(process.env.BASE_URL?.includes('foo') ? [] : ['PROXY']),
]
: [];
return [...new Set([...baseVars, ...userCredentials, ...integrationVars])];
};
const activeUsers = buildActiveUsers();
/**
* Validates that all required environment variables are set.
* Throws an error at module load time if any are missing.
*/
const validateEnvVars = () => {
const wrongProxySettings = process.env.BASE_URL?.includes('foo') && !!process.env.PROXY;
if (wrongProxySettings) {
throw new Error(
'\u001b[48;5;160mYou are trying to test against a locally running frontend while having PROXY set, please unset it.\x1b[0m',
);
}
const required = getRequiredEnvVars(activeUsers);
const missing = required.filter((v) => !process.env[v]);
if (missing.length > 0) {
throw new Error('\u001b[48;5;160mMissing env variables:\x1b[0m ' + missing.join(', '), {});
}
};
validateEnvVars();
test.describe('Setup Authentication States', () => {
test.describe.configure({ retries: 2 });
for (const user of activeUsers) {
test(`Authenticate ${user.key} user and save state`, async ({ page }) => {
test.setTimeout(60_000);
await disableTrackingAndConsent(page);
await logInWithUsernameAndPassword(
page,
process.env[user.credentialEnvVars[0]],
process.env[user.credentialEnvVars[1]],
);
await ensureNotInPreview(page);
await storeStorageStateAndToken(page, user.tokenEnvVar);
expect(process.env[user.tokenEnvVar]).toBeDefined();
// Logout unless it's the admin (last user)
if (user.key !== ADMIN_KEY) {
await logout(page);
}
});
}
});