-
Notifications
You must be signed in to change notification settings - Fork 0
/
dredd_hook.js
44 lines (36 loc) · 1.25 KB
/
dredd_hook.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
var hooks = require('hooks')
hooks.beforeAll(function(transactions) { })
session = {
token: null,
email: null,
todoName: null
}
hooks.beforeAll(function(transaction) {
var prefix = Math.round(Math.random() * 10000)
session.email = `user${prefix + ''}@example.com`
session.todoName = `todo ${prefix + ''}`
console.log("Email:", session.email)
})
hooks.before('/v1/signup > POST', function(transaction) {
var requestBody = JSON.parse(transaction.request.body);
requestBody['email'] = session.email
transaction.request.body = JSON.stringify(requestBody)
})
hooks.before('/v1/login > POST', function(transaction) {
var requestBody = JSON.parse(transaction.request.body);
requestBody['email'] = session.email
transaction.request.body = JSON.stringify(requestBody)
})
hooks.after('/v1/login > POST', function(transaction) {
session.token = JSON.parse(transaction.real.body).session_id
})
hooks.beforeEach(function(transaction) {
if (session.token) {
transaction.request.headers['Authorization'] = `Bearer ${session.token}`
}
})
hooks.before('/v1/todo > POST', function(transaction) {
var requestBody = JSON.parse(transaction.request.body);
requestBody['name'] = session.todoName
transaction.request.body = JSON.stringify(requestBody)
})