Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

fixes several issues with update-entity w/ test cases #531

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -89,13 +89,14 @@
"jshint": "^2.9.3",
"localstorage-memory": "^1.0.2",
"mocha": "^3.0.2",
"mongodb": "^2.2.29",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used to generate an ObjectID from a string value. I probably could stub-out similar functionality, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah. I'm happy with this. Thanks!

"nsp": "^2.6.1",
"passport-strategy": "^1.0.0",
"primus": "^7.0.0",
"rimraf": "^2.5.4",
"semistandard": "^11.0.0",
"sinon": "^2.1.0",
"sinon-chai": "^2.8.0",
"semistandard": "^11.0.0",
"socket.io-client": "^2.0.0",
"superagent": "^3.0.0",
"ws": "^2.2.3"
Expand Down
12 changes: 10 additions & 2 deletions src/socket/update-entity.js
Expand Up @@ -22,13 +22,21 @@ module.exports = function updateEntity (entity, meta) {

Object.keys(socketMap).forEach(socketId => {
const socket = socketMap[socketId];
const socketEntity = (socket.feathers && socket.feathers[authConfig.entity]) || socket.request.feathers[authConfig.entity];
const feathers = socket.feathers || socket.request.feathers;
const socketEntity = feathers && feathers[authConfig.entity];

if (socketEntity) {
const socketEntityId = socketEntity[idField];

if (entityId === socketEntityId) {
if (`${entityId}` === `${socketEntityId}`) {
// 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]);
}
}
});
Expand Down
117 changes: 117 additions & 0 deletions test/socket/update-entity.test.js
@@ -1,6 +1,9 @@
import { expect } from 'chai';
import { ObjectID } from 'mongodb';
import updateEntity from '../../src/socket/update-entity';

const TEST_OBJECT_ID = '59499c9a901604391cab65f5';

describe('Socket "Update Entity" Handler', function () {
it('updates the passed-in entity for socket.io', function () {
const app = {
Expand Down Expand Up @@ -104,4 +107,118 @@ describe('Socket "Update Entity" Handler', function () {

expect(app.primus.connections['my-socket'].request.feathers.user.email).to.equal('test@feathersjs.com');
});

it('gracefully handles unauthenticated connections', function () {
const app = {
get () {
return {
entity: 'user',
service: 'users'
};
},
io: {
sockets: {
sockets: {
'unauthenticated': {
request: {},
feathers: {}
},
'my-socket': {
feathers: {
user: { _id: 5, email: 'admin@feathersjs.com' }
}
}
}
}
},
services: {
users: {
id: '_id'
}
},
service (location) {
return this.services[location];
}
};
const user = { _id: 5, email: 'test@feathersjs.com' };

updateEntity(user, { app });

expect(app.io.sockets.sockets['my-socket'].feathers.user.email).to.equal('test@feathersjs.com');
});

it('updates the passed-in entity when the idField is an ObjectID', function () {
const app = {
get () {
return {
entity: 'user',
service: 'users'
};
},
io: {
sockets: {
sockets: {
'my-socket': {
feathers: {
user: { _id: new ObjectID(TEST_OBJECT_ID), email: 'admin@feathersjs.com' }
}
}
}
}
},
services: {
users: {
id: '_id'
}
},
service (location) {
return this.services[location];
}
};
const user = { _id: new ObjectID(TEST_OBJECT_ID), email: 'test@feathersjs.com' };

updateEntity(user, { app });

expect(app.io.sockets.sockets['my-socket'].feathers.user.email).to.equal('test@feathersjs.com');
});

it('socket entity should "deep equal" passed-in entity', 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 },
optional: true
}
}
}
}
}
},
services: {
users: {
id: '_id'
}
},
service (location) {
return this.services[location];
}
};
const user = { _id: 5, email: 'test@feathersjs.com', nested: { value: 3 } };

updateEntity(user, { app });

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