-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
187 lines (169 loc) · 6.64 KB
/
helper.js
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
/**
* Checks wether k6 response have the data that query asks
* @param {import("k6/http").RefinedResponse<ResponseType | undefined>} v
* @param {string} query
*/
export function isExists(v, query) {
const splittedQuery = query.split(".")
const json = v.json()
let val;
if (json) {
val = json
splittedQuery.forEach(query => {
const v = val[query]
if (!v) {
return false
}
if (typeof v === "boolean") {
val = v.toString()
} else {
val = v
}
});
return val
}
return false
}
/**
* Checks wether k6 response have the data that query asks and match it
* @param {import("k6/http").RefinedResponse<ResponseType | undefined >} v
* @param {string} query
* @param {any} expected
* @returns
*/
export function isEqual(v, query, expected) {
const i = isExists(v, query)
let e = expected
if (typeof expected === "boolean") {
e = e.toString()
}
return i && i === e
}
export function generateRandomPassword() {
const length = Math.floor(Math.random() * 11) + 5; // Random length between 5 and 15
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let password = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
password += characters.charAt(randomIndex);
}
return password;
}
export function generateUniqueUsername() {
// Define parts of names for generating random names
const prefixes = ['An', 'Ben', 'Jon', 'Xen', 'Lor', 'Sam', 'Max', 'Jen', 'Leo', 'Kay', 'Alex', 'Eva', 'Zoe'];
const middles = ['dra', 'vi', 'na', 'lo', 'ki', 'sa', 'ra', 'li', 'mo', 'ne', 'ja', 'mi', 'ko'];
const suffixes = ['son', 'ton', 'ly', 'en', 'er', 'an', 'ry', 'ley', 'leigh', 'sie', 'den', 'leya', 'vin', 'lyn', 'ley', 'don'];
let username = '';
while (username.length < 5 || username.length > 15) {
const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
const middle = middles[Math.floor(Math.random() * middles.length)];
const suffix = suffixes[Math.floor(Math.random() * suffixes.length)];
username = prefix + middle + suffix + Math.floor(Math.random() * 10000);
}
return username;
}
export function generateUniqueName() {
// Define parts of names for generating random names
const prefixes = ['An', 'Ben', 'Jon', 'Xen', 'Lor', 'Sam', 'Max', 'Jen', 'Leo', 'Kay'];
const middles = ['dra', 'vi', 'na', 'lo', 'ki', 'sa', 'ra', 'li', 'mo', 'ne'];
const suffixes = ['son', 'ton', 'ly', 'en', 'er', 'an', 'ry', 'ley', 'leigh', 'sie'];
const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
const middle = middles[Math.floor(Math.random() * middles.length)];
const suffix = suffixes[Math.floor(Math.random() * suffixes.length)];
const name = prefix + " " + middle + " " + suffix;
return name;
}
export function generateTestObjects(schema, validTemplate) {
const violations = [];
function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
function addViolation(path, violation) {
const testCase = clone(validTemplate);
let parts = path.split('.');
let subObject = testCase;
for (let i = 0; i < parts.length - 1; i++) {
if (parts[i].endsWith(']')) {
let index = parts[i].match(/\[(\d+)\]/)[1];
parts[i] = parts[i].replace(/\[\d+\]/, '');
subObject = subObject[parts[i]][index];
} else {
subObject = subObject[parts[i]];
}
}
let lastPart = parts[parts.length - 1];
if (lastPart.endsWith(']')) {
let index = lastPart.match(/\[(\d+)\]/)[1];
lastPart = lastPart.replace(/\[\d+\]/, '');
subObject[lastPart][index] = violation;
} else {
subObject[lastPart] = violation;
}
violations.push(testCase);
}
function generateDataTypeViolations(propPath, type) {
const dataTypes = {
'string': ["", 123, true, {}, []],
'number': ["notANumber", true, {}, []],
'boolean': ["notABoolean", 123, {}, []],
'object': ["notAnObject", 123, true, []], // Assuming a non-empty object is valid
'array': ["notAnArray", 123, true, {}]
};
dataTypes[type].forEach(violation => {
addViolation(propPath, violation);
});
}
function generateViolationsForProp(propPath, propRules, parentValue) {
if (propRules.notNull) {
addViolation(propPath, null);
}
if (propRules.isUrl) {
addViolation(propPath, "notAUrl");
addViolation(propPath, "http://incomplete");
}
if (propRules.type) {
generateDataTypeViolations(propPath, propRules.type);
}
switch (propRules.type) {
case 'string':
if (propRules.minLength !== undefined) {
addViolation(propPath, 'a'.repeat(propRules.minLength - 1));
}
if (propRules.maxLength !== undefined) {
addViolation(propPath, 'a'.repeat(propRules.maxLength + 1));
}
if (propRules.enum !== undefined) {
addViolation(propPath, 'notAnEnumValue');
}
break;
case 'number':
if (propRules.min !== undefined) {
addViolation(propPath, propRules.min - 1);
}
if (propRules.max !== undefined) {
addViolation(propPath, propRules.max + 1);
}
break;
case 'array':
if (propRules.items && propRules.items.notNull) {
addViolation(`${propPath}[0]`, null); // Violates notNull for array items
}
if (propRules.items && propRules.items.type === 'string') {
// Already handled by generateDataTypeViolations
}
break;
case 'object':
if (propRules.properties) {
Object.entries(propRules.properties).forEach(([nestedProp, nestedRules]) => {
generateViolationsForProp(`${propPath}.${nestedProp}`, nestedRules, parentValue[nestedProp]);
});
}
break;
}
}
Object.entries(schema).forEach(([prop, propRules]) => {
generateViolationsForProp(prop, propRules, validTemplate[prop]);
});
return violations;
}