-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtest-activate-methods.js
More file actions
186 lines (160 loc) · 5.4 KB
/
test-activate-methods.js
File metadata and controls
186 lines (160 loc) · 5.4 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
#!/usr/bin/env node
/**
* Test script to determine which activation method works with n8n API
* Tests both PATCH and PUT methods for workflow activation
*/
const axios = require('axios');
const fs = require('fs');
const path = require('path');
// Load configuration
const configPath = path.join(__dirname, '.config.json');
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
const env = config.environments[config.defaultEnv];
const API_BASE = env.n8n_host;
const API_KEY = env.n8n_api_key;
console.log('🧪 Testing n8n Workflow Activation Methods');
console.log(`📡 Server: ${API_BASE}`);
console.log('');
// Create axios instance
const api = axios.create({
baseURL: `${API_BASE}/api/v1`,
headers: {
'X-N8N-API-KEY': API_KEY,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
async function createTestWorkflow() {
console.log('1️⃣ Creating test workflow...');
const workflow = {
name: `Test Activation Methods ${Date.now()}`,
nodes: [
{
name: 'Schedule Trigger',
type: 'n8n-nodes-base.scheduleTrigger',
position: [250, 300],
parameters: {
rule: {
interval: [{ field: 'hours', hoursInterval: 1 }]
}
},
typeVersion: 1.1
}
],
connections: {},
settings: {
executionOrder: 'v1'
}
};
try {
const response = await api.post('/workflows', workflow);
console.log(`✅ Workflow created: ID=${response.data.id}, Name="${response.data.name}"`);
console.log(` Active status: ${response.data.active}`);
console.log('');
return response.data.id;
} catch (error) {
console.error('❌ Failed to create workflow:', error.response?.data || error.message);
process.exit(1);
}
}
async function testMethodPUT(workflowId) {
console.log('2️⃣ Testing Method 1: PUT /workflows/{id}/activate');
try {
const response = await api.put(`/workflows/${workflowId}/activate`, {});
console.log('✅ PUT /activate method WORKS!');
console.log(` Response: active=${response.data.active}`);
console.log('');
return true;
} catch (error) {
console.log('❌ PUT /activate method FAILED');
console.log(` Status: ${error.response?.status || 'N/A'}`);
console.log(` Error: ${error.response?.data?.message || error.message}`);
console.log('');
return false;
}
}
async function testMethodPATCH(workflowId) {
console.log('3️⃣ Testing Method 2: PATCH /workflows/{id} with {active: true}');
try {
const response = await api.patch(`/workflows/${workflowId}`, {
active: true,
settings: {},
staticData: null,
tags: []
});
console.log('✅ PATCH method WORKS!');
console.log(` Response: active=${response.data.active}`);
console.log('');
return true;
} catch (error) {
console.log('❌ PATCH method FAILED');
console.log(` Status: ${error.response?.status || 'N/A'}`);
console.log(` Error: ${error.response?.data?.message || error.message}`);
console.log('');
return false;
}
}
async function deactivateWorkflow(workflowId) {
console.log('4️⃣ Deactivating workflow for next test...');
try {
// Try PUT method first
const response = await api.put(`/workflows/${workflowId}/deactivate`, {});
console.log('✅ Deactivated using PUT /deactivate');
console.log('');
} catch (error) {
// Fallback to PATCH
try {
await api.patch(`/workflows/${workflowId}`, { active: false });
console.log('✅ Deactivated using PATCH with {active: false}');
console.log('');
} catch (err) {
console.log('⚠️ Could not deactivate workflow');
console.log('');
}
}
}
async function cleanupWorkflow(workflowId) {
console.log('🧹 Cleaning up test workflow...');
try {
await api.delete(`/workflows/${workflowId}`);
console.log('✅ Test workflow deleted');
} catch (error) {
console.log('⚠️ Could not delete workflow:', error.message);
}
}
async function main() {
let workflowId = null;
try {
// Create test workflow
workflowId = await createTestWorkflow();
// Test PUT method
const putWorks = await testMethodPUT(workflowId);
if (putWorks) {
// Deactivate before testing PATCH
await deactivateWorkflow(workflowId);
}
// Test PATCH method
const patchWorks = await testMethodPATCH(workflowId);
// Summary
console.log('📊 Test Results Summary:');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log(`PUT /workflows/{id}/activate: ${putWorks ? '✅ WORKS' : '❌ FAILS'}`);
console.log(`PATCH /workflows/{id} {active}: ${patchWorks ? '✅ WORKS' : '❌ FAILS'}`);
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('');
if (putWorks) {
console.log('💡 Recommendation: Use PUT /workflows/{id}/activate (dedicated endpoint)');
} else if (patchWorks) {
console.log('💡 Recommendation: Use PATCH /workflows/{id} with {active: true}');
} else {
console.log('⚠️ Neither method works! API may not support activation.');
}
} catch (error) {
console.error('💥 Test failed:', error.message);
} finally {
if (workflowId) {
await cleanupWorkflow(workflowId);
}
}
}
main();