Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit b7c744d

Browse files
Merge pull request #115 from Human-Connection/114_fix_change_of_usersettings_for_http_rest
114 fix change of usersettings for http rest
2 parents c0a8883 + a19cb53 commit b7c744d

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

features/api/usersettings.feature

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Feature: Save current newsfeed filters to usersettings
2+
As a user of human connection
3+
You would like to have my latest newsfeed filter settings saved
4+
In order to see the same selection of content next time I log in
5+
6+
Background:
7+
Given the Human Connection API is up and running
8+
And there is a user in Human Connection with these credentials:
9+
| email | password | isVerified |
10+
| user@example.com | 1234 | true |
11+
12+
Scenario: Save user's language
13+
Given you are authenticated
14+
When you create your user settings via POST request to "/usersettings" with:
15+
"""
16+
{
17+
"uiLanguage": "de"
18+
}
19+
"""
20+
Then your language "de" is stored in your user settings

features/step_definitions/steps.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ function authenticate(email, plainTextPassword) {
3030
.then(json => json.accessToken);
3131
}
3232

33+
function postRequest(route, body, callback) {
34+
const params = {
35+
method: 'post',
36+
body,
37+
headers: { 'Content-Type': 'application/json' },
38+
};
39+
if (currentUserAccessToken) {
40+
params.headers.Authorization = `Bearer ${currentUserAccessToken}`;
41+
}
42+
fetch(`${hcBackendUrl}${route}`, params)
43+
.then(response => response.json())
44+
.catch((err) => {
45+
throw (err);
46+
})
47+
.then((json) => {
48+
httpResponse = json;
49+
callback();
50+
});
51+
}
52+
3353
Given(/^the Human Connection API is up and running(?: on "http:\/\/localhost:3030")?/, (callback) => {
3454
waitOn({ resources: ['tcp:3030'], timeout: 30000 }, (err) => {
3555
if (err) throw (err);
@@ -53,25 +73,7 @@ Given('you are authenticated', () => authenticate(currentUser.email, currentUser
5373
currentUserAccessToken = accessToken;
5474
}));
5575

56-
When('you send a POST request to {string} with:', (route, body, callback) => {
57-
const params = {
58-
method: 'post',
59-
body,
60-
headers: { 'Content-Type': 'application/json' },
61-
};
62-
if (currentUserAccessToken) {
63-
params.headers.Authorization = `Bearer ${currentUserAccessToken}`;
64-
}
65-
fetch(`${hcBackendUrl}${route}`, params)
66-
.then(response => response.json())
67-
.catch((err) => {
68-
throw (err);
69-
})
70-
.then((json) => {
71-
httpResponse = json;
72-
callback();
73-
});
74-
});
76+
When('you send a POST request to {string} with:', (route, body, callback) => postRequest(route, body, callback));
7577

7678
Then('there is an access token in the response:', (jsonResponse) => {
7779
expect(httpResponse.accessToken).to.be.a('string');
@@ -88,3 +90,22 @@ Then('a new post is created', function () {
8890
});
8991
});
9092

93+
94+
Then('your language {string} is stored in your user settings', function (lang) {
95+
return this.app.service('usersettings').find({userId: currentUser._id.toString()}).then((settings) => {
96+
expect(settings.total).to.eq(1);
97+
expect(settings.data[0].uiLanguage).to.eq(lang);
98+
});
99+
});
100+
101+
Then('debug', function() {
102+
// eslint-disable-next-line no-debugger
103+
debugger;
104+
});
105+
106+
When('you create your user settings via POST request to {string} with:', function (route, body, callback) {
107+
let jsonBody = JSON.parse(body);
108+
jsonBody.userId = currentUser._id.toString();
109+
postRequest(route, JSON.stringify(jsonBody), callback);
110+
});
111+

server/services/usersettings/usersettings.hooks.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1+
const { iff, isProvider } = require('feathers-hooks-common');
12
const { restrictToOwner } = require('feathers-authentication-hooks');
23
const mapCreateToUpsert = require('../../hooks/map-create-to-upsert');
4+
const auth = require('@feathersjs/authentication');
35

46
module.exports = {
57
before: {
68
all: [],
79
find: [],
810
get: [],
9-
create: [
11+
create: [
12+
auth.hooks.authenticate('jwt'),
1013
mapCreateToUpsert(context => {
1114
const { data } = context;
1215
return { userId: data.userId };
1316
})
1417
],
1518
update: [
19+
auth.hooks.authenticate('jwt'),
1620
restrictToOwner()
1721
],
1822
patch: [
23+
auth.hooks.authenticate('jwt'),
1924
restrictToOwner()
2025
],
2126
remove: [
27+
auth.hooks.authenticate('jwt'),
2228
restrictToOwner()
2329
]
2430
},

0 commit comments

Comments
 (0)