-
Notifications
You must be signed in to change notification settings - Fork 340
Expand file tree
/
Copy pathvalidate-e2e-intake-user.js
More file actions
57 lines (48 loc) · 1.69 KB
/
validate-e2e-intake-user.js
File metadata and controls
57 lines (48 loc) · 1.69 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
import fs from 'fs';
import { pathToFileURL } from 'url';
const USER_FILE_LOCATION = 'apps/intake/playwright/user.json';
const TOKEN_VALIDITY_THRESHOLD_MINUTES = 60;
export const validateE2EIntakeUser = (fileLocation) => {
const expiries = [];
try {
const userFile = JSON.parse(fs.readFileSync(fileLocation, 'utf8'));
if (!userFile) {
throw new Error('Could not find user.json file');
}
// // oystehr, localhost, and stripe cookies
// const cookies = userFile.cookies?.map((cookie) => cookie.expires);
// expiries.push(...cookies);
const authItem = userFile.origins?.flatMap(
(origin) =>
origin.localStorage?.find(
(item) =>
// cSpell:disable-next spa js?
item.name?.includes('@@auth0spajs@@') &&
!item.name?.includes('@@user@@') &&
item.value?.includes('expiresAt')
)
)[0];
if (!authItem) {
throw new Error('Could not find Auth0 token');
}
console.log('Found Auth0 token');
const expiresAt = JSON.parse(authItem.value).expiresAt;
expiries.push(expiresAt);
} catch (e) {
console.error('Failed token check:', e.message);
throw new Error('Failed token check');
}
expiries.forEach((expiry) => {
if (!doesTokenLastMoreThanNeeded(expiry)) {
throw new Error('At least 1 token expires soon');
}
});
};
const doesTokenLastMoreThanNeeded = (expiresAt) => {
const timeLeft = (expiresAt * 1000 - Date.now()) / (1000 * 60);
console.log(`Time left: ${timeLeft.toFixed(1)} minutes`);
return timeLeft > TOKEN_VALIDITY_THRESHOLD_MINUTES;
};
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
validateE2EIntakeUser(USER_FILE_LOCATION);
}