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

Commit ef01ad1

Browse files
committed
Test+Implement optional authentication
1 parent 687c912 commit ef01ad1

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

server/services/contributions/contributions.hooks.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const {authenticate} = require('@feathersjs/authentication').hooks;
2-
const {discard, when, unless, isProvider, populate, softDelete, setNow} = require('feathers-hooks-common');
2+
const {iff, discard, when, unless, isProvider, populate, softDelete, setNow} = require('feathers-hooks-common');
33
const {
44
//queryWithCurrentUser,
55
associateCurrentUser,
@@ -110,7 +110,10 @@ module.exports = {
110110
unless(isModerator(),
111111
excludeDisabled()
112112
),
113-
authenticate('jwt'),
113+
iff(
114+
hook => hook.params.headers && hook.params.headers.authorization,
115+
authenticate('jwt')
116+
),
114117
excludeBlacklisted(),
115118
when(isProvider('server'),
116119
includeAll()

test/services/contributions.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const service = app.service('contributions');
44
const userService = app.service('users');
55
const notificationService = app.service('notifications');
66
const categoryService = app.service('categories');
7+
const usersService = app.service('users');
8+
const usersettingsService = app.service('usersettings');
79
const { userData, adminData } = require('../assets/users');
810
const {
911
contributionData,
@@ -172,6 +174,46 @@ describe('\'contributions\' service', () => {
172174
'does not populate'
173175
);
174176
});
177+
178+
describe('of an author', () => {
179+
let author;
180+
beforeEach(async() => {
181+
author = await usersService.create({
182+
183+
name: 'Bad guy'
184+
});
185+
const data = { ...contributionData, userId: author._id };
186+
await service.create(data);
187+
});
188+
189+
context('who is not blacklisted', () => {
190+
it('contribution is included', async () => {
191+
const contributions = await service.find(params);
192+
assert.equal(contributions.total, 2);
193+
});
194+
});
195+
196+
context('who is blacklisted', () => {
197+
beforeEach(async() => {
198+
await usersettingsService.create({
199+
userId: user._id,
200+
blacklist: [author._id]
201+
});
202+
});
203+
204+
it('contribution is filtered', async () => {
205+
const contributions = await service.find(params);
206+
assert.equal(contributions.total, 1);
207+
});
208+
209+
context('but if user is not authenticated', () => {
210+
it('contribution is filtered', async () => {
211+
const contributions = await service.find();
212+
assert.equal(contributions.total, 2);
213+
});
214+
});
215+
});
216+
});
175217
});
176218

177219
describe('contributions find by slug', () => {

0 commit comments

Comments
 (0)