Skip to content

Commit 1eb88e3

Browse files
committed
Fix async errors, hash user password before saving
... and verify user before posting something
1 parent 1a0e4cd commit 1eb88e3

File tree

5 files changed

+154
-23
lines changed

5 files changed

+154
-23
lines changed

features/env/database.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const {Before, AfterAll, BeforeAll} = require('cucumber');
22
const mongoose = require('mongoose');
33
const sinon = require('sinon');
4+
45
const userSchema = require('../../human-connection-api/server/models/users.model.js');
56

67
mongoose.connect("mongodb://localhost/hc_api_test");
@@ -15,14 +16,17 @@ let User;
1516
db.on('error', console.error.bind(console, 'connection error:'));
1617
db.once('open', function() {
1718
User = userSchema(app); // initialize User model
18-
1919
});
2020

2121
// Asynchronous Promise
22-
Before(function () {
23-
User.remove();
22+
Before(function(_, callback) {
23+
User.remove(function (err) {
24+
if(err) throw(err);
25+
callback();
26+
});
2427
});
2528

26-
AfterAll(function() {
27-
return db.close();
29+
AfterAll(function(callback) {
30+
db.close();
31+
callback();
2832
});

features/importPost.feature

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ Feature: Import a post from an organization and publish it in Human Connection
2929

3030
Scenario: Publish a post
3131
Given I am authenticated
32+
And my user account is verified
3233
When I send a POST request to "/contributions" with:
3334
"""
3435
{
3536
"title": "Cool title",
3637
"content": "<p>A nice content</p>",
3738
"contentExcerpt": "Nice",
38-
"type": 'post',
39-
"language": 'de',
40-
"categoryIds: ['5ac7768f8d655d2ee6d48fe4']
39+
"type": "post",
40+
"language": "de",
41+
"categoryIds": ["5ac7768f8d655d2ee6d48fe4"]
4142
}
4243
"""
4344
Then a new post should be created

features/step_definitions/steps.js

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
const { Given, When, Then } = require('cucumber');
2+
const fetch = require('node-fetch');
23
const mongoose = require('mongoose');
4+
const expect = require('chai').expect;
5+
// Hack: Directly accessing the default password hashing function
6+
const encrypt = require('../../node_modules/feathers-authentication-local/lib/utils/hash');
7+
8+
let currentUser, currentUserPassword, httpResponse, currentUserAccessToken;
9+
const hcBackendUrl = 'http://localhost:3030';
10+
11+
12+
function authenticate(email, plainTextPassword){
13+
const formData = {
14+
email: email,
15+
password: plainTextPassword,
16+
strategy: 'local',
17+
};
18+
return fetch(`${hcBackendUrl}/authentication`, {
19+
method: 'post',
20+
body: JSON.stringify(formData),
21+
headers: { 'Content-Type': 'application/json' },
22+
}).then(response => response.json()).then((json) => {
23+
return json.accessToken;
24+
});
25+
}
326

427
Given('the Human Connection API is up and running', function () {
528
// Just documentation
@@ -9,30 +32,53 @@ Given("there is a 3rd party application running, e.g. 'Democracy'", function ()
932
// Just documentation
1033
});
1134

12-
Given('there is an organization in Human Connection with these credentials:', function (dataTable) {
13-
const table = dataTable.hashes()
14-
dataTable.hashes().forEach((row) => {
15-
const User = mongoose.model('users');
16-
const aUser = new User(row);
17-
aUser.save(function (err, user) {
18-
if(err) throw(err);
35+
Given('there is an organization in Human Connection with these credentials:', function (dataTable, callback) {
36+
const User = mongoose.model('users');
37+
params = dataTable.hashes()[0];
38+
encrypt(params.password).then((hashedPassword) => {
39+
currentUserPassword = params.password; // remember plain text password
40+
params.password = hashedPassword; // hashed password goes into db
41+
currentUser = new User(params);
42+
currentUser.save(function (err, user) {
43+
if(err) callback(err);
44+
callback();
1945
});
2046
});
2147
});
2248

2349
Given('I am authenticated', function () {
24-
// Write code here that turns the phrase above into concrete actions
25-
return 'pending';
50+
return authenticate(currentUser.email, currentUserPassword).then((accessToken) => {
51+
currentUserAccessToken = accessToken;
52+
});
2653
});
2754

28-
When('I send a POST request to {string} with:', function (string, docString) {
55+
Given('my user account is verified', function (callback) {
2956
// Write code here that turns the phrase above into concrete actions
30-
return 'pending';
57+
currentUser.isVerified = true;
58+
currentUser.save(function(err, user) {
59+
if(err) throw(err);
60+
callback();
61+
});
62+
});
63+
64+
When('I send a POST request to {string} with:', function (route, body, callback) {
65+
let params = {
66+
method: 'post',
67+
body: body,
68+
headers: { 'Content-Type': 'application/json' },
69+
};
70+
if (currentUserAccessToken) {
71+
params.headers.Authorization = `Bearer ${currentUserAccessToken}`;
72+
}
73+
fetch(`${hcBackendUrl}${route}`, params).then(response => response.json()).then((json) => {
74+
httpResponse = json;
75+
callback();
76+
});
3177
});
3278

3379
Then('there is an access token in the response:', function (docString) {
34-
// Write code here that turns the phrase above into concrete actions
35-
return 'pending';
80+
expect(httpResponse.accessToken).to.be.a('string');
81+
expect(httpResponse.accessToken.length).to.eq(342);
3682
});
3783

3884
Then('a new post should be created', function () {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"dependencies": {
3+
"chai": "^4.1.2",
34
"cucumber": "^4.2.1",
45
"eslint-plugin-you-dont-need-lodash-underscore": "^6.3.1",
6+
"feathers-authentication-local": "^0.4.4",
57
"lodash": "^4.17.5",
68
"mongoose": "^5.0.16",
79
"node-fetch": "^2.1.2",

yarn.lock

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ assertion-error-formatter@^2.0.1:
9797
pad-right "^0.2.2"
9898
repeat-string "^1.6.1"
9999

100+
assertion-error@^1.0.1:
101+
version "1.1.0"
102+
resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.1.0.tgz#e60b6b0e8f301bd97e5375215bda406c85118c0b"
103+
100104
101105
version "2.1.4"
102106
resolved "https://registry.yarnpkg.com/async/-/async-2.1.4.tgz#2d2160c7788032e4dd6cbe2502f1f9a2c8f6cde4"
@@ -140,6 +144,10 @@ bcrypt-pbkdf@^1.0.0:
140144
dependencies:
141145
tweetnacl "^0.14.3"
142146

147+
bcryptjs@^2.3.0:
148+
version "2.4.3"
149+
resolved "https://registry.yarnpkg.com/bcryptjs/-/bcryptjs-2.4.3.tgz#9ab5627b93e60621ff7cdac5da9733027df1d0cb"
150+
143151
becke-ch--regex--s0-0-v1--base--pl--lib@^1.2.0:
144152
version "1.4.0"
145153
resolved "https://registry.yarnpkg.com/becke-ch--regex--s0-0-v1--base--pl--lib/-/becke-ch--regex--s0-0-v1--base--pl--lib-1.4.0.tgz#429ceebbfa5f7e936e78d73fbdc7da7162b20e20"
@@ -197,6 +205,17 @@ caseless@~0.12.0:
197205
version "0.12.0"
198206
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"
199207

208+
chai@^4.1.2:
209+
version "4.1.2"
210+
resolved "https://registry.yarnpkg.com/chai/-/chai-4.1.2.tgz#0f64584ba642f0f2ace2806279f4f06ca23ad73c"
211+
dependencies:
212+
assertion-error "^1.0.1"
213+
check-error "^1.0.1"
214+
deep-eql "^3.0.0"
215+
get-func-name "^2.0.0"
216+
pathval "^1.0.0"
217+
type-detect "^4.0.0"
218+
200219
chalk@^1.1.3:
201220
version "1.1.3"
202221
resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98"
@@ -219,6 +238,10 @@ chardet@^0.4.0:
219238
version "0.4.2"
220239
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.4.2.tgz#b5473b33dc97c424e5d98dc87d55d4d8a29c8bf2"
221240

241+
check-error@^1.0.1:
242+
version "1.0.2"
243+
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
244+
222245
circular-json@^0.3.1:
223246
version "0.3.3"
224247
resolved "https://registry.yarnpkg.com/circular-json/-/circular-json-0.3.3.tgz#815c99ea84f6809529d2f45791bdf82711352d66"
@@ -377,12 +400,18 @@ [email protected], debug@^2.6.8, debug@^2.6.9:
377400
dependencies:
378401
ms "2.0.0"
379402

380-
debug@^3.1.0:
403+
debug@^3.0.0, debug@^3.1.0:
381404
version "3.1.0"
382405
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
383406
dependencies:
384407
ms "2.0.0"
385408

409+
deep-eql@^3.0.0:
410+
version "3.0.1"
411+
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df"
412+
dependencies:
413+
type-detect "^4.0.0"
414+
386415
deep-is@~0.1.3:
387416
version "0.1.3"
388417
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"
@@ -634,6 +663,25 @@ fast-levenshtein@~2.0.4:
634663
version "2.0.6"
635664
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
636665

666+
feathers-authentication-local@^0.4.4:
667+
version "0.4.4"
668+
resolved "https://registry.yarnpkg.com/feathers-authentication-local/-/feathers-authentication-local-0.4.4.tgz#eed529520f69e68503291adf6ba76e3e4f615872"
669+
dependencies:
670+
bcryptjs "^2.3.0"
671+
debug "^3.0.0"
672+
feathers-errors "^2.4.0"
673+
lodash.get "^4.4.2"
674+
lodash.merge "^4.6.0"
675+
lodash.omit "^4.5.0"
676+
lodash.pick "^4.4.0"
677+
passport-local "^1.0.0"
678+
679+
feathers-errors@^2.4.0:
680+
version "2.9.2"
681+
resolved "https://registry.yarnpkg.com/feathers-errors/-/feathers-errors-2.9.2.tgz#96ca0e5fe50cc56f0eccc90ce3fa5e1f8840828d"
682+
dependencies:
683+
debug "^3.0.0"
684+
637685
[email protected], figures@^2.0.0:
638686
version "2.0.0"
639687
resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962"
@@ -693,6 +741,10 @@ functional-red-black-tree@^1.0.1:
693741
version "1.0.1"
694742
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
695743

744+
get-func-name@^2.0.0:
745+
version "2.0.0"
746+
resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41"
747+
696748
getpass@^0.1.1:
697749
version "0.1.7"
698750
resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa"
@@ -1002,6 +1054,18 @@ [email protected], lodash.get@^4.4.2:
10021054
version "4.4.2"
10031055
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
10041056

1057+
lodash.merge@^4.6.0:
1058+
version "4.6.1"
1059+
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.1.tgz#adc25d9cb99b9391c59624f379fbba60d7111d54"
1060+
1061+
lodash.omit@^4.5.0:
1062+
version "4.5.0"
1063+
resolved "https://registry.yarnpkg.com/lodash.omit/-/lodash.omit-4.5.0.tgz#6eb19ae5a1ee1dd9df0b969e66ce0b7fa30b5e60"
1064+
1065+
lodash.pick@^4.4.0:
1066+
version "4.4.0"
1067+
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
1068+
10051069
lodash@^4.14.0:
10061070
version "4.17.10"
10071071
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
@@ -1225,6 +1289,16 @@ parse-json@^2.2.0:
12251289
dependencies:
12261290
error-ex "^1.2.0"
12271291

1292+
passport-local@^1.0.0:
1293+
version "1.0.0"
1294+
resolved "https://registry.yarnpkg.com/passport-local/-/passport-local-1.0.0.tgz#1fe63268c92e75606626437e3b906662c15ba6ee"
1295+
dependencies:
1296+
passport-strategy "1.x.x"
1297+
1298+
1299+
version "1.0.0"
1300+
resolved "https://registry.yarnpkg.com/passport-strategy/-/passport-strategy-1.0.0.tgz#b5539aa8fc225a3d1ad179476ddf236b440f52e4"
1301+
12281302
path-exists@^2.0.0:
12291303
version "2.1.0"
12301304
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-2.1.0.tgz#0feb6c64f0fc518d9a754dd5efb62c7022761f4b"
@@ -1259,6 +1333,10 @@ path-type@^2.0.0:
12591333
dependencies:
12601334
pify "^2.0.0"
12611335

1336+
pathval@^1.0.0:
1337+
version "1.1.0"
1338+
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0"
1339+
12621340
performance-now@^2.1.0:
12631341
version "2.1.0"
12641342
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
@@ -1712,7 +1790,7 @@ type-check@~0.3.2:
17121790
dependencies:
17131791
prelude-ls "~1.1.2"
17141792

1715-
type-detect@^4.0.5:
1793+
type-detect@^4.0.0, type-detect@^4.0.5:
17161794
version "4.0.8"
17171795
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
17181796

0 commit comments

Comments
 (0)