mirror of
https://github.com/mclueppers/xo-server.git
synced 2025-07-21 21:14:54 +02:00
Add new functions test
This commit is contained in:
parent
3c52c56315
commit
f4d7ce72c2
15
src/api.js
15
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);
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -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();
|
||||
});
|
||||
|
||||
|
@ -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',
|
||||
}));
|
||||
},
|
||||
|
||||
},
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
Loading…
x
Reference in New Issue
Block a user