This repository was archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsteps.js
More file actions
201 lines (174 loc) · 6.19 KB
/
steps.js
File metadata and controls
201 lines (174 loc) · 6.19 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* eslint no-unused-expressions: off */
/* eslint func-names: off */
/* eslint no-underscore-dangle: off */
const { Given, When, Then } = require('cucumber');
const fetch = require('node-fetch');
const { expect } = require('chai');
const hcBackendUrl = 'http://localhost:3031';
let currentUser;
let currentUserPassword;
let httpResponse;
let currentUserAccessToken;
let lastPost;
let blacklistedUser;
function authenticate(email, plainTextPassword) {
const formData = {
email,
password: plainTextPassword,
strategy: 'local',
};
return fetch(`${hcBackendUrl}/authentication`, {
method: 'post',
body: JSON.stringify(formData),
headers: { 'Content-Type': 'application/json' },
}).then(response => response.json())
.catch((err) => {
throw (err);
})
.then(json => json.accessToken);
}
function postRequest(route, body, callback) {
const params = {
method: 'post',
body,
headers: { 'Content-Type': 'application/json' },
};
return request(params, route, callback);
}
function getRequest(route, callback){
const params = {
method: 'get',
headers: { 'Content-Type': 'application/json' },
};
return request(params, route, callback);
}
function request(params, route, callback) {
const requestParams = Object.assign({}, params);
if (currentUserAccessToken) {
requestParams.headers.Authorization = `Bearer ${currentUserAccessToken}`;
}
fetch(`${hcBackendUrl}${route}`, requestParams)
.then(response => response.json())
.catch((err) => {
throw (err);
})
.then((json) => {
httpResponse = json;
callback();
});
}
Given('this is your user account:', function (dataTable) {
const params = dataTable.hashes()[0];
currentUserPassword = params.password;
return this.app.service('users').create(params).then((user) => {
currentUser = user;
});
});
Given('these user accounts exist:', function (dataTable) {
return Promise.all(dataTable.hashes().map(params => {
return this.app.service('users').create(params);
}));
});
Given('you are authenticated', () => authenticate(currentUser.email, currentUserPassword).then((accessToken) => {
currentUserAccessToken = accessToken;
}));
When('you send a POST request to {string} with:', (route, body, callback) => postRequest(route, body, callback));
Then('there is an access token in the response:', (jsonResponse) => {
expect(httpResponse.accessToken).to.be.a('string');
expect(httpResponse.accessToken.length).to.eq(342);
const expectedAccessToken = JSON.parse(jsonResponse).accessToken;
const expectedFirstPartOfJwt = expectedAccessToken.split('.')[0];
expect(httpResponse.accessToken.split('.')[0]).to.eq(expectedFirstPartOfJwt);
});
Then('a new post is created', function () {
return this.app.service('contributions').find({}).then((contributions) => {
expect(contributions.total).to.eq(1);
expect(contributions.data[0].type).to.eq('post');
});
});
Then('these category ids are stored in your user settings', function () {
return this.app.service('usersettings').find({userId: currentUser._id.toString()}).then((settings) => {
expect(settings.total).to.eq(1);
let usersettings = settings.data[0];
expect(usersettings.uiLanguage).to.eq('en');
expect(usersettings.filter.categoryIds).to.be.an('array')
.that.does.include('5b310ab8b801653c1eb6c426')
.that.does.include('5b310ab8b801653c1eb6c427')
.that.does.include('5b310ab8b801653c1eb6c428');
});
});
Then('your language {string} is stored in your user settings', function (lang) {
return this.app.service('usersettings').find({query: {userId: currentUser._id.toString()}}).then((settings) => {
expect(settings.total).to.eq(1);
expect(settings.data[0].uiLanguage).to.eq(lang);
});
});
Then('debug', function() {
// eslint-disable-next-line no-debugger
debugger;
});
When('you create your user settings via POST request to {string} with:', function (route, body, callback) {
let jsonBody = JSON.parse(body);
jsonBody.userId = currentUser._id.toString();
postRequest(route, JSON.stringify(jsonBody), callback);
});
Then('you will stop seeing posts of the user with id {string}', function (blacklistedUserId) {
return this.app.service('usersettings').find({userId: currentUser._id.toString()}).then((settings) => {
expect(settings.total).to.eq(1);
expect(settings.data[0].blacklist).to.be.an('array').that.does.include(blacklistedUserId);
});
});
Given('you blacklisted the user {string} before', async function (blacklistedUserName) {
const res = await this.app.service('users').find({query: {name: blacklistedUserName}});
blacklistedUser = res.data[0];
const params = {
userId: currentUser._id,
blacklist: [blacklistedUser._id]
};
return this.app.service('usersettings').create(params);
});
When('this user publishes a post', function () {
const params = {
title: 'Awful title',
content: 'disgusting content',
language: 'en',
type: 'post',
userId: blacklistedUser._id
};
return this.app.service('contributions').create(params);
});
When('you read your current news feed', function (callback) {
getRequest('/contributions', callback);
});
Then('this post is not included', function () {
expect(httpResponse.data).to.be.an('array').that.is.empty;
});
Given('there is a post {string} by user {string}', async function (postTitle, userName) {
const users = await this.app.service('users').find({ query: {name: userName} });
const user = users.data[0];
const params = {
title: postTitle,
content: 'blah',
language: 'en',
type: 'post',
userId: user._id
};
lastPost = await this.app.service('contributions').create(params);
return lastPost;
});
Given('the blacklisted user wrote a comment on that post:', function (comment) {
const commentParams = {
userId: blacklistedUser._id,
content: comment,
contributionId: lastPost._id
};
return this.app.service('comments').create(commentParams);
});
When('you read through the comments of that post', function (callback) {
getRequest('/comments', callback);
});
Then('you will see a hint instead of a comment:', function (hint) {
const comment = httpResponse.data[0];
expect(comment.content).to.eq(hint);
expect(comment.contentExcerpt).to.eq(hint);
});