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
163 lines (140 loc) · 4.96 KB
/
steps.js
File metadata and controls
163 lines (140 loc) · 4.96 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
/* 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;
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);
});
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;
});
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);
});