Skip to content

Commit b3df69c

Browse files
committed
chore: improved Feathers hooks tests with authentication
1 parent 73c6b44 commit b3df69c

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

lib/hooks/hooks.feathers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export function connectFeathers (options = {}) {
5252
client.configure(feathers.authentication({
5353
path: options.authentication.path || '/authentication'
5454
}))
55-
client.authenticate(_.omit(options.authentication, ['path']))
55+
const payload = _.omit(options.authentication, ['path'])
56+
await client.authenticate(payload)
5657
}
5758
}
5859
_.set(item, options.clientPath || 'client', client)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
"xmldom": "^0.6.0"
114114
},
115115
"devDependencies": {
116+
"@feathersjs/authentication": "^5.0.8",
116117
"@feathersjs/memory": "^5.0.8",
117118
"c8": "^7.11.2",
118119
"chai": "^4.2.0",

test/hooks.feathers.test.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import fs from 'fs-extra'
66
import fsStore from 'fs-blob-store'
77
import mongo from 'mongodb'
88
import { feathers } from '@feathersjs/feathers'
9+
import { AuthenticationService, JWTStrategy, authenticate } from '@feathersjs/authentication'
910
import errors from '@feathersjs/errors'
1011
import socketio from '@feathersjs/socketio'
1112
import { MemoryService } from '@feathersjs/memory'
@@ -240,7 +241,7 @@ function createTests (servicePath, feathersHook, options = {}) {
240241
}
241242

242243
describe('krawler:hooks:feathers', () => {
243-
let mongoClient, app, server
244+
let mongoClient, app, server, accessToken
244245

245246
before(async () => {
246247
chailint(chai, util)
@@ -250,12 +251,38 @@ describe('krawler:hooks:feathers', () => {
250251
await Model.createIndex({ id: 1 }, { unique: true })
251252
app = feathers()
252253
.configure(socketio({ path: '/ws' }))
254+
.use('users', new MemoryService({ store: { 1: { name: 'Jack Doe', id: 1 } }, startId: 1 }))
253255
.use('geojson-memory', new CustomMemoryService({ multi: true }), { methods: ['find', 'get', 'create', 'update', 'patch', 'remove', 'custom'] })
254256
.use('geojson-mongodb', new CustomMongoDBService({ multi: true, Model }), { methods: ['find', 'get', 'create', 'update', 'patch', 'remove', 'custom'] })
257+
app.set('authentication', {
258+
secret: '1234',
259+
entity: 'user',
260+
service: 'users',
261+
entityId: 'id',
262+
authStrategies: ['jwt'],
263+
jwtOptions: {
264+
header: { typ: 'access' },
265+
audience: 'https://yourdomain.com',
266+
issuer: 'feathers',
267+
algorithm: 'HS256',
268+
expiresIn: '1d'
269+
}
270+
})
271+
const authService = new AuthenticationService(app)
272+
authService.register('jwt', new JWTStrategy())
273+
app.use('api/authentication', authService)
274+
feathersOptions.authentication.accessToken = await authService.createAccessToken({}, { subject: '1' })
275+
// Add authentication hooks on services
276+
app.service('geojson-memory').hooks({
277+
before: {
278+
all: authenticate('jwt')
279+
}
280+
})
255281
// Add required hook to manage upsert
256-
// Also a hook to simulate an error
282+
// Also a hook to simulate an error
257283
app.service('geojson-mongodb').hooks({
258284
before: {
285+
all: authenticate('jwt'),
259286
patch: [
260287
(hook) => { _.set(hook, 'params.mongodb', { upsert: _.get(hook, 'params.query.upsert', false) }) },
261288
(hook) => { if (hook.data.properties === null) throw new BadRequest('Properties cannot be null') }
@@ -272,7 +299,11 @@ describe('krawler:hooks:feathers', () => {
272299
customMethods: [{
273300
servicePath: 'geojson-memory', methods: ['custom'] }, {
274301
servicePath: 'geojson-mongodb', methods: ['custom']
275-
}]
302+
}],
303+
authentication: {
304+
path: 'api/authentication',
305+
strategy: 'jwt'
306+
}
276307
}
277308

278309
const feathersHook = {

0 commit comments

Comments
 (0)