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

Commit 792df32

Browse files
committed
user tests and optimization
1 parent deab057 commit 792df32

File tree

6 files changed

+165
-10
lines changed

6 files changed

+165
-10
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ Getting up and running is as easy as 1, 2, 3, 4 ... 5.
8181
We are using [Thumbor](https://github.com/thumbor/thumbor) as a Thumbnail Microservice.
8282
You can install it locally if you like but this is totally optional.
8383
84-
- At first you have to [install](http://thumbor.readthedocs.io/en/latest/installing.html) it locally.
85-
- After installation start it via the console with `thumbor`
86-
- Set the `thumbor.url` in `config/local.json` to `http://localhost:8888` if not defined differently. The `thumbor.key` does not necessarily have to be defined, it just makes the URL more secure.
84+
**Install OR use docker**
85+
86+
- At first you have to [install](http://thumbor.readthedocs.io/en/latest/installing.html) it locally and start it in the console with `thumbor` **OR** run it with docker `docker run -p 8000:8000 apsl/thumbor`
87+
- Set the `thumbor.url` in `config/local.json` to `http://localhost:8888` (with docker `http://localhost:8000`) if not defined differently. The `thumbor.key` does not necessarily have to be defined, it just makes the URL more secure.
8788
8889
> Do not forget to always start it if you choose that setup or otherwise you will not see any pictures at all.
8990

server/hooks/is-single-item.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Check if the query is for a single item
22
module.exports = () => hook => {
3-
if (hook && hook.params && hook.params.query && hook.params.query.$limit === 1) {
3+
if (hook && hook.params && hook.params.query && (hook.params.query.$limit === 1 || hook.params.query.slug)) {
44
return true;
55
} else {
66
return false;

server/logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ logger.add(new transports.Console({
2222
return `${levelColors[info.level]}${date.toLocaleTimeString()} | ${info.level}: ${JSON.stringify(info.message)}\u001b[39m`;
2323
})
2424
),
25-
level: env === 'development' ? 'debug' : 'warn'
25+
level: env === 'development' || 'test' ? 'debug' : 'warn'
2626
}));
2727
module.exports = logger;

server/services/users/users.hooks.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const restrictUserRole = require('./hooks/restrict-user-role');
88
const createAdmin = require('./hooks/create-admin');
99
const createSlug = require('../../hooks/create-slug');
1010
const thumbnails = require('../../hooks/thumbnails');
11+
const isModerator = require('../../hooks/is-moderator-boolean');
12+
const isSingleItem = require('../../hooks/is-single-item');
1113
const inviteCode = require('./hooks/invite-code')();
1214
const search = require('feathers-mongodb-fuzzy-search');
1315
const isOwnEntry = require('./hooks/is-own-entry');
@@ -155,16 +157,24 @@ module.exports = {
155157

156158
after: {
157159
all: [
158-
populate({ schema: badgesSchema }),
159-
populate({ schema: candosSchema }),
160-
populate({ schema: userSettingsSchema }),
161160
cleanupBasicData
162161
],
163162
find: [
163+
when(isModerator(),
164+
populate({ schema: userSettingsSchema })
165+
),
166+
when(isSingleItem(),
167+
populate({ schema: badgesSchema }),
168+
populate({ schema: candosSchema }),
169+
populate({ schema: userSettingsSchema })
170+
),
164171
thumbnails(thumbnailOptions),
165172
cleanupPersonalData
166173
],
167174
get: [
175+
populate({ schema: badgesSchema }),
176+
populate({ schema: candosSchema }),
177+
populate({ schema: userSettingsSchema }),
168178
thumbnails(thumbnailOptions),
169179
// remove personal data if its not the current authenticated user
170180
iff(isOwnEntry(false),

test/assets/users.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const adminData = {
2+
3+
password: '1234',
4+
name: 'Peter',
5+
timezone: 'Europe/Berlin',
6+
badgeIds: [],
7+
role: 'admin'
8+
};
9+
10+
const userData = {
11+
12+
password: '1234',
13+
name: 'John',
14+
timezone: 'Europe/Berlin',
15+
badgeIds: [],
16+
role: 'user'
17+
};
18+
19+
module.exports = {
20+
adminData,
21+
userData
22+
};

test/services/users.test.js

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,132 @@
11
const assert = require('assert');
22
const app = require('../../server/app');
3+
const service = app.service('users');
4+
const { userData, adminData } = require('../assets/users');
35

46
describe('\'users\' service', () => {
5-
it('registered the service', () => {
6-
const service = app.service('users');
7+
let admin;
8+
let adminParams;
9+
let user;
10+
let userParams;
11+
12+
before(function(done) {
13+
this.server = app.listen(3031);
14+
this.server.once('listening', () => done());
15+
});
16+
17+
after(function(done) {
18+
this.server.close(done);
19+
});
20+
21+
beforeEach(async () => {
22+
await app.get('mongooseClient').connection.dropDatabase();
23+
admin = await service.create(adminData);
24+
adminParams = {
25+
user: admin
26+
};
27+
user = await service.create(userData);
28+
userParams = {
29+
user: user
30+
};
31+
});
732

33+
afterEach(async () => {
34+
await app.get('mongooseClient').connection.dropDatabase();
35+
admin = null;
36+
adminParams = null;
37+
user = null;
38+
userParams = null;
39+
});
40+
41+
it('registered the service', () => {
842
assert.ok(service, 'Registered the service');
943
});
44+
45+
describe('users create', () => {
46+
it('runs create', () => {
47+
assert.ok(user, 'Created user');
48+
});
49+
50+
it('does not generate slug', () => {
51+
assert.strictEqual(user.slug, undefined);
52+
});
53+
});
54+
55+
describe('users patch', () => {
56+
it('runs patch', async () => {
57+
const result = await service.patch(user._id, {
58+
name: 'Alice'
59+
}, userParams);
60+
assert.ok(result, 'Patched user');
61+
assert.equal(result.name, 'Alice', 'Patched user name');
62+
});
63+
64+
it('generates slug from name', async () => {
65+
const result = await service.patch(user._id, {
66+
name: 'Alice'
67+
}, userParams);
68+
assert.equal(result.slug, 'alice');
69+
});
70+
});
71+
72+
describe('users find', () => {
73+
it('returns users', async () => {
74+
const result = await service.find();
75+
assert.ok(result.data[0]);
76+
});
77+
78+
it('does not populate badges, candos and userSettings', async () => {
79+
const result = await service.find(userParams);
80+
assert.strictEqual(result.data[0].badges, undefined);
81+
assert.strictEqual(result.data[0].candos, undefined);
82+
assert.strictEqual(result.data[0].userSettings, undefined);
83+
});
84+
85+
it('populates userSettings for admins', async () => {
86+
const result = await service.find(adminParams);
87+
assert.notStrictEqual(result.data[0].userSettings, undefined);
88+
});
89+
});
90+
91+
describe('users find by slug', () => {
92+
let query;
93+
94+
// Slug is generated in first patch call
95+
beforeEach(async () => {
96+
const result = await service.patch(user._id, {
97+
name: user.name
98+
}, userParams);
99+
user.slug = result.slug;
100+
query = {
101+
slug: user.slug
102+
};
103+
});
104+
105+
it('returns one user', async () => {
106+
const result = await service.find({ query });
107+
assert.ok(result.data[0], 'returns data');
108+
assert.equal(result.data.length, 1), 'returns only one entry';
109+
});
110+
111+
it('populates badges, candos and userSettings', async () => {
112+
const result = await service.find({ query });
113+
assert.notStrictEqual(result.data[0].badges, undefined, 'has badges');
114+
assert.notStrictEqual(result.data[0].candos, undefined, 'has candos');
115+
assert.notStrictEqual(result.data[0].userSettings, undefined, 'has userSettings');
116+
});
117+
});
118+
119+
describe('users get', () => {
120+
it('returns user', async () => {
121+
const result = await service.get(user._id);
122+
assert.ok(result, 'returns data');
123+
});
124+
125+
it('populates badges, candos and userSettings', async () => {
126+
const result = await service.get(user._id);
127+
assert.notStrictEqual(result.badges, undefined, 'has badges');
128+
assert.notStrictEqual(result.candos, undefined, 'has candos');
129+
assert.notStrictEqual(result.userSettings, undefined, 'has userSettings');
130+
});
131+
});
10132
});

0 commit comments

Comments
 (0)