-
Notifications
You must be signed in to change notification settings - Fork 122
/
hooks.js
204 lines (173 loc) · 5.4 KB
/
hooks.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
var
async = require('async'),
q = require('q'),
util = require('util');
var myHooks = function () {
this.BeforeFeature((event, callback) => {
var
api = restApi(),
fixtures = require('../fixtures/functionalTestsFixtures.json'),
promises = [];
Object.keys(fixtures).forEach(index => {
promises.push((function () {
var deferred = q.defer();
api.deleteIndex(index)
.then(response => {
deferred.resolve(response);
})
.catch(error => {
// ignoring errors
// console.log('Error deleting index ' + index + '. Ignoring...');
deferred.resolve({});
});
return deferred.promise;
}));
Object.keys(fixtures[index]).forEach(collection => {
promises.push(() => {
return api.bulkImport(fixtures[index][collection], index, collection);
});
});
promises.push(() => {
return api.refreshIndex(index);
});
});
promises.reduce(q.when, q())
.then(() => {
callback();
});
});
this.AfterFeature((event, callback) => {
var
api = restApi(),
promises = [];
// give a little time to run the After hook before proceeding
setTimeout(() => {
[api.world.fakeIndex, api.world.fakeAltIndex, api.world.fakeNewIndex].forEach(index => {
promises.push(api.deleteIndex(index));
});
q.all(promises)
.then(() => {
callback();
})
.catch(error => { callback(new Error(error)); });
}, 0);
});
/**
* API LOADING AND RELEASING
* Until cucumber.js supports BeforeAll and AfterAll tags, we have to open/close connections
* on each test case.
*
* We could also load all the tested API at the beginning of each test case, using reentrant init() functions,
* and close them all at the very end using the AfterFeatures event.
* This method involves a cucumber.js hack, where we save a 'world' reference at the end of each test case so that
* we can use it when the AfterFeatures event is emitted.
*
* Problem is, there is no guarantee that the world we saved still exists when this event is sent. In fact, the
* Cucumber.js documentation states that it should be destroyed at this point of time.
*
* And we don't want to deal with destroyed worlds, this is all too messy. And dangerous.
*/
this.Before('@usingREST', function (scenario, callback) {
this.api = setAPI(this, 'REST');
callback();
});
this.Before('@usingWebsocket', function (scenario, callback) {
this.api = setAPI(this, 'Websocket');
callback();
});
this.Before('@usingMQTT', function (scenario, callback) {
this.api = setAPI(this, 'MQTT');
callback();
});
this.Before('@usingAMQP', function (scenario, callback) {
this.api = setAPI(this, 'AMQP');
callback();
});
this.Before('@usingSTOMP', function (scenario, callback) {
this.api = setAPI(this, 'STOMP');
callback();
});
this.After(function (scenario, callback) {
this.api.truncateCollection()
.then(() => {
this.api.refreshIndex(this.fakeIndex);
this.api.disconnect();
callback();
})
.catch(e => { callback(); });
});
this.After('@unsubscribe', function (scenario, callback) {
async.each(Object.keys(this.api.subscribedRooms), (socketName, callbackSocketName) => {
async.each(Object.keys(this.api.subscribedRooms[socketName]), (room, callbackRoom) => {
this.api.unsubscribe(room, socketName)
.then(() => callbackRoom())
.catch(error => callbackRoom(error));
}, error => {
this.api.subscribedRooms[socketName] = {};
callbackSocketName(error);
});
}, error => callback(error));
});
this.Before('@cleanSecurity', function (scenario, callback) {
cleanSecurity.call(this, callback);
});
this.After('@cleanSecurity', function (scenario, callback) {
cleanSecurity.call(this, callback);
});
};
module.exports = myHooks;
function setAPI (world, apiName) {
var
Api = require('./api' + apiName),
api = new Api();
api.init(world);
return api;
}
function restApi () {
var
W = require('./world'),
world = new (new W()).World();
return setAPI(world, 'REST');
}
function cleanSecurity (callback) {
if (this.currentUser) {
delete this.currentUser;
}
this.api.listIndexes()
.then(response => {
if (response.result.indexes.indexOf('%kuzzle') === -1) {
return q.reject(new ReferenceError('%kuzzle index not found'));
}
})
.then(() => {
return this.api.deleteByQuery(
{ filter: { regexp: { _uid: 'users.' + this.idPrefix + '.*' } } },
'%kuzzle',
'users'
);
})
.then(() => {
return this.api.deleteByQuery(
{ filter: { regexp: { _uid: 'profiles.' + this.idPrefix + '.*' } } },
'%kuzzle',
'profiles'
);
})
.then(() => {
return this.api.deleteByQuery(
{filter: { regexp: { _uid: 'roles.' + this.idPrefix + '.*' } } },
'%kuzzle',
'roles'
);
})
.then(() => {
callback();
})
.catch(error => {
if (error instanceof ReferenceError && error.message === '%kuzzle index not found') {
// The %kuzzle index is not created yet. Is not a problem if the tests are run for the first time.
callback();
}
callback(error);
});
}