diff --git a/src/api.js b/src/api.js index 8836b71..923703b 100644 --- a/src/api.js +++ b/src/api.js @@ -352,10 +352,8 @@ Api.fn.user = { } var users = this.xo.users; - - return users.get(user_id).then(function (user) { + return users.first(user_id).then(function (user) { // Get the current user to check its permission. - if (!user.hasPermission('admin')) { throw Api.err.UNAUTHORIZED; @@ -363,8 +361,9 @@ Api.fn.user = { // @todo Check there are no invalid parameter. - - return users.first(p_id); + return users.first(p_id).fail(function () { + throw Api.err.INVALID_PARAMS; + }); }).then(function (user) { // @todo Check user exists. @@ -378,7 +377,7 @@ Api.fn.user = { if (p_password) { - return user.setPassword(p_password).then(user); + return user.setPassword(p_password).thenResolve(user); } return user; @@ -386,9 +385,7 @@ Api.fn.user = { // Save the updated user. return users.update(user); - }).thenResolve(true).fail(function () { - throw Api.err.INVALID_PARAMS; - }); + }).thenResolve(true); }, }; diff --git a/src/main.js b/src/main.js index 15b42e1..608415b 100644 --- a/src/main.js +++ b/src/main.js @@ -181,7 +181,11 @@ xo.on('started', function () { socket.on('message', function (request) { json_api_call(session, request).then(function (response) { - socket.send(response); + // Send response if session still open. + if (socket.readyState === socket.OPEN) + { + socket.send(response); + } }).done(); }); diff --git a/tests/websocket.js b/tests/websocket.js index f2e15f9..692ec05 100644 --- a/tests/websocket.js +++ b/tests/websocket.js @@ -3,6 +3,7 @@ var assert = require('assert'); var sync = require('sync'); var WS = require('ws'); +var _ = require('underscore'); ////////////////////////////////////////////////////////////////////// @@ -139,7 +140,7 @@ var tests = { conn('session.signInWithToken', {'token': token}); // Delete the tokens. - conn('token.delete', {'token': token}); + this.master('token.delete', {'token': token}); // Checks the connection is closed. assert.throws(function () { @@ -164,11 +165,105 @@ var tests = { assert(conn('user.create', { 'email': 'tintin@gmail.com', 'password': 'abc', - 'permission': 'none', + 'permission': 'admin', })); }, - }, + 'Delete user': function() { + // Connects, sign in (with a password). + var user_id = this.master('user.create', { + 'email': 'fox@gmail.com', + 'password': '123', + 'permission': 'none', + }); + + // Delete user + assert(this.master('user.delete', { + 'id': user_id, + })); + }, + + 'Connection close out when user removed': function() { + // Connects, sign in (with a password). + var user_id = this.master('user.create', { + 'email': 'fox@gmail.com', + 'password': '123', + 'permission': 'none', + }); + + // Connects, sign in (with a password) + var conn = this.connect(); + conn('session.signInWithPassword', { + 'email': 'fox@gmail.com', + 'password': '123', + }); + + // Delete the user + this.master('user.delete', { + 'id': user_id, + }); + + // Checks the connection is closed. + assert.throws(function () { + conn('session.getUserId'); + }); + }, + + 'Change password': function() { + // Create new account. + this.master('user.create', { + 'email': 'fox@gmail.com', + 'password': '123', + 'permission': 'none', + }); + + // Connects, sign in (with a password). + var conn = this.connect(); + conn('session.signInWithPassword', { + 'email': 'fox@gmail.com', + 'password': '123', + }); + + // Change password. + conn('user.changePassword', { + 'old': '123', + 'new': 'abc', + }); + + // Check if password has changed + var conn2 = this.connect(); + assert(conn2('session.signInWithPassword', { + 'email': 'fox@gmail.com', + 'password': 'abc', + })); + }, + + 'Get all users': function() { + var users = this.master('user.getAll'); + assert(_.isArray(users)); + }, + + 'Set user': function() { + var user_id = this.master('user.create', { + 'email': 'link@gmail.com', + 'password': 'abc', + 'permission': 'none', + }); + + this.master('user.set', { + 'id': user_id, + 'email': 'mario@gmail.com', + 'password': '123', + }); + + var conn = this.connect(); + assert(conn('session.signInWithPassword', { + 'email': 'mario@gmail.com', + 'password': '123', + })); + }, + + }, }; //////////////////////////////////////////////////////////////////////