Skip to content

Commit

Permalink
fix: Only merge authenticated property on update
Browse files Browse the repository at this point in the history
  • Loading branch information
claustres authored and daffl committed Oct 25, 2018
1 parent 1356a1c commit 8a564f7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/authentication/lib/socket/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ module.exports = function setupSocketHandler (app, options, { feathersParams, pr
// Only bind the handlers on receiving the first socket connection.
if (!isUpdateEntitySetup) {
isUpdateEntitySetup = true;
entityService.on('updated', updateEntity(app));
entityService.on('patched', updateEntity(app));
entityService.on('updated', updateEntity(app, 'update'));
entityService.on('patched', updateEntity(app, 'patch'));
}
};
};
15 changes: 9 additions & 6 deletions packages/authentication/lib/socket/update-entity.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

module.exports = (app) => (entity) => {
module.exports = (app, operation = 'update') => (entity) => {
const authConfig = app.get('auth');
let idField = app.service(authConfig.service).id;

Expand Down Expand Up @@ -31,11 +31,14 @@ module.exports = (app) => (entity) => {
// Need to assign because of external references
Object.assign(socketEntity, entity);

// Delete any removed entity properties
const entityProps = new Set(Object.keys(entity));
Object.keys(socketEntity)
.filter(prop => !entityProps.has(prop))
.forEach(prop => delete socketEntity[prop]);
// Delete any removed entity properties on update
// On patch we can only update existing properties so this is not required
if (operation === 'update') {
const entityProps = new Set(Object.keys(entity));
Object.keys(socketEntity)
.filter(prop => !entityProps.has(prop))
.forEach(prop => delete socketEntity[prop]);
}
}
}
});
Expand Down
39 changes: 39 additions & 0 deletions packages/authentication/test/socket/update-entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,43 @@ describe('Socket "Update Entity" Handler', function () {

expect(app.io.sockets.sockets['my-socket'].feathers.user).to.deep.equal(user);
});

it('socket entity should be correctly updated on entity patch', function () {
const app = {
get () {
return {
entity: 'user',
service: 'users'
};
},
io: {
sockets: {
sockets: {
'my-socket': {
feathers: {
user: {
_id: 5,
email: 'admin@feathersjs.com',
nested: { value: 1 }
}
}
}
}
}
},
services: {
users: {
id: '_id'
}
},
service (location) {
return this.services[location];
}
};
const patchedUser = { _id: 5, email: 'test@feathersjs.com' };

updateEntity(app, 'patch')(patchedUser);

expect(app.io.sockets.sockets['my-socket'].feathers.user).to.deep.equal({ _id: 5, email: 'test@feathersjs.com', nested: { value: 1 } });
});
});

0 comments on commit 8a564f7

Please sign in to comment.