This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
246 lines (221 loc) · 6.47 KB
/
index.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
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
require('dotenv').config()
const util = require('util')
const rp = require('request-promise-native')
const uuidv1 = require('uuid/v1')
const program = require('commander')
const xform = require('./artefacts/xform.js')
const rawSubmission = require('./artefacts/submission.js')
// program
// .option('-c, --submission-count', 'Number of submissions to make', parseInt, 5)
// .option('-i, --submission-interval', 'Interval between submissions (in ms)', parseInt, 500)
// .parse(process.argv)
// console.log(`Count: ${program.submissionCount}`)
// console.log(`Interval: ${program.submissionInterval}`)
// console.log(program.args)
program
.option('-c, --submission-count [number]', 'Number of submissions to make', 1)
.option('-i, --submission-interval [number]', 'Interval between submissions in ms', 500)
.parse(process.argv);
const urls = {
odk: process.env.ODK_URL,
kernel: process.env.KERNEL_URL
}
const tokens = {
odk: process.env.ODK_ADMIN_TOKEN,
kernel: process.env.KERNEL_ADMIN_TOKEN
}
let surveyorId, projectId
const setTimeoutPromise = util.promisify(setTimeout)
const log = (logText, logLevel = 0) => {
if (logLevel > 0) {
const d = new Date()
console.log(d.toLocaleTimeString('de-DE') + ' ' + logText)
}
}
const generateOptions = (aetherModule, entity, payload, method, id, pathExtension) => {
const plural = entity == 'entity' ? 'entities' : `${entity}s`
let options = {
uri: `${urls[aetherModule]}/${plural}/` +
(id ? id + '/' : '') +
(pathExtension ? pathExtension + '/' : ''),
method: method || 'POST',
headers: {
'Authorization': 'Token ' + tokens[aetherModule]
},
json: true
}
if (payload) {
options.body = payload
}
return options
}
const createSurveyor = () => {
const options = generateOptions('odk', 'surveyor', {
'username': 'deployment-test',
'password': 'deployment-test'
})
return rp(options)
.then(response => {
surveyorId = response.id
})
}
const deleteSurveyor = () => {
const options = generateOptions('odk', 'surveyor', {}, 'DELETE', surveyorId)
return rp(options)
}
const createProject = () => {
log('Creating project')
const options = generateOptions('odk', 'project', {
'name': 'deployment-test',
'surveyors': [ surveyorId ]
})
return rp(options)
.then(response => {
projectId = response.project_id
return projectId
})
.catch(err => {
throw new Error('Failed to create project: ' + err)
})
}
const deleteProject = () => {
const options = generateOptions('odk', 'project', {}, 'DELETE', projectId)
return rp(options)
}
const deleteKernelProject = () => {
const options = generateOptions('kernel', 'project', {}, 'DELETE', projectId)
return rp(options)
}
const createXForm = (projectId) => {
log(`Creating XForm (project: ${projectId})`)
const options = generateOptions('odk', 'xform', {
'title': 'deployment-test',
'xml_data': xform,
'project': projectId
})
return rp(options)
}
const propagate = () => {
log(`Propagating (project: ${projectId})`)
const options = generateOptions('odk', 'project', {}, 'PATCH', projectId, 'propagate')
return rp(options)
}
const submitData = () => {
log('Submitting data')
const uuid = uuidv1()
const base64EncodedData = new Buffer('deployment-test:deployment-test').toString('base64')
const options = {
uri: urls.odk + '/submission',
method: 'POST',
headers: {
'Authorization': 'Basic ' + base64EncodedData
},
formData: {
'xml_submission_file': {
value: rawSubmission.replace('#!uuid', uuid),
options: {
filename: 'xml_submission_file'
}
},
'attachment_test': {
value: rawSubmission,
options: {
filename: 'attachment_test'
}
}
}
}
return rp(options)
.then(() => uuid)
}
const checkSubmission = (uuid) => {
log(`Checking submission ${uuid}`)
const options = generateOptions('kernel', 'submission', false, 'GET')
options.uri += '?payload__meta__instanceID=uuid:' + uuid
+ '&project=' + projectId
return rp(options)
.then(response => {
if (response.count !== 1) {
throw new Error('Submission not found (Project: ' + projectId + ')')
}
return response.results[0]
})
}
const checkAttachment = (submission) => {
log(`Checking attachment ${submission.id}`)
let attachmentUrl = submission.attachments
.find(a => a.name === 'attachment_test')
.url
const isBucket = attachmentUrl.indexOf('/media/') === -1
if (!isBucket) {
attachmentUrl = attachmentUrl.replace('/media/', '/media-basic/')
}
const options = {
uri: attachmentUrl
}
if (!isBucket) {
const base64EncodedData = new Buffer('admin:' + process.env.KERNEL_ADMIN_PASSWORD).toString('base64')
options.headers = {
'Authorization': 'Basic ' + base64EncodedData
}
}
return rp(options)
.then(attachment => {
if (attachment === rawSubmission) {
return submission
} else {
throw new Error(attachment)
}
})
}
const checkEntity = (submission) => {
log(`Checking entity ${submission.id}`)
let options = generateOptions('kernel', 'entity', false, 'GET')
options.uri += '?submission=' + submission.id
return rp(options)
.then(response => {
if (response.count !== 1) {
throw new Error('Entity not found (Submission: ' + submission.id + ')')
}
return true
})
}
const submitAndCheck = () => {
return submitData()
.then(checkSubmission)
.then(checkAttachment)
.then(checkEntity)
.then(() => {
log('Round trip completed 🚀', 1)
return true
})
}
const submissions = () => {
log('program.submissionCount: ' + program.submissionCount)
const totalSubmissions = [...Array(Number(program.submissionCount)).keys()].map(key => {
return setTimeoutPromise(key * Number(program.submissionInterval))
.then(submitAndCheck)
})
log(totalSubmissions.length + ' submissions')
return Promise.all(totalSubmissions)
}
const tearDown = () => {
log('Tear down')
return deleteSurveyor()
.then(deleteProject)
.then(deleteKernelProject)
.catch(err => {
console.error('Failed to tear down: ' + err)
console.log('To clean up manually, delete the Kernel project, the ODK project and the ODK surveyor, all called "deployment-test"')
})
}
createSurveyor()
.then(createProject)
.then(createXForm)
.then(propagate)
.then(submissions)
.then(tearDown)
.catch(err => {
console.error(err)
tearDown()
})