/
tagValidation.ts
212 lines (180 loc) · 5.19 KB
/
tagValidation.ts
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Suite } from "..";
import { TagValidationReport } from "../reports/tagValidationReport";
import Resource, { ResourceOptions } from "./resource";
import Step from "./step";
import Test from "./test";
export enum Operator {
EQUALS = "equals",
IS_ONE_OF = "is_one_of",
STARTS_WITH = "starts_with",
ENDS_WITH = "ends_with",
CONTAINS = "contains",
REGEXP_MATCH = "regexp_match",
}
export interface QueryValidation {
key: string,
value: string,
operator: Operator,
json_path?: string,
use_json_path: boolean,
decode_result_as?: string,
}
export interface TagValidationOptions extends ResourceOptions {
enabled?: boolean,
detect_duplicates?: boolean,
validate_absence?: boolean,
hostname_validation?: string,
pathname_validation?: string,
hostname_detection?: string,
pathname_detection?: string,
query_detection?: string,
account_id?: string,
do_validation?: boolean,
interception?: {
intercept?: boolean,
intercept_status?: number,
intercept_headers?: string,
intercept_body?: string,
},
}
export interface TagDefinition {
key: string,
}
export enum TagValidationContexts {
TEST = "test",
STEP = "step",
}
export default class TagValidation extends Resource {
public static readonly resourceType: string = "tag_validation";
public static readonly childTypes: readonly string[] = [];
private queryValidations: QueryValidation[] = [];
public tagDefinition: TagDefinition;
public options: TagValidationOptions = {};
public constructor(
name: string,
key: string,
protected contextType: TagValidationContexts = TagValidationContexts.STEP,
protected contextID?: number,
options: TagValidationOptions = {}
) {
super(name);
this.tagDefinition = {
key: key,
};
this.setOptions(options);
}
public static async fromID(id: number): Promise<TagValidation> {
const resource = await super.getResource(id, TagValidation.resourceType);
return TagValidation.fromJSON(JSON.parse(resource) as Record<string, any>);
}
public static fromJSON(
obj: Record<string, any>,
copy: boolean = false
): TagValidation {
const {
name,
id,
test_id: testId,
step_id: stepId,
tag_definition: tagDefinition,
query_validation: queryValidation,
...options
} = obj;
let contextID: number | undefined = undefined;
let contextType: TagValidationContexts | undefined = undefined;
if (testId !== undefined) {
contextID = testId;
contextType = TagValidationContexts.TEST;
} else if (stepId !== undefined) {
contextID = stepId;
contextType = TagValidationContexts.STEP;
}
const tagValidation = new TagValidation(
name,
tagDefinition.key,
contextType,
contextID
);
if (!copy) {
tagValidation.setResourceID(id);
}
tagValidation.setOptions(options, true);
if (queryValidation !== undefined) {
queryValidation.forEach((queryValidationObj: QueryValidation) => {
tagValidation.insertQueryValidation(queryValidationObj);
});
}
return tagValidation;
}
public insertQueryValidation(
queryValidation: QueryValidation,
index: number = this.queryValidations.length
): void {
super.insertChild(queryValidation, index, "queryValidations");
}
public deleteQueryValidation(index: number): void {
this.queryValidations.splice(index, 1);
}
public getQueryValidations(): readonly QueryValidation[] {
return this.queryValidations.slice();
}
public setOptions(
options: TagValidationOptions,
override: boolean = false
): void {
if (override) {
this.options = options;
} else {
this.options = {
...this.options,
...options,
interception: {
...this.options.interception,
...options.interception,
},
};
}
super.setOptions(options, override);
}
public setContextType(contextType: TagValidationContexts): void {
this.contextType = contextType;
}
public toJSON(): Promise<Record<string, any>> {
const obj: Record<string, any> = {
id: this.resourceID,
name: this.name,
tag_definition: this.tagDefinition,
query_validation: this.queryValidations,
...this.options,
interception: {
...this.options.interception,
},
};
return Promise.resolve(obj);
}
public async report(): Promise<TagValidationReport> {
const id = this.getResourceID();
const contextID = this.getContextID();
if (id === undefined) {
throw new Error("Resource ID must be set");
}
if (contextID === undefined) {
throw new Error("Context ID must be set");
}
let accountID: number;
try {
let test: Test;
if (this.getContextType() === TagValidationContexts.STEP) {
const step = await Step.fromID(contextID);
test = await Test.fromID(step.getContextID()!);
} else {
test = await Test.fromID(contextID);
}
accountID = (await Suite.fromID(test.getContextID()!)).getContextID()!;
} catch (e) {
throw new Error("Failed to determine account ID");
}
return new TagValidationReport(accountID)
.where("tag_validation_id", "==", id);
}
}