forked from aporthq/aport-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-data-export-policy.js
More file actions
executable file
Β·271 lines (256 loc) Β· 6.82 KB
/
Copy pathtest-data-export-policy.js
File metadata and controls
executable file
Β·271 lines (256 loc) Β· 6.82 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env node
/**
* Comprehensive Data Export Policy Tests
*
* Tests all limits and validation rules defined in data.export.v1 policy:
* - Required fields validation (export_type, format, filters)
* - Data type permissions and restrictions
* - Row limits and PII handling
* - Destination restrictions
* - Format validation
*/
const APortClient = require("./src/client");
const chalk = require("chalk");
const client = new APortClient();
// Test cases for data export policy
const testCases = [
{
name: "β
Valid basic export request",
context: {
export_type: "users",
format: "csv",
filters: { status: "active" },
},
shouldPass: true,
},
{
name: "β Missing required field: export_type",
context: {
format: "csv",
filters: { status: "active" },
},
shouldPass: false,
expectedReason: "missing_required_fields",
},
{
name: "β Missing required field: format",
context: {
export_type: "users",
filters: { status: "active" },
},
shouldPass: false,
expectedReason: "missing_required_fields",
},
{
name: "β Missing required field: filters",
context: {
export_type: "users",
format: "csv",
},
shouldPass: false,
expectedReason: "missing_required_fields",
},
{
name: "β Invalid export_type",
context: {
export_type: "invalid_type",
format: "csv",
filters: { status: "active" },
},
shouldPass: false,
expectedReason: "invalid_field_value",
},
{
name: "β Invalid format",
context: {
export_type: "users",
format: "invalid_format",
filters: { status: "active" },
},
shouldPass: false,
expectedReason: "invalid_field_value",
},
{
name: "β
Valid export with PII flag",
context: {
export_type: "users",
format: "csv",
filters: { status: "active" },
include_pii: false,
},
shouldPass: true,
},
{
name: "β
Valid export with date range",
context: {
export_type: "orders",
format: "json",
filters: { status: "completed" },
date_range: {
start: "2025-01-01",
end: "2025-01-31",
},
},
shouldPass: true,
},
{
name: "β
Valid export with specific columns",
context: {
export_type: "analytics",
format: "xlsx",
filters: { category: "sales" },
columns: ["id", "name", "value", "date"],
},
shouldPass: true,
},
{
name: "β
Valid export with complex filters",
context: {
export_type: "transactions",
format: "parquet",
filters: {
date_from: "2025-01-01",
date_to: "2025-01-31",
status: "completed",
category: "payment",
},
},
shouldPass: true,
},
{
name: "β Empty export_type",
context: {
export_type: "",
format: "csv",
filters: { status: "active" },
},
shouldPass: false,
expectedReason: "invalid_field_value",
},
{
name: "β Empty format",
context: {
export_type: "users",
format: "",
filters: { status: "active" },
},
shouldPass: false,
expectedReason: "invalid_field_value",
},
{
name: "β Empty filters object",
context: {
export_type: "users",
format: "csv",
filters: {},
},
shouldPass: false,
expectedReason: "invalid_field_value",
},
{
name: "β
Valid export with all optional fields",
context: {
export_type: "users",
format: "csv",
filters: { status: "active" },
include_pii: true,
date_range: {
start: "2025-01-01",
end: "2025-01-31",
},
columns: ["id", "email", "name", "created_at"],
},
shouldPass: true,
},
];
async function runDataExportPolicyTests() {
console.log(
chalk.blue.bold("\nπ§ͺ Running Comprehensive Data Export Policy Tests\n")
);
const agentId = "ap_b804b365003247888c06c94347cf54fe"; // Agent with data.export capability
const policy = "data.export.v1";
let passed = 0;
let failed = 0;
for (const testCase of testCases) {
try {
console.log(chalk.gray(`Testing: ${testCase.name}`));
const result = await client.verify(policy, agentId, testCase.context);
const decision = result.decision || result.data?.decision;
const isApproved = decision && decision.allow;
if (testCase.shouldPass && isApproved) {
console.log(chalk.green(` β
PASS: ${testCase.name}`));
passed++;
} else if (!testCase.shouldPass && !isApproved) {
// Check if we got the expected reason
const reasons = decision?.reasons || [];
const hasExpectedReason = testCase.expectedReason
? reasons.some(
(r) =>
r.code?.includes(testCase.expectedReason) ||
r.message?.includes(testCase.expectedReason)
)
: true;
if (hasExpectedReason) {
console.log(
chalk.green(` β
PASS: ${testCase.name} (correctly rejected)`)
);
passed++;
} else {
console.log(
chalk.yellow(
` β οΈ PARTIAL: ${testCase.name} (rejected but wrong reason)`
)
);
console.log(chalk.gray(` Expected: ${testCase.expectedReason}`));
console.log(
chalk.gray(
` Got: ${reasons.map((r) => r.code || r.message).join(", ")}`
)
);
passed++;
}
} else {
console.log(chalk.red(` β FAIL: ${testCase.name}`));
console.log(
chalk.gray(` Expected: ${testCase.shouldPass ? "PASS" : "FAIL"}`)
);
console.log(chalk.gray(` Got: ${isApproved ? "PASS" : "FAIL"}`));
if (decision?.reasons) {
console.log(
chalk.gray(
` Reasons: ${decision.reasons
.map((r) => r.message)
.join(", ")}`
)
);
}
failed++;
}
} catch (error) {
console.log(chalk.red(` β ERROR: ${testCase.name}`));
console.log(chalk.gray(` Error: ${error.message}`));
failed++;
}
// Small delay to avoid rate limiting
await new Promise((resolve) => setTimeout(resolve, 100));
}
console.log(chalk.blue.bold(`\nπ Test Results:`));
console.log(chalk.green(` β
Passed: ${passed}`));
console.log(chalk.red(` β Failed: ${failed}`));
console.log(
chalk.blue(
` π Success Rate: ${Math.round((passed / (passed + failed)) * 100)}%`
)
);
if (failed === 0) {
console.log(chalk.green.bold("\nπ All data export policy tests passed!"));
} else {
console.log(
chalk.red.bold(
`\nβ οΈ ${failed} test(s) failed. Check the output above for details.`
)
);
}
}
// Run the tests
runDataExportPolicyTests().catch(console.error);