@jahia/cypress 8.0.0 → 8.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +65 -0
  3. package/dist/injections/bash-data.d.ts +1 -0
  4. package/dist/injections/bash-data.js +57 -0
  5. package/dist/injections/chars-data.d.ts +1 -0
  6. package/dist/injections/chars-data.js +25 -0
  7. package/dist/injections/htmlentities-data.d.ts +1 -0
  8. package/dist/injections/htmlentities-data.js +22 -0
  9. package/dist/injections/numbers-data.d.ts +1 -0
  10. package/dist/injections/numbers-data.js +66 -0
  11. package/dist/injections/sql-data.d.ts +1 -0
  12. package/dist/injections/sql-data.js +82 -0
  13. package/dist/injections/xss-data.d.ts +1 -0
  14. package/dist/injections/xss-data.js +740 -0
  15. package/dist/support/apollo/apollo.d.ts +2 -0
  16. package/dist/support/apollo/apollo.js +77 -15
  17. package/dist/support/browserHelper.d.ts +10 -0
  18. package/dist/support/browserHelper.js +167 -0
  19. package/dist/support/index.d.ts +3 -0
  20. package/dist/support/index.js +3 -0
  21. package/dist/support/jfaker.d.ts +60 -0
  22. package/dist/support/jfaker.js +241 -0
  23. package/dist/support/modSince.d.ts +52 -0
  24. package/dist/support/modSince.js +185 -0
  25. package/dist/support/provisioning/executeGroovy.js +41 -2
  26. package/dist/support/provisioning/runProvisioningScript.d.ts +1 -1
  27. package/dist/support/provisioning/runProvisioningScript.js +84 -7
  28. package/dist/support/registerSupport.js +34 -0
  29. package/dist/utils/JahiaPlatformHelper.d.ts +9 -0
  30. package/dist/utils/JahiaPlatformHelper.js +17 -6
  31. package/docs/browser-helper.md +158 -0
  32. package/docs/jfaker.md +450 -0
  33. package/package.json +3 -1
  34. package/src/injections/bash-data.ts +54 -0
  35. package/src/injections/chars-data.ts +22 -0
  36. package/src/injections/htmlentities-data.ts +19 -0
  37. package/src/injections/numbers-data.ts +63 -0
  38. package/src/injections/sql-data.ts +79 -0
  39. package/src/injections/xss-data.ts +737 -0
  40. package/src/support/apollo/apollo.ts +74 -11
  41. package/src/support/browserHelper.ts +186 -0
  42. package/src/support/index.ts +3 -0
  43. package/src/support/jfaker.ts +245 -0
  44. package/src/support/modSince.ts +229 -0
  45. package/src/support/provisioning/executeGroovy.md +7 -1
  46. package/src/support/provisioning/executeGroovy.ts +46 -2
  47. package/src/support/provisioning/runProvisioningScript.ts +89 -12
  48. package/src/support/registerSupport.ts +29 -0
  49. package/src/utils/JahiaPlatformHelper.ts +16 -5
  50. package/tests/cypress/e2e/jfaker.spec.ts +411 -0
  51. package/tests/cypress/e2e/modSince.spec.ts +334 -0
  52. package/tests/cypress.config.ts +23 -0
  53. package/tests/package.json +41 -0
  54. package/tests/reporter-config.json +13 -0
  55. package/tests/yarn.lock +8578 -0
  56. package/tsconfig.json +3 -0
@@ -8,9 +8,11 @@ declare global {
8
8
  }
9
9
  export type FileQueryOptions = Partial<QueryOptions> & {
10
10
  queryFile?: string;
11
+ sourcePackage?: string;
11
12
  };
12
13
  export type FileMutationOptions = Partial<MutationOptions> & {
13
14
  mutationFile?: string;
15
+ sourcePackage?: string;
14
16
  };
15
17
  export type ApolloOptions = (QueryOptions | MutationOptions | FileQueryOptions | FileMutationOptions) & Partial<Cypress.Loggable>;
16
18
  export declare const apollo: (apollo: ApolloClient<any>, options: ApolloOptions) => void;
@@ -28,6 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.apollo = void 0;
30
30
  var graphql_tag_1 = __importDefault(require("graphql-tag"));
31
+ var graphql_1 = require("graphql");
31
32
  function isQuery(options) {
32
33
  return options.query !== undefined;
33
34
  }
