-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-function.js
72 lines (69 loc) · 2.16 KB
/
test-function.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
const fli = require('fli-webtask');
const wt = require('webtask-tools');
const bodyParser = require('body-parser');
const express = fli.npm.express;
const request = fli.npm.request;
const as = fli.npm.async;
const _ = fli.npm.lodash;
const loader = fli.lib.loader;
const responseHandler = fli.lib.responseHandler;
const app = express();
const router = express.Router();
const validateMiddleware = (req, res, next) => {
if(req.webtaskContext.secrets.token !== req.query.token) {
const errMsgToken = 'No token.';
responseHandler(errMsgToken, res);
return next(errMsgToken);
}
return next();
};
router
.all('/flow', function (req, res) {
console.log(`-- test flow`);
as.waterfall([
// 1 - create item in test db
// 2 - trigger new state
// 3 - call scheduler
// 4 - check that item has status unqueued
(next) => loader({
method: 'put',
url: `${req.webtaskContext.secrets.storeFunction}/${req.webtaskContext.secrets.id}`,
qs: {token: req.webtaskContext.secrets.token},
json: {
state: 'test'
}
}, () => next()),
(next) => loader({
method: 'get',
url: req.webtaskContext.secrets.scheduledFunction,
qs: {token: req.webtaskContext.secrets.token, reset: true}
}, () => next()),
(next) => loader({
method: 'put',
url: `${req.webtaskContext.secrets.storeFunction}/${req.webtaskContext.secrets.id}`,
qs: {token: req.webtaskContext.secrets.token},
json: {
state: 'new'
}
},() => next()),
(next) => setTimeout(()=>next(), 1000),
(next) => loader({
method: 'get',
url: req.webtaskContext.secrets.scheduledFunction,
qs: {token: req.webtaskContext.secrets.token}
}, () => next()),
(next) => setTimeout(()=>next(), 3000),
(next) => loader({
method: 'get',
url: `${req.webtaskContext.secrets.storeFunction}/${req.webtaskContext.secrets.id}`,
qs: {token: req.webtaskContext.secrets.token}
}, next)
],
(err, response) => {
responseHandler(err, res, _.get(response, 'state'));
});
});
app
.use(bodyParser.json())
.use('/', validateMiddleware, router);
module.exports = wt.fromExpress(app);