@@ -37,53 +38,114 @@ function isQueryFile(options) {
37
38
  function isMutationFile(options) {
38
39
  return options.mutationFile !== undefined;
39
40
  }
41
+ function getOperationLabel(doc, opType) {
42
+ var _a, _b, _c, _d, _e;
43
+ var opDef = (0, graphql_1.getOperationAST)(doc);
44
+ if ((_a = opDef === null || opDef === void 0 ? void 0 : opDef.name) === null || _a === void 0 ? void 0 : _a.value) {
45
+ return "[".concat(opType, "] ").concat(opDef.name.value);
46
+ }
47
+ // Anonymous operation: traverse up to 2 selection levels for a meaningful label
48
+ var firstSel = (_c = (_b = opDef === null || opDef === void 0 ? void 0 : opDef.selectionSet) === null || _b === void 0 ? void 0 : _b.selections) === null || _c === void 0 ? void 0 : _c[0];
49
+ if ((firstSel === null || firstSel === void 0 ? void 0 : firstSel.kind) === 'Field') {
50
+ var firstName = firstSel.name.value;
51
+ var secondSel = (_e = (_d = firstSel.selectionSet) === null || _d === void 0 ? void 0 : _d.selections) === null || _e === void 0 ? void 0 : _e[0];
52
+ if ((secondSel === null || secondSel === void 0 ? void 0 : secondSel.kind) === 'Field') {
53
+ return "[".concat(opType, "] ").concat(firstName, " \u203A ").concat(secondSel.name.value);
54
+ }
55
+ return "[".concat(opType, "] ").concat(firstName);
56
+ }
57
+ return "[".concat(opType, "]");
58
+ }
59
+ function getQueryBody(doc) {
60
+ var _a, _b, _c;
61
+ return (_c = (_b = (_a = doc === null || doc === void 0 ? void 0 : doc.loc) === null || _a === void 0 ? void 0 : _a.source) === null || _b === void 0 ? void 0 : _b.body) !== null && _c !== void 0 ? _c : (0, graphql_1.print)(doc);
62
+ }
40
63
  // eslint-disable-next-line default-param-last, @typescript-eslint/no-shadow
41
64
  var apollo = function (apollo, options) {
42
65
  if (apollo === void 0) { apollo = this.currentApolloClient; }
43
66
  var result;
44
67
  var logger;
68
+ var duration;
45
69
  var optionsWithDefaultCache = __assign({ fetchPolicy: 'no-cache' }, options);
46
70
  if (!apollo) {
47
71
  cy.apolloClient().apollo(optionsWithDefaultCache);
48
72
  }
49
73
  else if (isQueryFile(optionsWithDefaultCache)) {
50
- var queryFile = optionsWithDefaultCache.queryFile, apolloOptions_1 = __rest(optionsWithDefaultCache, ["queryFile"]);
51
- cy.fixture(queryFile).then(function (content) {
52
- cy.apollo(__assign({ query: (0, graphql_tag_1.default)(content) }, apolloOptions_1));
74
+ var _a = optionsWithDefaultCache, queryFile_1 = _a.queryFile, sourcePackage_1 = _a.sourcePackage, apolloOptions_1 = __rest(_a, ["queryFile", "sourcePackage"]);
75
+ cy.fixture(queryFile_1).then(function (content) {
76
+ var fileLabel = sourcePackage_1 ? "".concat(queryFile_1, " @ ").concat(sourcePackage_1) : queryFile_1;
77
+ cy.apollo(__assign(__assign({ query: (0, graphql_tag_1.default)(content) }, apolloOptions_1), { _sourceFile: fileLabel }));
53
78
  });
54
79
  }
55
80
  else if (isMutationFile(optionsWithDefaultCache)) {
56
- var mutationFile = optionsWithDefaultCache.mutationFile, apolloOptions_2 = __rest(optionsWithDefaultCache, ["mutationFile"]);
57
- cy.fixture(mutationFile).then(function (content) {
58
- cy.apollo(__assign({ mutation: (0, graphql_tag_1.default)(content) }, apolloOptions_2));
81
+ var _b = optionsWithDefaultCache, mutationFile_1 = _b.mutationFile, sourcePackage_2 = _b.sourcePackage, apolloOptions_2 = __rest(_b, ["mutationFile", "sourcePackage"]);
82
+ cy.fixture(mutationFile_1).then(function (content) {
83
+ var fileLabel = sourcePackage_2 ? "".concat(mutationFile_1, " @ ").concat(sourcePackage_2) : mutationFile_1;
84
+ cy.apollo(__assign(__assign({ mutation: (0, graphql_tag_1.default)(content) }, apolloOptions_2), { _sourceFile: fileLabel }));
59
85
  });
60
86
  }
61
87
  else {
62
- var _a = optionsWithDefaultCache.log, log = _a === void 0 ? true : _a, apolloOptions_3 = __rest(optionsWithDefaultCache, ["log"]);
88
+ var _c = optionsWithDefaultCache.log, log = _c === void 0 ? true : _c, apolloOptions = __rest(optionsWithDefaultCache, ["log"]);
89
+ var doc = isQuery(apolloOptions) ?
90
+ apolloOptions.query :
91
+ apolloOptions.mutation;
92
+ var opType_1 = isQuery(apolloOptions) ? 'Query' : 'Mutation';
93
+ var operationLabel_1 = getOperationLabel(doc, opType_1);
94
+ var queryBody_1 = getQueryBody(doc);
95
+ var variables_1 = apolloOptions.variables;
96
+ var sourceLabel_1 = optionsWithDefaultCache._sourceFile ? " (".concat(optionsWithDefaultCache._sourceFile, ")") : '';
97
+ var variablesLabel_1 = variables_1 && Object.keys(variables_1).length > 0 ?
98
+ " \u2014 ".concat(JSON.stringify(variables_1)) :
99
+ '';
63
100
  if (log) {
64
101
  logger = Cypress.log({
65
102
  autoEnd: false,
66
103
  name: 'apollo',
67
104
  displayName: 'apollo',
68
- message: isQuery(apolloOptions_3) ? "Execute Graphql Query: ".concat(apolloOptions_3.query.loc.source.body) : "Execute Graphql Mutation: ".concat(apolloOptions_3.mutation.loc.source.body),
105
+ message: "".concat(operationLabel_1).concat(sourceLabel_1).concat(variablesLabel_1),
69
106
  consoleProps: function () {
70
- return {
71
- Options: apolloOptions_3,
72
- Yielded: result
73
- };
107
+ var _a;
108
+ var _b, _c, _d;
109
+ var errors = (_c = (_b = result === null || result === void 0 ? void 0 : result.errors) !== null && _b !== void 0 ? _b : result === null || result === void 0 ? void 0 : result.graphQLErrors) !== null && _c !== void 0 ? _c : null;
110
+ var isCaughtError = result instanceof Error;
111
+ var hasErrors = ((errors === null || errors === void 0 ? void 0 : errors.length) > 0) || isCaughtError;
112
+ return _a = {
113
+ Operation: operationLabel_1,
114
+ Variables: variables_1 !== null && variables_1 !== void 0 ? variables_1 : {}
115
+ },
116
+ _a["".concat(opType_1, " Body")] = queryBody_1,
117
+ _a.Duration = duration === undefined ? 'pending' : "".concat(duration, "ms"),
118
+ _a.Status = hasErrors ?
119
+ "error".concat(isCaughtError ? ": ".concat(result.message) : '') :
120
+ 'success',
121
+ _a.Data = (_d = result === null || result === void 0 ? void 0 : result.data) !== null && _d !== void 0 ? _d : null,
122
+ _a.Errors = errors,
123
+ _a.Yielded = result,
124
+ _a;
74
125
  }
75
126
  });
76
127
  }
77
- cy.wrap({}, { log: true })
128
+ var startTime_1 = Date.now();
129
+ cy.wrap({}, { log: false })
78
130
  .then(function () { return (isQuery(optionsWithDefaultCache) ? apollo.query(optionsWithDefaultCache).catch(function (error) {
79
- cy.log("Caught Graphql Query Error: ".concat(JSON.stringify(error)));
131
+ var _a;
132
+ cy.log("Caught GraphQL query error: ".concat((_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : JSON.stringify(error)));
80
133
  return error;
81
134
  }) : apollo.mutate(optionsWithDefaultCache).catch(function (error) {
82
- cy.log("Caught Graphql Mutation Error: ".concat(JSON.stringify(error)));
135
+ var _a;
136
+ cy.log("Caught GraphQL mutation error: ".concat((_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : JSON.stringify(error)));
83
137
  return error;
84
138
  }))
85
139
  .then(function (r) {
140
+ var _a;
86
141
  result = r;
142
+ duration = Date.now() - startTime_1;
143
+ if (logger) {
144
+ var errors = (_a = r === null || r === void 0 ? void 0 : r.errors) !== null && _a !== void 0 ? _a : r === null || r === void 0 ? void 0 : r.graphQLErrors;
145
+ var hasErrors = (r instanceof Error) || ((errors === null || errors === void 0 ? void 0 : errors.length) > 0);
146
+ var prefix = hasErrors ? '❌ ' : '✅ ';
147
+ logger.set('message', "".concat(prefix).concat(operationLabel_1).concat(sourceLabel_1).concat(variablesLabel_1));
148
+ }
87
149
  logger === null || logger === void 0 ? void 0 : logger.end();
88
150
  return r;
89
151
  }); });
@@ -0,0 +1,10 @@
1
+ export declare const BrowserHelper: {
2
+ logCookies: () => Cypress.Chainable<void>;
3
+ logCookie: (cookieName: string) => Cypress.Chainable<void>;
4
+ logSessionStorage: () => Cypress.Chainable<void>;
5
+ logLocalStorage: () => Cypress.Chainable<void>;
6
+ clearSessionCookies: () => Cypress.Chainable<void>;
7
+ clearPersistentCookies: () => Cypress.Chainable<void>;
8
+ simulateClose: () => void;
9
+ resetState: () => void;
10
+ };
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ /*
3
+ * Contains helpers for printing or clearing browser's storage and cookies.
4
+ * These are intended for interactive debugging and log full values by design.
5
+ * Use with caution in automated tests to avoid exposing sensitive data in logs.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.BrowserHelper = void 0;
9
+ /**
10
+ * Prints cookie details in a structured format for debugging.
11
+ *
12
+ * Note: This helper logs the full cookie value by design.
13
+ * @param {Cypress.Cookie} cookie Cookie object returned by Cypress.
14
+ * @returns {void}
15
+ * @private
16
+ */
17
+ var printCookieValues = function (cookie) {
18
+ var cookieType = cookie.expiry ? 'Persistent' : 'Session';
19
+ var expiryDate = cookie.expiry ? new Date(cookie.expiry * 1000).toISOString() : 'Session only';
20
+ var daysUntilExpiry = cookie.expiry ? Math.round(((cookie.expiry * 1000) - Date.now()) / 1000 / 60 / 60 / 24) : null;
21
+ cy.log('-'.repeat(60));
22
+ cy.log("Cookie: ".concat(cookie.name));
23
+ cy.log('-'.repeat(60));
24
+ cy.log("Type: ".concat(cookieType));
25
+ cy.log("Value: ".concat(cookie.value));
26
+ cy.log("Domain: ".concat(cookie.domain));
27
+ cy.log("Path: ".concat(cookie.path));
28
+ cy.log("Secure: ".concat(cookie.secure ? '✔ Yes' : '✘ No'));
29
+ cy.log("HttpOnly: ".concat(cookie.httpOnly ? '✔ Yes' : '✘ No'));
30
+ cy.log("SameSite: ".concat(cookie.sameSite || '(not set)'));
31
+ if (cookie.expiry) {
32
+ cy.log("Expires: ".concat(expiryDate));
33
+ cy.log("Days left: ".concat(daysUntilExpiry, " days"));
34
+ cy.log("Unix time: ".concat(cookie.expiry));
35
+ }
36
+ else {
37
+ cy.log('Expires: When browser closes (session cookie)');
38
+ }
39
+ };
40
+ /**
41
+ * Clears cookies based on their persistence type.
42
+ * @param {'session'|'persistent'} [type='session'] Cookie category to clear.
43
+ * @returns {Cypress.Chainable<void>} Cypress chainable resolved when clearing is complete.
44
+ */
45
+ var clearCookiesByType = function (type) {
46
+ if (type === void 0) { type = 'session'; }
47
+ return cy.getCookies().then(function (cookies) {
48
+ var cookiesToClear = cookies.filter(function (cookie) { return (type === 'session' ? !cookie.expiry : Boolean(cookie.expiry)); });
49
+ cy.step("\uD83D\uDDD1\uFE0F CLEAR ".concat(cookiesToClear.length, " ").concat(type, " cookie(s):"), function () {
50
+ cookiesToClear.forEach(function (cookie) {
51
+ var info = cookie.expiry ? "expires ".concat(new Date(cookie.expiry * 1000).toISOString()) : 'session only';
52
+ cy.log(" ... clearing ".concat(cookie.name, " (").concat(info, ")"));
53
+ cy.clearCookie(cookie.name);
54
+ });
55
+ });
56
+ }).then(function () { return undefined; });
57
+ };
58
+ /**
59
+ * Logs all available cookies with metadata and values.
60
+ *
61
+ * Intended for interactive debugging when full cookie visibility is needed.
62
+ * @returns {Cypress.Chainable<void>} Cypress chainable resolved when logging is complete.
63
+ */
64
+ var logCookies = function () {
65
+ return cy.getCookies().then(function (cookies) {
66
+ if (cookies.length === 0) {
67
+ cy.log('No cookies found');
68
+ return;
69
+ }
70
+ cy.step("COOKIES REPORT - Total: ".concat(cookies.length), function () {
71
+ var sessionCookies = cookies.filter(function (c) { return !c.expiry; });
72
+ var persistentCookies = cookies.filter(function (c) { return Boolean(c.expiry); });
73
+ cy.log("Session Cookies: ".concat(sessionCookies.length));
74
+ cy.log("Persistent Cookies: ".concat(persistentCookies.length));
75
+ cookies.forEach(function (cookie) {
76
+ printCookieValues(cookie);
77
+ });
78
+ });
79
+ }).then(function () { return undefined; });
80
+ };
81
+ /**
82
+ * Logs a specific cookie by name in a detailed format.
83
+ *
84
+ * Intended for interactive debugging when full cookie visibility is needed.
85
+ * @param {string} cookieName Name of the cookie to read and print.
86
+ * @returns {Cypress.Chainable<void>} Cypress chainable resolved when logging is complete.
87
+ */
88
+ var logCookie = function (cookieName) {
89
+ return cy.getCookie(cookieName).then(function (cookie) {
90
+ if (!cookie) {
91
+ cy.log("Cookie \"".concat(cookieName, "\" not found"));
92
+ return;
93
+ }
94
+ printCookieValues(cookie);
95
+ }).then(function () { return undefined; });
96
+ };
97
+ /**
98
+ * Clears Session cookies
99
+ * @returns {Cypress.Chainable<void>} Cypress chainable resolved when logging is complete.
100
+ */
101
+ var clearSessionCookies = function () {
102
+ return clearCookiesByType('session').then(function () { return undefined; });
103
+ };
104
+ /**
105
+ * Clears Persistent cookies
106
+ * @returns {Cypress.Chainable<void>} Cypress chainable resolved when logging is complete.
107
+ */
108
+ var clearPersistentCookies = function () {
109
+ return clearCookiesByType('persistent').then(function () { return undefined; });
110
+ };
111
+ /**
112
+ * Simulates a browser close by clearing session storage and cookies.
113
+ * Persistent cookies are kept intentionally.
114
+ * @returns {void}
115
+ */
116
+ var simulateClose = function () {
117
+ cy.log('Simulating browser close...');
118
+ // Clear session storage
119
+ cy.clearAllSessionStorage();
120
+ // Clear session cookies only
121
+ clearSessionCookies();
122
+ cy.log('Browser close simulated (session storage and cookies are cleared)');
123
+ };
124
+ /**
125
+ * Resets browser state by clearing all storages and all cookies.
126
+ * Use this when a test needs a fully clean client-side state.
127
+ * @returns {void}
128
+ */
129
+ var resetState = function () {
130
+ cy.log('Reset browser state...');
131
+ // Clear all storage
132
+ cy.clearAllLocalStorage();
133
+ cy.clearAllSessionStorage();
134
+ // Clear all cookies
135
+ cy.clearAllCookies();
136
+ cy.log('Browser reset is done (all storages and cookies cleared)');
137
+ };
138
+ /**
139
+ * Logs all sessionStorage entries grouped by origin.
140
+ * Intended for interactive debugging and logs full values.
141
+ * @returns {Cypress.Chainable<void>} Cypress chainable resolved when logging is complete.
142
+ */
143
+ var logSessionStorage = function () {
144
+ return cy.getAllSessionStorage().then(function (session) {
145
+ cy.log("sessionStorage: ".concat(JSON.stringify(session)));
146
+ }).then(function () { return undefined; });
147
+ };
148
+ /**
149
+ * Logs all localStorage entries grouped by origin.
150
+ * Intended for interactive debugging and logs full values.
151
+ * @returns {Cypress.Chainable<void>} Cypress chainable resolved when logging is complete.
152
+ */
153
+ var logLocalStorage = function () {
154
+ return cy.getAllLocalStorage().then(function (local) {
155
+ cy.log("localStorage: ".concat(JSON.stringify(local)));
156
+ }).then(function () { return undefined; });
157
+ };
158
+ exports.BrowserHelper = {
159
+ logCookies: logCookies,
160
+ logCookie: logCookie,
161
+ logSessionStorage: logSessionStorage,
162
+ logLocalStorage: logLocalStorage,
163
+ clearSessionCookies: clearSessionCookies,
164
+ clearPersistentCookies: clearPersistentCookies,
165
+ simulateClose: simulateClose,
166
+ resetState: resetState
167
+ };
@@ -5,3 +5,6 @@ export * from './logout';
5
5
  export * from './repeatUntil';
6
6
  export * from './testStep';
7
7
  export * from './jsErrorsLogger';
8
+ export * from './jfaker';
9
+ export * from './browserHelper';
10
+ export * from './modSince';
@@ -21,3 +21,6 @@ __exportStar(require("./logout"), exports);
21
21
  __exportStar(require("./repeatUntil"), exports);
22
22
  __exportStar(require("./testStep"), exports);
23
23
  __exportStar(require("./jsErrorsLogger"), exports);
24
+ __exportStar(require("./jfaker"), exports);
25
+ __exportStar(require("./browserHelper"), exports);
26
+ __exportStar(require("./modSince"), exports);
@@ -0,0 +1,60 @@
1
+ /**
2
+ * jfaker - A flexible data generator for Cypress tests, supporting both faker.js generated data and custom injection payloads.
3
+ *
4
+ * This module provides a unified interface to generate either faker data or custom injection payloads based on the method called and global settings.
5
+ * It uses a dynamic Proxy to handle method calls and determine whether to generate faker data or injection data.
6
+ *
7
+ * IMPORTANT:
8
+ * When using the generated strings from jfaker in Cypress commands like `.type()`, make sure to:
9
+ * use `parseSpecialCharSequences: false`, e.g.: `<input>.type(text, {parseSpecialCharSequences: false})`
10
+ * to prevent Cypress from interpreting special characters in the generated strings (e.g., {, }, [, ], etc.) as commands,
11
+ * which is especially important for injection payloads that may contain such characters.
12
+ */
13
+ /**
14
+ * Interface to Fake Data generator (using DeepApi proxy to handle dynamic method calls)
15
+ * @param {Record<string, unknown>} options Options for data generation (length for injections, faker options, safe flag), \
16
+ * e.g.: `{length: 100}` for injections to specify desired length of the generated string, \
17
+ * or `{provider: 'example.com'}` for faker to pass options to the faker method. \
18
+ * For faker data generation, an additional option `safe` can be set to `true` \
19
+ * to force faker generation regardless of global type settings \
20
+ * (useful for specific cases where faker data is needed even when global type is set to injection).
21
+ * @remarks
22
+ * Available injection methods:
23
+ * - `.xss()` - Generate XSS injection payloads
24
+ * - `.sql()` - Generate SQL injection payloads
25
+ * - `.bash()` - Generate Bash injection payloads
26
+ * - `.chars()` - Generate random special characters
27
+ * - `.htmlentities()` - Generate HTML entities
28
+ * - `.numbers()` - Generate random numbers entities and edge cases
29
+ * - or any faker.js method can also be called (e.g., `person.firstName()`, `internet.email()`)
30
+ *
31
+ * @returns {string} Generated data string based on the method called and options provided
32
+ *
33
+ * @see https://fakerjs.dev/api/ for available faker methods and options
34
+ * @example
35
+ * ```typescript
36
+ *
37
+ * // Generate faker data with entity.
38
+ * const name = jfaker.person.firstName();
39
+ *
40
+ * // Entity will always be generated using faker (safe: true)
41
+ * const name = jfaker.person.firstName({safe: true});
42
+ *
43
+ * // Generate faker data with options.
44
+ * const email = jfaker.internet.email({provider: 'example.com'});
45
+ *
46
+ * // Generate injection payloads (random between min and max items joined into a single string)..
47
+ * // Entity will always be generated using 'xss'.
48
+ * const xssName = jfaker.xss();
49
+ *
50
+ * // Generate injection payloads with specific length.
51
+ * // Entity will always be generated using 'xss'.
52
+ * const xssName = jfaker.xss({length: 100});
53
+ *
54
+ * // Use all SQL injections.
55
+ * // Entity will always be generated using 'sql'.
56
+ * const allSql = jfaker.sql({length: -1});
57
+ * ```
58
+ */
59
+ declare const jfaker: any;
60
+ export { jfaker };
@@ -0,0 +1,241 @@
1
+ "use strict";
2
+ /* eslint-disable no-else-return */
3
+ /* eslint-disable @typescript-eslint/no-explicit-any */
4
+ /**
5
+ * jfaker - A flexible data generator for Cypress tests, supporting both faker.js generated data and custom injection payloads.
6
+ *
7
+ * This module provides a unified interface to generate either faker data or custom injection payloads based on the method called and global settings.
8
+ * It uses a dynamic Proxy to handle method calls and determine whether to generate faker data or injection data.
9
+ *
10
+ * IMPORTANT:
11
+ * When using the generated strings from jfaker in Cypress commands like `.type()`, make sure to:
12
+ * use `parseSpecialCharSequences: false`, e.g.: `<input>.type(text, {parseSpecialCharSequences: false})`
13
+ * to prevent Cypress from interpreting special characters in the generated strings (e.g., {, }, [, ], etc.) as commands,
14
+ * which is especially important for injection payloads that may contain such characters.
15
+ */
16
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
17
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
18
+ if (ar || !(i in from)) {
19
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
20
+ ar[i] = from[i];
21
+ }
22
+ }
23
+ return to.concat(ar || Array.prototype.slice.call(from));
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.jfaker = void 0;
27
+ var faker_1 = require("@faker-js/faker");
28
+ // Import injection data from corresponding files in injections-ts directory
29
+ var xss_data_1 = require("../injections/xss-data");
30
+ var sql_data_1 = require("../injections/sql-data");
31
+ var bash_data_1 = require("../injections/bash-data");
32
+ var chars_data_1 = require("../injections/chars-data");
33
+ var htmlentities_data_1 = require("../injections/htmlentities-data");
34
+ var numbers_data_1 = require("../injections/numbers-data");
35
+ var injectionData = {
36
+ xss: xss_data_1.xssData,
37
+ sql: sql_data_1.sqlData,
38
+ bash: bash_data_1.bashData,
39
+ chars: chars_data_1.charsData,
40
+ htmlentities: htmlentities_data_1.htmlentitiesData,
41
+ numbers: numbers_data_1.numbersData
42
+ };
43
+ // Environment variable key for storing injection type in Cypress env
44
+ // Can be set either using corresponding FakeData method or as an env var from CI/CD pipeline
45
+ var ENV_INJECTIONS_TYPE = 'JAHIA_CYPRESS_INJECTION_TYPE';
46
+ // Default range for random length of injection payloads; used when length is undefined
47
+ // Random items within the range will be picked and joined into a single string
48
+ var injectionsDefaultLength = { min: 2, max: 5 };
49
+ /**
50
+ * Store FakeData type in Cypress env for persistence across specs
51
+ * @param {string} type FakeData type: 'faker' | 'xss' | 'sql' | 'bash' | 'chars' | 'htmlentities' | 'numbers'
52
+ * @returns void
53
+ */
54
+ function setDataType(type) {
55
+ Cypress.env(ENV_INJECTIONS_TYPE, type);
56
+ }
57
+ /**
58
+ * Retrieve FakeData type from Cypress env (defaults to 'faker' if not set)
59
+ * @returns {string} Type of FakeData to use
60
+ */
61
+ function getDataType() {
62
+ return Cypress.env(ENV_INJECTIONS_TYPE) || 'faker';
63
+ }
64
+ /**
65
+ * Generate injection data based on the specified type and length
66
+ * @param {string} type Injection type to generate (xss, sql, bash, chars, htmlentities, numbers)
67
+ * @param {number} length Length of the generated injection (optional)
68
+ * @returns {string} Generated injection string
69
+ */
70
+ function generateInjection(type, length) {
71
+ var result = [];
72
+ // Type is specified and exists in injectionData, use it to generate data
73
+ var data = injectionData[type];
74
+ if (!data || data.length === 0) {
75
+ throw new Error("[jFaker EXCEPTION] No injection data found for type: ".concat(type, "."));
76
+ }
77
+ if (length === -1) {
78
+ // If length is -1, use all available items from the data array
79
+ result = data;
80
+ }
81
+ else if (length && length > 0) {
82
+ // If length is specified and greater than 0, pick random items until the combined length meets the requirement
83
+ while (result.join('').length < length) {
84
+ var randomIndex = Math.floor(Math.random() * data.length);
85
+ result.push(data[randomIndex]);
86
+ }
87
+ // Trim the combined string to the specified length
88
+ var combined = result.join('');
89
+ result = [combined.substring(0, length)];
90
+ }
91
+ else {
92
+ // If length is not specified, pick a random number of items within the default range
93
+ var itemsCount = Math.floor(Math.random() * injectionsDefaultLength.max) + injectionsDefaultLength.min;
94
+ for (var i = 0; i < itemsCount; i++) {
95
+ var randomIndex = Math.floor(Math.random() * data.length);
96
+ result.push(data[randomIndex]);
97
+ }
98
+ }
99
+ return result.join('');
100
+ }
101
+ /**
102
+ * Generate faker data based on the specified entity and options
103
+ * @param {string} entity Faker entity to generate (e.g., "person.firstName")
104
+ * @param {Record<string, unknown> | string | number | boolean | undefined} options Options to pass to the faker method (optional)
105
+ * @returns {string} Generated faker string
106
+ */
107
+ function generateFake(entity, options) {
108
+ var generator;
109
+ var result = [];
110
+ var parts = entity.split('.');
111
+ var fakerMethod = faker_1.faker;
112
+ for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
113
+ var part = parts_1[_i];
114
+ if (fakerMethod && typeof fakerMethod[part] !== 'undefined') {
115
+ fakerMethod = fakerMethod[part];
116
+ }
117
+ else {
118
+ throw new Error("[jFaker EXCEPTION] Invalid faker method: ".concat(entity));
119
+ }
120
+ }
121
+ if (typeof fakerMethod === 'function') {
122
+ generator = (options && Object.keys(options).length > 0) ? function () { return fakerMethod(options); } : function () { return fakerMethod(); };
123
+ }
124
+ else {
125
+ throw new Error("[jFaker EXCEPTION] ".concat(entity, " is not a function"));
126
+ }
127
+ result.push(generator());
128
+ return result.join('');
129
+ }
130
+ /**
131
+ * Escape string to prevent issues when used in contexts like HTML or JavaScript
132
+ * @param str
133
+ */
134
+ function escape(str) {
135
+ return JSON.stringify(str).slice(1, -1);
136
+ }
137
+ /**
138
+ * Simple DeepApi class to create a dynamic nested Proxy for generating fake data using faker or injection data based on method calls
139
+ */
140
+ var DeepApi = /** @class */ (function () {
141
+ function DeepApi(handler) {
142
+ this.handler = handler;
143
+ return this.createProxy([]);
144
+ }
145
+ DeepApi.prototype.createProxy = function (path) {
146
+ var _this = this;
147
+ return new Proxy(
148
+ // The target is a function, so the proxy itself is callable
149
+ function () { }, {
150
+ // Handle property access - go deeper
151
+ get: function (target, prop) {
152
+ return _this.createProxy(__spreadArray(__spreadArray([], path, true), [String(prop)], false));
153
+ },
154
+ // Handle function call - execute handler
155
+ apply: function (target, thisArg, args) {
156
+ return _this.handler(path.join('.'), args);
157
+ }
158
+ });
159
+ };
160
+ return DeepApi;
161
+ }());
162
+ /**
163
+ * Interface to Fake Data generator (using DeepApi proxy to handle dynamic method calls)
164
+ * @param {Record<string, unknown>} options Options for data generation (length for injections, faker options, safe flag), \
165
+ * e.g.: `{length: 100}` for injections to specify desired length of the generated string, \
166
+ * or `{provider: 'example.com'}` for faker to pass options to the faker method. \
167
+ * For faker data generation, an additional option `safe` can be set to `true` \
168
+ * to force faker generation regardless of global type settings \
169
+ * (useful for specific cases where faker data is needed even when global type is set to injection).
170
+ * @remarks
171
+ * Available injection methods:
172
+ * - `.xss()` - Generate XSS injection payloads
173
+ * - `.sql()` - Generate SQL injection payloads
174
+ * - `.bash()` - Generate Bash injection payloads
175
+ * - `.chars()` - Generate random special characters
176
+ * - `.htmlentities()` - Generate HTML entities
177
+ * - `.numbers()` - Generate random numbers entities and edge cases
178
+ * - or any faker.js method can also be called (e.g., `person.firstName()`, `internet.email()`)
179
+ *
180
+ * @returns {string} Generated data string based on the method called and options provided
181
+ *
182
+ * @see https://fakerjs.dev/api/ for available faker methods and options
183
+ * @example
184
+ * ```typescript
185
+ *
186
+ * // Generate faker data with entity.
187
+ * const name = jfaker.person.firstName();
188
+ *
189
+ * // Entity will always be generated using faker (safe: true)
190
+ * const name = jfaker.person.firstName({safe: true});
191
+ *
192
+ * // Generate faker data with options.
193
+ * const email = jfaker.internet.email({provider: 'example.com'});
194
+ *
195
+ * // Generate injection payloads (random between min and max items joined into a single string)..
196
+ * // Entity will always be generated using 'xss'.
197
+ * const xssName = jfaker.xss();
198
+ *
199
+ * // Generate injection payloads with specific length.
200
+ * // Entity will always be generated using 'xss'.
201
+ * const xssName = jfaker.xss({length: 100});
202
+ *
203
+ * // Use all SQL injections.
204
+ * // Entity will always be generated using 'sql'.
205
+ * const allSql = jfaker.sql({length: -1});
206
+ * ```
207
+ */
208
+ var jfaker = new DeepApi(function (path, args) {
209
+ var _a, _b, _c, _d;
210
+ // Handle non-faker methods first (escape, setFakeDataType, getFakeDataType)
211
+ switch (path) {
212
+ case 'escape':
213
+ return escape(args[0]);
214
+ case 'setDataType':
215
+ return setDataType(args[0]);
216
+ case 'getDataType':
217
+ return getDataType();
218
+ case Object.prototype.hasOwnProperty.call(injectionData, path) ? path : 'default':
219
+ // If the path corresponds to a valid injection type, generate data using that type,
220
+ // or fallback to default case which treats the path as a faker entity.
221
+ // Keep (safe === true) logic in "default" case for a readability.
222
+ return generateInjection(path, (_a = args[0]) === null || _a === void 0 ? void 0 : _a.length);
223
+ default: {
224
+ // For faker data generation, check if the generation should be persistent based on global settings and options passed.
225
+ var safe = ((_b = args[0]) === null || _b === void 0 ? void 0 : _b.safe) === true;
226
+ // Delete the 'safe' property from options to avoid passing it to faker methods
227
+ (_c = args[0]) === null || _c === void 0 ? true : delete _c.safe;
228
+ // Here path represents a faker entity.
229
+ // Check desired data type and 'safe' option to determine whether to generate faker data or injection data.
230
+ // If global type is set to injection (to override faker data) but safe is explicitly set to true in options -
231
+ // generate faker data for this specific call regardless of global settings.
232
+ if (getDataType() === 'faker' || safe) {
233
+ return generateFake(path, args[0]);
234
+ }
235
+ else {
236
+ return generateInjection(getDataType(), (_d = args[0]) === null || _d === void 0 ? void 0 : _d.length);
237
+ }
238
+ }
239
+ }
240
+ });
241
+ exports.jfaker = jfaker;